使用DiscordUtils的Discord音乐机器人播放歌曲中途停止问题求助
问题解决方法
核心原因及修复方案
- 第一处错误是创建播放器的参数拼写错误,你代码中的
ffmpeg_error_bettercix=True参数名拼写错误,正确参数名为ffmpeg_error_betterfix,拼写错误会导致FFmpeg自动错误修复逻辑未生效,流媒体传输中断时直接终止播放。
修复后的创建播放器代码如下:
player = music.create_player(ctx, ffmpeg_error_betterfix=True, ffmpeg_options={ 'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn' })
新增的FFmpeg参数会让FFmpeg在遇到流媒体链接断开时自动尝试重连,避免因为YouTube临时流链接有效期到期导致的播放中断。
- 第二处关联问题是DiscordUtils本身已停止维护,其内置的YouTube流抓取逻辑使用的是旧版接口,获取到的音频流链接默认只有半首歌左右的有效时长,到期后链接失效就会停止播放。如果修改参数后问题仍然存在,你可以替换库内部的流抓取逻辑使用yt-dlp实现,或是更换为仍在维护的Discord音乐库。
其他代码错误修复
你提供的代码中还有两处功能性笔误,也需要同步修改:
nowplaying命令逻辑写反,当前代码判断song.name is None时才发送播放信息,实际应该改为:
@client.command() async def nowplaying(ctx): player = music.get_player(guild_id=ctx.guild.id) song = player.now_playing() if song: await ctx.send(f'{song.name} is currently playing.')
remove命令缺少方法调用的括号和参数传递,修改为:
@client.command() async def remove(ctx, index: int): player = music.get_player(guild_id=ctx.guild.id) song = await player.remove_from_queue(int(index)) await ctx.send(f'Removed {song.name} from the song queue!')
内容的提问来源于stack exchange,提问作者Execed
相关产品推荐
相关产品推荐

