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 "@constatic/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.

Modules

You can pass an array of glob patterns relative to the current folder. All files and directories found in this pattern will be imported before the bot starts.

src/index.ts
await bootstrap({ 
    meta: import.meta,
    modules: [
        "./MyCommands/**", 
        "./alt/events", 
        "./tools/**/*.mod.{ts,js}"
    ]
});

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

You can also load files with side effects, allowing you to register global variables, change prototypes of existing objects, and more.


Load logs

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

Before load

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

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

Generate imports file (generateImportsFile)

This option generates a file within src containing all imports from files in the discord folder—as well as those from paths provided to the modules option—using relative paths.

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

The result is something like this:

src/imports.ts
import "./discord/commands/public/ping.ts";
import "./discord/commands/public/counter.ts";
import "./discord/responders/buttons/reminder.ts";

It is possible to customize the file location by specifying its path instead of using true. However, it is recommended to always generate it within the src directory:

src/helpers/imports.ts
await bootstrap({ 
    meta: import.meta,
    generateImportsFile: "./helpers/imports.ts"
});

This feature can be useful if you want to compile your bot project into an executable file using Bun. In this scenario, the project must know in advance which files need to be imported before they are bundled into a single file. By default, @constatic/base loads files at runtime whenever the project starts, using a glob function to retrieve all valid files—a process that is no longer possible once the project has been compiled into a single executable file.

Então você pode simplesmente adicionar essa importação no index.ts:

src/index.ts
import { bootstrap } from "@constatic/base";
import { env } from "#env"
import "./imports.js"; 

await bootstrap({ 
    meta: import.meta, env,
    generateImportsFile: true
});

This way, Bun is able to determine all the files it needs to include in the final executable.


On this page