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

discord.py编写错误处理器提示discord.ext.commands无error属性如何解决

报错原因

触发AttributeError的直接原因是:discord.py的discord.ext.commands模块下不存在名为error的子模块,所有命令相关的异常类直接暴露在commands根命名空间下,commands.error.xxx的调用路径完全非法。

除此之外你提供的代码还存在多处语法、逻辑错误:

  • 类型判断函数拼写错误,正确写法为isinstance,你误写为ifistance
  • isinstance参数缺失,该函数需要传入「待判断对象」和「目标类型」两个参数,你没有传入捕获到的error对象
  • 异常类名拼写错误,权限缺失的正确异常类名为MissingPermissions,你误写为MissingPermissons
  • 不符合Python缩进规则,函数内部的逻辑代码没有做缩进处理
修复后代码
from discord.ext import commands

# bot初始化等其他代码保持不变
async def on_command_error(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("You do not have permission to run this command!")
核心修复点
  • 移除非法的.error中间层级,直接通过commands.MissingPermissions调用权限缺失异常类
  • 修正isinstance的拼写错误,补全error作为第一个判断参数
  • 修正异常类名的拼写错误
  • 补全代码缩进,符合Python语法要求

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 21:06:03