Search code examples
pythondiscordnextcord

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. I have no idea how to do this so I cannot provide an 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.")

Solution

  • 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.")