无法在 discord.py 中获取语音通道

时间:2021-03-07 18:36:24

标签: python discord.py

    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”,或者只是没有正确实现它。 (代码是原始问题中代码的一部分)

1 个答案:

答案 0 :(得分:1)

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

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

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