【发布时间】:2020-04-27 12:14:30
【问题描述】:
我正在尝试捕获一些可能由用户错误地使用命令产生的错误。
为了测试这一点,我创建了一个按需引发错误的命令和一个应该捕获它们的函数。
问题是,我试图捕捉commands.BotMissingPermisions、commands.MissingPermissions 和commands.CommandOnCooldown 似乎被忽略并作为commands.CommandInvokeError 处理。其他错误,例如 TooManyArguments 或 NotOwner 都能很好地捕获。
这是我的代码:
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)
这是我第一次在这里提问,所以如果您需要更多信息,请告诉我
【问题讨论】:
标签: python-3.x error-handling command discord.py