# Interactions and responses

> Replies, ephemeral replies, deferrals, message updates, follow-ups and modals; the 3-second and 15-minute rules.

An **interaction** is a member using one of the bot's commands, clicking a component on one of its
messages, typing in an autocomplete option, or submitting its modal.

| `type` | Interaction | TypeScript | Rust |
|---|---|---|---|
| 2 | application command | `bot.command(builder, execute)` | `on_command` |
| 3 | message component | `bot.component(customId, handler)` | `on_component` |
| 4 | autocomplete | the third argument of `bot.command`; `interaction.respond(choices)` | `on_autocomplete` (return the choices) |
| 5 | modal submit | `bot.modal(customId, handler)` | `on_modal` |

In TypeScript, `bot.on('interactionCreate', …)` also sees every interaction. A custom id ending
in `:` matches as a prefix (`'vote:'` matches `'vote:yes'`); a `RegExp` or a function works too.

## The first answer: within 3 seconds

| Callback `type` | TypeScript | Rust | Allowed for | Effect |
|---|---|---|---|---|
| 4 | `reply(..)`, `reply({ ephemeral: true, .. })` | `reply`, `reply_ephemeral` | command, component, modal | a message in the channel, or one only the invoker sees |
| 5 | `deferReply({ ephemeral? })` | `defer(ephemeral)` | command, component, modal | "thinking…", finished later with an edit |
| 6 | `deferUpdate()` | `defer_update` | component, modal opened from a component | nothing visible yet |
| 7 | `update(..)` | `update_message` | component, modal opened from a component | replaces the clicked message (it must be the bot's) |
| 8 | `respond(choices)` | return from `on_autocomplete` | autocomplete | up to 25 suggestions |
| 9 | `showModal(modal)` | `show_modal` | command, component | opens a form |

**TypeScript:**

```ts
bot.command(new SlashCommandBuilder().setName('report').setDescription('Build a report'), async (interaction) => {
  interaction.deferReply();                  // "thinking…"
  const result = buildReport();              // slow work
  interaction.editReply(result);             // replaces the thinking state
});
```

**Rust:**

```rust
fn on_command(&mut self, ctx: Context, cmd: CommandData) {
    let _ = ctx.defer(false);              // "thinking…"
    let result = slow_work(&cmd);
    let _ = ctx.edit_original(result);     // replaces the thinking state
}
```

## After the first answer: 15 minutes

| TypeScript | Rust | Does |
|---|---|---|
| `followUp(..)` | `followup(..)` | up to 10 more messages (ephemeral or not) |
| `editReply(..)` | `edit_original(..)` | replaces the first answer |
| `deleteReply()` | `delete_original()` | removes it |

If no answer arrives in time, the invoker sees "the bot did not respond". The runtime sends a
deferral by itself at 2.5 s while your handler is still running, so in practice that happens only
when the bot crashes.

## Ephemeral replies

An ephemeral reply (`reply({ content, ephemeral: true })` in TypeScript, `ctx.reply_ephemeral(..)`
in Rust) reaches only the invoker. It is never stored in the channel and disappears with the
interaction (15 minutes). Use it for confirmations, errors and private results.

## What a handler receives

The interaction carries its id, the channel, the invoker (`interaction.member`, and
`interaction.userId` in TypeScript), the bot's effective permissions in the channel, the invoker's
locale when known, and for components the message holding the component. The data:

- **Commands:** name, sub-command path, options (validated), resolved members, channels, roles
  and attachments, and the target of user and message commands (`targetMember`, `targetMessage`
  in TypeScript; `target_id` in Rust).
- **Components:** `customId` and, for selects, the chosen `values`.
- **Modals:** the modal's `customId` and each input (`interaction.fields.getTextInputValue('subject')`
  in TypeScript, `modal.text("subject")` in Rust).

In TypeScript the type guards `isChatInputCommand()`, `isUserContextMenuCommand()`,
`isMessageContextMenuCommand()`, `isButton()`, `isStringSelectMenu()`, `isModalSubmit()` and
`isAutocomplete()` narrow the type.

In TypeScript, a handler that throws is logged and emitted as `error`, and the invoker gets an
ephemeral "Something went wrong."

## Security

Only the bot that received an interaction can answer it: the host checks that the answer comes from
that bot's own connection, so there are no tokens to leak. A click is accepted only if the member can
read the message and the component exists and is enabled; a modal submission only if this member has
that modal open.
