47 lines
1.3 KiB
JavaScript
47 lines
1.3 KiB
JavaScript
import path from 'path';
|
|
import { glob } from 'glob';
|
|
import { ApplicationCommand } from 'discord.js';
|
|
|
|
/**
|
|
* Get all command modules from the commands directory.
|
|
* @returns {Promise<string[]>} The file paths of all command modules.
|
|
*/
|
|
async function getCommandModulePaths() {
|
|
return await glob(path.join(process.cwd(), 'src/commands/**/*.js'));
|
|
}
|
|
|
|
/**
|
|
* Load a single command module.
|
|
* @param {string} modulePath The path to the command module.
|
|
* @returns {Promise<ApplicationCommand>} The loaded command module.
|
|
*/
|
|
async function loadCommandModule(modulePath) {
|
|
try {
|
|
const module = await import(path.resolve(modulePath));
|
|
const commandModule = module.default;
|
|
|
|
if (!commandModule.data || !commandModule.execute) {
|
|
console.warn(`Invalid command module at ${modulePath}`);
|
|
return;
|
|
}
|
|
|
|
console.info(`Loaded command module: ${commandModule.data.name}`);
|
|
return commandModule;
|
|
} catch (error) {
|
|
console.error(`Error loading module at ${modulePath}.`, error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Load all command modules.
|
|
* @returns {Promise<ApplicationCommand[]>} The loaded command modules.
|
|
*/
|
|
export async function loadCommandModules() {
|
|
const commandModulePaths = await getCommandModulePaths();
|
|
const commandModules = await Promise.all(
|
|
commandModulePaths.map(loadCommandModule)
|
|
);
|
|
|
|
return commandModules.filter((module) => module);
|
|
}
|