module.exports = { config: { name: "join", aliases: ["boxlist"], version: "1.6.0", author: "MOHAMMAD AKASH", role: 2, shortDescription: "Paginated active group list & add yourself", category: "system", countDown: 10 }, onStart: async function ({ api, event, threadsData }) { const { threadID, messageID, senderID } = event; const botID = api.getCurrentUserID(); const perPage = 10; try { let dbThreads = []; if (threadsData && typeof threadsData.getAll === "function") { try { dbThreads = await threadsData.getAll(); } catch (e) { console.error(e); } } if (!dbThreads.length && global.db && Array.isArray(global.db.allThreadData)) { dbThreads = global.db.allThreadData; } const dbGroupMap = new Map(); if (Array.isArray(dbThreads)) { for (const t of dbThreads) { if (t && t.isGroup && !t.threadID?.includes("@msgr")) { const isBotInGroup = Array.isArray(t.members) ? t.members.some(m => m.userID == botID && m.inGroup !== false) : true; if (isBotInGroup) { dbGroupMap.set(String(t.threadID), t); } } } } let liveThreads = []; if (api && typeof api.getThreadList === "function") { try { const fetched = await api.getThreadList(100, null, ["INBOX"]); if (Array.isArray(fetched)) { liveThreads = fetched.filter(t => t && t.isGroup && !t.threadID?.includes("@msgr")); } } catch (e) { console.error(e); } } let groups = []; if (liveThreads.length > 0) { groups = liveThreads.map(t => { const tid = String(t.threadID); const dbData = dbGroupMap.get(tid); return { threadID: tid, name: t.threadName || t.name || dbData?.threadName || dbData?.name || "Unnamed Group" }; }); } else { groups = Array.from(dbGroupMap.values()).map(t => ({ threadID: String(t.threadID), name: t.threadName || t.name || "Unnamed Group" })); } if (!groups.length) { return api.sendMessage("⚠️ Bot is not currently in any group.", threadID, messageID); } const page = 1; const start = (page - 1) * perPage; const end = start + perPage; const currentGroups = groups.slice(start, end); let msg = `📦 | 𝙱𝙾𝚇 𝙻𝙸𝚂𝚃 (𝙿𝙰𝙶𝙴 ${page})\n\n`; currentGroups.forEach((g, i) => { msg += `${start + i + 1}. ${g.name || "Unnamed Group"}\n`; msg += `🆔 ${g.threadID}\n\n`; }); msg += "↩️ Rᴇᴘʟʏ Wɪᴛʜ: ᴀᴅᴅ 1 | ᴀᴅᴅ 2 5\n➡️ Oʀ ᴘᴀɢᴇ 2 ... Tᴏ sᴇᴇ MᴏRᴇ Gʀᴏ𝚄ᴘs"; api.sendMessage(msg.trim(), threadID, (err, info) => { if (err) return console.error(err); if (global.GoatBot && global.GoatBot.onReply) { global.GoatBot.onReply.set(info.messageID, { commandName: this.config.name, author: senderID, groups, page, perPage }); } }, messageID); } catch (e) { console.error(e); api.sendMessage("❌ Failed to fetch active group list.", threadID, messageID); } }, onReply: async function ({ api, event, Reply }) { if (event.senderID !== Reply.author) return; const args = event.body.trim().toLowerCase().split(/\s+/); const perPage = Reply.perPage || 10; // PAGE if (args[0] === "page") { const pageNum = parseInt(args[1]); if (isNaN(pageNum) || pageNum < 1) return api.sendMessage("❌ Invalid page number", event.threadID); const start = (pageNum - 1) * perPage; const end = start + perPage; const currentGroups = Reply.groups.slice(start, end); if (!currentGroups.length) return api.sendMessage("⚠️ No more groups", event.threadID); let msg = `📦 | 𝙱𝙾𝚇 𝙻𝙸𝚂𝚃 (𝙿𝙰𝙶𝙴 ${pageNum})\n\n`; currentGroups.forEach((g, i) => { msg += `${start + i + 1}. ${g.name || "Unnamed Group"}\n`; msg += `🆔 ${g.threadID}\n\n`; }); msg += `↩️ Rᴇᴘʟʏ Wɪᴛʜ: Aᴅᴅ 1 | Aᴅᴅ 2 5\n➡️ Oʀ Pᴀɢᴇ ${pageNum + 1} ... to see more groups`; api.sendMessage(msg.trim(), event.threadID, (err, info) => { if (err) return console.error(err); if (global.GoatBot && global.GoatBot.onReply) { global.GoatBot.onReply.set(info.messageID, { commandName: Reply.commandName, author: Reply.author, groups: Reply.groups, page: pageNum, perPage }); } }); return; } // ADD if (args[0] === "add") { const addUserToGroup = async (uid, tid, name) => { try { await api.addUserToGroup(uid, tid); await api.sendMessage(`✅ Aᴅᴅᴇᴅ Yᴏᴜ Tᴏ: ${name}`, event.threadID); } catch (err) { await api.sendMessage(`❌ Fᴀɪʟᴇᴅ Tᴏ Aᴅᴅ Yᴏᴜ ᴛᴏ: ${name}`, event.threadID); } }; for (let i = 1; i < args.length; i++) { const index = parseInt(args[i]) - 1; if (isNaN(index) || index < 0 || index >= Reply.groups.length) { await api.sendMessage(`❌ Iɴᴠᴀ🇱🇮🇩 Nᴜᴍʙᴇʀ: ${args[i]}`, event.threadID); continue; } const g = Reply.groups[index]; await addUserToGroup(event.senderID, g.threadID, g.name || "Unnamed Group"); } } } };