discord-interactions.py

Messages and updates

Send, reply, defer, edit, then change the components of an existing message.

Every sending function shares the same parameters: content, components, files, allowed_mentions, ephemeral, flags.

Responding to an interaction#

Function When
respond_to_interaction(i, ...) Initial response, within 3 seconds
update_message(i, ...) After a click: edits the component's message
defer_interaction(i) Long task: answer within 15 minutes
send_followup(i, ...) Following messages
edit_original_response(i, ...) Change the first response
respond(i, ...) Initial response or followup, depending on the state
await defer_interaction(interaction, ephemeral=True)result = await long_task()await send_followup(interaction, f"Result: {result}", ephemeral=True)

Sending outside an interaction#

send_message accepts a channel, a thread, a commands.Context, or a member for a direct message.

message = await send_message(channel, components=[container("## Announcement")])await send_message(channel, "Thanks everyone", reply_to=message)await send_message(member, "Your ticket is closed.")await edit_message(message, components=[container("## Announcement over")])

Allowed mentions#

await ctx.respond(text, allowed_mentions={"parse": ["users"]})await ctx.respond(text, allowed_mentions={"roles": [role]})await ctx.respond(text, allowed_mentions={"users": False, "roles": False, "everyone": False})

The discord.js style, the API payload and discord.AllowedMentions are all accepted.

Changing the components of a message#

These functions reload the message, change its components at any depth (rows, sections, containers), then edit it.

await defer_interaction(interaction, update=True)await update_component(interaction.message, "confirm", {"disabled": True, "label": "Confirmed"})await remove_component(interaction.message, "cancel") await row_update(interaction.message, "confirm", lambda button: {**button, "style": 2})
Function Effect
update_component(message, selector, fields) Replaces fields of the matching components
remove_component(message, selector) Removes components, and the rows left empty
row_update(message, row, function) Transforms every component of a row
disable_all(message, delay_ms) Disables everything, now or after a delay

The selector is an exact custom_id or a (component) -> bool function.

Automatic expiry#

timer = disable_all_by_message_id(channel, message_id, delay_ms=30_000)timer.refresh(30_000)  # restart on every activitytimer.cancel()

Type a name: create_button, ModalBuilder, cooldown.