【问题标题】:How to allow only admins to execute a command如何只允许管理员执行命令
【发布时间】:2019-01-19 18:01:22
【问题描述】:

我正在编写以下命令

@bot.command(pass_context=True)
async def admins_only_command(ctx, *, args):
    '''do stuff

我怎样才能将此命令限制为仅限管理员?我试着查看ctx.author.roles.role,上面写着@everyone。如何检查给定用户是否为admin

【问题讨论】:

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


    【解决方案1】:

    有两种方式:使用has_any_role的角色白名单

    @bot.command(pass_context=True)
    @commands.has_any_role("Big Cheese", "Medium Cheese")
    async def admins_only_command(ctx, *, args):
        '''do stuff'''
    

    或经许可使用has_permissions

    @bot.command(pass_context=True)
    @commands.has_permissions(administrator=True)
    async def admins_only_command(ctx, *, args):
        '''do stuff'''
    

    这两个装饰器都是Checks,如果它们失败,它们会引发CommandError 的一些子类,供您选择处理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多