Search code examples
pythondiscordnextcord

So, I am trying to restrict a slash command to a certain role. I am using nextcord.H


I am trying to make a slash command (/test) and I am getting a TypeError.

My code:

import nextcord
from nextcord.utils import get

client=nextcord.Client(intents=nextcord.Intents.all())

@client.slash_command(name="test")
async def test(interaction:nextcord.Interaction):
  if interaction.user in get(interaction.guild.roles, name="Test Role"):
    await interaction.send("You have this role!")
  else:
    await interaction.send("You do not have this role.")
client.run("token")

The error:

if interaction.user in get(interaction.guild.roles,name='Test Role'):
TypeError: argument of type 'Role' is not iterable

The above exception was the direct cause of the following exception:

Hope you can help, Thanks


Solution

  • As the error states it is returning a single varible ie Test Role and not a iterable, what you should do is check for the role within the user's roles something like this:

    @client.slash_command(name="test")
    async def test(interaction:nextcord.Interaction):
      role = nextcord.utils.get(interaction.guild.roles, name="Test Role")
      if role in interaction.user.roles:
        await interaction.send("You have this role!")
      else:
        await interaction.send("You do not have this role.")