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

Function handler

plugins/mega.js:33–81  ·  view source on GitHub ↗
(sock, message, args, context)

Source from the content-addressed store, hash-verified

31 description: 'Download from MEGA with real-time progress',
32 usage: '.mega <mega-url>',
33 async handler(sock, message, args, context) {
34 const chatId = context.chatId || message.key.remoteJid;
35 const text = args.join(' ').trim();
36 if (!text) {
37 return sock.sendMessage(chatId, { text: `*Usage:* .mega https://mega.nz/file/xxxx#xxxx` }, { quoted: message });
38 }
39 try {
40 const file = File.fromURL(text);
41 await file.loadAttributes();
42 if (file.size >= 500 * 1024 * 1024) {
43 return sock.sendMessage(chatId, { text: '❌ *Error:* File too large (Limit: 500MB)' }, { quoted: message });
44 }
45 const { key } = await sock.sendMessage(chatId, {
46 text: `🌩️ *MEGA DOWNLOAD*\n\n▢ *File:* ${file.name}\n▢ *Size:* ${formatBytes(file.size)}\n\n*Progress:* 0% [░░░░░░░░░░]`
47 }, { quoted: message });
48 const stream = file.download();
49 const chunks = [];
50 let lastUpdate = Date.now();
51 stream.on('progress', async (info) => {
52 const { bytesLoaded, bytesTotal } = info;
53 const percentage = Math.floor((bytesLoaded / bytesTotal) * 100);
54 if (Date.now() - lastUpdate > 3000 || percentage === 100) {
55 const bar = generateBar(percentage);
56 await sock.sendMessage(chatId, {
57 text: `🌩️ *MEGA DOWNLOAD*\n\n▢ *File:* ${file.name}\n▢ *Size:* ${formatBytes(bytesTotal)}\n\n*Progress:* ${percentage}% [${bar}]`,
58 edit: key
59 });
60 lastUpdate = Date.now();
61 }
62 });
63 stream.on('data', (chunk) => chunks.push(chunk));
64 stream.on('end', async () => {
65 const buffer = Buffer.concat(chunks);
66 const ext = path.extname(file.name ?? '').toLowerCase();
67 await sock.sendMessage(chatId, {
68 document: buffer,
69 fileName: file.name,
70 mimetype: MIME_TYPES[ext] || 'application/octet-stream',
71 caption: `✅ *Download Complete*\n▢ *File:* ${file.name}\n▢ *Size:* ${formatBytes(file.size)}`
72 }, { quoted: message });
73 });
74 stream.on('error', async (err) => {
75 await sock.sendMessage(chatId, { text: `❌ *Download Error:* ${err.message}` }, { quoted: message });
76 });
77 }
78 catch (error) {
79 await sock.sendMessage(chatId, { text: `❌ *Error:* ${error.message}` }, { quoted: message });
80 }
81 }
82};

Callers

nothing calls this directly

Calls 3

formatBytesFunction · 0.85
generateBarFunction · 0.85
downloadMethod · 0.80

Tested by

no test coverage detected