Discord bot 加入 VC

时间:2021-05-12 23:48:01

标签: python discord.py

我正在尝试让我的机器人加入 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'

1 个答案:

答案 0 :(得分:1)

如果您查看 discord.VoiceState object 的文档,您会发现该属性是 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)