Skip to main content

InteractionExt

Trait InteractionExt 

Source
pub trait InteractionExt: Interaction {
Show 17 methods // Provided methods fn reply<B: Backend>( &self, ctx: &Context<B>, body: impl Into<MessageCallbackData>, ) -> impl Future<Output = Result<(), Error>> + Send { ... } fn defer_reply<B: Backend>( &self, ctx: &Context<B>, flags: MessageFlags, ) -> impl Future<Output = Result<(), Error>> + Send { ... } fn update<B: Backend>( &self, ctx: &Context<B>, body: impl Into<MessageCallbackData>, ) -> impl Future<Output = Result<(), Error>> + Send { ... } fn defer_update<B: Backend>( &self, ctx: &Context<B>, ) -> impl Future<Output = Result<(), Error>> + Send { ... } fn show_modal<B: Backend>( &self, ctx: &Context<B>, modal: ModalCallbackData, ) -> impl Future<Output = Result<(), Error>> + Send { ... } fn respond<B: Backend>( &self, ctx: &Context<B>, choices: AutocompleteChoices, ) -> impl Future<Output = Result<(), Error>> + Send { ... } fn launch_activity<B: Backend>( &self, ctx: &Context<B>, ) -> impl Future<Output = Result<(), Error>> + Send { ... } fn fetch_reply<B: Backend>( &self, ctx: &Context<B>, ) -> impl Future<Output = Result<Arc<CachedMessage>, Error>> + Send { ... } fn edit_reply<B: Backend>( &self, ctx: &Context<B>, body: impl Into<EditWebhookMessage>, ) -> impl Future<Output = Result<Arc<CachedMessage>, Error>> + Send { ... } fn delete_reply<B: Backend>( &self, ctx: &Context<B>, ) -> impl Future<Output = Result<(), Error>> + Send { ... } fn follow_up<B: Backend>( &self, ctx: &Context<B>, body: impl Into<CreateFollowupMessage>, ) -> impl Future<Output = Result<Arc<CachedMessage>, Error>> + Send { ... } fn fetch_follow_up<B: Backend>( &self, ctx: &Context<B>, message_id: MessageId, ) -> impl Future<Output = Result<Arc<CachedMessage>, Error>> + Send { ... } fn edit_follow_up<B: Backend>( &self, ctx: &Context<B>, message_id: MessageId, body: impl Into<EditWebhookMessage>, ) -> impl Future<Output = Result<Arc<CachedMessage>, Error>> + Send { ... } fn delete_follow_up<B: Backend>( &self, ctx: &Context<B>, message_id: MessageId, ) -> impl Future<Output = Result<(), Error>> + Send { ... } fn component_collector<B: Backend>( &self, ctx: &Context<B>, ) -> CollectorBuilder<ComponentInteraction> { ... } fn await_component<B: Backend>( &self, ctx: &Context<B>, ) -> CollectorBuilder<ComponentInteraction, First> { ... } fn await_modal_submit<B: Backend>( &self, ctx: &Context<B>, time: Duration, ) -> CollectorBuilder<ModalInteraction, First> { ... }
}
Expand description

Responses to an Interaction (D-248): one method per Discord callback, plus the original response and follow-up messages. Works on the InteractionCreate payload too, which dereferences to the interaction.

This trait is sealed.

discord.js counterpart: the methods of CommandInteraction, MessageComponentInteraction and ModalSubmitInteraction.

§The order of responses

Nothing is tracked locally (D-248): the rules are Discord’s, and Discord rejects a request that breaks them.

  1. Within 3 seconds, exactly one initial response:

    • reply — a message (commands, components, modals);
    • defer_reply — “thinking…”, the message comes later with edit_reply;
    • update / defer_update — edit the message the component is on, now or later (components, modals opened from one);
    • show_modal — open a modal (commands, components);
    • respond — autocomplete suggestions (autocomplete only);
    • launch_activity — start the app’s Activity.

    A second initial response fails with error 40060 (“Interaction has already been acknowledged”); a late one with 10062 (“Unknown interaction”).

  2. For 15 minutes after it, the interaction token allows fetch_reply, edit_reply, delete_reply and the follow-up messages (follow_up and the *_follow_up methods).

These requests are authorized by the interaction token, not the bot token, and do not count against the global rate limit. Messages they return are cached (D-233).

