feat(commands): add slash command loader

Refs: #24
This commit is contained in:
nocode committed 2026-09-13 12:25:04 +02:00
1 parent baf8db059b
commit 15e7279fc0
1 file changed
+28
+28
View File
@@ -0,0 +1,28 @@
import { readdir } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
const commandsDir = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'commands');
/**
* Loads every command module from src/commands/<category>/<name>.js.
* A command module exports: `data` (SlashCommandBuilder), `access` (ACCESS_LEVELS) and `execute`.
*/
export async function loadCommands() {
const entries = await readdir(commandsDir, { recursive: true, withFileTypes: true });
const commands = [];
for (const entry of entries) {
if (!entry.isFile() || !entry.name.endsWith('.js')) continue;
const filePath = path.join(entry.parentPath, entry.name);
const command = await import(pathToFileURL(filePath).href);
if (!command.data || typeof command.execute !== 'function') {
throw new Error(`Invalid command module: ${path.relative(commandsDir, filePath)}`);
}
commands.push(command);
}
return commands;
}