MCPcopy Create free account
hub / github.com/GlobalTechInfo/MEGA-MD / CommandHandler

Class CommandHandler

lib/commandHandler.js:6–235  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4const __dirname = dirname(__filename);
5import fs from 'fs';
6class CommandHandler {
7 constructor() {
8 this.commands = new Map();
9 this.aliases = new Map();
10 this.categories = new Map();
11 this.stats = new Map();
12 this.cooldowns = new Map();
13 this.disabledCommands = new Set();
14 this.prefixlessCommands = new Map();
15 this.watchPlugins();
16 }
17 async watchPlugins() {
18 const pluginsPath = path.join(process.cwd(), 'plugins');
19 if (!fs.existsSync(pluginsPath))
20 return;
21 fs.watch(pluginsPath, async (_eventType, filename) => {
22 if (filename && filename.endsWith('.js')) {
23 const filePath = path.join(pluginsPath, filename);
24 try {
25 if (fs.existsSync(filePath)) {
26 const plugin = (await import(pathToFileURL(filePath).href)).default || (await import(pathToFileURL(filePath).href));
27 if (plugin.command) {
28 this.registerCommand(plugin);
29 if (plugin.isPrefixless === true) {
30 const cmdKey = plugin.command.toLowerCase();
31 this.prefixlessCommands.set(cmdKey, cmdKey);
32 if (plugin.aliases && Array.isArray(plugin.aliases)) {
33 plugin.aliases.forEach((alias) => {
34 this.prefixlessCommands.set(alias.toLowerCase(), cmdKey);
35 });
36 }
37 }
38 console.log(`[WATCHER] Hot-reloaded: ${filename}`);
39 }
40 }
41 }
42 catch (error) {
43 console.error(`[WATCHER] Error reloading ${filename}:`, error.message);
44 }
45 }
46 });
47 }
48 async loadCommands() {
49 const pluginsPath = path.join(process.cwd(), 'plugins');
50 const files = fs.readdirSync(pluginsPath).filter(f => f.endsWith('.js'));
51 for (const file of files) {
52 try {
53 const filePath = path.join(pluginsPath, file);
54 const plugin = (await import(pathToFileURL(filePath).href)).default || (await import(pathToFileURL(filePath).href));
55 if (plugin.command) {
56 this.registerCommand(plugin);
57 if (plugin.isPrefixless === true) {
58 const cmdKey = plugin.command.toLowerCase();
59 this.prefixlessCommands.set(cmdKey, cmdKey);
60 if (plugin.aliases && Array.isArray(plugin.aliases)) {
61 plugin.aliases.forEach((alias) => {
62 this.prefixlessCommands.set(alias.toLowerCase(), cmdKey);
63 });

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected