-
Notifications
You must be signed in to change notification settings - Fork 0
/
load-commands.ts
38 lines (35 loc) · 1.27 KB
/
load-commands.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import * as fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { DiscordCommandData } from "./scripts/generate-command.js";
const __filename = fileURLToPath(import.meta.url);
export interface CommandData {
handlerPath: string;
config: DiscordCommandData;
}
const commandsDir = path.join(__filename, "../../commands");
export const commands = fs
.readdirSync(commandsDir)
.filter((file) => fs.statSync(path.join(commandsDir, file)).isDirectory())
.map((dir) => {
const files = fs.readdirSync(path.join(commandsDir, dir));
const configFileName = files.find((file) => file === "config.json");
const handlerFileName = files.find((file) => file === "Handler.flyde");
if (!configFileName || !handlerFileName) {
throw new Error(`Missing config or handler for ${dir}`);
}
try {
const configJson = JSON.parse(
fs.readFileSync(path.join(commandsDir, dir, configFileName), "utf-8")
);
if (configJson.name !== dir) {
console.warn(`Config name does not match directory name for ${dir}`);
}
return {
config: configJson,
handlerPath: path.join("commands", dir, handlerFileName),
};
} catch (e) {
throw new Error(`Error parsing config for ${dir}: ${e}`);
}
});