// code from goatstore // api: https://store.agi.bd const axios = require("axios"); const API_CONFIG_URL = "https://raw.githubusercontent.com/goatbotnx/xalmanx210/refs/heads/main/apis.json"; const API_KEY = "xalman-hub"; let apiBaseUrl = null; let apiConfigRequest = null; async function getApiBaseUrl() { if (apiBaseUrl) return apiBaseUrl; if (!apiConfigRequest) { apiConfigRequest = axios .get(API_CONFIG_URL, { timeout: 15000 }) .then(({ data }) => { const baseUrl = data?.[API_KEY]; if (typeof baseUrl !== "string" || !baseUrl.trim()) { throw new Error(`Missing API key in apis.json: ${API_KEY}`); } apiBaseUrl = baseUrl.replace(/\/+$/, ""); return apiBaseUrl; }) .finally(() => { apiConfigRequest = null; }); } return apiConfigRequest; } const CATEGORY_ALIASES = { bn: "bn", bangla: "bn", bengali: "bn", en: "en", english: "en", math: "math", maths: "math", mathematics: "math" }; const CATEGORY_LABELS = { bn: "šŸ‡§šŸ‡© Bangla", en: "šŸ‡¬šŸ‡§ English", math: "🧮 Math" }; function normalizeCategory(input) { if (!input) return null; const key = String(input).toLowerCase().trim(); return CATEGORY_ALIASES[key] || null; } module.exports = { config: { name: "quiz", aliases: ["qz"], version: "9.0", author: "xalman", countDown: 5, role: 0, description: "Play a random quiz with elegant design and automatic clean-up", category: "GAMES", guide: "{pn} : random bangla quiz\n{pn} bn / bangla : bangla quiz\n{pn} en / english : english quiz\n{pn} math : math quiz\n{pn} list : total questions (all categories)\n{pn} list : total questions in a category" }, onStart: async function ({ event, message, args, api }) { const { senderID } = event; const BASE_URL = `${await getApiBaseUrl()}/api/quiz`; if (args[0] === "list" || args[0] === "total") { const rawCategory = args[1]; const category = normalizeCategory(rawCategory); if (rawCategory && !category) { return message.reply( `āŒ Invalid category: "${rawCategory}"\nāœ… Valid categories: bn/bangla, en/english, math` ); } try { const url = category ? `${BASE_URL}?list=true&category=${category}` : `${BASE_URL}?list=true`; const res = await axios.get(url); const data = res.data; let listMsg; if (data.by_category) { listMsg = `šŸ“Š š—¤š—Øš—œš—­ š—¦š—§š—”š—§š—œš—¦š—§š—œš—–š—¦\n━━━━━━━━━━━━━━━━━━━━━━\n` + `šŸ“ Total Questions : ${data.total_questions}\n` + `šŸ‡§šŸ‡© Bangla : ${data.by_category.bn}\n` + `šŸ‡¬šŸ‡§ English : ${data.by_category.en}\n` + `🧮 Math : ${data.by_category.math}\n` + `šŸ‘¤ Database Author : ${data.author}\n` + `🟢 System Status : Active\n━━━━━━━━━━━━━━━━━━━━━━`; } else { listMsg = `šŸ“Š š—¤š—Øš—œš—­ š—¦š—§š—”š—§š—œš—¦š—§š—œš—–š—¦ (${data.category})\n━━━━━━━━━━━━━━━━━━━━━━\n` + `šŸ“ Total Questions : ${data.total_questions}\n` + `šŸ‘¤ Database Author : ${data.author}\n` + `🟢 System Status : Active\n━━━━━━━━━━━━━━━━━━━━━━`; } return message.reply(listMsg); } catch (e) { return message.reply("āŒ Unable to fetch quiz database information."); } } const rawCategory = args[0]; const requestedCategory = normalizeCategory(rawCategory); if (rawCategory && !requestedCategory) { return message.reply( `āŒ Invalid category: "${rawCategory}"\nāœ… Valid categories: bn/bangla, en/english, math\n\n` + `Usage:\n${this.config.guide.replace(/{pn}/g, this.config.name)}` ); } try { const url = requestedCategory ? `${BASE_URL}?category=${requestedCategory}` : BASE_URL; const res = await axios.get(url); const quiz = res.data; if (!quiz.status) return message.reply("āŒ API returned an invalid response."); const categoryLabel = CATEGORY_LABELS[quiz.category] || quiz.category; const labels = ["š—”", "š—•", "š—–", "š——"]; let optionsText = ""; quiz.options.forEach((opt, index) => { optionsText += `ā—ˆ [ ${labels[index]} ] ${opt}\n`; }); const msgText = `ā•­ā”€ć€Œ ⚔ š—¤š—Øš—œš—­ š—§š—œš— š—˜ ${categoryLabel} ć€ā”€ā•®\n\n` + `ā” š—¤š—Øš—˜š—¦š—§\n${quiz.question}\n\n` + `${optionsText}\n` + `ā±ļø šŸ²šŸ¬š˜€ ā”ƒ š—„š—˜š—£š—Ÿš—¬ āžœ A / B / C / D\n` + `╰────────────────────╯`; return message.reply(msgText, (err, info) => { if (err) return; global.GoatBot.onReply.set(info.messageID, { commandName: this.config.name, messageID: info.messageID, author: senderID, correctAnswer: quiz.answer, correctText: quiz.correct_text }); setTimeout(() => { if (global.GoatBot.onReply.has(info.messageID)) { api.unsendMessage(info.messageID); global.GoatBot.onReply.delete(info.messageID); } }, 60000); }); } catch (e) { return message.reply("āŒ Unable to establish a connection with the quiz server."); } }, onReply: async function ({ event, Reply, message, usersData, api }) { const { senderID, body } = event; if (senderID !== Reply.author) { return; } const userAnswer = body.trim().toUpperCase(); const validOptions = ["A", "B", "C", "D"]; if (!validOptions.includes(userAnswer)) return; try { api.unsendMessage(Reply.messageID); let resultMsg = ""; if (userAnswer === Reply.correctAnswer) { const reward = 500; const userData = await usersData.get(senderID); const currentMoney = parseInt(userData.money || 0); await usersData.set(senderID, { money: currentMoney + reward }); resultMsg = `šŸŽ‰ š—–š—¢š—„š—„š—˜š—–š—§ š—”š—”š—¦š—Ŗš—˜š—„!\n━━━━━━━━━━━━━━━━━━━━━━\nāœ… You chose [ ${userAnswer} ].\n\nšŸ“– Explanation:\n${Reply.correctText}\n\nšŸ’° Reward: +${reward.toLocaleString()} ą§³`; } else { resultMsg = `šŸ˜ž š—Ŗš—„š—¢š—”š—š š—”š—”š—¦š—Ŗš—˜š—„!\n━━━━━━━━━━━━━━━━━━━━━━\nāŒ Your choice was [ ${userAnswer} ].\n\nšŸ“– The correct answer is:\n[ ${Reply.correctAnswer} ] : ${Reply.correctText}`; } message.reply(resultMsg); global.GoatBot.onReply.delete(Reply.messageID); } catch (e) { console.error(e); return message.reply("āŒ An unexpected error occurred while processing your answer."); } } };