Discord.py on_command_error触发冷却但不发送Embed问题求助
问题解决:Discord.py冷却命令的错误提示无法发送
可能原因及解决方法
1. 全局错误处理被其他逻辑拦截
如果你的机器人存在多个on_command_error处理逻辑(比如不同Cog中的监听事件),当某个处理函数未重新抛出未处理的异常时,后续的错误处理(包括你定义的全局事件)会被拦截,无法触发。
解决方式:
检查所有错误处理代码,确保未处理的异常被重新抛出,让全局处理逻辑接手。示例:
@commands.Cog.listener() async def on_command_error(ctx, error): # 仅处理特定类型错误 if isinstance(error, commands.MissingPermissions): await ctx.send("你没有执行该命令的权限") # 其他错误重新抛出 else: raise error
2. 机器人缺少发送嵌入消息的权限
即使之前运行正常,频道权限可能被修改,导致机器人无法发送Embed或普通消息。
解决方式:
- 检查机器人在目标频道的权限,确保开启「发送消息」和「嵌入链接」权限;
- 在错误处理中添加额外捕获,排查发送时的具体错误:
@bot.event async def on_command_error(ctx, error): if isinstance(error, commands.CommandOnCooldown): embed = discord.Embed(title="错误!", description=f"该命令正在冷却,{round(error.retry_after, 2)}秒后可再次使用。", color=discord.Color.red()) try: await ctx.send(embed=embed) except Exception as send_err: print(f"发送冷却提示失败: {send_err}") else: raise error
3. 直接在命令内处理冷却异常
绕过全局错误处理,直接在命令逻辑中捕获冷却异常并发送提示,避免全局事件的兼容性问题。
修改后的命令代码:
@bot.command() @commands.cooldown(1, 10, commands.BucketType.user) async def scrungler(ctx): try: await ctx.send("<:scrungler:1082698194502287400>") except commands.CommandOnCooldown as cd_error: embed = discord.Embed(title="错误!", description=f"该命令正在冷却,{round(cd_error.retry_after, 2)}秒后可再次使用。", color=discord.Color.red()) await ctx.send(embed=embed)
内容的提问来源于stack exchange,提问作者Yohu
相关产品推荐
相关产品推荐

