discord-interactions.py

Reference

Components V2, modals and slash commands for discord.py, through direct Discord API calls.

Buttons

Select menus

Builders

Components V2

Modal fields

Types and constants

  • ButtonStyleButton styles: Primary (1), Secondary (2), Success (3), Danger (4), Link (5) and Premium (6).
  • ComponentTypeType numbers of every Discord component, from action row (1) to checkbox (23).
  • SeparatorSpacingSeparator spacing: Small (1) or Large (2).
  • TextInputStyleText input style: Short (single line) or Paragraph (multiple lines).
  • create_componentRaw component: {"type": component_type, **fields}. Handy for a type the module does not cover yet.

Building modals

  • ModalBuilderModern modal builder: every field sits in a Label with a title and description. 5 components max.

Reading values

  • ModalSubmitSubmitted values keyed by custom_id. Text and radio as str, checkbox as bool, everything else as list.
  • get_modal_valuesModal values as a {custom_id: value} dict.
  • parse_modalReads the values of a submitted modal. Same as parse_values.
  • parse_valuesReads the values of a modal or a select menu, with entities and files resolved.

Sending and replying

  • defer_interactionAcknowledges now to answer later (15 minutes). On a component, update=True shows nothing.
  • delete_original_responseDeletes the original response, or a followup with message_id.
  • edit_messageEdits an existing message. Attachments are kept when attachments is omitted.
  • edit_original_responseEdits the original response, or a followup with message_id.
  • get_original_responseFetches the original response (API dict), for example to get its ID.
  • respondResponds to the interaction, or sends a followup if it was already answered.
  • respond_to_interactionInitial interaction response. With update=True, edits the message of the clicked component.
  • send_followupFollowup message after a response or a defer.
  • send_messageSends a message to a channel, a thread, a context, a member's DMs, or as an interaction response.
  • send_modalOpens a modal. Accepts a ModalBuilder, a create_modal dict or the three separate arguments.
  • update_messageEdits the message of the clicked component. Shortcut for respond_to_interaction(..., update=True).

Updating components

Payloads

  • MessageFlagsMessage flags: EPHEMERAL, IS_COMPONENTS_V2, SUPPRESS_EMBEDS, SUPPRESS_NOTIFICATIONS and the other API flags.
  • build_message_payloadBuilds a message payload for any endpoint. Adds IS_COMPONENTS_V2 and turns content into a text block when needed.
  • parse_allowed_mentionsNormalizes allowed mentions: discord.py object, API payload, discord.js style or boolean switches.

Routing

  • InteractionRouterRoutes buttons, select menus and modals to handlers by the base_id of their custom_id.

Signed custom_id

Shortcuts

custom_id

HTTP

  • CallbackTypeInteraction response types: message (4), deferred (5, 6), update (7), autocomplete (8), modal (9).

Slash commands

  • SlashCommandA declared command. Created by @slash.command().
  • SlashCommandManagerEntry point for slash commands: declaration, execution, autocomplete and sync. Alias: SlashManager.
  • SlashGroupCommand group: /group subcommand or /group subgroup subcommand.
  • optionDecorator: adds an option to the command. Writing order is kept.
  • optionsDecorator: adds several options at once.

Context

  • SlashContextCommand context: interaction, author, channel, options and response methods.
  • create_contextCreates a SlashContext (internal use or tests).

Options

Permissions

Cooldowns

Autocomplete

Middlewares

Localization

Sync

  • CommandSyncFine-grained sync: targeted read, create, update and delete of commands.
  • SyncResultSync report: created, updated, deleted, errors.
  • SyncModeSync strategies.

Errors

Pseudo containers (deprecated)

Other

Type a name: create_button, ModalBuilder, cooldown.