// code from goatstore // api: https://store.agi.bd const axios = require("axios"); const fs = require("fs-extra"); const path = require("path"); const VIP_FILE = path.join(__dirname, "vip.json"); module.exports = { config: { name: "e", version: "1.1.0", author: "IMRAN + VIP Lock by NIROB", cooldowns: 5, role: 0, category: "image", description: "AI image editing using prompt + image or attachment (VIP only)", usages: "edit [prompt] + reply image or link", dependencies: { axios: "" } }, langs: { en: { notVip: "❌ | You are not a VIP user. Type !vip to see how to get VIP access." } }, onStart: async function ({ api, event, args, message, getLang }) { try { // === VIP check === let vipDB = []; if (fs.existsSync(VIP_FILE)) { try { vipDB = JSON.parse(fs.readFileSync(VIP_FILE)); } catch { vipDB = []; } } const senderID = event.senderID; const isVip = vipDB.some( user => user.uid === senderID && (user.expire === 0 || user.expire > Date.now()) ); if (!isVip) return message.reply(getLang("notVip")); // ================= // === Image + Prompt handling === let linkanh = event.messageReply?.attachments?.[0]?.url || null; // Extract prompt and image URL from args let prompt = ""; if (args.length > 0) { if (linkanh) { // If replying to image, all args are prompt prompt = args.join(" ").trim(); } else { // If no reply, check for pipe separator const argsString = args.join(" "); if (argsString.includes("|")) { const parts = argsString.split("|"); prompt = parts[0]?.trim(); linkanh = parts[1]?.trim(); } else { prompt = argsString; } } } if (!linkanh) { return api.sendMessage( `πŸ“Έ π™€π˜Ώπ™„π™β€’π™„π™ˆπ™‚\n━━━━━━━━━━━━━━━━━━━━━━\n` + `⛔️ 𝙔𝙀π™ͺ 𝙒π™ͺ𝙨𝙩 π™₯π™§π™€π™«π™žπ™™π™š 𝙖𝙣 π™žπ™’π™–π™œπ™š!\n\n` + `✨ Example:\n` + `1. Reply to an image with: e make this cartoon\n` + `2. Or type: e make this cartoon | image_url\n\n` + `πŸ–ΌοΈ Please provide an image.`, event.threadID, event.messageID ); } if (!prompt) { return api.sendMessage( `πŸ“Έ π™€π˜Ώπ™„π™β€’π™„π™ˆπ™‚\n━━━━━━━━━━━━━━━━━━━━━━\n` + `⛔️ π™‹π™‘π™šπ™–π™¨π™š π™₯π™§π™€π™«π™žπ™™π™š 𝙖 π™₯𝙧𝙀𝙒π™₯𝙩!\n\n` + `✨ Example:\n` + `Reply to an image with: e make this cartoon`, event.threadID, event.messageID ); } linkanh = linkanh.replace(/\s/g, ""); if (!/^https?:\/\//.test(linkanh)) { return api.sendMessage( `⚠️ Invalid image URL!\nπŸ”— Must start with http:// or https://`, event.threadID, event.messageID ); } // Correct API endpoint with proper parameters const apiUrl = `https://4k-api-black.vercel.app/api/nirob?prompt=${encodeURIComponent(prompt)}&image=${encodeURIComponent(linkanh)}`; console.log("πŸ” Request URL:", apiUrl); // Send waiting message const waitMsg = await api.sendMessage(`⏳ Generating your image... Please wait`, event.threadID); // Make API request with proper headers const response = await axios({ method: "GET", url: apiUrl, headers: { 'Accept': 'application/json', 'User-Agent': 'Mozilla/5.0' }, timeout: 60000 // 60 seconds timeout }); console.log("πŸ“¦ Full API Response:", JSON.stringify(response.data, null, 2)); // Extract image URL based on response structure let imageUrl = null; if (response.data && response.data.status === true) { // Check different possible response structures if (response.data.data?.url) { imageUrl = response.data.data.url; } else if (response.data.data?.imageUrl) { imageUrl = response.data.data.imageUrl; } else if (response.data.url) { imageUrl = response.data.url; } else if (response.data.imageUrl) { imageUrl = response.data.imageUrl; } else if (response.data.data?.imageKey) { // If we have imageKey, construct URL (you may need to adjust this) imageUrl = `https://oss.banana-ai.org/image/2026-03-04/${response.data.data.imageKey}.png`; } else if (typeof response.data === 'string' && response.data.startsWith('http')) { imageUrl = response.data; } } if (!imageUrl) { console.log("❌ Could not extract image URL from response"); return api.sendMessage( `❌ Failed to get image URL from API.\nAPI Response: ${JSON.stringify(response.data)}`, event.threadID, event.messageID ); } console.log("βœ… Extracted Image URL:", imageUrl); // Download the image const tempPath = path.join(__dirname, "cache", `edited_${Date.now()}.jpg`); // Ensure cache directory exists await fs.ensureDir(path.join(__dirname, "cache")); // Download image from the URL const imageResponse = await axios({ method: "GET", url: imageUrl, responseType: "stream", timeout: 30000 }); const writer = fs.createWriteStream(tempPath); imageResponse.data.pipe(writer); writer.on("finish", () => { api.sendMessage( { body: `βœ… π™€π˜Ώπ™„π™ π™Žπ™π˜Ύπ˜Ύπ™€π™Žπ™Žπ™π™π™‡!\n━━━━━━━━━━━━━━━━━━━━━━\nπŸ” Prompt: β€œ${prompt}”\n🎨 Generated by AI Image Generator\n✨ Enjoy your edited image!`, attachment: fs.createReadStream(tempPath) }, event.threadID, () => { fs.unlinkSync(tempPath); // cleanup api.unsendMessage(waitMsg.messageID); }, event.messageID ); }); writer.on("error", (err) => { console.error("Write error:", err); api.sendMessage("❌ Failed to save the image file.", event.threadID, event.messageID); api.unsendMessage(waitMsg.messageID); }); } catch (error) { console.error("❌ Error details:", error); let errorMessage = "Failed to generate image."; if (error.response) { // The request was made and the server responded with a status code // that falls out of the range of 2xx errorMessage += `\nStatus: ${error.response.status}`; errorMessage += `\nData: ${JSON.stringify(error.response.data)}`; } else if (error.request) { // The request was made but no response was received errorMessage += "\nNo response from API server."; } else { // Something happened in setting up the request that triggered an Error errorMessage += `\n${error.message}`; } api.sendMessage( `❌ ${errorMessage}`, event.threadID, event.messageID ); } } };