【问题标题】:How to get "error" message from @commands.has_role()如何从@commands.has_role() 获取“错误”消息
【发布时间】:2019-10-28 01:17:53
【问题描述】:

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

@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.*")

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

【问题讨论】:

    标签: python error-handling bots discord discord.py


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 2011-03-06
      • 1970-01-01
      • 1970-01-01
      • 2017-06-08
      • 2011-02-12
      • 2012-11-05
      相关资源
      最近更新 更多