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

Method registerCommand

lib/commandHandler.js:73–136  ·  view source on GitHub ↗
(plugin)

Source from the content-addressed store, hash-verified

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

Callers 4

watchPluginsMethod · 0.95
loadCommandsMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected