// code from goatstore // api: https://store.agi.bd const axios = require("axios"); const API_URL = "https://marufx7.vercel.app/quiz"; const TIMEOUT_MS = 40000; const RESULT_DELETE_MS = 5000; const REWARD_COINS = 1000; function getMessageID(result) {   if (typeof result === "string" || typeof result === "number") return String(result);   if (result && typeof result === "object")     return result.messageID || result.messageId || result.message_id || result.id || null;   return null; } async function unsend(api, id) {   if (!id) return false;   try {     await api.unsendMessage(id);     return true;   } catch (error) {     console.error("Quiz unsend error:", error.message);     return false;   } } function getUserData(userID) {   const db = global.db;   if (!db || !Array.isArray(db.allUserData)) return null;   return (     db.allUserData.find(       user => String(user.userID) === String(userID)     ) || null   ); } async function addCoins(userID, amount) {   try {     const db = global.db;     const user = getUserData(userID);     if (!user) return false;     const balance = Number(user.money || 0);     if (!Number.isFinite(balance) || balance < 0) return false;     const newBalance = balance + amount;     if (typeof db.updateUserData === "function") {       await db.updateUserData(userID, { money: newBalance });     }     user.money = newBalance;     return true;   } catch (error) {     console.error("Quiz reward error:", error.message);     return false;   } } module.exports = {   config: {     name: "quiz",     aliases: ["qz"],     version: "3.0.0",     author: "𝗠𝗮𝗿𝘂𝗳",     countDown: 2,     role: 0,     shortDescription: { en: "Play quiz and earn coins" },     longDescription: { en: "Answer within 40 seconds to earn 1000 coins." },     category: "fun",     guide: { en: "{pn} | {pn} bal" }   },   onStart: async function ({ api, message, event, args }) {     const input = (Array.isArray(args) ? args : [])       .join(" ")       .trim()       .toLowerCase();     if (["bal", "balance"].includes(input)) {       const user = getUserData(event.senderID);       if (!user) {         return message.reply("⚠️ Balance database unavailable or user not found.");       }       const balance = Number(user.money || 0);       return message.reply(         "💸 𝗤𝗨𝗜𝗭 𝗕𝗔𝗟𝗔𝗡𝗖𝗘\n" +         "━━━━━━━━━━━━━━━━━━\n" +         "👤 User ID: " + event.senderID + "\n" +         "💰 Total Balance: " + balance.toLocaleString("en-US") + " Coins"       );     }     try {       const response = await axios.get(API_URL, { timeout: 15000 });       const data = response.data;       if (         !data.ok ||         !data.id ||         !data.question ||         !Array.isArray(data.options) ||         data.options.length === 0 ||         !data.answer       ) {         return message.reply("⚠️ Quiz unavailable. Please try again later.");       }       const labels = ["Ⓐ", "Ⓑ", "Ⓒ", "Ⓓ", "Ⓔ", "Ⓕ"];       const optionLines = data.options         .map((option, index) =>           " " + (labels[index] || String(index + 1)) + " " + option         )         .join("\n");       const body =         "✦────── ǫᴜɪᴢ ──────✦\n\n" +         "❓ " + data.question + "\n\n" +         optionLines + "\n\n" +         "💰 Reward: 1,000 Coins\n" +         "⏰ 40s · Reply with letter";       const sent = await message.reply(body);       const quizMessageID = getMessageID(sent);       if (!quizMessageID) {         console.error("Quiz message ID missing:", sent);         return;       }       const entry = {         commandName: this.config.name,         messageID: String(quizMessageID),         author: event.senderID,         threadID: event.threadID,         options: data.options,         answer: data.answer,         api,         message,         answered: false,         timeoutId: null       };       global.GoatBot.onReply.set(String(quizMessageID), entry);       entry.timeoutId = setTimeout(async () => {         const current = global.GoatBot.onReply.get(String(quizMessageID));         if (!current || current.answered) return;         current.answered = true;         global.GoatBot.onReply.delete(String(quizMessageID));         try {           const result = await current.message.reply(             "⏰ 𝗧𝗜𝗠𝗘 𝗨𝗣\n" +             "━━━━━━━━━━━━━━━━━━\n" +             "❌ Time over\n\n" +             "✅ Correct answer ❯ " + current.answer + "\n" +             "💰 Coins earned ❯ 0"           );           const resultID = getMessageID(result);           if (resultID) {             setTimeout(() => unsend(api, resultID), RESULT_DELETE_MS);           }           await unsend(api, quizMessageID);         } catch (error) {           console.error("Quiz timeout error:", error.message);         }       }, TIMEOUT_MS);     } catch (error) {       console.error("Quiz start error:", error.message);       return message.reply("⚠️ Failed to fetch the quiz. Please try again.");     }   },   onReply: async function ({ api, message, event, Reply }) {     const quizMessageID = String(Reply.messageID);     const entry = global.GoatBot.onReply.get(quizMessageID);     if (!entry || entry.answered) return;     if (String(event.senderID) !== String(entry.author)) {       return message.reply("🔒 Only the quiz starter can answer.");     }     if (!event.body || !event.body.trim()) return;     const input = event.body.trim().replace(/[০-৯]/g, digit =>       String("০১২৩৪৫৬৭৮৯".indexOf(digit))     );     const labels = ["A", "B", "C", "D", "E", "F"];     let index = -1;     if (/^[1-9]$/.test(input)) {       index = Number(input) - 1;     } else {       index = labels.findIndex(         (label, i) => i < entry.options.length && label.toLowerCase() === input.toLowerCase()       );       if (index === -1) {         index = entry.options.findIndex(           option => String(option).trim().toLowerCase() === input.toLowerCase()         );       }     }     if (index < 0 || index >= entry.options.length) {       return message.reply(         "⚠️ Invalid option\n\n" +         "Reply with a letter (A-" + labels[entry.options.length - 1] +         ") or number (1-" + entry.options.length + ")."       );     }     entry.answered = true;     clearTimeout(entry.timeoutId);     global.GoatBot.onReply.delete(quizMessageID);     const selected = entry.options[index];     const correct =       String(selected).trim().toLowerCase() ===       String(entry.answer).trim().toLowerCase();     let resultText;     if (correct) {       const rewarded = await addCoins(entry.author, REWARD_COINS);       resultText =         "🎉 𝗖𝗢𝗡𝗚𝗥𝗔𝗧𝗨𝗟𝗔𝗧𝗜𝗢𝗡𝗦\n" +         "━━━━━━━━━━━━━━━━━━\n" +         "🏆 Correct answer!\n\n" +         "🎯 Your answer ❯ " + selected + "\n" +         "🪙 Coins earned ❯ +1,000";     } else {   resultText =     "💔 𝗕𝗘𝗧𝗧𝗘𝗥 𝗟𝗨𝗖𝗞 𝗡𝗘𝗫𝗧 𝗧𝗜𝗠𝗘\n" +     "━━━━━━━━━━━━━━━━━━\n" +     "❌ Wrong answer\n\n" +     "✅ Correct answer ❯ " + entry.answer; }     try {       const result = await message.reply(resultText);       const resultID = getMessageID(result);       if (resultID) {         setTimeout(() => unsend(api, resultID), RESULT_DELETE_MS);       } else {         console.error("Quiz result message ID missing:", result);       }     } catch (error) {       console.error("Quiz result send error:", error.message);     }     await unsend(api, quizMessageID);   } };