discord-interactions.py

Buttons and select menus

Buttons, links, select menus and reading the selected values.

Every create_* function returns a dict ready for the Discord API. Put them in rows, and the rows go out with the message.

Buttons#

from discord_interactions import create_action_row, create_button, create_link_button row = create_action_row(    create_button("confirm", "Confirm", style="success", emoji="✅"),    create_button("cancel", "Cancel", style="secondary"),    create_link_button("https://discord.com", "Help"),)await ctx.respond("Confirm the order?", components=[row])

Available styles: primary, secondary, success, danger, link and premium. Custom emojis are written as in Discord: "<:name:123456789012345678>".

A row holds 5 buttons or a single select menu. Every custom_id must be unique within the message.

Data in the custom_id#

A button can carry a small JSON dict. It comes back on click, no database needed.

create_button("cart", "Add", data={"item": 42, "qty": 1})  @router.on("cart")async def cart(interaction, data):    item = data["item"]

Select menus#

from discord_interactions import create_action_row, create_select_menu, create_role_select_menu, select_option languages = create_select_menu(    "language",    placeholder="Your language",    options=[select_option("Python", "py", "Readable", emoji="🐍"), "Rust", ("Go", "go")],)roles = create_role_select_menu("roles", "Roles to grant", max_values=3)await ctx.respond(components=[create_action_row(languages), create_action_row(roles)])

Options accept select_option(...), a dict, a (label, value, description, emoji, default) tuple or a plain string.

Reading the values#

parse_values returns the values, with users, roles and channels resolved into discord.py objects.

@router.select("roles")async def roles(interaction):    values = parse_values(interaction)    roles = values.roles("roles")    await respond_to_interaction(interaction, " ".join(r.mention for r in roles), ephemeral=True)
Menu Method Returned type
Text values["id"] list[str]
Users values.users("id") discord.Member or discord.User
Roles values.roles("id") discord.Role
Channels values.channels("id") discord.py channel
Mentionables values.mentionables("id") members and roles

Type a name: create_button, ModalBuilder, cooldown.