(sock, message, args, context)
| 90 | description: 'Convert Instagram post/reel to cropped sticker', |
| 91 | usage: '.igsc <instagram URL>', |
| 92 | async handler(sock, message, args, context) { |
| 93 | const { chatId, channelInfo } = context; |
| 94 | try { |
| 95 | const text = message.message?.conversation || message.message?.extendedTextMessage?.text || ''; |
| 96 | const urlMatch = text.match(/https?:\/\/\S+/); |
| 97 | if (!urlMatch) { |
| 98 | await sock.sendMessage(chatId, { |
| 99 | text: `Send an Instagram post/reel link.\nUsage: .igsc <url>`, |
| 100 | ...channelInfo |
| 101 | }, { quoted: message }); |
| 102 | return; |
| 103 | } |
| 104 | await sock.sendMessage(chatId, { react: { text: '🔄', key: message.key } }); |
| 105 | const downloadData = await igdl(urlMatch[0]).catch(() => null); |
| 106 | if (!downloadData || !downloadData.data) { |
| 107 | await sock.sendMessage(chatId, { |
| 108 | text: '❌ Failed to fetch media from Instagram link.', |
| 109 | ...channelInfo |
| 110 | }, { quoted: message }); |
| 111 | return; |
| 112 | } |
| 113 | const rawItems = (downloadData?.data || []).filter((m) => m && m.url); |
| 114 | const seenUrls = new Set(); |
| 115 | const items = []; |
| 116 | for (const m of rawItems) { |
| 117 | if (!seenUrls.has(m.url)) { |
| 118 | seenUrls.add(m.url); |
| 119 | items.push(m); |
| 120 | } |
| 121 | } |
| 122 | if (items.length === 0) { |
| 123 | await sock.sendMessage(chatId, { |
| 124 | text: '❌ No media found at the provided link.', |
| 125 | ...channelInfo |
| 126 | }, { quoted: message }); |
| 127 | return; |
| 128 | } |
| 129 | const maxItems = Math.min(items.length, 10); |
| 130 | const seenHashes = new Set(); |
| 131 | for (let i = 0; i < maxItems; i++) { |
| 132 | try { |
| 133 | const media = items[i]; |
| 134 | const mediaUrl = media.url; |
| 135 | const isVideo = (media?.type === 'video') || /\.(mp4|mov|avi|mkv|webm)$/i.test(mediaUrl); |
| 136 | const buffer = await fetchBufferFromUrl(mediaUrl); |
| 137 | const { createHash } = await import('crypto'); |
| 138 | const hash = createHash('sha1').update(buffer).digest('hex'); |
| 139 | if (seenHashes.has(hash)) |
| 140 | continue; |
| 141 | seenHashes.add(hash); |
| 142 | const stickerBuffer = await stickercropFromBuffer(buffer, isVideo); |
| 143 | await sock.sendMessage(chatId, { |
| 144 | sticker: stickerBuffer, |
| 145 | ...channelInfo |
| 146 | }, { quoted: message }); |
| 147 | if (i < maxItems - 1) { |
| 148 | await new Promise(r => setTimeout(r, 800)); |
| 149 | } |
nothing calls this directly
no test coverage detected