logo

Constatic

Discord Bot BaseCreators

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;
            }
        }
    },
});

Event options

You can set some event options in the setupCreators function:

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

/* ... */ setupCreators({
    events: {
        async middleware(event){
            console.log("Event", event.name);
        }
    },
});

As with commands and responders, you can do a lot with this, such as displaying standardized logs for all executed events, injecting additional information into the arguments of specific events, or even blocking execution based on checks.

In the first argument, an object with the event data is received, where name is the name of the event emitted and args are the arguments of that event, so checking what the name of the event is, the typing of the arguments is automatically defined:

/* ... */ setupCreators({
    events: {
        async middleware(event) {
            if (event.name === "guildMemberUpdate"){
                const [oldMember, newMember] = event.args;
                // ...
                return;
            }
            if (event.name === "guildAuditLogEntryCreate"){
                const [entry, guild] = event.args;
                // ...
                return;
            }
            if (event.name === "messageCreate"){
                const [message] = event.args;
                // ...
                return;
            }
        },
    },
});

You can run the block function to block the execution of events.

/* ... */ setupCreators({
    responders: {
        async middleware(event, block){
            if (event.name === "messageDelete"){
                const [message] = event.args;
                if (message.inGuild() && message.author.id == message.guild.id){
                    block();
                    return;
                }
            }
        }
    },
});

In the case of events, it is also possible to pass tags as arguments to the block function. This way, you can have 3 events of the same type, for example messageCreate, but block only those with the previously defined tags:

createEvent({
    name: "Morning workflow",
    event: "messageCreate",
    tags: ["morning"],
    async run(message) {
        // ...
    },
});
 
createEvent({
    name: "Night workflow",
    event: "messageCreate",
    tags: ["night"],
    async run(message) {
        // ...
    },
});
 
createEvent({
    name: "Messages Workflow",
    event: "messageCreate",
    async run(message) {
        // ...
    },
});

You can pass as many tags as you want to the block function.

// ...
block("morning", "night", "foo", "bar", "baz")
// ...

All events that contain any of the tags passed as arguments to the block function will not be executed.

On this page