refactor: Migrate to ES modules and implement command loading utility for improved structure and logging

This commit is contained in:
2025-02-19 12:01:09 -05:00
parent 77f2824986
commit 79835a3b47
4 changed files with 57 additions and 34 deletions
+31
View File
@@ -0,0 +1,31 @@
import { readdirSync } from 'node:fs';
import { join } from 'node:path';
export async function loadCommands(commandsPath, logger) {
const commands = [];
const commandFiles = readdirSync(commandsPath).filter(file => file.endsWith('.js'));
await Promise.all(
commandFiles.map(async file => {
try {
const filePath = join(commandsPath, file);
const commandModule = await import(filePath);
const command = commandModule.default;
if (!command?.data || !command?.execute) {
logger.warn(
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
);
return;
}
commands.push(command);
logger.log(`Loaded command: ${command.data.name}`);
} catch (error) {
logger.error(`Error loading command from ${file}:`, error);
}
})
);
return commands;
}