How to allow only admins to execute a command

2020-02-15 05:21发布

i am writing following command

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

how can i restrict this command to admins only? I tried looking at ctx.author.roles.role and it says @everyone. How can i check if the given user is an admin or not?

1条回答
该账号已被封号
2楼-- · 2020-02-15 06:13

There are two ways: by a whitelist of roles using 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'''

or by permission using has_permissions

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

Both of these decorators are Checks, and will raise some subclass of CommandError for you to optionally handle if they fail.

查看更多
登录 后发表回答