(sock, message, args, context)
| 12 | usage: '.crun <c++ code>', |
| 13 | ownerOnly: true, |
| 14 | async handler(sock, message, args, context) { |
| 15 | const { chatId, channelInfo } = context; |
| 16 | // Get code from: args, quoted message, or document |
| 17 | const quoted = message?.message?.extendedTextMessage?.contextInfo?.quotedMessage; |
| 18 | const quotedText = quoted?.conversation || quoted?.extendedTextMessage?.text || ''; |
| 19 | const hasDoc = !!quoted?.documentMessage; |
| 20 | let code = ''; |
| 21 | if (hasDoc) { |
| 22 | const { downloadMediaMessage } = await import('@whiskeysockets/baileys'); |
| 23 | const msgObj = { message: { documentMessage: quoted.documentMessage } }; |
| 24 | const buf = await downloadMediaMessage(msgObj, 'buffer', {}); |
| 25 | code = buf.toString('utf8'); |
| 26 | } |
| 27 | else { |
| 28 | // Preserve newlines from raw message text |
| 29 | const rawText = message?.message?.conversation || |
| 30 | message?.message?.extendedTextMessage?.text || ''; |
| 31 | // Strip the command prefix and command name from raw text |
| 32 | const cmdMatch = rawText.match(/^[.!/]\w+\s*/); |
| 33 | code = cmdMatch ? rawText.slice(cmdMatch[0].length) : args.join(' '); |
| 34 | if (!code.trim()) |
| 35 | code = quotedText; |
| 36 | } |
| 37 | code = code.trim(); |
| 38 | if (!code) { |
| 39 | return await sock.sendMessage(chatId, { |
| 40 | text: `⚡ *C++ Runner*\n\n` + |
| 41 | `*Usage:* \`.crun <code>\`\n\n` + |
| 42 | `*Example:*\n` + |
| 43 | `\`.crun #include<iostream>\nusing namespace std;\nint main(){cout<<"Hello World!"<<endl;return 0;}\`\n\n` + |
| 44 | `• Max execution time: 10 seconds\n` + |
| 45 | `• No file/network access\n` + |
| 46 | `• Auto-wraps in main() if not present`, |
| 47 | ...channelInfo |
| 48 | }, { quoted: message }); |
| 49 | } |
| 50 | // Auto-wrap if no main() found |
| 51 | if (!code.includes('main(') && !code.includes('main (')) { |
| 52 | code = `#include<iostream>\n#include<cmath>\n#include<string>\n#include<vector>\nusing namespace std;\nint main(){\n${code}\nreturn 0;\n}`; |
| 53 | } |
| 54 | const id = Date.now(); |
| 55 | const srcFile = path.join(TEMP_DIR, `crun_${id}.cpp`); |
| 56 | const binFile = path.join(TEMP_DIR, `crun_${id}`); |
| 57 | try { |
| 58 | fs.mkdirSync(TEMP_DIR, { recursive: true }); |
| 59 | fs.writeFileSync(srcFile, code); |
| 60 | await sock.sendMessage(chatId, { |
| 61 | text: '⚙️ *Compiling...*', |
| 62 | ...channelInfo |
| 63 | }, { quoted: message }); |
| 64 | // Compile |
| 65 | try { |
| 66 | await execAsync(`g++ -o ${binFile} ${srcFile} -std=c++17 -O2`, { timeout: 15000 }); |
| 67 | } |
| 68 | catch (compileErr) { |
| 69 | return await sock.sendMessage(chatId, { |
| 70 | text: `❌ *Compilation Error:*\n\n\`\`\`\n${compileErr.stderr || compileErr.message}\n\`\`\``, |
| 71 | ...channelInfo |
nothing calls this directly
no test coverage detected