Interactions
An interaction is Discord telling your bot that somebody did something deliberate: ran a command, clicked a button, chose from a menu, or submitted a form.
The kinds of interaction
| Kind | How it starts | Page |
|---|---|---|
| Slash command | Someone types /name | Slash Commands |
| Context menu | Someone right-clicks a user or message and picks an app action | Context Menus |
| Modal | Your bot shows a form and someone submits it | Modals |
| Button | Someone clicks a button on a message | Buttons |
| Select menu | Someone chooses from a dropdown | Select Menus |
Slash commands, context menus and modals live under Interactions in the toolbox. Buttons and select menus live under Components, because they are parts of a message.
The three second rule
Discord gives your bot 3 seconds to respond to any interaction. Miss it and the user sees "This interaction failed", and you cannot recover.
If your handler is quick, reply to the interaction is all you need.
If it is not, defer reply as the very first block. Discord shows "the bot is thinking...", and you then have 15 minutes.
After deferring, answer with edit the bot's reply, not another reply.
Defer whenever your handler fetches something, loops over members, or waits on anything at all.
Ephemeral replies
Every reply block has a visible only to the user option. Turn it on for anything the rest of the channel does not need: errors, confirmations, personal information, and settings menus.
What you get from any interaction
Four blocks work in every interaction handler:
| Block | Returns |
|---|---|
| The member who triggered it, inside that server. | |
| The user who triggered it. | |
| The channel it happened in. | |
| The server it happened in. |
Use member of the interaction for anything server related, such as checking permissions or adding a role. Use user of the interaction when you only need the account.
Registering commands and menus
Slash commands and context menus have to be registered with Discord before they appear. That is what create slash commands / context menus does, and it belongs inside when the bot is logged in from Main.
- Leave test guild ID blank to register globally. Global commands can take up to an hour to appear everywhere.
- Put a server ID in it to register only there. Those appear immediately, which is what you want while building.
Buttons, select menus and modals are not registered. They exist as soon as your bot sends them.
One event, many handlers
Every interaction event fires for all interactions of its kind, so the first block inside each handler is nearly always an if that checks the name or custom ID.
You can also have several copies of the same event block, each checking for a different command. Both approaches work. Splitting them up makes each command easier to find; a single switch keeps the routing in one place.