mirror of
https://github.com/aydenjahola/discord-multipurpose-bot.git
synced 2024-11-22 08:45:55 +00:00
trivia: avoid adding duplicate questiosn to the db
This commit is contained in:
parent
83fc8d207e
commit
eb6445b0f9
1 changed files with 30 additions and 16 deletions
|
@ -100,6 +100,12 @@ const fetchTriviaQuestion = async (categoryId, categoryName) => {
|
||||||
`https://opentdb.com/api.php?amount=1&category=${categoryId}&token=${sessionToken}`
|
`https://opentdb.com/api.php?amount=1&category=${categoryId}&token=${sessionToken}`
|
||||||
);
|
);
|
||||||
const retryApiQuestion = retryResponse.data.results[0];
|
const retryApiQuestion = retryResponse.data.results[0];
|
||||||
|
triviaQuestion = await TriviaQuestion.findOne({
|
||||||
|
question: decode(retryApiQuestion.question),
|
||||||
|
category: categoryName,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!triviaQuestion) {
|
||||||
await TriviaQuestion.create({
|
await TriviaQuestion.create({
|
||||||
question: decode(retryApiQuestion.question),
|
question: decode(retryApiQuestion.question),
|
||||||
correct_answer: decode(retryApiQuestion.correct_answer),
|
correct_answer: decode(retryApiQuestion.correct_answer),
|
||||||
|
@ -112,7 +118,14 @@ const fetchTriviaQuestion = async (categoryId, categoryName) => {
|
||||||
question: decode(retryApiQuestion.question),
|
question: decode(retryApiQuestion.question),
|
||||||
category: categoryName,
|
category: categoryName,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
triviaQuestion = await TriviaQuestion.findOne({
|
||||||
|
question: decode(apiQuestion.question),
|
||||||
|
category: categoryName,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!triviaQuestion) {
|
||||||
await TriviaQuestion.create({
|
await TriviaQuestion.create({
|
||||||
question: decode(apiQuestion.question),
|
question: decode(apiQuestion.question),
|
||||||
correct_answer: decode(apiQuestion.correct_answer),
|
correct_answer: decode(apiQuestion.correct_answer),
|
||||||
|
@ -126,6 +139,7 @@ const fetchTriviaQuestion = async (categoryId, categoryName) => {
|
||||||
category: categoryName,
|
category: categoryName,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
LAST_API_CALL.time = Date.now(); // Update the last API call time
|
LAST_API_CALL.time = Date.now(); // Update the last API call time
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue