Discord机器人加入VC



我正试图让我的机器人加入一个VC,但它不起作用。

这是我得到的

@client.command(pass_context=True)
async def join(ctx):
channel = ctx.author.voice.voice_channel
await client.join_voice_channel(channel)

我不知道这是否有用,但这是我收到的错误消息:

Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 74, in join
channel = ctx.author.voice.voice_channel
AttributeError: 'VoiceState' object has no attribute 'voice_channel'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'VoiceState' object has no attribute 'voice_channel'

如果检查discord.VoiceState对象的文档,您会发现该属性是channel,而不是voice_channel。因此:

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

或者只是

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