§Examples

client.on(Events::InteractionCreate, |ctx, interaction| async move {
    let Some(command) = interaction.command() else {
        return Ok(());
    };
    match command.name.as_str() {
        "ping" => interaction.reply(&ctx, "Pong!").await?,
        "slow" => {
            interaction.defer_reply(&ctx, Default::default()).await?;
            // … up to 15 minutes of work …
            interaction.edit_reply(&ctx, "Done").await?;
        }
        _ => {}
    }
    Ok(())
});

Provided Methods§

Source

fn reply<B: Backend>( &self, ctx: &Context<B>, body: impl Into<MessageCallbackData>, ) -> impl Future<Output = Result<(), Error>> + Send

Responds with a message (ChannelMessageWithSource); accepts anything that converts into MessageCallbackData, such as &str. Set MessageFlags::EPHEMERAL in its flags for a message only the user sees.

An initial response (see the order of responses).

discord.js counterpart: reply.

§Errors

Returns the REST error if the request failed.

Source

fn defer_reply<B: Backend>( &self, ctx: &Context<B>, flags: MessageFlags, ) -> impl Future<Output = Result<(), Error>> + Send

Acknowledges the interaction and shows “thinking…” (DeferredChannelMessageWithSource); the message follows with edit_reply. flags can only be MessageFlags::EPHEMERAL (for an ephemeral message) or empty.

An initial response (see the order of responses).

discord.js counterpart: deferReply.

§Errors

Returns the REST error if the request failed.

Source

fn update<B: Backend>( &self, ctx: &Context<B>, body: impl Into<MessageCallbackData>, ) -> impl Future<Output = Result<(), Error>> + Send

Edits the message the component is on (UpdateMessage): fields left None keep their value. Component interactions and modals opened from a component.

An initial response (see the order of responses).

discord.js counterpart: update.

§Errors

Returns the REST error if the request failed.

Source

fn defer_update<B: Backend>( &self, ctx: &Context<B>, ) -> impl Future<Output = Result<(), Error>> + Send

Acknowledges a component interaction without a loading state (DeferredUpdateMessage); the message can be edited later with edit_reply.

An initial response (see the order of responses).

discord.js counterpart: deferUpdate.

§Errors

Returns the REST error if the request failed.

Source

fn show_modal<B: Backend>( &self, ctx: &Context<B>, modal: ModalCallbackData, ) -> impl Future<Output = Result<(), Error>> + Send

Opens a modal (Modal), for example one built with djs_builders::ModalBuilder. Its submission arrives as a new interaction. Not for modal-submit and autocomplete interactions.

An initial response (see the order of responses).

discord.js counterpart: showModal.

§Errors

Returns the REST error if the request failed.

Source

fn respond<B: Backend>( &self, ctx: &Context<B>, choices: AutocompleteChoices, ) -> impl Future<Output = Result<(), Error>> + Send

Suggests choices for the option being typed in an autocomplete interaction (AutocompleteResult); up to 25, typed like the option. An empty list shows “no suggestions”.

An initial response (see the order of responses).

discord.js counterpart: AutocompleteInteraction#respond.

§Errors

Returns the REST error if the request failed.

Source

fn launch_activity<B: Backend>( &self, ctx: &Context<B>, ) -> impl Future<Output = Result<(), Error>> + Send

Launches the app’s Activity (LaunchActivity; apps with Activities enabled).

An initial response (see the order of responses).

discord.js counterpart: launchActivity.

§Errors

Returns the REST error if the request failed.

Source

fn fetch_reply<B: Backend>( &self, ctx: &Context<B>, ) -> impl Future<Output = Result<Arc<CachedMessage>, Error>> + Send

The message of the initial response (Get Original Interaction Response).

discord.js counterpart: fetchReply.

§Errors

Returns the REST error if the request failed (for example before an initial response).

Source

fn edit_reply<B: Backend>( &self, ctx: &Context<B>, body: impl Into<EditWebhookMessage>, ) -> impl Future<Output = Result<Arc<CachedMessage>, Error>> + Send

Edits the message of the initial response — also how the message of a defer_reply is sent, and how a defer_updated message is edited (Edit Original Interaction Response). Accepts anything that converts into an EditWebhookMessage, such as &str.

