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
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:
A little correction for the code:
What I changed here:
@has_permissions
decorator and called it directlyctx
tointeraction:discord.Interaction
, because slash commands are responding to an interaction not tocommands.Context
(ctx) itself.*
from the function and added themessage:str
instead because slash command arguments are taking in everything until the user clicks out of themctx.send
tointeraction.response.send_message
as this is the proper way to respond to a slash interactionephemeral=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.