python - 如何从@commands.has_role()获取 "error"消息

标签 python error-handling bots discord discord.py

我最近使用discord.py制作了一个discord机器人。我尝试为某些命令授予权限。我有这个测试命令功能:

@client.command()
@commands.has_role('Moderator')
async def cool(ctx):
    await ctx.send("You are cool indeed!")

当用户没有“主持人”角色时,如何返回消息(错误)?

我已经尝试过这个:

async def on_command_error(ctx, error):
    if isinstance(error, commands.NoPrivateMessage):
        await ctx.send("*Private messages.* ")
    elif isinstance(error, commands.MissingRequiredArgument):
        await ctx.send("*Command is missing an argument:* ")
    elif isinstance(error, commands.DisabledCommand):
        await ctx.send("*This command is currenlty disabled. Please try again later.* ")
    elif isinstance(error, commands.CheckFailure):
        await ctx.send("*You do not have the permissions to do this.* ")
    elif isinstance(error, commands.CommandNotFound):
        await ctx.send("*This command is not listed in my dictionary.*")

但我没有从中得到任何返回。

最佳答案

on_command_error 函数中,检查 commands.MissingRolecommands.MissingAnyRole .

if isinstance(error, (commands.MissingRole, commands.MissingAnyRole)):
    ...

关于python - 如何从@commands.has_role()获取 "error"消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56580601/

相关文章:

powershell - 我是否需要try/catch和-ErrorAction?

iis-7 - 经典ASP网站的IIS7自定义错误处理问题

python - 为 python 脚本(bot)启动预定作业的最佳方式

python - 如何在 python 中有效地将带有条件的操作(例如 if)应用于大型 numpy 数组?

python - 计算允许 R 中的 QWERTY 错误的 Levenshtein 距离

python - 添加新列并删除重复项,以逐列替换空值

python - 自定义Python JSON object_hook

asp.net - 良好的错误处理实践

javascript - 类型错误 : Cannot read property 'message' of undefined - Twitter API

botframework - 带有 URL 的 Microsoft Teams 任务模块不起作用