discord.js counterpart: editReply.

§Errors

Returns the REST error if the request failed.

Source

fn delete_reply<B: Backend>( &self, ctx: &Context<B>, ) -> impl Future<Output = Result<(), Error>> + Send

Deletes the message of the initial response (Delete Original Interaction Response).

discord.js counterpart: deleteReply.

§Errors

Returns the REST error if the request failed.

Source

fn follow_up<B: Backend>( &self, ctx: &Context<B>, body: impl Into<CreateFollowupMessage>, ) -> impl Future<Output = Result<Arc<CachedMessage>, Error>> + Send

Sends another message for the interaction (Create Followup Message); accepts anything that converts into a CreateFollowupMessage, such as &str. After a defer_reply, the first follow-up replaces “thinking…”.

discord.js counterpart: followUp.

§Errors

Returns the REST error if the request failed (40094 after the 5 follow-ups an app installed only for the user may send).

Source

fn fetch_follow_up<B: Backend>( &self, ctx: &Context<B>, message_id: MessageId, ) -> impl Future<Output = Result<Arc<CachedMessage>, Error>> + Send

A follow-up message (Get Followup Message).

discord.js counterpart: fetchReply with a message ID.

§Errors

Returns the REST error if the request failed.

Source

fn edit_follow_up<B: Backend>( &self, ctx: &Context<B>, message_id: MessageId, body: impl Into<EditWebhookMessage>, ) -> impl Future<Output = Result<Arc<CachedMessage>, Error>> + Send

Edits a follow-up message (Edit Followup Message); accepts anything that converts into an EditWebhookMessage, such as &str.

discord.js counterpart: editReply with a message ID.

§Errors

Returns the REST error if the request failed.

Source

fn delete_follow_up<B: Backend>( &self, ctx: &Context<B>, message_id: MessageId, ) -> impl Future<Output = Result<(), Error>> + Send

Deletes a follow-up message (Delete Followup Message).

discord.js counterpart: deleteReply with a message ID.

§Errors

Returns the REST error if the request failed.

Source

fn component_collector<B: Backend>( &self, ctx: &Context<B>, ) -> CollectorBuilder<ComponentInteraction>

A collector of the component interactions on this interaction’s reply (and its follow-ups), matched by the interaction they belong to: no need to fetch the reply. For a component interaction (whose response edits its message), the components of that message.

It ends at its limits, or when the reply, its channel, thread or guild is deleted.

discord.js counterpart: InteractionResponse#createMessageComponentCollector.

§Examples
client.command("counter", |ctx, cmd| async move {
    cmd.reply(&ctx, "Count: 0").await?; // with a "+1" button
    let mut clicks = cmd.component_collector(&ctx).time(Duration::from_secs(300)).build();
    let mut count = 0;
    while let Some(click) = clicks.next().await {
        count += 1;
        click.update(&ctx, format!("Count: {count}")).await?;
    }
    Ok(())
});
Source

fn await_component<B: Backend>( &self, ctx: &Context<B>, ) -> CollectorBuilder<ComponentInteraction, First>

Waits for one component interaction on this interaction’s reply (D-268); set a time.

discord.js counterpart: InteractionResponse#awaitMessageComponent.

§Errors

CollectorError with the reason if the collector ended first.

Source

fn await_modal_submit<B: Backend>( &self, ctx: &Context<B>, time: Duration, ) -> CollectorBuilder<ModalInteraction, First>

Waits up to time for the user of this interaction to submit a modal (D-268), usually the one shown with show_modal. Add a filter on the modal’s custom_id when several can be open. time is required, as in discord.js: a closed modal sends nothing.

discord.js counterpart: awaitModalSubmit.

§Errors

CollectorError with EndReason::Time if nothing was submitted in time.

§Examples
client.command("feedback", |ctx, cmd| async move {
    // cmd.show_modal(&ctx, …) with a text input "text" in the modal "feedback"
    let submitted = cmd
        .await_modal_submit(&ctx, Duration::from_secs(300))
        .filter(|modal| modal.custom_id == "feedback")
        .await?;
    let text = submitted.text_value("text").unwrap_or_default();
    submitted.reply(&ctx, format!("Thanks for: {text}")).await?;
    Ok(())
});

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl InteractionExt for Interaction

Implementors§