LakeYS / Discord-Trivia-Bot

TriviaBot: Play trivia in Discord! Powered by discord.js and OpenTDB, with 24 categories and three modes of play.
http://lakeys.net/triviabot/
Apache License 2.0
84 stars 39 forks source link

Questions without incorrect answers #247

Open Mte90 opened 2 years ago

Mte90 commented 2 years ago

As I have a db with questions without incorrect answers I had to edit the bot to ignore that feature. It is possible to implement it natively?

Mte90 commented 2 years ago

The diff of my changes to be able of that:

diff --git a/lib/database/filedb.js b/lib/database/filedb.js
index 2b8753b..59e6fe5 100644
--- a/lib/database/filedb.js
+++ b/lib/database/filedb.js
@@ -58,14 +58,15 @@ class FileDB extends Database {
     }

     // Invalid incorrect_answers
-    if(typeof question.incorrect_answers !== "object" || question.incorrect_answers === null) {
-      throw new Error(`Unable to identify the "incorrect-answers" field for the question "${question.question}"`);
-    }
-
-    // Wrong count for incorrect_answers
-    if(question.incorrect_answers.length !== 1 && question.incorrect_answers.length !== 3) {
-      throw new Error(`Invalid number of answers for the question "${question.question}"`);
-    }
+//     if(typeof question.incorrect_answers !== "object" || question.incorrect_answers === null) {
+//       throw new Error(`Unable to identify the "incorrect-answers" field for the question "${question.question}"`);
+//     }
+// 
+//     // Wrong count for incorrect_answers
+//     if(question.incorrect_answers.length !== 1 && question.incorrect_answers.length !== 3) {
+//       throw new Error(`Invalid number of answers for the question "${question.question}"`);
+//     }
+question.incorrect_answers = ''

     // Invalid difficulty
     if(typeof question.difficulty !== "string" || question.difficulty === null) {
@@ -315,12 +316,11 @@ class FileDB extends Database {
           // Prep to filter the question pool based on type and difficulty parameters.
           if(typeof options.type !== "undefined" || typeof options.difficulty !== "undefined") {
             exclusionPool.forEach((el) => {
-              el.type = this.types[el.incorrect_answers.length];

               if(typeof options.difficulty !== "undefined" && el.difficulty !== options.difficulty) {
                 exclusionArray.push(el.id);
               }
-              else if(typeof options.type !== "undefined" && el.type !== options.type) {
+              else if(typeof options.type !== "undefined") {
                 exclusionArray.push(el.id);
               }
             });
@@ -338,20 +338,20 @@ class FileDB extends Database {
             exclusionPoolCache.push(el.id);
           });

-          for(var iB in exclusionArray) {
-            // Match the ID in the array to an ID in the exclusion pool.
-            // In order to filter/splice the questions correctly, We will need to acquire the
-            // "exclusion ID", or the index of the question in the cache during and after splicing.
-            var exclusionID = exclusionPoolCache.indexOf(exclusionArray[iB]);
-
-            var result = exclusionPool.splice(exclusionID, 1);
-            var resultB = exclusionPoolCache.splice(exclusionID, 1); // Keep the cache in sync
-
-            // If the splice doesn't echo the question back, something has gone horribly wrong.
-            if(result.length === 0 || resultB.length === 0) {
-              throw new Error(`Failed to process question pool at index ${iB}/${length}`);
-            }
-          }
+//           for(var iB in exclusionArray) {
+//             // Match the ID in the array to an ID in the exclusion pool.
+//             // In order to filter/splice the questions correctly, We will need to acquire the
+//             // "exclusion ID", or the index of the question in the cache during and after splicing.
+//             var exclusionID = exclusionPoolCache.indexOf(exclusionArray[iB]);
+// 
+//             var result = exclusionPool.splice(exclusionID, 1);
+//             var resultB = exclusionPoolCache.splice(exclusionID, 1); // Keep the cache in sync
+// 
+//             // If the splice doesn't echo the question back, something has gone horribly wrong.
+//             if(result.length === 0 || resultB.length === 0) {
+//               throw new Error(`Failed to process question pool at index ${iB}/${length}`);
+//             }
+//           }

           var erAfter = Date.now();
           this.emit("debuglog", `Spliced ${exclusionArray.length} questions of ${length} with remainder ${exclusionPool.length} in ${erAfter-erBefore}ms`);
@@ -382,7 +382,7 @@ class FileDB extends Database {
         // Add additional information based on the question.
         // Note that the parameter .category is the same as .categoryName in the master pool.
         question.category = categoryName;
-        question.type = this.types[question.incorrect_answers.length];
+        question.type = '';

         // Add it to the question pool for export.
         questions.push(question);
diff --git a/lib/game.js b/lib/game.js
index 234dcba..918a8c7 100644
--- a/lib/game.js
+++ b/lib/game.js
@@ -268,7 +268,7 @@ class Game extends EventEmitter {
     this.inRound = false;

     // Parse the game type and difficulty
-    this.question.isTrueFalse = question.incorrect_answers.length === 1;
+    this.question.isTrueFalse = 0;
     if(!this.getConfig("hide-difficulty")) {
       switch(difficultyReceived) {
         case "easy":

Probably instead commenting is better to add a check to execute that code but atleast the line involved are there.