← back to browse
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.");
    }
  }
};

View raw file