const axios = require("axios"); 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: "8.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 = "https://xalman-apis.vercel.app/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 = ["A", "B", "C", "D"]; let optionsText = ""; quiz.options.forEach((opt, index) => { optionsText += `šŸ”  [ ${labels[index]} ] : ${opt}\n`; }); const msgText = `🧠 š—¤š—Øš—œš—­ š—–š—›š—”š—Ÿš—Ÿš—˜š—”š—šš—˜ (${categoryLabel})\n━━━━━━━━━━━━━━━━━━━━━━\nā“ š—¤š—Øš—˜š—¦š—§š—œš—¢š—”:\n${quiz.question}\n\nšŸ“ š—¢š—£š—§š—œš—¢š—”š—¦:\n${optionsText}\n━━━━━━━━━━━━━━━━━━━━━━\nā³ You have 60 seconds to reply with the correct letter (A, B, C, or 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."); } } };