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

Function handler

plugins/wordcloud.ts:40–120  ·  view source on GitHub ↗
(sock: any, message: any, args: any[], context: BotContext)

Source from the content-addressed store, hash-verified

38 usage: '.wordcloud <text or reply to any message/file>',
39
40 async handler(sock: any, message: any, args: any[], context: BotContext) {
41 const { chatId, channelInfo } = context;
42 const scriptPath = path.join(process.cwd(), 'lib', 'wordcloud.py');
43
44 if (!fs.existsSync(scriptPath)) {
45 return await sock.sendMessage(chatId, {
46 text: `❌ wordcloud.py not found in lib/. Please check installation.`,
47 ...channelInfo
48 }, { quoted: message });
49 }
50
51 const quoted = getQuoted(message);
52 const quotedText = quoted?.conversation || quoted?.extendedTextMessage?.text || '';
53 const hasDoc = !!quoted?.documentMessage;
54
55 const textInput = args.join(' ').trim() || quotedText;
56
57 if (!textInput && !hasDoc) {
58 return await sock.sendMessage(chatId, {
59 text: `📝 *Word Cloud Analyzer*\n\n` +
60 `*Usage:*\n` +
61 `\`.wordcloud <paste any text here>\`\n\n` +
62 `*Or reply to:*\n` +
63 `• Any text message\n` +
64 `• A .txt or document file\n\n` +
65 `*Output includes:*\n` +
66 `📊 Word count, unique words, sentences\n` +
67 `🏆 Top 20 most used words\n` +
68 `⏱️ Reading time estimate\n` +
69 `📖 Lexical diversity score`,
70 ...channelInfo
71 }, { quoted: message });
72 }
73
74 await sock.sendMessage(chatId, { text: '🔍 Analyzing text...', ...channelInfo }, { quoted: message });
75
76 const tempDir = path.join(process.cwd(), 'temp');
77 fs.mkdirSync(tempDir, { recursive: true });
78 const id = Date.now();
79
80 try {
81 let cmd: string;
82
83 if (hasDoc && quoted) {
84 // Download document
85 const msgObj = { message: { documentMessage: quoted.documentMessage } };
86 const buf = await downloadMediaMessage(msgObj as any, 'buffer', {} as any) as Buffer;
87 const tmpFile = path.join(tempDir, `wc_in_${id}.txt`);
88 fs.writeFileSync(tmpFile, buf);
89 cmd = `python3 "${scriptPath}" --file "${tmpFile}"`;
90
91 const { stdout } = await execAsync(cmd, { timeout: 30000 });
92 try { fs.unlinkSync(tmpFile); } catch {}
93
94 const data = JSON.parse(stdout.trim());
95 if (data.error) {
96 return await sock.sendMessage(chatId, { text: `❌ ${data.error}`, ...channelInfo }, { quoted: message });
97 }

Callers

nothing calls this directly

Calls 4

formatResultFunction · 0.85
getQuotedFunction · 0.70
downloadMediaMessageFunction · 0.70
sendResultFunction · 0.70

Tested by

no test coverage detected