【发布时间】:2021-06-29 15:43:48
【问题描述】:
我的 cog 有一个错误处理程序,cog_command_error,但我希望,如果错误不能在本地错误处理程序中处理,它将被发送到通用错误处理程序 on_command_error。 如何发布错误?
我的本地错误处理程序(短路):
# mod.py
async def cog_command_error(self, ctx, error):
if ctx.command.name == 'purge':
if isinstance(error, commands.BadArgument):
await ctx.send(embed=Embed(color=Color.orange(), description="The number must be a integrer number!"))
if ctx.command.name == "kick" or "ban":
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send(embed=Embed(color=Color.orange(), description="You must mention the member you will ban!"))
else:
# Post the error to on_command_error
一般错误处理程序(短):
# error.py
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
# check for local error handler
if hasattr(ctx.command, 'on_error'):
return
# check for cog error handler
if ctx.cog:
cog = ctx.cog
if cog._get_overridden_method(cog.cog_command_error) is not None:
return
if isinstance(error, commands.MissingPermissions):
await ctx.send("You have not the permission to execute this command!")
return
else:
print('Ignoring exception in command {}:'.format(ctx.command), file=sys.stderr)
traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr)
感谢您的帮助
【问题讨论】:
-
我不知道这是否是一个复制错误,但您在
cog_command_error方法中缺少一些awaits,应该等待ctx.send。 -
它不见了,我修好了
标签: python error-handling discord.py