我的Discord Bot如何与语音通道断开连接

时间:2020-04-11 14:17:42

标签: python python-3.x discord discord.py

我正在尝试制作一个简单的Discord Bot(在Python 3中为Wirtten),该命令在命令执行后就可以连接和断开连接。连接命令可以正常工作,但我的Discconect命令不起作用。这是两个命令的代码片段:

async def join(message):
    channel = message.message.author.voice.channel
    await channel.connect()


@client.command(pass_context = True)
async def leave(message):
    channel = message.message.author.voice.channel
    await channel.disconnect()```

1 个答案:

答案 0 :(得分:0)

尝试将您的leave函数替换为:

@commands.command()
    async def leave(ctx):
        channel = ctx.message.author.voice.channel
        voice = get(self.bot.voice_clients, guild=ctx.guild)
        if voice and voice.is_connected():
            await ctx.send(f'Disconnecting from channel **{channel}**')
            await voice.disconnect()
        else:
            await ctx.send("I'm not connected in any channel")