(sock, message, args, context)
| 9 | description: 'Upload to X0.at (anonymous)', |
| 10 | usage: '.xoat (reply to media or caption on media)', |
| 11 | async handler(sock, message, args, context) { |
| 12 | const chatId = context.chatId || message.key.remoteJid; |
| 13 | try { |
| 14 | const hasMedia = message.message?.imageMessage || |
| 15 | message.message?.videoMessage || |
| 16 | message.message?.stickerMessage || |
| 17 | message.message?.documentMessage; |
| 18 | const quotedMsg = message.message?.extendedTextMessage?.contextInfo?.quotedMessage; |
| 19 | if (!hasMedia && !quotedMsg) { |
| 20 | await sock.sendMessage(chatId, { text: '⚠️ Please send media with caption or reply to media!' }, { quoted: message }); |
| 21 | return; |
| 22 | } |
| 23 | const mediaSource = hasMedia ? message.message : quotedMsg; |
| 24 | const type = Object.keys(mediaSource).find(key => ['imageMessage', 'videoMessage', 'stickerMessage', 'documentMessage'].includes(key)); |
| 25 | if (!type) { |
| 26 | await sock.sendMessage(chatId, { text: '⚠️ Unsupported media type!' }, { quoted: message }); |
| 27 | return; |
| 28 | } |
| 29 | await sock.sendMessage(chatId, { text: 'Uploading to X0at...' }, { quoted: message }); |
| 30 | const mediaType = type === 'stickerMessage' ? 'sticker' : type.replace('Message', ''); |
| 31 | const stream = await downloadContentFromMessage(mediaSource[type], mediaType); |
| 32 | let buffer = Buffer.from([]); |
| 33 | for await (const chunk of stream) { |
| 34 | buffer = Buffer.concat([buffer, chunk]); |
| 35 | } |
| 36 | let ext = 'bin'; |
| 37 | if (type === 'imageMessage') |
| 38 | ext = 'jpg'; |
| 39 | else if (type === 'videoMessage') |
| 40 | ext = 'mp4'; |
| 41 | else if (type === 'stickerMessage') |
| 42 | ext = 'webp'; |
| 43 | else if (mediaSource[type].fileName) { |
| 44 | ext = mediaSource[type].fileName.split('.').pop() || 'bin'; |
| 45 | } |
| 46 | const tempDir = path.join('./temp'); |
| 47 | if (!fs.existsSync(tempDir)) |
| 48 | fs.mkdirSync(tempDir, { recursive: true }); |
| 49 | const tempPath = path.join(tempDir, `x0_${Date.now()}.${ext}`); |
| 50 | fs.writeFileSync(tempPath, buffer); |
| 51 | const result = await uploadToX0(tempPath); |
| 52 | await sock.sendMessage(chatId, { |
| 53 | text: `✅ *X0at Upload Success!*\n\n🔗 ${result.url}` |
| 54 | }, { quoted: message }); |
| 55 | fs.unlinkSync(tempPath); |
| 56 | } |
| 57 | catch (error) { |
| 58 | console.error('X0.at Error:', error); |
| 59 | await sock.sendMessage(chatId, { text: `❌ Error: ${error.message}` }, { quoted: message }); |
| 60 | } |
| 61 | } |
| 62 | }; |
nothing calls this directly
no test coverage detected