Search code examples
pythonyoutube-dl

What is the origin of this error in my Discord bot code?


I'm stuck with this, can you help me find the origin of the error? What I've tried so far: Posting this question to StackOverflow. 😄

But seriously, it looks like it's an error in YoutubeDL; I'm not sure why this is happening. The code:

@bot.command()
async def play(ctx, url):
    channel = ctx.author.voice.channel
    voice_client = get(bot.voice_clients , guild = ctx.guild)

    if voice_client == None:
        await ctx.channel.send("Joined")
        await channel.connect()
        voice_client = get(bot.voice_clients , guild = ctx.guild)

    YDL_OPTIONS = {'format : bestaudio', 'noplaylist : True'}
    FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}


    if not voice_client.is_playing():
        with YoutubeDL(YDL_OPTIONS) as ydl:
            info = ydl.extract_info(url, download=False)
        URL = info['formats'][0]['url']
        voice_client.play(discord.FFmpegPCMAudio(URL, **FFMPEG_OPTIONS))
        voice_client.is_playing()
    else:
        await ctx.channel.send("Song has already played")
        return

Here's the trace:

Traceback (most recent call last):
  File "C:\Users\Turbo349-PC\Desktop\discord-bot\discord-bot\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "discord-bot.py", line 69, in play
    with YoutubeDL(YDL_OPTIONS) as ydl:
  File "C:\Users\Turbo349-PC\Desktop\discord-bot\discord-bot\lib\site-packages\youtube_dl\YoutubeDL.py", line 356, in __init__
    self._screen_file = [sys.stdout, sys.stderr][params.get('logtostderr', False)]
AttributeError: 'set' object has no attribute 'get'

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

Traceback (most recent call last):
  File "C:\Users\Turbo349-PC\Desktop\discord-bot\discord-bot\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Turbo349-PC\Desktop\discord-bot\discord-bot\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Turbo349-PC\Desktop\discord-bot\discord-bot\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: AttributeError: 'set' object has no attribute 'get'

Thanks in advance for your help!


Solution

  • Your YDL_OPTIONS is malformed: Instead of providing a dict you're providing a set because you've placed the colons inside of strings.

    Instead of {'format : bestaudio', 'noplaylist : True'} you want the -- subtly different, but crucially totally other data structure -- YDL_OPTIONS = {"format": "bestaudio", "noplaylist": True}.