不和.py重写自定义错误

2024-05-18 06:52:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我对编码非常陌生,我想知道如何在这里实现“缺少开发人员角色”之类的自定义错误:

    @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()

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

^{pr2}$

但它总是输出无效的命令


Tags: 角色编码开发人员bot错误anyawaittemp
1条回答
网友
1楼 · 发布于 2024-05-18 06:52:33

您可以检查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>")

相关问题 更多 >