generated from nhcarrigan/template
feat: migrate from github
This commit is contained in:
36
src/utils/loadCommands.ts
Normal file
36
src/utils/loadCommands.ts
Normal file
@ -0,0 +1,36 @@
|
||||
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 [];
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user