Sunar

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.

Handle all supported interactions

This setup configures Sunar to efficiently manage various Discord interactions using a Signal named interactionCreate. It prepares the framework to handle incoming events such as slash commands, buttons, and other interactions initiated by users in Discord.

src/signals/interaction-create.js
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.

src/signals/interaction-create.js
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 };

This setup allows your bot to efficiently manage and respond to specific interaction types on Discord, ensuring accurate and timely responses tailored to the user's actions. Customize the handling logic within each handler function (handleSlash, handleContextMenu, etc.) as needed to implement your bot's functionality and interaction flow according to Discord's API capabilities and user experience requirements.

Last updated on

On this page

GitHubEdit on Github ↗