如何从@ commands.has_role()获取“错误”消息

时间:2019-06-13 12:33:15

标签: 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.*")

但是我从中什么也得不到。

1 个答案:

答案 0 :(得分:0)

if isinstance(error, (commands.MissingRole, commands.MissingAnyRole)): ... 函数中,检查commands.MissingRolecommands.MissingAnyRole

function method (b, c) {
  ...
}

const Module = {
  install (x, y) {
    ...
    method(b, c)
    ...
  }
}

export default Module