logo

Constatic

Discord Bot Base

Creators

Main base structures for creating commands and systems

On this base, to create commands and systems you must use the creator functions, which are createCommand, createEvent and createResponder.

When executing these functions passing the data of the respective structures, they will be created and registered.

You can set some options in the index file of the discord folder, see:

src/discord/index.ts

export const { createCommand, createEvent, createResponder } = setupCreators();

This function receives options and returns the creators of commands, events and responders for you to use. By exporting this return you can import them at any time using the base import alias:

import { createCommand, createEvent, createResponder } from "#base";

Command Options

You can set some command options in the setupCreators function:

Set what the default permissions will be for all commands, if none are defined with the defaultMemberPermissions option (It is the same permissions array as the command).

/* ... */ setupCreators({
    commands: {
        defaultMemberPermissions: ["SendMessages", "Connect"]
    },
});

This way, when you create a command and do not set any default member permissions, what you set in the setupCreators function will be the default for all commands.

command.ts
createCommand({
    name: "ping",
    description: "Responde com pong",
    type: ApplicationCommandType.ChatInput,
    // defaultMemberPermissions: [] <-- Not set!
    // But by default it will be registered in the application with ["SendMessages", "Connect"]
    async run(interaction){
        // ...
    }
})

Responder Options

You can set some responder options in the setupCreators function:

You can set a middleware function that will be executed before the responders` run function

/* ... */ setupCreators({
    responders: {
        async middleware(interaction, params){
            console.log("Comand executed:", interaction.customId);
            console.log("Params:", params);
        }
    },
});

Just like with commands, you can do a lot with this, such as displaying standardized logs for all executed responders, injecting additional information into the interaction, or even blocking execution based on checks.

You can run the block function to block responders from executing.

/* ... */ setupCreators({
    responders: {
        async middleware(interaction, _params, block){
            if (interaction.isButton() && blockedUsers.includes(interaction.user.id)){
                interaction.reply({ content: "You can't click any buttons!" });
                block();
                return;
            }
        }
    },
});

On this page