(sock, message, args)
| 31 | category: 'general', |
| 32 | desc: 'Remove background from images', |
| 33 | async exec(sock, message, args) { |
| 34 | try { |
| 35 | const chatId = message.key.remoteJid; |
| 36 | let imageUrl = null; |
| 37 | |
| 38 | // Check if args contain a URL |
| 39 | if (args.length > 0) { |
| 40 | const url = args.join(' '); |
| 41 | if (isValidUrl(url)) { |
| 42 | imageUrl = url; |
| 43 | } else { |
| 44 | return sock.sendMessage(chatId, { |
| 45 | text: '❌ Invalid URL provided.\n\nUsage: `.removebg https://example.com/image.jpg`' |
| 46 | }, { quoted: message }); |
| 47 | } |
| 48 | } else { |
| 49 | // Try to get image from message or quoted message |
| 50 | imageUrl = await getQuotedOrOwnImageUrl(sock, message); |
| 51 | |
| 52 | if (!imageUrl) { |
| 53 | return sock.sendMessage(chatId, { |
| 54 | text: '📸 *Remove Background Command*\n\nUsage:\n• `.removebg <image_url>`\n• Reply to an image with `.removebg`\n• Send image with `.removebg`\n\nExample: `.removebg https://example.com/image.jpg`' |
| 55 | }, { quoted: message }); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | |
| 60 | // Call the remove background API |
| 61 | const apiUrl = `https://api.siputzx.my.id/api/iloveimg/removebg?image=${encodeURIComponent(imageUrl)}`; |
| 62 | |
| 63 | const response = await axios.get(apiUrl, { |
| 64 | responseType: 'arraybuffer', |
| 65 | timeout: 30000, // 30 second timeout |
| 66 | headers: { |
| 67 | 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' |
| 68 | } |
| 69 | }); |
| 70 | |
| 71 | if (response.status === 200 && response.data) { |
| 72 | // Send the processed image |
| 73 | await sock.sendMessage(chatId, { |
| 74 | image: response.data, |
| 75 | caption: '✨ *Background removed successfully!*\n\n𝗣𝗥𝗢𝗖𝗘𝗦𝗦𝗘𝗗 𝗕𝗬 𝗞𝗡𝗜𝗚𝗛𝗧-𝗕𝗢𝗧' |
| 76 | }, { quoted: message }); |
| 77 | } else { |
| 78 | throw new Error('Failed to process image'); |
| 79 | } |
| 80 | |
| 81 | } catch (error) { |
| 82 | console.error('RemoveBG Error:', error.message); |
| 83 | |
| 84 | let errorMessage = '❌ Failed to remove background.'; |
| 85 | |
| 86 | if (error.response?.status === 429) { |
| 87 | errorMessage = '⏰ Rate limit exceeded. Please try again later.'; |
| 88 | } else if (error.response?.status === 400) { |
| 89 | errorMessage = '❌ Invalid image URL or format.'; |
| 90 | } else if (error.response?.status === 500) { |
no test coverage detected