discorddiscord.py

How to have a list of options you can choose from an argument within discord.py slash commands?


I want to achieve something like this but it is not possible:

[Example]

I tried:

@app_commands.option(name='name', description="Choose a name", choices=["Alice", "Bob", "James", "Charlie"])

No solution.


Solution

  • You need app_commands.choice.

    Code:

    @bot.tree.command(name="choices")
    @app_commands.choices(options = [
        app_commands.Choice(name="a",value="This is a"),
        app_commands.Choice(name="b",value="This is b")
    ])
    async def choices(interaction:discord.Interaction,options:app_commands.Choice[str]):
        await interaction.response.send_message(f"{options.value}. You choose option {options.name}",ephemeral=True)
    

    You can't use this properly without a slash command. This code returns both the value and the name property of options.

    You can name options variable anything but it has to match with the variable in the async def... line.

    ephemeral=True is optional. It makes the response disappear for anybody other than the user who used the command.