test

Constatic

Discord Bot Base

Responders

Understand how the Responder class works

A classe Responder é poderosa para lidar com diferentes tipos de interações do discord. Com ela podemos responder botões, menus de seleção e modais, ou todos ao mesmo tempo.

Para criar um Responder é preciso importar a classe e o enum da base

The Responder class is powerful for handling different discord interaction types. With it we can reply to buttons, select menus and modals, or all at the same time.

To create a Responder you need to import the class and enum from the base

responder.ts
import { Responder, ResponderType } from "#base";

See a simple example below, let's send a button through a command and respond to it using the Responder class

command.ts
import { Command, Responder, ResponderType } from "#base";
import { createRow } from "@magicyan/discord";
import { ApplicationCommandType, ButtonBuilder, ButtonStyle } from "discord.js";
 
new Command({
    name: "ping",
    description: "Ping command",
    dmPermission: false,
    type: ApplicationCommandType.ChatInput,
    async run(interaction){
        const row = createRow(
            new ButtonBuilder({
                customId: "ping/button",
                label: "Ping", 
                style: ButtonStyle.Success
            })
        );
        interaction.reply({ ephemeral, components: [row] });
    }
});
 
new Responder({
    customId: "ping/button",
    type: ResponderType.Button, cache: "cached",
    async run(interaction) {
        interaction.reply({ ephemeral, content: "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.

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

On this page

No Headings