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

Python 我的discord机器人代码有问题,它一直显示相同的错误,python,bots,discord.py,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.comma

我在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

请帮助…

您没有定义
队列
变量。您需要在函数之上定义它,以便能够使用它

queue = []

@client.command() # rest of your code

你真的在上面定义了队列吗?这在您的代码片段中并不存在&非常相关,“全局队列”不是定义它吗?不,全局意味着您可以使用在函数范围之外定义的变量。你必须先定义它。通过使用Global,您永远不会给它一个类型,所以python没有办法知道队列应该是什么(list,int,…)哦,这很有意义,谢谢。我这样做了,它说“命令引发了一个异常:TypeError:预期的字符串或类似字节的对象”,这听起来像是一个不同的问题,带有不同的错误回溯。关于stackoverflow,你随时可以问另一个问题。这与我的回答或你的问题无关,没有适当的回溯,我无法帮助你。如果我的回答解决了您当前的问题,您应该将其标记为正确。请随意发布带有新错误的新问题。是的,很公平!非常感谢。