-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommandLoader.js
More file actions
36 lines (30 loc) · 1.07 KB
/
Copy pathcommandLoader.js
File metadata and controls
36 lines (30 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const commands = new Map();
export function addCommand(name, handler) {
commands.set(name.toLowerCase(), handler);
}
export function getCommand(name) {
return commands.get(name.toLowerCase());
}
export async function loadCommandsFrom(dir = './commands') {
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const commandDir = path.resolve(__dirname, dir);
const files = fs.readdirSync(commandDir).filter(f => f.endsWith('.js'));
for (const file of files) {
const filePath = path.join(commandDir, file);
try {
const mod = await import(`file://${filePath}`);
if (typeof mod.default === 'function') {
const commandName = path.basename(file, '.js');
addCommand(commandName, mod.default);
console.log(`✅ Loaded command: ${commandName}`);
} else {
console.warn(`⚠️ Skipped ${file}: missing default export`);
}
} catch (err) {
console.error(`❌ Failed to load ${file}:`, err);
}
}
}