Responders
Understand what Responders are and how they work
You can set some Responder options in the setupCreators function!
What are Responders?
The createResponder function is powerful for handling different types of discord interactions. With it, we can respond to buttons, selection menus and modals, or all of them at the same time.
To create a Responder you need to import the function and the enum from base
import { createResponder } from "#base";
import { ResponderType } from "@constatic/base";See a simple example below, let's send a button through a command and respond to it using the createResponder function
import { createCommand, createResponder, ResponderType } from "#base";
import { createComponents } from "@magicyan/discord";
import { ApplicationCommandType, ButtonBuilder, ButtonStyle } from "discord.js";
createCommand({
name: "ping",
description: "Ping command",
dmPermission: false,
type: ApplicationCommandType.ChatInput,
async run(interaction){
await interaction.reply({
flags: ["Ephemeral", "IsComponentsV2"],
components: createComponents(
new ButtonBuilder({
customId: "/ping/button",
label: "Ping",
style: ButtonStyle.Success
})
)
});
}
});import { createResponder } from "#base";
import { ResponderType } from "@constatic/base";
createResponder({
customId: "/ping/button",
types: [ResponderType.Button], cache: "cached",
async run(interaction) {
await interaction.reply({
flags: ["Ephemeral", "IsComponentsV2"],
content: createComponents(
"pong"
)
});
},
});This way we are responding to a fixed button component in a message where the customId is /ping/button, so any button that our bot sends that has this customId will be responded to by this function defined in the run of our Responder.
Note that we created a customId that starts with a / (slash). Therefore, all components and modals that are handled in Responders must have a customId that starts with a / (slash), since the handler treats Responders as HTTP routes, as you can see on this page.
Responders use the interactionCreate event, which means that even if the bot restarts, if someone uses that button again, it will still be responded to with the defined function. The same applies to select menus and modals
We can respond to interactions of different types but with the same customId by defining more types in the types array:
createResponder({
customId: "/form/register",
types: [
ResponderType.Button,
ResponderType.ModalComponent
], cache: "cached",
async run(interaction) {
if (interaction.isButton()){
await interaction.showModal({
// ^ ButtonInteraction
customId: interaction.customId,
// ...
})
return;
}
const { fields } = interaction;
// ^ ModalMessageModalSubmitInteraction
// ...
},
});In the example above, we have a button that show a modal, and in the same Responder we retrieve the data passed to the modal with the same customId. The type narrowing defined by the interactions with type guards restricted their types within the blocks.
Check out all the interaction types that Responders can receive:
| Type | Interaction | Details |
|---|---|---|
ResponderType.Button | ButtonInteraction | Emited when a button in a message is clicked |
ResponderType.StringSelect | StringSelectMenuInteraction | Emited when one or more options in a selection menu in a message are selected |
ResponderType.UserSelect | UserSelectMenuInteraction | Emited when one or more options in a user selection menu in a message are selected |
ResponderType.RoleSelect | RoleSelectMenuInteraction | Emited when one or more options in a role selection menu in a message are selected |
ResponderType.ChannelSelect | ChannelSelectMenuInteraction | Emited when one or more options in a channel selection menu in a message are selected |
ResponderType.Mentionable | MentionableSelectMenuInteraction | Emited when one or more options in a mentionable selection menu in a message are selected |
ResponderType.Select | AnySelectMenuInteraction | Emited when one or more options in a selection menu of any type in a message are selected |
ResponderType.Modal | ModalSubmitInteraction | Emited when a modal opened by a command is sent |
ResponderType.ModalComponent | ModalMessageModalSubmitInteraction | Emited when a modal opened by a message component is sent |
ResponderType.ChatInput | ChatInputCommandInteraction | Can only be emited by the emitResponder function passing a command of type ChatInputCommandInteraction as an argument |
| ResponderType.UserContextMenu | UserContextMenuCommandInteraction | Can only be emitted by the emitResponder function, passing a command of type UserContextMenuCommandInteraction as an argument |
| ResponderType.MessageContextMenu | MessageContextMenuCommandInteraction | Can only be emitted by the emitResponder function, passing a command of type MessageContextMenuCommandInteraction as an argument |