【问题标题】:discord.py - Post error to on_command_errordiscord.py - 将错误发布到 on_command_error
【发布时间】: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


【解决方案1】:

您绝对可以使用附加参数手动调用全局错误处理程序:

@commands.Cog.listener()
async def on_command_error(self, ctx, error, passed = False)
   if passed:
       #error is sent from cog handler by us
   else:
       #error is from an error event
async def cog_command_error(self, ctx, error):
   #other stuff
   else:
       await on_command_error(self, ctx, error, True)

注意:这暂时可行,但绝对建议全局使用单个错误处理程序。即使用on_command_errorcog_command_error。两者都不是。

【讨论】:

    猜你喜欢
    • 2021-04-25
    • 2021-02-17
    • 2021-05-28
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    相关资源
    最近更新 更多