discord-interactions.py

Select menus

Text, user, role and channel select menus, reading the choices, common errors.

A select menu works like a button: you send it with a custom_id, then you react when someone picks something. The only difference: instead of a click, you receive a list of values.

Every example assumes the skeleton from Getting started: slash, router and an on_interaction that calls router.handle.

1. The simplest menu#

from discord_interactions import create_action_row, create_select_menu, respond_to_interaction  @slash.command(description="Your favourite colour")async def colour(ctx):    menu = create_select_menu(        "colour",  # the custom_id the router will recognise        placeholder="Pick a colour",  # grey text before a choice        options=["Red", "Green", "Blue"],    )    await ctx.respond("Which colour?", components=[create_action_row(menu)])  @router.select("colour")async def colour_picked(interaction):    choice = interaction.data["values"][0]    await respond_to_interaction(interaction, f"You picked: {choice}", ephemeral=True)

What happens:

  1. /colour sends a message with a menu. Like a button, the menu goes in a row (create_action_row), alone.
  2. When someone picks "Green", Discord sends an interaction with custom_id = "colour" and values = ["Green"].
  3. @router.select("colour") catches it. interaction.data["values"] is always a list, even when only one choice is allowed: hence the [0].

2. A value different from the displayed text#

What the user sees (label) and what your code receives (value) can differ. Handy to receive IDs instead of sentences.

menu = create_select_menu(    "language",    options=[        select_option("Python", "py", "Simple and readable", emoji="🐍"),        select_option("Rust", "rs", "Fast and safe", emoji="🦀"),        select_option("JavaScript", "js", "Everywhere on the web", emoji="🟨"),    ],)

select_option(label, value, description, emoji, default): only label is required. When "Python" is picked, your code receives ["py"].

Options accept several equivalent forms:

options = [    "Red",  # label = value = "Red"    ("Green", "green"),  # (label, value)    ("Blue", "blue", "The colour of the sky"),  # (label, value, description)    select_option("Black", "black", emoji="⚫", default=True),  # preselected]

3. Several choices at once#

menu = create_select_menu(    "toppings",    placeholder="Toppings (up to 3)",    min_values=1,  # at least 1    max_values=3,  # at most 3    options=["Cheese", "Ham", "Mushrooms", "Olives", "Pineapple"],)  @router.select("toppings")async def toppings(interaction):    choice = interaction.data["values"]  # e.g. ["Cheese", "Olives"]    await respond_to_interaction(interaction, "Your pizza: " + ", ".join(choice), ephemeral=True)

min_values=0 lets users clear everything, which is useful for "remove all my roles".

4. Keep the choice displayed in the menu#

After a choice, Discord resets the menu on screen. To keep the selection visible, send the menu back with the chosen option marked default=True, through update_message:

def size_menu(chosen=None):    options = [select_option(s, s.lower(), default=(s.lower() == chosen)) for s in ("Small", "Medium", "Large")]    return create_action_row(create_select_menu("size", "Size", options=options))  @slash.command(description="Order a coffee")async def coffee(ctx):    await ctx.respond("Which size?", components=[size_menu()])  @router.select("size")async def size(interaction):    chosen = interaction.data["values"][0]    await update_message(interaction, content=f"Size: **{chosen}**", components=[size_menu(chosen)])

The message is edited in place: the text changes and the menu keeps "Medium" displayed.

5. User select#

Discord fills the list with the server members itself. You write no options.

from discord_interactions import create_action_row, create_user_select_menu, parse_values  @slash.command(description="Build a team")async def team(ctx):    menu = create_user_select_menu("team", "Pick up to 4 players", max_values=4)    await ctx.respond(components=[create_action_row(menu)])  @router.select("team")async def team_picked(interaction):    values = parse_values(interaction)    players = values.users("team")  # list of discord.Member    await respond_to_interaction(interaction, "Team: " + " ".join(p.mention for p in players))

interaction.data["values"] would only hold IDs as text (["80351110224678912"]). parse_values(...).users(...) gives you the discord.py objects directly, with their name, roles and so on.

6. Role, channel and mentionable selects#

Same principle, one function per type:

create_role_select_menu("roles", "Pick roles", max_values=3)create_channel_select_menu("channel", "Pick a channel")create_channel_select_menu("voice", "Voice channel", channel_types=[discord.ChannelType.voice.value])create_mentionable_select_menu("target", "A member or a role")

And to read the choices:

Menu Read with What you get
Text interaction.data["values"] list[str] of the values
Users parse_values(interaction).users("id") discord.Member (or discord.User in DMs)
Roles parse_values(interaction).roles("id") discord.Role
Channels parse_values(interaction).channels("id") discord.py channels
Mentionables parse_values(interaction).mentionables("id") members and roles mixed

channel_types limits the channels offered: text (0), voice (2), category (4), news (5), forum (15)…

7. Prefill an entity select#

menu = create_user_select_menu("guests", "Guests", max_values=5, default_values=[ctx.user])menu = create_role_select_menu("roles", "Your roles", max_values=10, default_values=ctx.user.roles[1:])

default_values accepts discord.py objects or IDs. ctx.user.roles[1:] skips @everyone.

8. Full example: self-assignable roles#

Each member picks their notification roles. The menu only offers the roles you listed: nobody can give themselves a moderator role.

roles.py
ROLES = {    "123456789012345678": ("Announcements", "📢"),    "234567890123456789": ("Events", "🎉"),    "345678901234567890": ("Updates", "🔧"),}  @slash.command(description="Pick your notification roles")async def notifications(ctx):    current = {str(r.id) for r in ctx.user.roles}    options = [select_option(name, rid, emoji=emoji, default=rid in current) for rid, (name, emoji) in ROLES.items()]    menu = create_select_menu("notifs", "Your notifications", options=options, min_values=0, max_values=len(options))    await ctx.respond(components=[create_action_row(menu)], ephemeral=True)  @router.select("notifs")async def notifs(interaction):    await defer_interaction(interaction, update=True)  # several API calls: take our time    member = interaction.user    wanted = set(interaction.data["values"])    to_add = [interaction.guild.get_role(int(r)) for r in wanted if not member.get_role(int(r))]    to_remove = [interaction.guild.get_role(int(r)) for r in ROLES if r not in wanted and member.get_role(int(r))]    await member.add_roles(*to_add)    await member.remove_roles(*to_remove)    await send_followup(interaction, f"{len(to_add)} role(s) added, {len(to_remove)} removed.", ephemeral=True)

For this to work, the bot needs the Manage Roles permission, and its own role must be above the roles it hands out in the server role list. Otherwise: 403 Forbidden (50013).

9. A menu in a V2 card#

In a container, a menu goes in .row(...) like buttons:

card = container("## Preferences").add("Pick your language:").row(    create_select_menu("language", options=[("Français", "fr"), ("English", "en")]))await ctx.respond(components=[card])

10. A menu in a modal#

Menus also work in forms. See Modals: ModalBuilder(...).select("id", "Title", options).

Common errors#

What you see Why Fix
"This interaction failed" after picking No response within 3 seconds, or the custom_id matches no @router.select(...) Check the exact spelling of the ID; for slow work, defer_interaction(interaction, update=True) first
The menu goes back to empty after a choice Normal Discord behaviour Send the menu back with default=True on the chosen option (section 4)
TypeError: string indices must be integers or a single letter received You treated values as a string values is a list: interaction.data["values"][0]
400 Bad Request (50035) Must be 25 or fewer in length More than 25 options Split into several menus, or use a slash command with autocomplete
400 Bad Request (50035) on min_values/max_values max_values exceeds the number of options, or min_values > max_values max_values=len(options) at most
400 Bad Request (50035) The specified option value is already used Two options share a value Each value must be unique in the menu
400 Bad Request (50035) on components.0.components A menu shares its row with another component One menu = one row of its own
parse_values(...).users("id") returns an empty list Wrong custom_id, or a text menu read as an entity menu Pass exactly the menu's custom_id, and use .users only for a user select
403 Forbidden (50013) when giving a role The bot lacks "Manage Roles" or its role is too low Move the bot's role above the roles involved

The Common errors page details Discord's error codes.

Type a name: create_button, ModalBuilder, cooldown.