mirror of
https://github.com/aydenjahola/discord-multipurpose-bot.git
synced 2024-11-22 16:55:55 +00:00
trivia: maybe fix the session token issue?
This commit is contained in:
parent
6c3a6824a9
commit
82040aaba5
1 changed files with 26 additions and 9 deletions
|
@ -54,21 +54,37 @@ const resetSessionToken = async () => {
|
||||||
|
|
||||||
if (!session) {
|
if (!session) {
|
||||||
// If there's no session, create a new one as fallback
|
// If there's no session, create a new one as fallback
|
||||||
|
console.log("No session found, generating a new one.");
|
||||||
return await getSessionToken();
|
return await getSessionToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Log the old token for comparison
|
||||||
|
console.log("Old token:", session.token);
|
||||||
|
|
||||||
// Reset the session token
|
// Reset the session token
|
||||||
const response = await axios.get(
|
const response = await axios.get(
|
||||||
`https://opentdb.com/api_token.php?command=reset&token=${session.token}`
|
`https://opentdb.com/api_token.php?command=reset&token=${session.token}`
|
||||||
);
|
);
|
||||||
const newToken = response.data.token;
|
|
||||||
|
|
||||||
// Update token in the database
|
if (response.data.response_code === 0) {
|
||||||
session.token = newToken;
|
const newToken = response.data.token || session.token; // Sometimes the token might remain the same
|
||||||
session.last_updated = new Date();
|
|
||||||
await session.save();
|
|
||||||
|
|
||||||
return newToken;
|
// Log the new token to ensure it's correctly reset
|
||||||
|
console.log("New token:", newToken);
|
||||||
|
|
||||||
|
// Update token in the database
|
||||||
|
session.token = newToken;
|
||||||
|
session.last_updated = new Date();
|
||||||
|
await session.save();
|
||||||
|
|
||||||
|
return newToken;
|
||||||
|
} else {
|
||||||
|
console.error(
|
||||||
|
"Failed to reset the token, response code:",
|
||||||
|
response.data.response_code
|
||||||
|
);
|
||||||
|
throw new Error("Unable to reset the session token.");
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const fetchTriviaQuestion = async (categoryId, categoryName) => {
|
const fetchTriviaQuestion = async (categoryId, categoryName) => {
|
||||||
|
@ -93,9 +109,10 @@ const fetchTriviaQuestion = async (categoryId, categoryName) => {
|
||||||
const apiQuestion = response.data.results[0];
|
const apiQuestion = response.data.results[0];
|
||||||
|
|
||||||
// Check if the token is exhausted (response code 4 indicates this)
|
// Check if the token is exhausted (response code 4 indicates this)
|
||||||
if (response.data.response_code === 4) {
|
if (response.data.response_code === 3) {
|
||||||
sessionToken = await resetSessionToken(); // Reset session token
|
// Token not found
|
||||||
// Retry fetching the question with the new token
|
sessionToken = await getSessionToken(); // Create a new token
|
||||||
|
// Retry fetching the question
|
||||||
const retryResponse = await axios.get(
|
const retryResponse = await axios.get(
|
||||||
`https://opentdb.com/api.php?amount=1&category=${categoryId}&token=${sessionToken}`
|
`https://opentdb.com/api.php?amount=1&category=${categoryId}&token=${sessionToken}`
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in a new issue