You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord Bot突发无法播放YouTube音频问题求助

Discord音乐Bot突然无法播放音频问题

我的Discord Bot部署在家庭服务器的Docker容器中,此前正常运行数月且未做任何代码改动,昨晚起!play <URL>命令失效:Bot能加入语音频道,但无法播放音频,需手动断开后才能再次操作。首次播放失败后重试,出现以下错误信息:

[youtube] crOZk88eCcg: Downloading webpage
[youtube] crOZk88eCcg: Downloading android player API JSON
[youtube] Extracting URL: https://www.youtube.com/watch?v=crOZk88eCcg&ab_channel=0foofighter0
[youtube] crOZk88eCcg: Downloading webpage
[youtube] crOZk88eCcg: Downloading android player API JSON
[2023-03-22 00:27:59] [ERROR   ] discord.ext.commands.bot: Ignoring exception in command play
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/discord/ext/commands/core.py", line 190, in wrapped
    ret = await coro(*args, **kwargs)
  File "/usr/src/app/./main.py", line 180, in play
    voice.play(player, after=lambda e: print(f'Player error: {e}') if e else None)
  File "/usr/local/lib/python3.10/dist-packages/discord/voice_client.py", line 600, in play
    raise ClientException('Not connected to voice.')
discord.errors.ClientException: Not connected to voice.

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

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/discord/ext/commands/bot.py", line 1347, in invoke
    await ctx.command.invoke(ctx)
  File "/usr/local/lib/python3.10/dist-packages/discord/ext/commands/core.py", line 986, in invoke
    await injected(*ctx.args, **ctx.kwargs)  # type: ignore
  File "/usr/local/lib/python3.10/dist-packages/discord/ext/commands/core.py", line 199, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ClientException: Not connected to voice.
[2023-03-22 00:27:59] [INFO    ] discord.player: ffmpeg process 12 has not terminated. Waiting to terminate...
[2023-03-22 00:27:59] [INFO    ] discord.player: ffmpeg process 12 should have terminated with a return code of -9.

最初怀疑是ffmpeg出问题,但Bot未重启过,ffmpeg突然失效的可能性存疑,猜测可能是YouTube或Discord端有变更?

以下是!play命令的代码片段:

@client.command(pass_context = True)
async def play(ctx):
    url = ctx.message.content.split("!play ",1)[1]
    voice = discord.utils.get(client.voice_clients)
    if (ctx.author.voice):
        if voice == None:
            channel = ctx.message.author.voice.channel
            voice = await channel.connect()

        # Youtube Magic
        ydl_opts = {
        'format': 'worstaudio/worst',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
        }

        if "http" not in url:
            yt = YoutubeSearch(url, max_results=1).to_json()
            print(yt)
            yt_id = str(json.loads(yt)['videos'][0]['id'])
            yt_channel = str(json.loads(yt)['videos'][0]['channel']).strip().replace(" ", "")
            url = 'https://www.youtube.com/watch?v='+yt_id+"&ab_channel="+yt_channel

        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            song_info=ydl.extract_info(url, download=False)
        for file in os.listdir("./"):
            if file.endswith(".mp3"):
                os.rename(file, "song.mp3")

        voice.stop()

        embed_trk = discord.Embed(
                title="Now playing",
                color=ctx.author.color,
        )
        video_title = song_info.get('title', None)
        video_channel = song_info.get('uploader', None)
        embed_trk.add_field(name="Track title", value=video_title, inline=False)
        embed_trk.add_field(name="Channel", value=video_channel, inline=False)

        await ctx.send(embed=embed_trk)
        player = await YTDLSource.from_url(url, loop=client.loop, stream=True)
        voice.play(player, after=lambda e: print(f'Player error: {e}') if e else None)
        while voice.is_playing():
            await asyncio.sleep(1)
        
        await request_record(ctx, video_title)

    else:
        await ctx.send("You must be in a voice channel to run this command")

内容的提问来源于stack exchange,提问作者Aidan Tweedy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 05:33:31