// code from goatstore // api: https://store.agi.bd const axios = require("axios"); const fs = require("fs-extra"); const path = require("path"); module.exports = { config: { name: "bow4", version: "2.0", author: "๐Ÿ”ฐ๐‘๐€๐‡๐€๐“ ๐ˆ๐’๐‹๐€๐Œ๐Ÿ”ฐ", countDown: 5, role: 0, shortDescription: { en: "Image generate" }, longDescription: { en: "Generate a bow4 image for a mentioned, replied or specified user." }, category: "image", guide: { en: "{pn} @mention\n" + "{pn} reply to a message\n" + "{pn} UID\n" + "{pn} Facebook profile link" } }, onStart: async function ({ api, event, args }) { const API_JSON_URL = "https://raw.githubusercontent.com/Rahat-Islam10/-Rahat-Boss-/refs/heads/main/api.json"; // ============================== // Get UID by full name // ============================== async function getUIDByFullName(threadID, body) { if (!body || !body.includes("@")) { return null; } const match = body.match(/@(.+)/); if (!match) { return null; } const targetName = match[1] .trim() .toLowerCase() .replace(/\s+/g, " "); try { 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; } catch (error) { return null; } } // ============================== // Get API list // ============================== 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; } // ============================== // Generate frame // ============================== async function generateFrameWithFallback({ senderID, mention, credit, apiList }) { let lastError = null; for (const baseApi of apiList) { try { const cleanBase = baseApi.replace(/\/+$/, ""); const apiUrl = `${cleanBase}/api/frame` + `?type=bow4` + `&senderId=${encodeURIComponent(senderID)}` + `&mentionId=${encodeURIComponent(mention)}` + `&credit=${encodeURIComponent(credit)}`; 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 }; } throw new Error( "โŒ API เฆฅเง‡เฆ•เง‡ image เฆชเฆพเฆ“เงŸเฆพ เฆฏเฆพเงŸเฆจเฆฟ" ); } catch (error) { lastError = error; // 401 เฆนเฆฒเง‡ API error เฆธเฆฐเฆพเฆธเฆฐเฆฟ เฆฆเง‡เฆ–เฆพเฆฌเง‡ if ( error.response?.status === 401 && error.response?.data?.error ) { throw new Error( error.response.data.error ); } } } throw ( lastError || new Error( "โŒ เฆ•เง‹เฆจเง‹ API เฆ•เฆพเฆœ เฆ•เฆฐเฆ›เง‡ เฆจเฆพ" ) ); } try { let mention; // ============================== // 1. Reply // ============================== if ( event.type === "message_reply" && event.messageReply ) { mention = event.messageReply.senderID; } // ============================== // 2. Mention // ============================== else if ( event.mentions && Object.keys(event.mentions).length > 0 ) { mention = Object.keys( event.mentions )[0]; } // ============================== // 3. UID / Link / @name // ============================== else if ( args && args.length > 0 ) { const input = args[0]; // Facebook profile link if ( input.includes(".com/") ) { try { mention = await api.getUID( input ); } catch (e) { mention = null; } } // @name else if ( args.join(" ").includes("@") ) { mention = Object.keys( event.mentions || {} )[0]; if (!mention) { mention = await getUIDByFullName( event.threadID, args.join(" ") ); } } // Direct UID else { mention = input; } } // ============================== // No target // ============================== else { return api.sendMessage( "โŒ ๐—ฃ๐—น๐—ฒ๐—ฎ๐˜€๐—ฒ ๐—บ๐—ฒ๐—ป๐˜๐—ถ๐—ผ๐—ป ๐—ฎ ๐˜‚๐˜€๐—ฒ๐—ฟ\n\n" + "๐—จ๐˜€๐—ฎ๐—ด๐—ฒ:\n" + "โ€ข bow4 @mention\n" + "โ€ข Reply to a message\n" + "โ€ข bow4 UID\n" + "โ€ข bow4 Facebook profile link", event.threadID, event.messageID ); } // ============================== // Check user // ============================== if (!mention) { return api.sendMessage( "โŒ ๐—จ๐˜€๐—ฒ๐—ฟ ๐—ป๐—ผ๐˜ ๐—ณ๐—ผ๐˜‚๐—ป๐—ฑ ๐Ÿธ\n" + "๐—ฃ๐—น๐—ฒ๐—ฎ๐˜€๐—ฒ ๐—ฐ๐—ต๐—ฒ๐—ฐ๐—ธ ๐˜๐—ต๐—ฒ ๐˜‚๐˜€๐—ฒ๐—ฟ ๐—ฝ๐—ฟ๐—ผ๐—ณ๐—ถ๐—น๐—ฒ.", event.threadID, event.messageID ); } // ============================== // User info // ============================== const userInfo = await api.getUserInfo( mention ); const mentionName = userInfo?.[mention]?.name || "Unknown"; const senderID = event.senderID; const credit = "๐Ÿ”ฐ๐‘๐€๐‡๐€๐“ ๐ˆ๐’๐‹๐€๐Œ๐Ÿ”ฐ"; // ============================== // Loading // ============================== const waiting = await api.sendMessage( "โณ๐—ฃ๐—น๐—ฒ๐—ฎ๐˜€๐—ฒ ๐˜„๐—ฎ๐—ถ๐˜....", event.threadID ); try { // ============================== // API list // ============================== const apiConfigList = await getApiList( "bow4" ); // ============================== // Generate // ============================== const { imageBuffer, captionTemplate } = await generateFrameWithFallback({ senderID, mention, credit, apiList: apiConfigList }); // ============================== // Caption // ============================== const finalCaption = captionTemplate.replace( /{{name}}/g, mentionName ); // ============================== // Cache folder // ============================== const cacheDir = path.join( __dirname, "cache" ); await fs.ensureDir( cacheDir ); // ============================== // Save image // ============================== const outPath = path.join( cacheDir, `bow4_${Date.now()}.png` ); await fs.writeFile( outPath, imageBuffer ); // ============================== // Remove loading // ============================== try { 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 ); // ============================== // Auto delete after 2 minutes // ============================== setTimeout( async () => { try { if ( messageInfo?.messageID ) { await api.unsendMessage( messageInfo.messageID ); } if ( await fs.pathExists( outPath ) ) { await fs.remove( outPath ); } } catch (e) {} }, 120000 ); } catch (error) { // Remove loading if failed try { await api.unsendMessage( waiting.messageID ); } catch (e) {} throw error; } } catch (error) { console.error( "BOW4 ERROR:", error ); return api.sendMessage( `โš ๏ธ ${error.message || "Unknown error"}`, event.threadID, event.messageID ); } } };