Discord、py ffmpeg和youtube_dl音乐机器人未播放播放列表

2024-04-29 06:46:20 发布

您现在位置:Python中文网/ 问答频道 /正文

大家好,我的社区, 我遇到了以下问题:

我使用以下代码与我的discord.py bot一起播放音乐:

@bot.command()
async def activate(ctx, url):
    
    YDL_OPTIONS = {'format': 'bestaudio', 'noplaylist':'True'}
    FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
    voice = get(bot.voice_clients, guild=ctx.guild)

    if not voice.is_playing():
        with YoutubeDL(YDL_OPTIONS) as ydl:
            info = ydl.extract_info(url, download=False)
        URL = info['formats'][0]['url']
        voice.play(FFmpegPCMAudio(URL, **FFMPEG_OPTIONS))
        voice.is_playing()

        else:
            await ctx.send("Already playing song")
            return

但是,如果我现在粘贴播放列表链接,我会出现以下错误:

Ignoring exception in command activate:
Traceback (most recent call last):
  File "C:\Users\\PycharmProjects\MusicBot\venv\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:/Users//PycharmProjects/MusicBot/main.py", line 47, in activate
    URL = info['formats'][0]['url']
KeyError: 'formats'

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

Traceback (most recent call last):
  File "C:\Users\\PycharmProjects\MusicBot\venv\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\\PycharmProjects\MusicBot\venv\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\\PycharmProjects\MusicBot\venv\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: 'formats'

有没有办法修复它,让我的机器人播放播放列表? 谢谢你的回答


Tags: inpyurlbotlineusersextcommands