Westlenando
Westlenando

Reputation: 88

Only allow specific roles to use a command

I've been making a command to change a user's role, but currently, everyone can use it. I've tried doing a few things. Here is my code:

async def addrole(ctx, user: discord.Member, role: discord.Role):
    await user.add_roles(role)
    await ctx.send(f"I gave {user.name} the role {role.name}")

Here is the only thing that I've been able to think of:

allowed = ["Role 1"]
if message.author.role in allowed:
   async def addrole(ctx, user: discord.Member, role: discord.Role):
      await user.add_roles(role)
      await ctx.send(f"I gave {user.name} the role {role.name}")

Thanks in advance!

Upvotes: 0

Views: 107

Answers (1)

MrSpaar
MrSpaar

Reputation: 3994

Just add @commands.has_any_roles():

@bot.command()
@commands.has_any_roles("Role 1", "Role 2")
async def addrole(ctx, user: discord.Member, role: discord.Role):
    await user.add_roles(role)
    await ctx.send(f"I gave {user.name} the role {role.name}")

If the user isn't allowed to use the command, it will raise a commands.MissingAnyRole error.

Upvotes: 1

Related Questions