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

Function handler

plugins/grayscale.ts:15–75  ·  view source on GitHub ↗
(sock: any, message: any, args: any, context: BotContext)

Source from the content-addressed store, hash-verified

13 usage: 'Reply to an image with .grayscale',
14
15 async handler(sock: any, message: any, args: any, context: BotContext) {
16 const chatId = context.chatId || message.key.remoteJid;
17
18 try {
19 const quoted =
20 message.message?.extendedTextMessage?.contextInfo?.quotedMessage;
21
22 if (!quoted?.imageMessage) {
23 return await sock.sendMessage(
24 chatId,
25 { text: '🖤 *Grayscale Image*\n\nReply to an image to convert it to grayscale\n\nUsage:\n.grayscale' },
26 { quoted: message }
27 );
28 }
29
30 await sock.sendMessage(chatId, { react: { text: '🔄', key: message.key } });
31
32 const stream = await downloadContentFromMessage(quoted.imageMessage, 'image');
33 let buffer = Buffer.from([]);
34 for await (const chunk of stream) {
35 buffer = Buffer.concat([buffer, chunk]);
36 }
37
38 const tempFile = path.join(process.cwd(), `grayscale_${Date.now()}.jpg`);
39 fs.writeFileSync(tempFile, buffer);
40
41 const form = new FormData();
42 form.append('apikey', 'guru');
43 form.append('file', fs.createReadStream(tempFile));
44
45 const res = await axios.post(
46 'https://discardapi.dpdns.org/api/image/grayscale',
47 form,
48 { headers: form.getHeaders(), responseType: 'arraybuffer', timeout: 60000 }
49 );
50 fs.unlinkSync(tempFile);
51
52 if (!res?.data) throw new Error('Grayscale conversion failed');
53
54 const grayFile = path.join(process.cwd(), `grayscale_result_${Date.now()}.jpg`);
55 fs.writeFileSync(grayFile, res.data);
56
57 await sock.sendMessage(
58 chatId,
59 {
60 image: { url: grayFile },
61 caption: `🖤 *Grayscale Image*\n\nProcessed by: MEGA-MD`
62 },
63 { quoted: message }
64 );
65 fs.unlinkSync(grayFile);
66
67 } catch(err: any) {
68 console.error('Grayscale Plugin Error:', err);
69 await sock.sendMessage(
70 chatId,
71 { text: '❌ Failed to convert image to grayscale. Make sure the image is clear and try again.' },
72 { quoted: message }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected