logo

Constatic

Discord Bot Base

Bootstrap

Initial discord bot project function

Entry point

The index.ts file in the src folder is the entry point of this project, it is where the bootstrap function is executed to connect the discord bot application.

src/index.ts
import { bootstrap } from "#base";
 
await bootstrap({ meta: import.meta });

In this topic you will see some interesting options for this function.


Meta

This option should receive the import.meta of the current file. When running the project in development, the import.meta directory will be src, but when the project is built it will be build. This option is important so that the files in the discord folder can be imported before the bot starts.

Directories

You can pass an array of paths relative to the current folder that you want to be imported before the bot starts.

Any nested subfolders of the directories you pass in this property will also be loaded, no matter how deep.

src/index.ts
await bootstrap({
meta: import.meta,
directories: ["./MyCommands", "./alt/events"]
});

With this you can create commands, events and responders within these directories and they will be loaded before the bot starts.


Load logs (loadLogs)

It is possible to disable the load logs of structures in this base by setting false in the loadLogs option of the bootstrapApp function.

src/index.ts
await bootstrap({ 
    meta: import.meta,
    loadLogs: false
});

When ready (whenReady)

The base already has a listener for the bot's ready event, if you don't want to create a new listener for this, you can use it by just passing a function to this option.

src/index.ts
await bootstrap({ 
    meta: import.meta,
    whenReady(client) {
        console.log(`Online as ${client.user.displayName}`)
    },
});

Before load

You can run code before the directories are loaded. Receive the client not ready yet.

src/index.ts
await bootstrapApp({ 
    meta: import.meta,
    beforeLoad() {
        console.log(client.application) // null;
        console.log("This happens before the directories are loaded")
    },
});

On this page