-2

So basicly i have been making a music bot and it worked fine with !play {url link}, but when i wanted to change it from link to search i have been getting many errors and now i dont know what to do. Here is the code:

voice_clients = {}

yt_dl_opts = {'format': 'bestaudio/best', 'default_search': 'auto'}
ytdl = youtube_dl.YoutubeDL(yt_dl_opts)

ffmpeg_options = {'options': "-vn"}
        
#play
    @bot.command()
    async def play(msg, *, url):
      try:
        voice_client = await msg.author.voice.channel.connect()
        voice_clients[voice_client.guild.id] = voice_client
      except:
        print("Already in vc!")
    
  loop = asyncio.get_event_loop()
  data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=False))

  song = data[url]
  player = nextcord.FFmpegPCMAudio(song, **ffmpeg_options)

  voice_clients[msg.guild.id].play(player)
  await msg.send(f"Now playing {url}")

and here is the error im getting after trying to play a song:

    [download] Downloading playlist: macarena
[youtube:search] query "macarena": Downloading page 1
[youtube:search] playlist macarena: Downloading 1 videos
[download] Downloading video 1 of 1
[youtube] zWaymcVmJ-A: Downloading webpage
[download] Finished downloading playlist: macarena
Ignoring exception in command play:
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\nextcord\ext\commands\core.py", line 165, in wrapped
    ret = await coro(*args, **kwargs)
  File "c:\Users\user\Desktop\Python Koneita\Discord botit\Legoshi\bot.py", line 43, in play
    song = data[url]
KeyError: 'macarena'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\nextcord\ext\commands\bot.py", line 1382, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\nextcord\ext\commands\core.py", line 948, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\nextcord\ext\commands\core.py", line 174, in wrapped
    raise CommandInvokeError(exc) from exc
nextcord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: 'macarena'

It seems that the problem is whit song = data[url] but i dont know how to fix it and i have been researching too. This is the last place im looking for help. Thank you!

ELIAS
  • 1
  • 1
  • Can you post what print(data) puts out in your terminal? Put it above song = data[url] – pzutils Sep 08 '22 at 06:15
  • Too long text but it gives me the url of the song and its right. About formats types and all this stuff. Is there anything specific you want to know from it? – ELIAS Sep 08 '22 at 06:36
  • Actually its giving me many url's that are different but the song is same – ELIAS Sep 08 '22 at 06:37
  • I can't do much without seeing the full dictionary because that will determine how to find the key you're looking for. Maybe post 30 lines of it that might help. – pzutils Sep 08 '22 at 06:44
  • I would recommend reading into Python dictionaries and how to access nested dictionaries especially as I'm guessing you're getting this data from an API so there's a good chance you're getting nested dictionaries in response https://www.w3schools.com/python/python_dictionaries_nested.asp https://www.w3schools.com/python/python_dictionaries.asp – pzutils Sep 08 '22 at 06:46

1 Answers1

1

It looks like you're getting a KeyError when trying to find 'macarena' in data. data must not have any key named 'macarena' in it.

Whatever your await is returning as a dictionary doesn't have a key named 'macarena' in it, try printing data right after you define it so you can see what data looks like.

pzutils
  • 490
  • 5
  • 10