简体   繁体   English

如何检查用户是否在 nextcord 中具有角色?

[英]How do I check if a user has a role in nextcord?

I am using nextcord and I am trying to check if a user has a role when they run a command.我正在使用 nextcord 并且我正在尝试检查用户在运行命令时是否具有角色。 I have no idea how to do this so I cannot provide an MRE.我不知道该怎么做,所以我无法提供 MRE。 I imagine that the code will be something like this:我想代码会是这样的:

@client.slash_command(name="test")
async def test(interaction:nextcord.Interaction):
 if interaction.user.has_role("Cool"):
  await interaction.send("You are cool!")
 else:
  await interaction.send("You are not cool.")

To do this, you will need to get the role first, and then check if a specific member is in that role.为此,您需要先获取角色,然后检查特定成员是否在该角色中。 Here is an example, however I believe this is not the only way to do this:这是一个例子,但我相信这不是唯一的方法:

from nextcord.utils import get

role = get(ctx.guild.roles, name='search for role by name')

if interaction.user in role:
     do something
else:
     do a different thing

Hope this helps希望这可以帮助

I found the answer, It is sort of like this answer but backwards:我找到了答案,它有点像这个答案,但倒退了:

@client.slash_command(name="check_role", description="Check if a user has a role", guild_ids=GUILD_IDS)
async def check_role(interaction:nextcord.Interaction, user:nextcord.Member):
  if nextcord.utils.get(interaction.guild.roles, name="Role Name") in user.roles:
    await interaction.send("True!")
  else:
    await interaction.send("False.")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM