discord-interactions.py

Modals

Forms with text, select menus, radio buttons, checkboxes and file uploads.

A modal is a form that opens on top of Discord. ModalBuilder builds it field by field, parse_values reads the answers.

Opening a modal#

from discord_interactions import ModalBuilder  @slash.command(description="Open a ticket")async def ticket(ctx):    modal = (        ModalBuilder("ticket", "Open a ticket")        .text_input("subject", "Subject", max_length=80)        .paragraph("details", "Details", "Describe the issue in a few sentences")    )    await ctx.send_modal(modal)

Every field sits in a Label: a title (45 characters) and an optional description (100 characters). A modal holds 5 components at most.

Receiving the answers#

@router.modal("ticket")async def received(interaction, values):    subject = values["subject"]    details = values.get("details", "No details")    await respond_to_interaction(interaction, f"Ticket “{subject}” created.", ephemeral=True)

The router recognizes the modal by its custom_id and passes a ModalSubmit to the handler.

Every field#

Method Field Received value
text_input, paragraph Short or long text str
select Text menu list[str]
user_select, role_select, channel_select, mentionable_select Entity menus list of IDs, objects through .users(), .roles(), .channels()
radio Radio buttons, one choice str or None
checkboxes Checkboxes, several choices list[str]
checkbox Single checkbox bool
file_upload File upload list of IDs, files through .attachments()
text Introduction text none
modal = (    ModalBuilder("signup", "Sign up")    .text("Fill in the form, it takes 30 seconds.")    .radio("slot", "Slot", [("Saturday", "sat"), ("Sunday", "sun")])    .checkboxes("activities", "Activities", ["Tournament", "Workshop", "Quiz"])    .checkbox("reminder", "Get a reminder", default=True))

File uploads#

modal = ModalBuilder("portfolio", "Portfolio").file_upload(    "images", "Images", max_values=4, file_types=["image"])  @router.modal("portfolio")async def portfolio(interaction, values):    await defer_interaction(interaction)    files = [await piece.to_file() for piece in values.attachments("images")]    await send_followup(interaction, files=files)

file_types accepts "image", "video", "audio" or extensions such as ".pdf".

Type a name: create_button, ModalBuilder, cooldown.