This commit is contained in:
@@ -1,20 +1,20 @@
|
||||
import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js";
|
||||
|
||||
export interface Command {
|
||||
data: SlashCommandBuilder;
|
||||
execute: (interaction: ChatInputCommandInteraction) => Promise<void>;
|
||||
data: SlashCommandBuilder;
|
||||
execute: (interaction: ChatInputCommandInteraction) => Promise<void>;
|
||||
}
|
||||
|
||||
const commands = new Map<string, Command>();
|
||||
|
||||
export function registerCommand(command: Command) {
|
||||
commands.set(command.data.name, command);
|
||||
commands.set(command.data.name, command);
|
||||
}
|
||||
|
||||
export function getCommand(name: string): Command | undefined {
|
||||
return commands.get(name);
|
||||
return commands.get(name);
|
||||
}
|
||||
|
||||
export function getAllCommands(): Command[] {
|
||||
return Array.from(commands.values());
|
||||
return Array.from(commands.values());
|
||||
}
|
||||
|
||||
@@ -2,31 +2,39 @@ import { Client } from "discord.js";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
export default async function loadModulesFromDir(dir: string, client: Client): Promise<void> {
|
||||
if (!fs.existsSync(dir)) {
|
||||
console.warn(`[loadModules] Directory not found: ${dir}`);
|
||||
return;
|
||||
}
|
||||
|
||||
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
||||
|
||||
for (const entry of entries) {
|
||||
const fullPath = path.join(dir, entry.name);
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
await loadModulesFromDir(fullPath, client); // recurse
|
||||
} else if (entry.isFile() && /\.(ts|js)$/.test(entry.name)) {
|
||||
try {
|
||||
const mod = await import(fullPath);
|
||||
if (typeof mod.default === "function") {
|
||||
await mod.default(client);
|
||||
console.log(`[loadModules] Loaded: ${fullPath}`);
|
||||
} else {
|
||||
console.warn(`[loadModules] No default export function in: ${entry.name}`);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`[loadModules] Failed to load ${entry.name}:`, err);
|
||||
}
|
||||
export default async function loadModulesFromDir(
|
||||
dir: string,
|
||||
client: Client,
|
||||
): Promise<void> {
|
||||
if (!fs.existsSync(dir)) {
|
||||
console.warn(`[loadModules] Directory not found: ${dir}`);
|
||||
return;
|
||||
}
|
||||
|
||||
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
||||
|
||||
for (const entry of entries) {
|
||||
const fullPath = path.join(dir, entry.name);
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
await loadModulesFromDir(fullPath, client); // recurse
|
||||
} else if (entry.isFile() && /\.(ts|js)$/.test(entry.name)) {
|
||||
try {
|
||||
const mod = await import(fullPath);
|
||||
if (typeof mod.default === "function") {
|
||||
await mod.default(client);
|
||||
console.log(`[loadModules] Loaded: ${fullPath}`);
|
||||
} else {
|
||||
console.warn(
|
||||
`[loadModules] No default export function in: ${entry.name}`,
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(
|
||||
`[loadModules] Failed to load ${entry.name}:`,
|
||||
err,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user