MCPcopy Create free account
hub / github.com/GlobalTechInfo/MEGA-MDX / registerCommand

Method registerCommand

lib/commandHandler.ts:86–160  ·  view source on GitHub ↗
(plugin: any)

Source from the content-addressed store, hash-verified

84 }
85
86 registerCommand(plugin: any) {
87 const { command, aliases = [], category = 'misc', handler } = plugin;
88
89 if (!command || typeof handler !== 'function') {
90 console.error(`[SKIP] Plugin at ${command || 'unknown'} is missing a valid command name or handler function.`);
91 return;
92 }
93
94 const cmdKey = command.toLowerCase();
95
96 if (this.commands.has(cmdKey)) {
97 console.warn(`[REPLACED] Command "${cmdKey}" was already registered and has been overwritten.`);
98 }
99
100 this.stats.set(cmdKey, {
101 calls: 0,
102 errors: 0,
103 totalTime: 0n,
104 avgMs: 0
105 });
106
107 const monitoredHandler = async (sock: any, message: any, ...args: any[]) => {
108 const s = this.stats.get(cmdKey);
109
110 if (this.disabledCommands.has(cmdKey)) {
111 return await sock.sendMessage(message.key.remoteJid, {
112 text: `🚫 The command *${cmdKey}* is currently disabled.`
113 }, { quoted: message });
114 }
115
116 const userId = message.key.participant || message.key.remoteJid;
117 const now = Date.now();
118 const cooldownKey = `${userId}_${cmdKey}`;
119
120 if (this.cooldowns.has(cooldownKey)) {
121 const expirationTime = this.cooldowns.get(cooldownKey) + (plugin.cooldown || 3000);
122 if (now < expirationTime) return;
123 }
124
125 this.cooldowns.set(cooldownKey, now);
126 const start = process.hrtime.bigint();
127
128 try {
129 s.calls++;
130 return await handler(sock, message, ...args);
131 } catch(err: any) {
132 s.errors++;
133 throw err;
134 } finally {
135 const end = process.hrtime.bigint();
136 s.totalTime += (end - start);
137 s.avgMs = Number(s.totalTime / BigInt(s.calls || 1)) / 1_000_000;
138 }
139 };
140
141 this.commands.set(cmdKey, {
142 ...plugin,
143 command,

Callers 4

watchPluginsMethod · 0.95
loadCommandsMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected