// code from goatstore // api: https://store.agi.bd const fs = require("fs-extra"); const path = require("path"); const axios = require("axios"); const API_JSON_URL = "https://xrahat-dev-api.vercel.app/raw/All-Api"; const CMD_NAME = "cdi2"; module.exports = { config: { name: "cdi2", aliases: [], version: "2.0", author: "🔰𝐑𝐀𝐇𝐀𝐓 𝐈𝐒𝐋𝐀𝐌🔰", countDown: 5, role: 2, description: { en: "Generate CDI2 frame image with a user's avatar", bn: "User er avatar diye CDI2 frame image banai" }, category: "image", guide: { en: "{pn} [@mention/reply/uid/link]", bn: "{pn} [@mention/reply/uid/link]" } }, langs: { en: { noMention: "❌ Please mention a user", userNotFound: "❌ User not found 🐸\nPlease check her profile", wait: "⏳ Please wait....", error: "⚠️ %1" }, bn: { noMention: "❌ কাউকে mention করুন", userNotFound: "❌ ইউজার পাওয়া যায়নি 🐸\nপ্রোফাইল চেক করুন", wait: "⏳ অপেক্ষা করুন....", error: "⚠️ %1" } }, onStart: async function ({ api, event, args, message, getLang }) { try { let mention; /* -------- target resolve (unchanged logic) -------- */ if (event.type === "message_reply") { mention = event.messageReply.senderID; } else if (args[0]) { if (args[0].includes(".com/")) { try { mention = await api.getUID(args[0]); } catch (e) { const m = args[0].match(/[?&]id=(\d+)/); if (m) mention = m[1]; } } else if (args.join(" ").includes("@")) { mention = Object.keys(event.mentions || {})[0]; if (!mention) { mention = await getUIDByFullName(api, event.threadID, args.join(" ")); } } else { mention = args[0]; } } else { return message.reply(getLang("noMention")); } if (!mention) return message.reply(getLang("userNotFound")); /* -------- sender / mention names -------- */ const senderID = event.senderID; const credit = module.exports.config.author; let mentionName = "Unknown"; try { const userInfo = await api.getUserInfo(mention); mentionName = userInfo[mention]?.name || "Unknown"; } catch (_) {} const waiting = await message.reply(getLang("wait")); /* -------- fetch API + generate with fallback -------- */ const apiConfigList = await getApiList(CMD_NAME); const { imageBuffer, captionTemplate } = await generateFrameWithFallback({ senderID, mention, credit, apiList: apiConfigList }); const finalCaption = captionTemplate.replace(/{{name}}/g, mentionName); /* -------- save & send -------- */ const cacheDir = path.join(__dirname, "cache"); if (!fs.existsSync(cacheDir)) { fs.mkdirSync(cacheDir, { recursive: true }); } const outPath = path.join(cacheDir, `cdi2_${Date.now()}.png`); fs.writeFileSync(outPath, imageBuffer); try { await api.unsendMessage(waiting.messageID); } catch (_) {} await message.reply({ body: finalCaption, mentions: [{ tag: mentionName, id: mention }], attachment: fs.createReadStream(outPath) }); setTimeout(() => { try { fs.unlinkSync(outPath); } catch (_) {} }, 1500); } catch (err) { console.error("cdi2 error:", err); return message.reply(getLang("error", err.message)); } } }; /* ============== helpers (unchanged logic) ============== */ async function getUIDByFullName(api, threadID, body) { if (!body.includes("@")) return null; const match = body.match(/@(.+)/); if (!match) return null; const targetName = match[1].trim().toLowerCase().replace(/\s+/g, " "); const threadInfo = await api.getThreadInfo(threadID); const users = threadInfo.userInfo || []; const user = users.find(u => { if (!u.name) return false; const fullName = u.name.trim().toLowerCase().replace(/\s+/g, " "); return fullName === targetName; }); return user ? user.id : null; } async function getApiList(commandName) { const res = await axios.get(API_JSON_URL, { timeout: 15000 }); const data = res.data || {}; const cmdData = data[commandName]; if (!cmdData || !cmdData.api) { throw new Error(`"${commandName}" API পাওয়া যায়নি`); } const apiList = [cmdData.api, ...(cmdData.backupApis || [])].filter(Boolean); if (!apiList.length) { throw new Error(`"${commandName}" API পাওয়া যায়নি`); } return apiList; } async function generateFrameWithFallback({ senderID, mention, credit, apiList }) { let lastError = null; for (const baseApi of apiList) { const cleanBase = baseApi.replace(/\/+$/, ""); const apiUrl = `${cleanBase}/api/frame?type=cdi2&senderId=${senderID}&mentionId=${mention}&credit=${encodeURIComponent(credit)}`; try { const response = await axios.get(apiUrl, { timeout: 30000, responseType: "json" }); const data = response.data; if (data.image && data.captionTemplate) { const imageBuffer = Buffer.from(data.image, "base64"); return { imageBuffer, captionTemplate: data.captionTemplate }; } else { throw new Error("API Error"); } } catch (error) { lastError = error; if (error.response?.status === 401 && error.response?.data?.error) { throw new Error(error.response.data.error); } } } throw lastError || new Error("API কাজ করছে না"); }