// code from goatstore // api: https://store.agi.bd const axios = require("axios"); const fs = require("fs-extra"); const path = require("path"); const API_JSON_URL = "https://raw.githubusercontent.com/Rahat-Islam10/-Rahat-Boss-/refs/heads/main/api.json"; 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 = String(baseApi).replace(/\/+$/, ""); const apiUrl = `${cleanBase}/api/frame` + `?type=friend2` + `&senderId=${encodeURIComponent(senderID)}` + `&mentionId=${encodeURIComponent(mention)}` + `&credit=${encodeURIComponent(credit)}`; try { const response = await axios.get(apiUrl, { timeout: 30000, responseType: "json" }); const data = response.data; if (data && data.image && data.captionTemplate) { const imageBuffer = Buffer.from( data.image, "base64" ); return { imageBuffer, captionTemplate: data.captionTemplate }; } throw new Error("❌ API response invalid"); } catch (error) { lastError = error; if ( error.response?.status === 401 && error.response?.data?.error ) { throw new Error( error.response.data.error ); } } } throw ( lastError || new Error("❌ friend2 API কাজ করছে না") ); } module.exports = { config: { name: "friend2", version: "2.0", author: "🔰𝐑𝐀𝐇𝐀𝐓 𝐈𝐒𝐋𝐀𝐌🔰", countDown: 5, role: 0, shortDescription: "Friend image generate", longDescription: "Generate friend2 image", category: "image", guide: { en: "{pn} @mention / reply / uid / profile link" } }, onStart: async function ({ api, event, args }) { let waiting = null; let outPath = null; try { let mention; let mentionName; // Reply if (event.messageReply) { mention = event.messageReply.senderID; } // Mention else if ( event.mentions && Object.keys(event.mentions).length > 0 ) { mention = Object.keys(event.mentions)[0]; } // UID / Profile Link / @Name else if (args.length > 0) { const input = args.join(" ").trim(); // Profile link if (input.includes(".com/")) { try { mention = await api.getUID(input); } catch (e) { return api.sendMessage( "❌ Profile link থেকে User ID পাওয়া যায়নি।", event.threadID, event.messageID ); } } // @Name else if (input.includes("@")) { mention = Object.keys(event.mentions || {})[0]; if (!mention) { mention = await getUIDByFullName( api, event.threadID, input ); } } // Direct UID else { mention = input; } } // No target else { return api.sendMessage( "❌ 𝗣𝗹𝗲𝗮𝘀𝗲 𝗺𝗲𝗻𝘁𝗶𝗼𝗻 𝗮 𝘂𝘀𝗲𝗿", event.threadID, event.messageID ); } if (!mention) { return api.sendMessage( "❌ 𝗨𝘀𝗲𝗿 𝗻𝗼𝘁 𝗳𝗼𝘂𝗻𝗱 🐸\n" + "𝗣𝗹𝗲𝗮𝘀𝗲 𝗰𝗵𝗲𝗰𝗸 𝗵𝗲𝗿 𝗽𝗿𝗼𝗳𝗶𝗹𝗲", event.threadID, event.messageID ); } // Get user name const userInfo = await api.getUserInfo(mention); mentionName = userInfo?.[mention]?.name || "Unknown"; const senderID = event.senderID; const credit = module.exports.config.author; // Loading message waiting = await api.sendMessage( "⏳𝗣𝗹𝗲𝗮𝘀𝗲 𝘄𝗮𝗶𝘁....", event.threadID ); // Get API list const apiConfigList = await getApiList( module.exports.config.name ); // Generate image const { imageBuffer, captionTemplate } = await generateFrameWithFallback({ senderID, mention, credit, apiList: apiConfigList }); // Replace name const finalCaption = captionTemplate.replace( /{{name}}/g, mentionName ); // Save image outPath = path.join( __dirname, `friend2_${Date.now()}.png` ); await fs.writeFile( outPath, imageBuffer ); // Remove loading try { if (waiting?.messageID) { await api.unsendMessage( waiting.messageID ); } } catch (e) {} // Send image const messageInfo = await api.sendMessage( { body: finalCaption, mentions: [ { tag: mentionName, id: mention } ], attachment: fs.createReadStream(outPath) }, event.threadID, event.messageID ); // Delete after 2 minutes setTimeout(async () => { try { if (messageInfo?.messageID) { await api.unsendMessage( messageInfo.messageID ); } } catch (e) {} try { if ( outPath && await fs.pathExists(outPath) ) { await fs.remove(outPath); } } catch (e) {} }, 120000); } catch (error) { console.error( "FRIEND2 ERROR:", error ); // Remove loading on error try { if (waiting?.messageID) { await api.unsendMessage( waiting.messageID ); } } catch (e) {} return api.sendMessage( `⚠️ ${error.message || "Unknown error"}`, event.threadID, event.messageID ); } } };