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

Discord机器人执行leave命令无法退出语音频道问题求助

Discord机器人leave命令无法退出语音频道的解决办法

你的问题出在leave命令中:判断存在的是ctx.voice_client,但实际执行断开操作的是ctx.guild.voice_client,这两个对象可能存在不一致的情况,导致你断开的并非机器人当前所在的语音客户端,因此出现提示发送成功但机器人未退出的现象。

修改后的代码

@bot.command(pass_context=True)
async def join(ctx):
    if ctx.author.voice:
        channel = ctx.message.author.voice.channel
        await channel.connect()
    else:
        await ctx.send("First, go to the voice chat")

@bot.command(pass_context=True)
async def leave(ctx):
    if ctx.voice_client:
        # 直接使用已判断存在的ctx.voice_client执行断开操作
        await ctx.voice_client.disconnect()
        await ctx.send("I left the voice chat")
    else:
        await ctx.send("I am not in voice chat")

额外排查建议

  • 确认你的Discord.py版本为稳定版,旧版本可能存在语音客户端相关的bug,建议更新到最新稳定版
  • 可以在leave命令中添加调试打印,验证两个客户端是否一致:
    print(f"ctx.voice_client channel: {ctx.voice_client.channel if ctx.voice_client else 'None'}")
    print(f"ctx.guild.voice_client channel: {ctx.guild.voice_client.channel if ctx.guild.voice_client else 'None'}")
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 18:02:39