generated from nhcarrigan/template
37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
import { readdir } from "fs/promises";
|
|
import { join } from "path";
|
|
|
|
import { Command } from "../interfaces/Command";
|
|
import { ExtendedClient } from "../interfaces/ExtendedClient";
|
|
|
|
import { errorHandler } from "./errorHandler";
|
|
|
|
/**
|
|
* Reads the `/commands` directory and dynamically imports the files,
|
|
* then pushes the imported data to an array.
|
|
*
|
|
* @param {ExtendedClient} bot The bot's Discord instance.
|
|
* @returns {Command[]} Array of Command objects representing the imported commands.
|
|
*/
|
|
export const loadCommands = async (bot: ExtendedClient): Promise<Command[]> => {
|
|
try {
|
|
const result: Command[] = [];
|
|
const files = await readdir(
|
|
join(process.cwd(), "prod", "commands"),
|
|
"utf-8"
|
|
);
|
|
for (const file of files) {
|
|
const name = file.split(".")[0];
|
|
if (!name) {
|
|
continue;
|
|
}
|
|
const mod = await import(join(process.cwd(), "prod", "commands", file));
|
|
result.push(mod[name] as Command);
|
|
}
|
|
return result;
|
|
} catch (err) {
|
|
await errorHandler(bot, "slash command loader", err);
|
|
return [];
|
|
}
|
|
};
|