2024-09-05 18:49:18 +01:00
|
|
|
const { SlashCommandBuilder, EmbedBuilder } = require("discord.js");
|
|
|
|
const axios = require("axios");
|
|
|
|
const TriviaQuestion = require("../../models/TriviaQuestion");
|
|
|
|
const Leaderboard = require("../../models/Leaderboard");
|
|
|
|
const { decode } = require("html-entities");
|
|
|
|
|
|
|
|
const API_INTERVAL = 5000; // 5 seconds
|
|
|
|
const QUESTION_EXPIRY = 30 * 24 * 60 * 60 * 1000; // 1 month
|
2024-09-05 23:55:49 +01:00
|
|
|
const ongoingTrivia = new Set(); // Track users with ongoing trivia
|
2024-09-05 18:49:18 +01:00
|
|
|
|
|
|
|
let lastApiCall = 0;
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
data: new SlashCommandBuilder()
|
|
|
|
.setName("trivia")
|
2024-09-05 21:29:50 +01:00
|
|
|
.setDescription("Play a trivia game")
|
|
|
|
.addStringOption((option) =>
|
|
|
|
option
|
|
|
|
.setName("category")
|
|
|
|
.setDescription("Choose a trivia category")
|
|
|
|
.setRequired(true)
|
|
|
|
.addChoices(
|
2024-09-06 01:00:20 +01:00
|
|
|
{ name: "General Knowledge", value: "9" },
|
2024-09-05 21:29:50 +01:00
|
|
|
{ name: "Video Games", value: "15" },
|
2024-09-06 00:13:07 +01:00
|
|
|
{ name: "Anime & Manga", value: "31" },
|
2024-09-06 01:00:20 +01:00
|
|
|
{ name: "Computers", value: "18" },
|
|
|
|
{ name: "Board Games", value: "16" },
|
|
|
|
{ name: "Comics", value: "29" },
|
|
|
|
{ name: "Cartoons & Animations", value: "32" },
|
2024-09-06 01:14:01 +01:00
|
|
|
{ name: "Film", value: "11" },
|
2024-09-06 01:23:00 +01:00
|
|
|
{ name: "Science & Nature", value: "17" },
|
|
|
|
{ name: "Animals", value: "27" },
|
|
|
|
{ name: "Music", value: "12" },
|
|
|
|
{ name: "History", value: "23" },
|
2024-09-06 01:32:36 +01:00
|
|
|
{ name: "Geography", value: "22" },
|
|
|
|
{ name: "Mythology", value: "20" }
|
2024-09-05 21:29:50 +01:00
|
|
|
)
|
|
|
|
),
|
2024-09-05 18:49:18 +01:00
|
|
|
|
|
|
|
async execute(interaction, client) {
|
|
|
|
const userId = interaction.user.id;
|
|
|
|
const username = interaction.user.username;
|
|
|
|
const guild = interaction.guild;
|
|
|
|
const timeLimit = 30000; // Time limit for answering in milliseconds
|
|
|
|
|
2024-09-05 23:55:49 +01:00
|
|
|
// Check if the user already has an ongoing trivia game
|
|
|
|
if (ongoingTrivia.has(userId)) {
|
|
|
|
return interaction.reply({
|
|
|
|
content:
|
|
|
|
"You already have an ongoing trivia game. Please finish it before starting a new one.",
|
|
|
|
ephemeral: true, // Only visible to the user
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the user to the set of active trivia players
|
|
|
|
ongoingTrivia.add(userId);
|
|
|
|
|
2024-09-06 00:00:30 +01:00
|
|
|
try {
|
|
|
|
const categoryId = interaction.options.getString("category");
|
2024-09-06 01:00:20 +01:00
|
|
|
const categoryName = (() => {
|
|
|
|
switch (categoryId) {
|
|
|
|
case "15":
|
|
|
|
return "Video Games";
|
|
|
|
case "31":
|
|
|
|
return "Anime & Manga";
|
|
|
|
case "18":
|
|
|
|
return "Computers";
|
|
|
|
case "16":
|
|
|
|
return "Board Games";
|
|
|
|
case "29":
|
|
|
|
return "Comics";
|
|
|
|
case "32":
|
|
|
|
return "Cartoons & Animations";
|
|
|
|
case "11":
|
|
|
|
return "Film";
|
|
|
|
case "9":
|
|
|
|
return "General Knowledge";
|
2024-09-06 01:14:01 +01:00
|
|
|
case "17":
|
|
|
|
return "Science & Nature";
|
2024-09-06 01:23:00 +01:00
|
|
|
case "27":
|
|
|
|
return "Animals";
|
|
|
|
case "12":
|
|
|
|
return "Music";
|
|
|
|
case "23":
|
|
|
|
return "History";
|
|
|
|
case "22":
|
|
|
|
return "Geography";
|
2024-09-06 01:32:36 +01:00
|
|
|
case "20":
|
|
|
|
return "Mythology";
|
2024-09-06 01:00:20 +01:00
|
|
|
default:
|
|
|
|
return "Video Games";
|
|
|
|
}
|
2024-09-06 01:41:23 +01:00
|
|
|
})()
|
|
|
|
.replace(/[^a-zA-Z0-9 ]/g, "")
|
|
|
|
.trim(); // Remove special characters and trim the category name for MongoDB query purposes in the Emebed title
|
2024-09-06 00:00:30 +01:00
|
|
|
|
|
|
|
// Fetch a trivia question from the cache or the API
|
|
|
|
let triviaQuestion = await TriviaQuestion.findOne({
|
|
|
|
last_served: { $lt: new Date(Date.now() - QUESTION_EXPIRY) }, // Fetch questions not served recently
|
2024-09-05 21:29:50 +01:00
|
|
|
category: categoryName, // Filter by category
|
2024-09-06 00:00:30 +01:00
|
|
|
}).sort({ last_served: 1 });
|
2024-09-05 18:49:18 +01:00
|
|
|
|
2024-09-06 00:00:30 +01:00
|
|
|
if (!triviaQuestion || Date.now() - lastApiCall >= API_INTERVAL) {
|
|
|
|
// Fetch a new trivia question from OTDB
|
|
|
|
const response = await axios.get(
|
|
|
|
`https://opentdb.com/api.php?amount=1&category=${categoryId}`
|
|
|
|
);
|
2024-09-05 18:49:18 +01:00
|
|
|
|
2024-09-06 00:00:30 +01:00
|
|
|
triviaQuestion = response.data.results[0];
|
|
|
|
lastApiCall = Date.now();
|
2024-09-05 21:29:50 +01:00
|
|
|
|
2024-09-06 00:00:30 +01:00
|
|
|
// Save the new trivia question to MongoDB
|
|
|
|
await TriviaQuestion.create({
|
|
|
|
question: decode(triviaQuestion.question),
|
|
|
|
correct_answer: decode(triviaQuestion.correct_answer),
|
|
|
|
incorrect_answers: triviaQuestion.incorrect_answers.map(decode),
|
|
|
|
category: categoryName, // Include the category
|
|
|
|
last_served: null, // Initially not served
|
|
|
|
});
|
2024-09-05 18:49:18 +01:00
|
|
|
|
2024-09-06 00:00:30 +01:00
|
|
|
// Fetch the newly created question
|
|
|
|
triviaQuestion = await TriviaQuestion.findOne({
|
|
|
|
question: decode(triviaQuestion.question),
|
|
|
|
category: categoryName, // Filter by category
|
|
|
|
});
|
|
|
|
}
|
2024-09-05 18:49:18 +01:00
|
|
|
|
2024-09-06 00:00:30 +01:00
|
|
|
if (triviaQuestion) {
|
|
|
|
triviaQuestion.last_served = new Date();
|
|
|
|
await triviaQuestion.save();
|
2024-09-05 21:29:50 +01:00
|
|
|
}
|
|
|
|
|
2024-09-06 00:00:30 +01:00
|
|
|
const question = decode(triviaQuestion.question);
|
|
|
|
const correctAnswer = decode(triviaQuestion.correct_answer);
|
|
|
|
const incorrectAnswers = triviaQuestion.incorrect_answers.map(decode);
|
|
|
|
let allAnswers = [...incorrectAnswers, correctAnswer];
|
|
|
|
|
2024-09-06 00:22:36 +01:00
|
|
|
// Declare answerMap before any conditions
|
|
|
|
let answerMap = {};
|
|
|
|
|
2024-09-06 00:00:30 +01:00
|
|
|
// Handle True/False questions specifically
|
|
|
|
if (triviaQuestion.type === "boolean") {
|
2024-09-06 00:18:16 +01:00
|
|
|
// Always keep "True" as option 1 and "False" as option 2
|
|
|
|
answerMap = { 1: "True", 2: "False" };
|
|
|
|
} else {
|
|
|
|
// Shuffle answers for other types of questions
|
|
|
|
allAnswers = allAnswers.sort(() => Math.random() - 0.5);
|
|
|
|
|
2024-09-06 00:22:36 +01:00
|
|
|
// Assign the map without redeclaring
|
2024-09-06 00:18:16 +01:00
|
|
|
answerMap = allAnswers.reduce((map, answer, index) => {
|
|
|
|
map[index + 1] = answer;
|
|
|
|
return map;
|
|
|
|
}, {});
|
2024-09-06 00:00:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create an embed with the trivia question and numbered options
|
|
|
|
const triviaEmbed = new EmbedBuilder()
|
|
|
|
.setColor("#0099ff")
|
2024-09-06 01:41:23 +01:00
|
|
|
.setTitle(`${categoryName} Trivia Question`)
|
2024-09-06 00:00:30 +01:00
|
|
|
.setDescription(question)
|
|
|
|
.addFields(
|
|
|
|
Object.entries(answerMap).map(([number, answer]) => ({
|
|
|
|
name: `Option ${number}`,
|
|
|
|
value: answer,
|
|
|
|
inline: true,
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
.setTimestamp()
|
|
|
|
.setFooter({
|
|
|
|
text: `${guild.name} | Answer within ${timeLimit / 1000} seconds`,
|
|
|
|
iconURL: guild.iconURL(),
|
2024-09-05 21:29:50 +01:00
|
|
|
});
|
2024-09-06 00:00:30 +01:00
|
|
|
|
|
|
|
await interaction.reply({
|
|
|
|
embeds: [triviaEmbed],
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create a message collector specific to the user
|
|
|
|
const answerFilter = (response) => {
|
|
|
|
const userInput = response.content.trim();
|
|
|
|
const userAnswerNumber = parseInt(userInput, 10);
|
|
|
|
const userAnswerText =
|
|
|
|
allAnswers.includes(userInput) ||
|
|
|
|
(answerMap[userAnswerNumber] &&
|
|
|
|
answerMap[userAnswerNumber] === correctAnswer);
|
|
|
|
|
|
|
|
// Check if the input is a number within valid range or a text that matches one of the options
|
|
|
|
return (
|
|
|
|
response.author.id === userId &&
|
|
|
|
(userAnswerText || (userAnswerNumber >= 1 && userAnswerNumber <= 4))
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const answerCollector = interaction.channel.createMessageCollector({
|
|
|
|
filter: answerFilter,
|
|
|
|
max: 1,
|
|
|
|
time: timeLimit,
|
|
|
|
});
|
|
|
|
|
|
|
|
answerCollector.on("collect", async (message) => {
|
|
|
|
const userInput = message.content.trim();
|
|
|
|
const userAnswerNumber = parseInt(userInput, 10);
|
|
|
|
const userAnswer = answerMap[userAnswerNumber] || userInput;
|
|
|
|
|
|
|
|
let resultMessage = "Incorrect! Better luck next time.";
|
|
|
|
|
2024-09-05 18:49:18 +01:00
|
|
|
if (userAnswer === correctAnswer) {
|
2024-09-06 00:00:30 +01:00
|
|
|
resultMessage = "Correct!";
|
2024-09-05 18:49:18 +01:00
|
|
|
}
|
|
|
|
|
2024-09-06 00:00:30 +01:00
|
|
|
// Update leaderboard
|
|
|
|
let userScore = await Leaderboard.findOne({ userId });
|
|
|
|
if (!userScore) {
|
|
|
|
userScore = new Leaderboard({
|
|
|
|
userId,
|
|
|
|
username,
|
|
|
|
gamesPlayed: 1,
|
|
|
|
correctAnswers: userAnswer === correctAnswer ? 1 : 0,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
userScore.gamesPlayed += 1;
|
|
|
|
if (userAnswer === correctAnswer) {
|
|
|
|
userScore.correctAnswers += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await userScore.save();
|
2024-09-05 18:49:18 +01:00
|
|
|
|
2024-09-06 00:00:30 +01:00
|
|
|
await interaction.followUp(
|
|
|
|
`${resultMessage} <@${userId}> You've answered ${userScore.correctAnswers} questions correctly out of ${userScore.gamesPlayed} games.`
|
2024-09-05 18:49:18 +01:00
|
|
|
);
|
2024-09-05 23:55:49 +01:00
|
|
|
|
2024-09-06 00:00:30 +01:00
|
|
|
// Remove user from the ongoing trivia set once the game is finished
|
2024-09-05 23:55:49 +01:00
|
|
|
ongoingTrivia.delete(userId);
|
2024-09-06 00:00:30 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
answerCollector.on("end", (collected, reason) => {
|
|
|
|
if (reason === "time") {
|
|
|
|
interaction.followUp(
|
|
|
|
`<@${userId}> Time's up! You didn't answer in time.`
|
|
|
|
);
|
|
|
|
|
|
|
|
// Remove user from the ongoing trivia set when the game times out
|
|
|
|
ongoingTrivia.delete(userId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error fetching trivia question:", error);
|
|
|
|
|
|
|
|
// Inform the user about the error and let them retry
|
|
|
|
await interaction.reply({
|
|
|
|
content:
|
2024-09-06 00:03:44 +01:00
|
|
|
"Trivia API hit the rate limit. Please try again in 5 seconds.",
|
2024-09-06 00:00:30 +01:00
|
|
|
ephemeral: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Remove the user from the ongoing trivia set in case of an error
|
|
|
|
ongoingTrivia.delete(userId);
|
|
|
|
}
|
2024-09-05 18:49:18 +01:00
|
|
|
},
|
|
|
|
};
|