Проблема с подключением к голосовому чату
Я столкнулся с ошибкой при подключения бота к голосовому чату
Код:
from discord.ext import commands
from youtube_dl import YoutubeDL
from asyncio import sleep
YDL_OPTIONS = {'format': 'bestaudio', 'noplaylist':'False'}
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='>', intents=intents)
@bot.command()
async def play(ctx, arg):
global vc
try:
voice_channel = ctx.message.author.voice.channel
vc = await voice_channel.connect()
except:
print('Уже подключен или не удалось подключиться')
if vc.is_playing():
await ctx.send(f'{ctx.message.author.mention}, музыка уже проигрывается.')
else:
with YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(arg, download=False)
URL = info['formats'][0]['url']
vc.play(discord.FFmpegPCMAudio(executable="ffmpeg\\ffmpeg.exe", source=URL, **FFMPEG_OPTIONS))
while vc.is_playing():
await sleep(1)
if not vc.is_paused():
await vc.disconnect()
bot.run('токен не покажу)')
Ошибка:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 229, in wrapped
ret = await coro(*args, **kwargs)
File "C:/Users/user/PycharmProjects/***/main.py", line 29, in play
if vc.is_playing():
NameError: name 'vc' is not defined
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 1347, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 1023, in invoke
await injected(*ctx.args, **ctx.kwargs) # type: ignore
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 238, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'vc' is not defined
Если добавить vc = '', то появляется ошибка:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 229, in wrapped
ret = await coro(*args, **kwargs)
File "C:/Users/user/PycharmProjects/dojobot/main.py", line 29, in play
if vc.is_playing():
AttributeError: 'str' object has no attribute 'is_playing'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 1347, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 1023, in invoke
await injected(*ctx.args, **ctx.kwargs) # type: ignore
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 238, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'str' object has no attribute 'is_playing'
И при любом значении vc (0, '', без разницы) появляется эта ошибка...