# Interactions

> Answer clicks, menu picks and form submits in time, then send or change the reply.

An **interaction** is something a member does that Discord expects an answer to: running a slash command, clicking a button, picking from a menu, or submitting a form.

Discord gives the bot **3 seconds** to answer. With no answer, the member sees *"This interaction failed"*.

## Answering in time

Only three things count as an answer:

| Call | Use when |
|---|---|
| `defer_response(action="update")` | You will change the message the button is on. |
| `defer_response(ephemeral=True)` | You will send a new message later. `ephemeral=True` makes it visible to the clicker only. |
| `await send_modal(form)` | You open a form. Opening it is the answer. |

Answer **first**, before reading a database or editing messages. Those can use up the 3 seconds.

> **Slash commands answer for you:** With **Show "thinking…"** on, a slash command answers Discord at once. Your pre-hook then has as long as it needs. Turn it off only to open a form or to answer yourself.

`respond_interaction` is **not** an answer. It changes an answer that already exists. Call `defer_response` first.

## defer_response

Answers the current interaction now, and shows "thinking…" until you send the real reply. Works in component handlers, modal handlers and slash commands.

```python
defer_response(ephemeral=False, action="create", modal=None)
```

| Argument | What it does |
|---|---|
| `ephemeral` | `True`: the reply that follows is visible to the clicker only. |
| `action` | `"create"`: the reply is a new message. `"update"`: the reply changes the message with the button. |
| `modal` | A form to open instead. Same as `send_modal(form)`. |

Any `action` other than `"create"` is treated as `"update"`.

```python
# Button Click event
defer_response(ephemeral=True)
coins = int(member_var("coins", 0))
respond_interaction(interaction, f"You have {coins} coins.")
```

## defer

Does the same as `defer_response`, but for an interaction you pass in. Use it in View callbacks, where the click is the callback's `interaction`. Needs `await`.

```python
await defer(interaction, ephemeral=False, action="create", modal=None)
```

```python
@button(label="Next", custom_id="next")
async def next_page(self, interaction, component):
    await defer(interaction, action="update")
    self.page += 1
    await self.update_view()
```

## respond_interaction

Sends or changes the reply to an interaction you already answered.

```python
respond_interaction(
    interaction,
    content=...,
    embed=...,
    message=None,
    attachments=...,
    components=...,
    empty=False,
)
```

| Argument | What it does |
|---|---|
| `interaction` | The interaction. In a handler, use the `interaction` variable. |
| `content` | The text. |
| `embed` | One embed. |
| `message` | A `Message(...)` with text and an embed. |
| `attachments` | A list of files. `None` removes existing files. |
| `components` | A `ComponentBuilder` or a `View`. |
| `empty` | `True` changes nothing. |

Leave out an argument to keep what is there. Pass `None` to remove it.

```python
# Button Click event: change the panel after a click
defer_response(action="update")
respond_interaction(interaction, content="Ticket closed.", components=None)
```

## send_modal

Opens a form as the answer. Needs `await`. See [Modals](https://ccbot.app/docs/reference/modals).

```python
await send_modal(form)
await send_modal(form, interaction)   # inside a View callback
```

A handler that opens a form must **not** call `defer_response` first. That would be a second answer, and Discord refuses it.

## interaction

The `interaction` variable holds the current interaction. Useful parts:

| Part | What it is |
|---|---|
| `interaction.user`, `interaction.member` | Who did it. |
| `interaction.channel_id`, `interaction.guild_id` | Where. |
| `interaction.custom_id` | The button, menu or form name. Clicks and submits only. |
| `interaction.values` | What was picked in a menu, as a list. Menus only. |
| `interaction.command_name`, `interaction.options` | Slash commands only. |
| `interaction.locale` | The member's Discord language. |

See [Objects](https://ccbot.app/docs/reference/objects) for the full list.
