Interactions handling
Learn how to effectively manage interactions between your bot and users using Sunar. From responding to slash commands, context menu clicks, button presses, to select menu choices, Sunar offers robust mechanisms to handle diverse interactions within Discord.
Overview
Discord interactions are the way users communicate with your bot through various UI elements like commands, buttons, and menus. Sunar provides two approaches to handle these interactions:
- Global handler: A single handler for all interaction types (recommended for most bots)
- Specific handlers: Individual handlers for each interaction type (useful for complex bots)
Global Handler (Recommended)
The simplest way to handle all interactions is using the global handleInteraction function. This approach:
- Automatically routes interactions to the correct handlers
- Handles all interaction types out of the box
- Reduces boilerplate code
- Applies protectors and cooldowns automatically
import { Signal, execute } from 'sunar';
import { handleInteraction } from 'sunar/handlers';
const signal = new Signal('interactionCreate');
execute(signal, async (interaction) => {
await handleInteraction(interaction);
});
export { signal };Handle only specific interactions
This configuration sets up Sunar to selectively manage and respond to specific types of Discord interactions based on their nature, such as slash commands, context menu commands, buttons, modals, select menus, and autocomplete commands.
import { Signal, execute } from 'sunar';
import {
handleAutocomplete,
handleModal,
handleSelectMenu,
handleSlash,
handleButton,
handleContextMenu,
} from 'sunar/handlers';
const signal = new Signal('interactionCreate');
execute(signal, async (interaction) => {
if (interaction.isChatInputCommand()) await handleSlash(interaction);
if (interaction.isContextMenuCommand()) await handleContextMenu(interaction);
if (interaction.isButton()) await handleButton(interaction);
if (interaction.isModalSubmit()) await handleModal(interaction);
if (interaction.isAnySelectMenu()) await handleSelectMenu(interaction);
if (interaction.isAutocomplete()) await handleAutocomplete(interaction);
});
export { signal };Specific Handlers
For more complex bots that need granular control over interaction handling, you can use specific handlers for each interaction type. This approach allows you to:
- Add custom logic before/after handling specific interactions
- Handle only certain types of interactions
- Implement custom error handling per interaction type
- Add logging or monitoring for specific interaction types
Available Handlers
Sunar provides these specialized handlers:
| Handler | Interaction Type | Use Case |
|---|---|---|
handleSlash | Slash Commands | Process /commands |
handleContextMenu | Context Menus | Handle right-click menu actions |
handleButton | Buttons | Process button clicks |
handleModal | Modals | Handle form submissions |
handleSelectMenu | Select Menus | Process dropdown selections |
handleAutocomplete | Autocomplete | Handle command option suggestions |
Error Handling
You can implement custom error handling by wrapping the interaction handler in a try-catch block. This allows you to handle different types of errors and provide appropriate responses:
import { Signal, Signals, execute, clearCooldownsForInteraction } from 'sunar';
import { handleInteraction } from 'sunar/handlers';
const signal = new Signal(Signals.InteractionCreate);
execute(signal, async (interaction) => {
try {
await handleInteraction(interaction);
} catch (error) {
await handleError(interaction, error);
}
});
async function handleError(interaction, error) {
// Clear any cooldowns that might have been set
clearCooldownsForInteraction(interaction);
if (!interaction.isRepliable()) {
console.error('Non-repliable interaction error:', error);
return;
}
// Handle different types of errors
if (error instanceof UserError) {
return interaction.reply({
content: `❌ ${error.message}`,
ephemeral: true
});
}
// Log unexpected errors
console.error('Unexpected error:', error);
return interaction.reply({
content: '⚠️ An unexpected error occurred.',
ephemeral: true
});
}
export { signal };This example shows a basic error handling setup. You can extend it with custom error classes, error tracking services, and more sophisticated cooldown management for production use.
Best Practices
- Use Global Handler: Start with
handleInteractionunless you have specific needs - Error Handling: Always set up an error signal to catch and log interaction errors
- Type Safety: Use TypeScript to ensure type safety when working with interactions
- Validation: Use protectors to validate interactions before processing them
- Response Times: Always respond to interactions within 3 seconds or defer them
Discord requires interactions to be acknowledged within 3 seconds. If your command takes longer, use interaction.deferReply().
Further Reading
How is this guide?
Last updated on
Working with signals
Learn how to effectively use signals in Sunar to handle various events and interactions in your discord bot. This guide covers the basics of creating, managing, and responding to signals for a more interactive and responsive bot experience.
Registering commands
Learn how to register slash commands and context menus in Discord using Sunar. This guide includes global, guild-specific, dynamic registration, and usage of the --register flag.