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

Function handler

plugins/getfile.js:10–70  ·  view source on GitHub ↗
(sock, message, args, context)

Source from the content-addressed store, hash-verified

8 usage: '.getfile <filename>',
9 ownerOnly: true,
10 async handler(sock, message, args, context) {
11 const chatId = context.chatId || message.key.remoteJid;
12 const filename = args.join(' ').trim();
13 try {
14 if (!filename) {
15 return await sock.sendMessage(chatId, {
16 text: `*📄 Get File*\n\n*Usage:*\n.getfile <filename>\n\n*Examples:*\n• .getfile index.js\n• .getfile plugins/ping.js\n• .getfile settings.js\n• .getfile package.json`
17 }, { quoted: message });
18 }
19 // Check project root first, then dist/ for compiled files
20 let filePath = path.join(process.cwd(), filename);
21 try {
22 await fs.access(filePath);
23 }
24 catch {
25 // Try dist/ for .js files
26 const distPath = path.join(process.cwd(), 'dist', filename);
27 try {
28 await fs.access(distPath);
29 filePath = distPath;
30 }
31 catch { /* will fail below */ }
32 }
33 try {
34 await fs.access(filePath);
35 }
36 catch {
37 return await sock.sendMessage(chatId, {
38 text: `❌ *File not found!*\n\nNo file named "${filename}" exists.\n\n*Tip:* Use relative path from bot root directory.`
39 }, { quoted: message });
40 }
41 const fileContent = await fs.readFile(filePath, 'utf8');
42 if (!fileContent || fileContent.length === 0) {
43 return await sock.sendMessage(chatId, {
44 text: `⚠️ *File is empty*\n\nThe file "${filename}" has no content.`
45 }, { quoted: message });
46 }
47 if (fileContent.length > 60000) {
48 return await sock.sendMessage(chatId, {
49 text: `❌ *File too large!*\n\nThe file "${filename}" is too large to display (${Math.round(fileContent.length / 1024)}KB).\n\n*Limit:* 60KB\n\n*Tip:* Use a file manager or split the file.`
50 }, { quoted: message });
51 }
52 const stats = await fs.stat(filePath);
53 const fileSize = (stats.size / 1024).toFixed(2);
54 const lastModified = stats.mtime.toLocaleString();
55 const caption = `📄 *File: ${filename}*\n\n` +
56 `📊 *Size:* ${fileSize} KB\n` +
57 `📅 *Modified:* ${lastModified}\n` +
58 `📝 *Lines:* ${fileContent.split('\n').length}\n\n` +
59 `\`\`\`${fileContent}\`\`\``;
60 await sock.sendMessage(chatId, {
61 text: caption
62 }, { quoted: message });
63 }
64 catch (error) {
65 console.error('GetFile Error:', error);
66 await sock.sendMessage(chatId, {
67 text: `❌ *Error reading file*\n\n*Error:* ${error.message}\n\n*Possible reasons:*\n• File is corrupted\n• No read permissions\n• Invalid file path`

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected