I'd like to make my discord slash command only usable for admins

605 Views Asked by At

I want this command only usable for those who has one of my 3 specific roles.

@bot.tree.command(name = "addrole", description = "Rangadás", guild=discord.Object(id=my server's id))
async def addrole(interaction: discord.Interaction, member: discord.Member, role: discord.Role):
    await member.add_roles(role)
    await interaction.response.send_message(f"{member.mention}-ra ráadva: `{role.name}`!")

How can I make it? Thank you for your help!

I tried like this:

@bot.tree.command(name = "addrole", description = "Rangadás", guild=discord.Object(id=my server's id))
@commands.has_any_roles(id1, id2, id3)
async def addrole(interaction: discord.Interaction, member: discord.Member, role: discord.Role):
    await member.add_roles(role)
    await interaction.response.send_message(f"{member.mention}-ra ráadva: `{role.name}`!")
1

There are 1 best solutions below

0
Blue Robin On

How this works

Using guild_permissions (under the member class) with type permissions we can check if a member has admin.

Code

if interaction.user.guild_permissions.admin:
    #Add the code that you want to run
else:
    #Add error message

Resources