代码之家  ›  专栏  ›  技术社区  ›  Em Ae

如何只允许管理员执行命令

  •  1
  • Em Ae  · 技术社区  · 8 年前

    我在写以下命令

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

    ctx.author.roles.role 上面写着 @everyone admin 还是不呢?

    1 回复  |  直到 8 年前
        1
  •  1
  •   Patrick Haugh    7 年前

    有两种方法:使用 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 如果他们失败了,你可以随意处理。