restrict users from only requesting one trivia at a time, unless they answer their exisitng one

This commit is contained in:
Ayden Jahola 2024-09-05 23:55:49 +01:00
parent 379afca76b
commit 50bfb9c6e3
No known key found for this signature in database
GPG key ID: 71DD90AE4AE92742

View file

@ -6,6 +6,7 @@ const { decode } = require("html-entities");
const API_INTERVAL = 5000; // 5 seconds const API_INTERVAL = 5000; // 5 seconds
const QUESTION_EXPIRY = 30 * 24 * 60 * 60 * 1000; // 1 month const QUESTION_EXPIRY = 30 * 24 * 60 * 60 * 1000; // 1 month
const ongoingTrivia = new Set(); // Track users with ongoing trivia
let lastApiCall = 0; let lastApiCall = 0;
@ -30,6 +31,18 @@ module.exports = {
const guild = interaction.guild; const guild = interaction.guild;
const timeLimit = 30000; // Time limit for answering in milliseconds const timeLimit = 30000; // Time limit for answering in milliseconds
// 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);
const categoryId = interaction.options.getString("category"); const categoryId = interaction.options.getString("category");
const categoryName = categoryId === "15" ? "Video Games" : "Anime & Manga"; const categoryName = categoryId === "15" ? "Video Games" : "Anime & Manga";
@ -162,6 +175,9 @@ module.exports = {
await interaction.followUp( await interaction.followUp(
`${resultMessage} <@${userId}> You've answered ${userScore.correctAnswers} questions correctly out of ${userScore.gamesPlayed} games.` `${resultMessage} <@${userId}> You've answered ${userScore.correctAnswers} questions correctly out of ${userScore.gamesPlayed} games.`
); );
// Remove user from the ongoing trivia set once the game is finished
ongoingTrivia.delete(userId);
}); });
answerCollector.on("end", (collected, reason) => { answerCollector.on("end", (collected, reason) => {
@ -169,6 +185,9 @@ module.exports = {
interaction.followUp( interaction.followUp(
`<@${userId}> Time's up! You didn't answer in time.` `<@${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);
} }
}); });
}, },