so my discord bot get the video but cant play it for some reason. I want my bot to play music without downloading and without a url. so far it works if I input a url. But when I give it a name of a song (here 117) it isn't able to play the audio.
Here's The code:
@bot.command(name='play', help='Plays audio in connected voice channel')
async def play(ctx, q):
YDL_OPTIONS = {'default_search': 'auto', 'format': 'bestaudio', 'noplaylist':'True'}
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
voice = ctx.message.guild.voice_client
with YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(q, download=False)
a = info['url']
voice.play(FFmpegPCMAudio(a, **FFMPEG_OPTIONS))
Here's The error:
[download] Downloading playlist: 117
[youtube:search] query "117": Downloading page 1
[youtube:search] playlist 117: Downloading 1 videos
[download] Downloading video 1 of 1
[youtube] PUrsOQanozo: Downloading webpage
[youtube] Downloading just video PUrsOQanozo because of --no-playlist
[download] Finished downloading playlist: 117
Ignoring exception in command play:
Traceback (most recent call last):
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "E:\sa.py", line 49, in play
a = info['url']
KeyError: 'url'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\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: KeyError: 'url'
try this:
a = info['entries'][0]['formats'][0]['url']