检查用户是否具有特定角色 [英] Check if User has a certain role
问题描述
我有一些代码,您可以在其中键入 -giverole< user> < rolename>
例如 -giverole @Soup执行董事会
。
我现在需要的是一种检查用户键入命令是否具有特定角色的方法。
I have code in which you can type -giverole <user> <rolename>
e.g. -giverole @Soup Board of Executives
.
What I need now is a method that checks to see if the user typing the command has a certain role.
我有可以给某人角色的代码:
I have code that can give a role to someone:
@client.command(pass_context=True)
async def giverole(ctx, member: discord.Member, *, role: discord.Role):
await client.add_roles(member, role)
await client.say("The role '" + str(role) + "' has been given to " + member.mention + " .")
如果用户的排名正确,则应 await client.say()
。如果不这样做,则会引发错误消息。
It should do await client.say()
if the user has the right rank. If they don't, then it raises an error message.
推荐答案
您可以使用 commands.has_role
检查以确定调用命令的人是否具有特定角色:
You can use the commands.has_role
check to determine whether or not the person invoking the command has a particular role:
@client.command(pass_context=True)
@has_role("Role Name")
async def giverole(ctx, member: discord.Member, *, role: discord.Role):
await client.add_roles(member, role)
await client.say(f"The role '{role}' has been given to {member.mention}.")
没有角色的某人尝试调用它时, 命令。将引发CheckFailure
错误。然后,您可以处理该错误希望机器人说些什么:
When someone without the role tries to invoke it, a commands.CheckFailure
error will be raised. You can then handle that error if you want the bot to say something:
@giverole.error
async def giverole_error(error, ctx):
if isinstance(error, CheckFailure):
await client.send_message(ctx.message.channel, "You are lacking a required role")
else:
raise error
这篇关于检查用户是否具有特定角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!