goat
command
quiz
Play a random quiz with elegant design and automatic clean-up
0
views
0
likes
0
installs
Aliases: qz
raw source
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 <category> : 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.");
}
}
};