Files
joel/commands/index.ts
2024-05-02 13:22:15 +02:00

36 lines
1.2 KiB
TypeScript

import { readdirSync } from "node:fs";
import path from "node:path";
import type { Command } from "./command";
export const readCommands = async (): Promise<Command[]> => {
const foldersPath = path.join(__dirname);
const commandFolders = readdirSync(foldersPath, {
withFileTypes: true,
})
.filter((file) => file.isDirectory())
.map((file) => file.name);
let commands: Command[] = [];
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = readdirSync(commandsPath).filter((file) =>
file.endsWith(".ts")
);
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath).default as Command;
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ("data" in command && "execute" in command) {
commands.push({ data: command.data, execute: command.execute });
} else {
console.log(
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
);
}
}
}
return commands;
};