Бот Discord присоединяется к ВК

#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:

Если вы проверите документацию по 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)