【问题标题】:Discord.py Ignore Error Handling if Custom Handler Already ExistsDiscord.py 如果自定义处理程序已经存在,则忽略错误处理
【发布时间】:2021-07-23 09:29:12
【问题描述】:

我有一个 Discord.py 重写机器人,它通过 on_command_error 事件向某个频道发送错误消息。
我想知道,如果该命令的custom error handler 已经处理了错误,是否有任何方法可以忽略我的on_command_error() 事件。

【问题讨论】:

    标签: python error-handling discord.py


    【解决方案1】:

    您应该通过commands.Command.has_error_handler() 方法检查调用的命令是否具有错误处理程序。这是一个例子:

    @bot.command()
    async def my_command(ctx):
        raise Exception("oops")
    
    @my_command.error
    async def handle_error(ctx, error):
        # handle the error
    
    @bot.on_command_error(ctx, error):
        if ctx.command.has_error_handler():
            return  # since it has a custom error handler, do nothing here
        # if you reach this point, then there is no error handler
        # and you can handle it normally.
    

    参考资料:

    【讨论】:

    • 检查自定义错误处理程序,但如果我忘记添加 MissingPermissions 作为实例怎么办。如果自定义错误未处理,我将如何让我的 on_command_error 事件继续?
    猜你喜欢
    • 2012-12-06
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 2017-12-18
    • 1970-01-01
    • 2018-04-29
    • 2011-06-01
    • 1970-01-01
    相关资源
    最近更新 更多