mirror of
https://github.com/aydenjahola/discord-multipurpose-bot.git
synced 2024-11-22 16:55:55 +00:00
trivia: add the trivia source in the embed
This commit is contained in:
parent
616d823dde
commit
ec59d3dca2
1 changed files with 32 additions and 23 deletions
|
@ -28,38 +28,50 @@ const CATEGORY_MAP = {
|
||||||
|
|
||||||
const fetchTriviaQuestion = async (categoryId, categoryName) => {
|
const fetchTriviaQuestion = async (categoryId, categoryName) => {
|
||||||
try {
|
try {
|
||||||
let triviaQuestion = await TriviaQuestion.findOne({
|
let triviaQuestion;
|
||||||
|
let source = "API"; // Default to API
|
||||||
|
|
||||||
|
// Attempt to find a question in the database that hasn't been served recently
|
||||||
|
triviaQuestion = await TriviaQuestion.findOne({
|
||||||
last_served: { $lt: new Date(Date.now() - QUESTION_EXPIRY) },
|
last_served: { $lt: new Date(Date.now() - QUESTION_EXPIRY) },
|
||||||
category: categoryName,
|
category: categoryName,
|
||||||
}).sort({ last_served: 1 });
|
}).sort({ last_served: 1 });
|
||||||
|
|
||||||
if (!triviaQuestion || Date.now() - LAST_API_CALL.time >= API_INTERVAL) {
|
if (!triviaQuestion || Date.now() - LAST_API_CALL.time >= API_INTERVAL) {
|
||||||
|
// If no question was found in the database or API cooldown is over, fetch from 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];
|
const apiQuestion = response.data.results[0];
|
||||||
LAST_API_CALL.time = Date.now();
|
|
||||||
|
|
||||||
|
// Save the API question in the database
|
||||||
await TriviaQuestion.create({
|
await TriviaQuestion.create({
|
||||||
question: decode(triviaQuestion.question),
|
question: decode(apiQuestion.question),
|
||||||
correct_answer: decode(triviaQuestion.correct_answer),
|
correct_answer: decode(apiQuestion.correct_answer),
|
||||||
incorrect_answers: triviaQuestion.incorrect_answers.map(decode),
|
incorrect_answers: apiQuestion.incorrect_answers.map(decode),
|
||||||
category: categoryName,
|
category: categoryName,
|
||||||
last_served: null,
|
last_served: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Re-fetch it from the database to return the saved question object
|
||||||
triviaQuestion = await TriviaQuestion.findOne({
|
triviaQuestion = await TriviaQuestion.findOne({
|
||||||
question: decode(triviaQuestion.question),
|
question: decode(apiQuestion.question),
|
||||||
category: categoryName,
|
category: categoryName,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
LAST_API_CALL.time = Date.now(); // Update the last API call time
|
||||||
|
} else {
|
||||||
|
// If found in the database, set source to "Database"
|
||||||
|
source = "Database";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (triviaQuestion) {
|
if (triviaQuestion) {
|
||||||
|
// Update the `last_served` timestamp when serving the question
|
||||||
triviaQuestion.last_served = new Date();
|
triviaQuestion.last_served = new Date();
|
||||||
await triviaQuestion.save();
|
await triviaQuestion.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
return triviaQuestion;
|
return { triviaQuestion, source }; // Return both the question and its source
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error fetching or saving trivia question:", error);
|
console.error("Error fetching or saving trivia question:", error);
|
||||||
throw new Error("Error fetching trivia question");
|
throw new Error("Error fetching trivia question");
|
||||||
|
@ -94,7 +106,8 @@ const createTriviaEmbed = (
|
||||||
question,
|
question,
|
||||||
answerMap,
|
answerMap,
|
||||||
guild,
|
guild,
|
||||||
timeLimit
|
timeLimit,
|
||||||
|
source
|
||||||
) => {
|
) => {
|
||||||
return new EmbedBuilder()
|
return new EmbedBuilder()
|
||||||
.setColor("#0099ff")
|
.setColor("#0099ff")
|
||||||
|
@ -109,7 +122,9 @@ const createTriviaEmbed = (
|
||||||
)
|
)
|
||||||
.setTimestamp()
|
.setTimestamp()
|
||||||
.setFooter({
|
.setFooter({
|
||||||
text: `${guild.name} | Answer within ${timeLimit / 1000} seconds`,
|
text: `${guild.name} | Answer within ${
|
||||||
|
timeLimit / 1000
|
||||||
|
} seconds | Source: ${source}`,
|
||||||
iconURL: guild.iconURL(),
|
iconURL: guild.iconURL(),
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -262,22 +277,15 @@ module.exports = {
|
||||||
const categoryId = interaction.options.getString("category");
|
const categoryId = interaction.options.getString("category");
|
||||||
const categoryName = CATEGORY_MAP[categoryId] || "Video Games";
|
const categoryName = CATEGORY_MAP[categoryId] || "Video Games";
|
||||||
|
|
||||||
let triviaQuestion = await fetchTriviaQuestion(categoryId, categoryName);
|
const { triviaQuestion, source } = await fetchTriviaQuestion(
|
||||||
|
categoryId,
|
||||||
|
categoryName
|
||||||
|
);
|
||||||
|
|
||||||
if (!triviaQuestion) {
|
if (!triviaQuestion) {
|
||||||
// If all questions have been served, fetch questions from the database
|
throw new Error("No questions available.");
|
||||||
const shuffledQuestions = await getShuffledQuestions(categoryName);
|
|
||||||
if (shuffledQuestions.length === 0) {
|
|
||||||
throw new Error("No questions available for this category.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
triviaQuestion = shuffledQuestions[0];
|
|
||||||
triviaQuestion.last_served = new Date();
|
|
||||||
await triviaQuestion.save();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!triviaQuestion) throw new Error("Failed to fetch trivia question");
|
|
||||||
|
|
||||||
const question = decode(triviaQuestion.question);
|
const question = decode(triviaQuestion.question);
|
||||||
const correctAnswer = decode(triviaQuestion.correct_answer);
|
const correctAnswer = decode(triviaQuestion.correct_answer);
|
||||||
const incorrectAnswers = triviaQuestion.incorrect_answers.map(decode);
|
const incorrectAnswers = triviaQuestion.incorrect_answers.map(decode);
|
||||||
|
@ -300,7 +308,8 @@ module.exports = {
|
||||||
question,
|
question,
|
||||||
answerMap,
|
answerMap,
|
||||||
guild,
|
guild,
|
||||||
timeLimit
|
timeLimit,
|
||||||
|
source // Pass the source flag (API or Database)
|
||||||
);
|
);
|
||||||
|
|
||||||
await interaction.reply({ embeds: [triviaEmbed] });
|
await interaction.reply({ embeds: [triviaEmbed] });
|
||||||
|
|
Loading…
Reference in a new issue