1 file changed
+28
@@ -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;
|
||||
}
|
||||
Reference in new issue
Block a user