How to allow only admins to execute a command

一曲冷凌霜 提交于 2019-11-28 12:01:45

问题


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:


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.



来源:https://stackoverflow.com/questions/51814995/how-to-allow-only-admins-to-execute-a-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!