Yet another Fluxer bot built with TypeScript and Bun
1import path from "node:path";
2import type { CommandRegistry } from "./commandRegistry";
3import { isCommandCtor } from "@/utils";
4import { Logger } from "tslog";
5
6const logger = new Logger({
7 type: "pretty",
8 name: "loadCommands",
9});
10
11export async function loadCommands(
12 registry: CommandRegistry,
13 prefix: string,
14 pattern = "src/commands/**/*.ts",
15): Promise<void> {
16 const glob = new Bun.Glob(pattern);
17
18 for await (const file of glob.scan(".")) {
19 const absPath = path.resolve(file);
20
21 logger.debug(`Loading "${absPath}"`);
22
23 // Import without caching.
24 const mod = await import(Bun.pathToFileURL(absPath).href);
25
26 for (const exported of Object.values(mod)) {
27 if (isCommandCtor(exported)) {
28 registry.register(prefix, absPath, exported);
29 }
30 }
31 }
32}