【问题标题】:Can't get a voice channel in discord.py在 discord.py 中无法获得语音频道
【发布时间】:2021-06-05 18:09:11
【问题描述】:
    import os
    from discord.ext import commands
    
    bot = commands.Bot(command_prefix='~')
    
    for fileName in os.listdir('cogs'):
        if fileName.endswith('.py') and fileName != '__init__.py':
            bot.load_extension(f'cogs.{fileName[:-3]}')
    bot.run(* TOKEN * )

问题出在下面的代码上:

    import discord
        from discord.ext import commands
        
        class MoveMembers(commands.Cog):
            def __init__(self, bot):
                self.bot = bot
                self.channel_to_move= discord.Client().get_channel(* channel ID *)
        
        
            @commands.command()
            async def send_to_channel(self, ctx, *members: discord.Member):
                for member in members:
                    if member.voice:
                        print(type(self.channel_to_move))
                        await member.move_to(self.channel_to_move)
                await ctx.send('message recieved')

            def setup(bot):
                bot.add_cog(MoveMembers(bot))

这个函数的重点是移动一个或多个被引用到特定语音通道的成员。

示例:“~send_to_channel @person”

该函数被调用,但由于某种原因“discord.Client().get_channel(* channel ID *)”没有返回语音通道或任何通道。我已经阅读了堆栈溢出中的文档和其他一些答案,但我不知道出了什么问题...

**** 编辑 **** 还有谁知道为什么这段代码不起作用:

def move_rights():
    def predicate(ctx):
        return commands.check_any(commands.is_owner(), 
                    commands.has_role("Move Rights"))
    return commands.check(predicate)



    @commands.command()
    @move_rights()
    async def send_to_gulag(self, ctx, *members: discord.Member):
        for member in members:
            if member.voice:
                await member.move_to(self.gulag)
        await ctx.send('message recieved')

每个人都可以使用这个命令,所以我不知道“move_rights()”函数是否总是返回“true”或者只是没有正确实现它。 (代码是原题部分代码)

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    self.channel_to_move= discord.Client().get_channel(* channel ID *) 中,您不应该引用类本身,而是引用实例

    self.channel_to_move = self.bot.get_channel(* channel ID *)
    

    了解类和实例之间的区别很重要

    【讨论】:

    • 谢谢。我对 Python 不是很熟悉,但它似乎是构建我所需要的不和谐机器人的最佳语言
    • discord.py 不应该被初学者使用,你将不得不学习更多的python,否则你会遇到很多像这样的颠簸。
    • 我知道。我对编程有些熟悉,只是不熟悉python。在尝试使用它开始项目之前,我通常会花时间学习一门语言,但我只需要制作一个特定的不和谐机器人,所有社区支持似乎都针对用 python 或 node.js 编写的机器人。所以我在 discord.py 上开了一枪……
    猜你喜欢
    • 2017-08-11
    • 2018-04-04
    • 1970-01-01
    • 2021-06-06
    • 2021-01-07
    • 2021-11-06
    • 2018-06-16
    • 2021-03-30
    • 1970-01-01
    相关资源
    最近更新 更多