discord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: 'url'

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: 'url'

所以我的 discord 机器人得到了视频,但由于某种原因无法播放。我希望我的机器人无需下载且无需 url 即可播放音乐。到目前为止,如果我输入 url,它就可以工作。但是当我给它一个歌曲的名称(这里是 117)时,它无法播放音频。

代码如下:

@bot.command(name='play', help='Plays audio in connected voice channel')
async def play(ctx, q):
    YDL_OPTIONS = {'default_search': 'auto', 'format': 'bestaudio', 'noplaylist':'True'}
    FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
    voice = ctx.message.guild.voice_client

    
    with YoutubeDL(YDL_OPTIONS) as ydl:
        info = ydl.extract_info(q, download=False)
        a = info['url']   
    voice.play(FFmpegPCMAudio(a, **FFMPEG_OPTIONS))

这是错误:

[download] Downloading playlist: 117
[youtube:search] query "117": Downloading page 1
[youtube:search] playlist 117: Downloading 1 videos
[download] Downloading video 1 of 1
[youtube] PUrsOQanozo: Downloading webpage
[youtube] Downloading just video PUrsOQanozo because of --no-playlist
[download] Finished downloading playlist: 117
Ignoring exception in command play:
Traceback (most recent call last):
  File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "E:\sa.py", line 49, in play
    a = info['url']
KeyError: 'url'

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

Traceback (most recent call last):
  File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python310\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: KeyError: 'url'

试试这个:

a = info['entries'][0]['formats'][0]['url']