【问题标题】:How can i prevent multiple error instances from triggering? discord.py rewrite如何防止触发多个错误实例? discord.py 重写
【发布时间】:2020-04-22 05:40:00
【问题描述】:

所以我为我的不和谐机器人制作了一个错误处理程序。我相信我已经将所有实例都放入了 if 语句中。但是,当发生错误时,它一次会发送 2-3 个错误实例消息。我怎样才能解决这个问题?我在想一个错误消息计数器,但我不知道该怎么做。

这是我的处理程序:

错误处理程序

@bot.event
async def on_command_error(ctx, error):
    
    if isinstance(error, commands.CheckFailure):
        await ctx.send("[HANDLER] You do not have the required role to use this command!")
        print(error)
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("[HANDLER] You do not have sufficient permission settings to use this command!")
        print(error)
    if isinstance(error, commands.UserInputError):
        await ctx.send("[HANDLER] Invalid input! You have most likely typed something wrong; check spelling and try again. Use $help for more information.")
        print(error)
    if isinstance(error, commands.CommandNotFound):
        await ctx.send("[HANDLER] That command doesn't exist! You may have typed it wrong, try again. Use $help for more information")
        print(error)
    if isinstance(error, commands.BotMissingPermissions):
        await ctx.send("[HANDLER] I'm missing the permissions to use this command!")
        print(error)
    if isinstance(error, commands.MissingRequiredArgument):
        await ctx.send("[HANDLER] You missed an argument! For help do $help [command].")
        print(error)
    if isinstance(error, commands.BadArgument):
        await ctx.send("[HANDLER] Could not find that value!")
        print(error)
    if isinstance(error, commands.DisabledCommand):
        await ctx.send("[HANDLER] This command has been disabled!")
        print(error)
    if isinstance(error, commands.NoPrivateMessage):
        await ctx.send("[HANDLER] This command can not be used inside private messages!")

提前致谢!!!

【问题讨论】:

    标签: python bots discord discord.py discord.py-rewrite


    【解决方案1】:

    您没有指定是否收到不同的错误实例消息,但由于您的 if 语句只是 if 语句而不是 if else 语句,因此如果错误属于一种以上类型,则会输出两条错误消息。

    【讨论】:

    • 如果我把它放在一个单独的 if 语句中,它们之间有一个“或”,它会解决它吗?那行得通吗?
    • 不,我的意思是用 elif 替换第一个 if 之后的 if 语句,这样一旦打印了一条消息,它就不会检查其他 if 语句。
    • 成功了,谢谢。这些都是错误实例吗?
    • 看起来可能有更多错误类型,但我不完全确定它们是否都与您希望通过机器人告知用户的错误类型相关。 This 链接有各种命令错误,但就像我说的那样,由您决定哪些是足够相关的以发送消息。
    猜你喜欢
    • 2016-11-29
    • 1970-01-01
    • 2010-11-20
    • 2010-12-24
    • 2016-06-25
    • 1970-01-01
    • 2011-02-03
    • 1970-01-01
    • 2011-01-11
    相关资源
    最近更新 更多