const axios = require("axios"); const fs = require("fs-extra"); const path = require("path"); const API = "https://gptimage2-automaton.vercel.app"; module.exports = { config: { name: "gpt", aliases: ["gpt", "g2"], version: "2.1.0", author: "Rafi", countDown: 60, role: 0, shortDescription: "GPTImage2 unlimited", longDescription: "gpt | reply to photo + gpt ", category: "ai-image", guide: { en: "{pn} " } }, onStart: async function ({ api, event, args, message }) { const prompt = args.join(" ").trim(); if (!prompt) return message.reply("g gpt "); const { threadID, messageID, messageReply } = event; const att = messageReply?.attachments?.[0]; const isEdit = !!(att && (att.type === "photo" || att.type === "image") && att.url); api.setMessageReaction("⏳", messageID, threadID, () => {}, true); const cacheDir = path.join(__dirname, "cache"); let outPath = null; try { let res; if (isEdit) { const dl = await axios.get(att.url, { responseType: "arraybuffer", timeout: 30000, headers: { "User-Agent": "Mozilla/5.0" } }); const b64 = Buffer.from(dl.data).toString("base64"); const ctype = dl.headers["content-type"]?.startsWith("image/") ? dl.headers["content-type"] : "image/jpeg"; const dataUrl = `data:${ctype};base64,${b64}`; res = await axios.post(API + "/api/edit", { prompt, imageBase64: dataUrl }, { headers: { "Content-Type": "application/json" }, timeout: 100000 }); } else { res = await axios.post(API + "/api/generate", { prompt }, { headers: { "Content-Type": "application/json" }, timeout: 100000 }); } if (!res.data?.success) throw new Error(JSON.stringify(res.data).slice(0,300)); let imageUrl = res.data.imageUrl, jobId = res.data.jobId, email = res.data.email; if (!imageUrl && jobId) { for (let i = 1; i <= 12; i++) { await new Promise(r => setTimeout(r, 4000)); const jr = await axios.get(API + "/api/job/" + jobId, { params: { email }, timeout: 20000 }); if (jr.data?.imageUrl) { imageUrl = jr.data.imageUrl; break; } if (jr.data?.step === "failed") throw new Error("job failed"); } } if (!imageUrl) throw new Error("no imageUrl"); const dl2 = await axios.get(imageUrl, { responseType: "arraybuffer", timeout: 60000, headers: { "User-Agent": "Mozilla/5.0" } }); const buf = Buffer.from(dl2.data); await fs.ensureDir(cacheDir); outPath = path.join(cacheDir, `gpt_${Date.now()}.png`); await fs.writeFile(outPath, buf); api.setMessageReaction("✅", messageID, threadID, () => {}, true); return message.reply({ body: prompt, attachment: fs.createReadStream(outPath) }, () => { if (outPath && fs.existsSync(outPath)) fs.unlinkSync(outPath); }); } catch (e) { console.error("[gpt]", e.message); api.setMessageReaction("❌", messageID, threadID, () => {}, true); if (outPath && fs.existsSync(outPath)) try { fs.unlinkSync(outPath); } catch {} const msg = e.response?.data ? JSON.stringify(e.response.data).slice(0,300) : e.message; return message.reply("❌ " + msg.slice(0,250)); } } };