我的Discord机器人代码有问题,它一直显示相同的错误

时间:2020-11-06 19:58:52

标签: python bots discord.py

我跟踪了这​​段代码的youtube视频,但是它不起作用,这是代码中出现错误的部分:

    @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])

这是显示的错误:

    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

请帮助...

1 个答案:

答案 0 :(得分:1)

您没有定义queue变量。您需要在函数上方定义它才能使用它。

queue = []

@client.command() # rest of your code
相关问题