From 228a583255a5e9c84a1dfb0d2d2929856a1a9ab3 Mon Sep 17 00:00:00 2001 From: nocode Date: Sun, 13 Sep 2026 12:19:09 +0200 Subject: [PATCH] feat(commands): add slash command loader Refs: #24 Co-Authored-By: Claude Opus 5 (1M context) --- src/core/command-loader.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/core/command-loader.js diff --git a/src/core/command-loader.js b/src/core/command-loader.js new file mode 100644 index 0000000..706c889 --- /dev/null +++ b/src/core/command-loader.js @@ -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//.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; +}