Need help making a slash command (again) but this time with with arguments

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}.`")
1 Like

Alright, got it! And can the default value be anything? And how will it show up?

Also, I get an error when I write @app_commands.initiate(), it says “describe is not a known member of module ‘discord’”, even though it works.

Yes it can! Just remember to match the type of the parameter you’re defining.

Check to see if you are using the latest version.

I’m on v2.3.2, but it works anyway. Also, I made a mistake. Instead of @app.commands.initiate(), it’s .describe.

Another thing, what are all the types for a discord.Interaction aside from str?

There’s a ton of them (str, boolean, int, float, discord.user, discord.emoji, etc), you can check the docs and see what you want to use.

1 Like

And how can I make predefined choices? I can’t quite understand the docs on that topic.

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.

For example:

# Define your choices
color_choices = [
    app_commands.Choice(name="Red", value="red"),
    app_commands.Choice(name="Blue", value="blue"),
    app_commands.Choice(name="Green", value="green"),

And then

# 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}!")
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.