29 lines
999 B
JavaScript
29 lines
999 B
JavaScript
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;
|
|
}
|