Discord.py:我的程序似乎无法区分错误(MissingPerms、BotMissingPerms),因此使用了错误的处理程序

Discord.py: My programm seems to be unable to differentiate between errors(MissingPerms, BotMissingPerms) and therefore uses the wrong handler

我正在尝试捕获用户错误使用命令可能产生的一些错误。 为了对此进行测试,我创建了一个按需引发错误的命令和一个应该捕获错误的函数。 问题是,我尝试捕获 commands.BotMissingPermisionscommands.MissingPermissionscommands.CommandOnCooldown 似乎被忽略并被处理为 commands.CommandInvokeError。其他错误,例如 TooManyArgumentsNotOwner 会被很好地捕获。

那是我的代码:

Import discord
from discord.ext Import commands

bot = commands.Bot(command_prefix='~')

@bot.event
async def on_command_error(ctx, error):
  if isinstance(error, commands.MissingPermissions):
    await ctx.send('missing perms')
  elif isinstance(error, commands.BotMissingPermissions):
    await ctx.send('bot missing perms')
  elif isinstance(error, commands.CommandOnCooldown):
    await ctx.send('cooldown')
  elif isinstance(error, commands.CommandInvokeError):
    await ctx.send('invoke')
  else:
    await ctx.send('should not happen')

@bot.command
async def doError(ctx, type : int):
  if(type == 0):
    raise commands.MissingPermissions
  elif(type == 1):
    raise commands.BotMissingPermissions
  elif(type == 2):
    raise commands.CommandOnCooldown

bot.run(token)

这是我第一次在这里提问,如果您需要更多信息,请告诉我

您尝试捕获的命令错误旨在在执行命令的回调(来自检查、转换器等)之前引发。回调中引发的异常将包装在 CommandInvokeError 中,因此它们的来源一目了然。

例如,您可以有一个像

这样的错误处理程序
@bot.event
async def on_command_error(ctx, error):
  if isinstance(error, commands.TooManyArguments):
    await ctx.send('too many arguments')
  elif isinstance(error, commands.CommandInvokeError):
    await ctx.send('invoke')
  else:
    await ctx.send('should not happen')

和像

这样的命令
@bot.command
async def doError(ctx, type : int):
  raise commands.TooManyArguments

如果您实际上向命令传递了太多参数,那么命令处理机制将产生一个 TooManyArguments 异常并将其传递给处理程序。另一方面,如果 你的回调 产生了一个 TooManyArguments 异常,那么命令机制将接收该异常,将其包装在 CommandInvokeError 中,然后传递给它给处理程序。