如何将机器人连接到语音通道?

How do you connect a bot to a voice channel?

rewrite中如何让bot加入语音频道? 我尝试的任何方法都不起作用,也不会给出错误。 以下是我的尝试:

@client.command()
async def join(ctx):
    channel = ctx.author.voice.channel
    await channel.connect()
@client.command()
async def join(ctx):
    channel = client.get_channel(ctx.author.voice.channel.id)
    await channel.connect()

编辑:我关闭了错误处理程序,现在我收到了这个错误: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'channel'

编辑 2:我在机器人可以连接的语音通道中。 ctx.author.voice_channel 不起作用,我得到这个错误: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Member' object has no attribute 'voice_channel'

编辑 3:命令有效。我安装了 PyNaCl,它工作得很好。

试试这些代码。

@client.command()
async def join(ctx):
    channel = ctx.author.voice.channel
    await channel.connect()

@client.command()
async def leave(ctx):
    await ctx.voice_client.disconnect()