mirror of
https://github.com/aydenjahola/discord-multipurpose-bot.git
synced 2024-11-22 16:55:55 +00:00
trivia: try minimise duplicate questions
This commit is contained in:
parent
d38b9e531a
commit
99dc71fc60
1 changed files with 17 additions and 13 deletions
|
@ -6,9 +6,8 @@ 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
|
const ONGOING_TRIVIA = new Set(); // Track users with ongoing trivia
|
||||||
|
const LAST_API_CALL = { time: 0 }; // Track last API call
|
||||||
let lastApiCall = 0;
|
|
||||||
|
|
||||||
const CATEGORY_MAP = {
|
const CATEGORY_MAP = {
|
||||||
15: "Video Games",
|
15: "Video Games",
|
||||||
|
@ -34,13 +33,16 @@ const fetchTriviaQuestion = async (categoryId, categoryName) => {
|
||||||
category: categoryName,
|
category: categoryName,
|
||||||
}).sort({ last_served: 1 });
|
}).sort({ last_served: 1 });
|
||||||
|
|
||||||
if (!triviaQuestion || Date.now() - lastApiCall >= API_INTERVAL) {
|
// If no old question or API call interval has passed
|
||||||
|
if (!triviaQuestion || Date.now() - LAST_API_CALL.time >= API_INTERVAL) {
|
||||||
|
// Fetch a new question from the API
|
||||||
const response = await axios.get(
|
const response = await axios.get(
|
||||||
`https://opentdb.com/api.php?amount=1&category=${categoryId}`
|
`https://opentdb.com/api.php?amount=1&category=${categoryId}`
|
||||||
);
|
);
|
||||||
triviaQuestion = response.data.results[0];
|
triviaQuestion = response.data.results[0];
|
||||||
lastApiCall = Date.now();
|
LAST_API_CALL.time = Date.now();
|
||||||
|
|
||||||
|
// Save the new question to the database
|
||||||
await TriviaQuestion.create({
|
await TriviaQuestion.create({
|
||||||
question: decode(triviaQuestion.question),
|
question: decode(triviaQuestion.question),
|
||||||
correct_answer: decode(triviaQuestion.correct_answer),
|
correct_answer: decode(triviaQuestion.correct_answer),
|
||||||
|
@ -49,12 +51,14 @@ const fetchTriviaQuestion = async (categoryId, categoryName) => {
|
||||||
last_served: null,
|
last_served: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Fetch the newly created question
|
||||||
triviaQuestion = await TriviaQuestion.findOne({
|
triviaQuestion = await TriviaQuestion.findOne({
|
||||||
question: decode(triviaQuestion.question),
|
question: decode(triviaQuestion.question),
|
||||||
category: categoryName,
|
category: categoryName,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update the last served timestamp
|
||||||
if (triviaQuestion) {
|
if (triviaQuestion) {
|
||||||
triviaQuestion.last_served = new Date();
|
triviaQuestion.last_served = new Date();
|
||||||
await triviaQuestion.save();
|
await triviaQuestion.save();
|
||||||
|
@ -154,14 +158,14 @@ const handleAnswerCollection = async (
|
||||||
`${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.`
|
||||||
);
|
);
|
||||||
|
|
||||||
ongoingTrivia.delete(userId);
|
ONGOING_TRIVIA.delete(userId);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error processing collected answer:", error);
|
console.error("Error processing collected answer:", error);
|
||||||
await interaction.followUp({
|
await interaction.followUp({
|
||||||
content: "There was an error processing your answer.",
|
content: "There was an error processing your answer.",
|
||||||
ephemeral: true,
|
ephemeral: true,
|
||||||
});
|
});
|
||||||
ongoingTrivia.delete(userId);
|
ONGOING_TRIVIA.delete(userId);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -170,7 +174,7 @@ const handleAnswerCollection = async (
|
||||||
interaction.followUp(
|
interaction.followUp(
|
||||||
`<@${userId}> Time's up! You didn't answer in time.`
|
`<@${userId}> Time's up! You didn't answer in time.`
|
||||||
);
|
);
|
||||||
ongoingTrivia.delete(userId);
|
ONGOING_TRIVIA.delete(userId);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -179,7 +183,7 @@ const handleAnswerCollection = async (
|
||||||
content: "There was an error handling your response.",
|
content: "There was an error handling your response.",
|
||||||
ephemeral: true,
|
ephemeral: true,
|
||||||
});
|
});
|
||||||
ongoingTrivia.delete(userId);
|
ONGOING_TRIVIA.delete(userId);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -206,7 +210,7 @@ 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
|
||||||
|
|
||||||
if (ongoingTrivia.has(userId)) {
|
if (ONGOING_TRIVIA.has(userId)) {
|
||||||
return interaction.reply({
|
return interaction.reply({
|
||||||
content:
|
content:
|
||||||
"You already have an ongoing trivia game. Please finish it before starting a new one.",
|
"You already have an ongoing trivia game. Please finish it before starting a new one.",
|
||||||
|
@ -214,7 +218,7 @@ module.exports = {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
ongoingTrivia.add(userId);
|
ONGOING_TRIVIA.add(userId);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const categoryId = interaction.options.getString("category");
|
const categoryId = interaction.options.getString("category");
|
||||||
|
@ -267,10 +271,10 @@ module.exports = {
|
||||||
console.error("Error executing trivia command:", error);
|
console.error("Error executing trivia command:", error);
|
||||||
await interaction.reply({
|
await interaction.reply({
|
||||||
content:
|
content:
|
||||||
"Trivia API hit the rate limit. Please try again in 5 seconds.",
|
"Trivia API hit the rate limit or encountered an issue. Please try again in 5 seconds.",
|
||||||
ephemeral: true,
|
ephemeral: true,
|
||||||
});
|
});
|
||||||
ongoingTrivia.delete(userId);
|
ONGOING_TRIVIA.delete(userId);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue