$defer
Defers the interaction response, giving the bot extra time to process a command before Discord's 3-second timeout. Must be called at the very beginning of a command.
$defer
$defer is essential for any command that may take longer than 3 seconds to execute. Discord enforces a strict 3-second timeout on interaction responses: if your bot doesn’t respond within that window, the interaction fails with “This interaction failed.” $defer tells Discord “I got your command, I’m working on it” — resetting the timeout and giving you up to 15 minutes to complete processing.
How It Works
When called, $defer sends a deferred response via BotCreatorActionType.respondWithMessage with deferred: true. This:
- Acknowledges the interaction to Discord.
- Prevents the “This interaction failed” error.
- Gives you time to perform slow operations (API calls, database queries, file processing, etc.).
- You can then use
$sendMessageor embed functions to deliver the actual response.
Critical Rules
- Must be the FIRST line of your command. Any code before
$defermay cause the 3-second timeout to trigger before the defer is sent. - Only once per command. Calling
$defermultiple times has no additional effect. - Use
$sendMessageafter, not before. The actual response content is sent after your processing completes.
Common Use Cases
- Commands that make HTTP requests to slow external APIs.
- Commands that process large datasets or files.
- Commands with artificial delays (
$wait). - Commands that wait for user input (
$awaitFunc).
Example: Without vs With $defer
Without $defer (will fail):
$wait[5s]
$sendMessage[Done!]
→ Discord shows “This interaction failed” because no response was sent within 3 seconds.
With $defer (works):
$defer
$wait[5s]
$sendMessage[Done!]
→ The interaction is acknowledged immediately, and the message is sent when ready.
Note
$defer is specifically for interaction-based commands (slash commands, context menu commands, etc.). In message-based (prefix) commands, Discord does not enforce the same interaction timeout, so $defer is not needed — though calling it does no harm.