test

Constatic

Discord Bot BaseResponders

Params

Transform custom id of components into http routes

You can use a feature from this base named Custom Id Params to reply to any component or modal dynamically, see:

First let's create a user context command:

src/discord/commands/context/manage.ts
 
new Command({
    name: "Manage",
    type: ApplicationCommandType.User,
    async run(interaction){
        const { targetUser } = interaction;
 
        const embed = new EmbedBuilder({ description: `Manage ${targetUser}` });
        const row = createRow(
            new ButtonBuilder({ 
                customId: `manage/user/${targetUser.id}/kick`, 
                label: "Kick", style: ButtonStyle.Secondary 
            }),
            new ButtonBuilder({ 
                customId: `manage/user/${targetUser.id}/ban`, 
                label: "Ban", style: ButtonStyle.Danger 
            }),
            new ButtonBuilder({ 
                customId: `manage/user/${targetUser.id}/timeout`, 
                label: "Timeout", style: ButtonStyle.Danger 
            }),
            new ButtonBuilder({ 
                customId: `manage/user/${targetUser.id}/alert`, 
                label: "Alert", style: ButtonStyle.Primary 
            })
        );
 
        interaction.reply({ ephemeral, embeds: [embed], components: [row] });
    }
});

Note that the customId of all buttons is similar, they both follow a pattern. They are similar to API http routes.

With this we can create a Responder that expects any button component that follows this pattern in the customId.

It must start with manage/user and then it must have one more two segments separated by /, where the first will be the user id and the second will be an action.

Then we can define this pattern and any button where the customId follows it will be responded to by the defined function. You can get the parameters in the function's second argument.

src/discord/responders/manage.ts
// Dynamic button component function
new Responder({
    customId: "manage/user/:userId/:action",
    type: ComponentType.Button, cache: "cached",
    async run(interaction, params) {
        const { action, userId } = params;
        const targetMember = await interaction.guild.members.fetch(userId);
 
        switch(action){
            case "kick": {
                targetMember.kick();
                // do things ...
                break;
            }
            case "ban": {
                targetMember.ban();
                // do things ...
                break;
            }
            case "timeout": {
                targetMember.timeout(60000);
                // do things ...
                break;
            }
            case "alert": {
                targetMember.send({ /* ... */ });
                // do things ...
                break;
            }
        }
    },
});

You can use this feature with any type of Responder, but don't forget that discord has a 100 character limit on customIds.

On this page

No Headings