Discord.py - Make a slash command with administrator permission

744 Views Asked by At

im making a bot for discord, using python and discord.py, but i have a problem, i need make a command for only administrator use, but i like this command appear how a slash command, i have this code:

@bot.tree.command(name="setbienvenida")
@commands.has_permissions(administrator=True)
async def set_welcome_message(ctx, *, message):
    global welcome_message
    welcome_message = message
    await ctx.send("Mensaje de bienvenida configurado.")

The command allows the discord server administrator to change the welcome message when someone joins the server to the liking of the administrator, also allowing mention of the name of the corresponding user and a channel that the administrator wants to put, such as an information channel. I have to change some more things, but if I change them it gives me an error, and if I don't change them, they don't work.I would like to know how I can do this, how I have to modify that code so that it does what I want, and at the same time it is a command only for administrators and is like a slash command. You have to think that I am new to making a discord bot, I am learning as I go by searching for information, yes I have programmed other things but never a discord bot, if I am here it is because I am already desperate, be patient with me and explain it to me as easily as possible.

  • watch many youtube tutorials
  • look for a lot of information in discord.py documentation
  • search for information in many different forums, this one included
  • think of many possible ways to do it
  • ask other programmers I know
1

There are 1 best solutions below

5
On

Two things got to my mind reading this.

1. I don't know if you have set up tree commands or not so I'll provide this example for it:

@bot.event
async def on_ready():
    print("Bot is online!")
    try:
        synced = await bot.tree.sync()
        print(f"Synced {len(synced)} commands.")
    except Exception as e:
        print(e)

A little correction for the code:

from discord.ext.commands import has_permissions

@bot.tree.command(name="setbienvenida")
@has_permissions(administrator=True)
async def set_welcome_message(interaction:discord.Interaction,message:str):
    global welcome_message
    welcome_message = message
    await interaction.response.send_message("Mensaje de bienvenida configurado.",ephemeral=True)

What I changed here:

  • I imported the @has_permissions decorator and called it directly
  • I changed ctx to interaction:discord.Interaction, because slash commands are responding to an interaction not to commands.Context (ctx) itself.
  • I removed the * from the function and added the message:str instead because slash command arguments are taking in everything until the user clicks out of them
  • I changed ctx.send to interaction.response.send_message as this is the proper way to respond to a slash interaction
  • I added ephemeral=True to the confirmation message. In this way it will only be visible to the administrator who sent the command (this is not necessary just a feature)

And that's it.