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

Function handler

plugins/vnote.js:24–69  ·  view source on GitHub ↗
(sock, message, _args, _context)

Source from the content-addressed store, hash-verified

22 description: 'Convert any audio message into a voice note',
23 usage: 'Reply to an audio file with .vnote',
24 async handler(sock, message, _args, _context) {
25 const chatId = message.key.remoteJid;
26 const quoted = message.message?.extendedTextMessage?.contextInfo?.quotedMessage;
27 if (!quoted?.audioMessage) {
28 return sock.sendMessage(chatId, {
29 text: '❌ Please reply to an *audio file* to convert it to a voice note.'
30 }, { quoted: message });
31 }
32 const tmpDir = path.join(process.cwd(), 'tmp');
33 if (!fs.existsSync(tmpDir))
34 fs.mkdirSync(tmpDir, { recursive: true });
35 const tmpIn = path.join(tmpDir, `vnote_in_${Date.now()}`);
36 const tmpOut = path.join(tmpDir, `vnote_out_${Date.now()}.ogg`);
37 try {
38 const stream = await downloadContentFromMessage(quoted.audioMessage, 'audio');
39 let buffer = Buffer.from([]);
40 for await (const chunk of stream)
41 buffer = Buffer.concat([buffer, chunk]);
42 console.log('[VNOTE] original size:', buffer.length, 'mimetype:', quoted.audioMessage.mimetype);
43 fs.writeFileSync(tmpIn, buffer);
44 await execAsync(`ffmpeg -y -i "${tmpIn}" -c:a libopus -b:a 64k -ar 48000 -ac 1 "${tmpOut}"`);
45 const opusBuffer = fs.readFileSync(tmpOut);
46 console.log('[VNOTE] converted size:', opusBuffer.length);
47 await sock.sendMessage(chatId, {
48 audio: opusBuffer,
49 ptt: true,
50 mimetype: 'audio/ogg; codecs=opus'
51 }, { quoted: message });
52 }
53 catch (error) {
54 console.error('[VNOTE] Error:', error.message);
55 await sock.sendMessage(chatId, {
56 text: '❌ Failed to convert audio to voice note.'
57 }, { quoted: message });
58 }
59 finally {
60 try {
61 fs.unlinkSync(tmpIn);
62 }
63 catch { }
64 try {
65 fs.unlinkSync(tmpOut);
66 }
67 catch { }
68 }
69 }
70};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected