Hello! I had a question for making a slash command with my Discord bot (Fixed, link is here: Need help making a slash command)
Now I have another question: How do I add arguments?
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)
@tree.command(name = "initiate-all", description = "Initiate the PyGoose command g4-sAB.GXC")
async def initiate(interaction):
commandname = "g4-sAB.GXC"
await interaction.response.send_message(f"`initiated {commandname}.`")
@client.event
async def on_ready():
print(f'Using discord.py version {discord.__version__}')
print('Logged in as {0.user}'.format(client))
await tree.sync()
The most important part is the @tree.command, where I declare my command as I learned, but I would like to have initiate-all written as initiate followed by an optional argument (in this case, all). How do I do this?
Specify the parameters in your command function. For example you can add an argument called option to the initiate function and give it a default value, like None or anything like that.
@tree.command(name="initiate", description="Initiate a command with an optional argument")
@app_commands.describe(option="The optional argument, can be 'all'")
async def initiate(interaction: discord.Interaction, option: str = None):
commandname = "g4-sAB.GXC"
if option == 'all':
await interaction.response.send_message(f"`initiated {commandname} with option {option}.`")
else:
await interaction.response.send_message(f"`initiated {commandname}.`")
You can use the choice class (discord.app_commands.Choice), which allows you to create a list of options that the user can select from when using the command.
# You can use the @app_commands.choices decorator and add the parameter
@tree.command(name="choose_color", description="Choose a color")
@app_commands.choices(color=color_choices)
async def choose_color(interaction: discord.Interaction, color: app_commands.Choice[str]):
await interaction.response.send_message(f"You chose the color {color.value}!")