【问题标题】:Discord.py Rewrite custom errorsDiscord.py 重写自定义错误
【发布时间】:2019-04-12 04:16:59
【问题描述】:

我对编码非常陌生,我想知道如何在此处实现自定义错误,例如“缺少开发角色”:

    @bot.command()
@commands.has_any_role("Temp.", "Owner")
async def sh(ctx):
    await ctx.message.add_reaction(':true:508022488093949973')
    await ctx.send("<a:siren:507952050181636098> `Shutting down` <a:siren:507952050181636098>")
    await bot.logout()

我有一个像这样的简单处理程序

@bot.event
async def on_command_error(ctx, error):
    await ctx.message.add_reaction(':false:508021839981707304')
    await ctx.send("<a:siren:507952050181636098> `Invalid command` <a:siren:507952050181636098>")

但它总是输出无效的命令

【问题讨论】:

    标签: python python-3.x discord.py discord.py-rewrite


    【解决方案1】:

    您可以检查error 的类以确定您正在处理的错误类型。如果将此与特定于命令的error handler 配对,则可以编写一个响应来告诉用户他们缺少什么:

    @sh.error
    async def sh_error(ctx, error):
        if isinstance(error, commands.CheckFailure):
            await ctx.send("You do not have the correct roles Temp. or Owner")
    
    @bot.event
    async def on_command_error(ctx, error):
        if not isinstance(error, commands.CheckFailure): 
            await ctx.message.add_reaction(':false:508021839981707304')
            await ctx.send("<a:siren:507952050181636098> `Invalid command` <a:siren:507952050181636098>")
    

    【讨论】:

      猜你喜欢
      • 2021-04-04
      • 2021-07-15
      • 2012-04-26
      • 1970-01-01
      • 2023-04-03
      • 2014-06-07
      • 2021-03-09
      • 2020-12-18
      • 2021-03-31
      相关资源
      最近更新 更多