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

发布于 2021-01-29 17:57:30

我正在写以下命令

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

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

关注者
0
被浏览
50
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    有两种方法:通过使用的角色白名单
    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'''
    

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



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看