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

Disnake机器人执行voice_channel.connect()后代码停止运行求助

Disnake机器人执行voice_channel.connect()后代码停止执行的解决办法

我为机器人编写了如下斜杠命令:

@commands.slash_command(name='radio', description="Music", guild_ids=[])
async def radio_voice_cmd(self, inter: disnake.CommandInteraction,
                          voice_channel: disnake.VoiceChannel = commands.Param(description="Channel")):
    await inter.response.defer()
    source = FFmpegPCMAudio('http://stream.radioparadise.com')
    voice_client = await voice_channel.connect()
    await voice_client.play(source)
    print("Connected")
    await inter.edit_original_message(content="Успешно!")

该命令用于让机器人连接语音频道并播放音乐,但执行voice_client = await voice_channel.connect()后,后续代码不再继续执行。

核心原因

  1. 重复连接阻塞:如果机器人已经连接到当前服务器的任意语音频道,重复调用connect()会因默认参数(如self_deaf=True)或权限校验卡住,甚至抛出未捕获的异常终止代码。
  2. 连接超时未处理:connect()默认超时60秒,若频道连接时遇网络波动、权限不足等问题,会一直阻塞到超时,表现为代码停止执行。
  3. FFmpeg依赖问题:若服务器未安装FFmpeg,FFmpegPCMAudio初始化失败会直接终止代码,看起来像是connect()后的问题。

解决方案

方案1:检查现有连接,避免重复调用

在连接前先判断服务器是否已有活跃语音客户端,复用或移动连接:

@commands.slash_command(name='radio', description="Music", guild_ids=[])
async def radio_voice_cmd(self, inter: disnake.CommandInteraction,
                          voice_channel: disnake.VoiceChannel = commands.Param(description="Channel")):
    await inter.response.defer()
    
    # 获取当前服务器的语音客户端
    voice_client = voice_channel.guild.voice_client
    
    if voice_client:
        # 若已连接,直接移动到目标频道
        await voice_client.move_to(voice_channel)
    else:
        # 未连接则创建新连接,设置超时避免无限阻塞
        voice_client = await voice_channel.connect(timeout=10.0)
    
    # 初始化音频源(确保FFmpeg已安装)
    source = FFmpegPCMAudio('http://stream.radioparadise.com')
    
    # 停止当前播放的音频(如果有)
    if voice_client.is_playing():
        voice_client.stop()
    
    voice_client.play(source)
    print("Connected")
    await inter.edit_original_message(content="Успешно!")

方案2:添加异常捕获,定位具体问题

通过异常捕获明确连接或初始化过程中的错误:

@commands.slash_command(name='radio', description="Music", guild_ids=[])
async def radio_voice_cmd(self, inter: disnake.CommandInteraction,
                          voice_channel: disnake.VoiceChannel = commands.Param(description="Channel")):
    await inter.response.defer()
    try:
        # 初始化音频源前先确认FFmpeg路径(可选)
        source = FFmpegPCMAudio('http://stream.radioparadise.com', executable='/usr/bin/ffmpeg')
        voice_client = await voice_channel.connect(timeout=10.0)
        
        if voice_client.is_playing():
            voice_client.stop()
        voice_client.play(source)
        
        print("Connected")
        await inter.edit_original_message(content="Успешно!")
    except Exception as e:
        error_msg = f"执行失败: {str(e)}"
        print(error_msg)
        await inter.edit_original_message(content=error_msg)

必备检查项

  • 确保机器人角色拥有连接语音频道、发言、使用语音活动的权限。
  • 服务器必须安装FFmpeg,可通过ffmpeg -version验证,若路径非默认需在FFmpegPCMAudio中指定executable参数。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 04:54:53