refactor: Migrate to ES modules and implement command loading utility for improved structure and logging
This commit is contained in:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user