mohamed aziz
mohamed aziz

Reputation: 9

I have a problem with my code for a discord bot, it keeps showing the same error

I followed a youtube video for this code and it just doesn't work, this is the part of the code where the error is:

    @client.command(name='queue', help='This command adds a song to the queue')
async def queue_(ctx, url):
    global queue

    queue.append(url)
    await ctx.send(f'`{url}` added to queue!')

@client.command(name='play', help='This command plays songs')
async def play(ctx):
    global queue

    server = ctx.message.guild
    voice_channel = server.voice_client

    async with ctx.typing():
        player = await YTDLSource.from_url(queue, loop=client.loop)
        voice_channel.play(player, after=lambda e: print('Player error: %s' %e) if e else None)

    await ctx.send('*Now playing:* {}'.format(player.title))
    del(queue[0])

And this is the error it shows:

    Ignoring exception in command play:
Traceback (most recent call last):
  File "C:\Users\moham\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\moham\Desktop\musicbot.py", line 102, in play
    player = await YTDLSource.from_url(queue, loop=client.loop)
NameError: name 'queue' is not defined

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

Traceback (most recent call last):
  File "C:\Users\moham\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\moham\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\moham\AppData\Local\Programs\Python\Python38-32\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: NameError: name 'queue' is not defined

please help...

Upvotes: -1

Views: 727

Answers (2)

Jonah Paddison
Jonah Paddison

Reputation: 1

as an answer said, define queue = list(), but change player = await YTDLSource.from_url(queue, loop=client.loop) to player = await YTDLSource.from_url(queue[0], loop=client.loop) and add queue.pop(0) after voice_channel.play(player, after=lambda e: print('Player error: %s' %e) if e else None), so it removes the song its now playing from the list.

Upvotes: 0

stijndcl
stijndcl

Reputation: 5647

You didn't define the queue variable. You need to define it above your functions to be able to use it.

queue = []

@client.command() # rest of your code

Upvotes: 0

Related Questions