Retrieve Trivia Questions With easy-trivia

Easy Trivia

Easy Trivia is a small, simple and fast wrapper for OpenTriviaDatabase – A Free to use, user-contributed trivia question database. This module is lightweight, easy to use and fully typed!

⚠️ This module is a ES6 Module, no Commo…


This content originally appeared on DEV Community and was authored by Elitezen

Easy Trivia

Easy Trivia is a small, simple and fast wrapper for OpenTriviaDatabase - A Free to use, user-contributed trivia question database. This module is lightweight, easy to use and fully typed!

⚠️ This module is a ES6 Module, no CommonJS support.

If you use Discord.JS, checkout discord-trivia

Setup

Setup a JavaScript or TypeScript project. Install Easy Trivia via NPM with

npm i easy-trivia

Then create an index.js file.

Basic API Calls

The bread and butter of this library is the getQuestions() function. Provide options describing what kind of questions you want to retrieve such as

amount - The amount of questions to fetch (min. 1, max. 50)
difficulty - The difficulty of questions.
type - Question type (true/false or multiple choice)
category - The category of questions.

import { Category, getQuestions } from 'easy-trivia';

async function sample() {
   const questions = await getQuestions({
      amount: 3,
      difficulty: 'easy',
      type: 'multiple',
      category: Category.random()
   });

   console.log(questions);
}

sample();

The result will be an array of questions which will include the question itself, metadata, and a function that receives a string and checks it against the question's correct answer:

[
{
    value: 'The Italian automaker Lamborghini uses what animal as its logo?',
    category: 'Vehicles',
    type: 'multiple',
    difficulty: 'easy',
    correctAnswer: 'Bull',
    incorrectAnswers: [ 'Bat', 'Horse', 'Snake' ],
    allAnswers: [ 'Bat', 'Horse', 'Snake', 'Bull' ],
    checkAnswer: [Function: checkAnswer]
  },
...
]

Categories

OpenTDB contains 23 categories to choose from

GENERAL_KNOWLEDGE,
ENTERTAINMENT_BOOKS,
ENTERTAINMENT_FILM,
ENTERTAINMENT_MUSIC,
ENTERTAINMENT_MUSICALS_AND_THEATRES,
ENTERTAINMENT_TELEVISION,
ENTERTAINMENT_VIDEO_GAMES,
ENTERTAINMENT_BOARD_GAMES,
SCIENCE_AND_NATURE,
SCIENCE_COMPUTERS,
SCIENCE_MATHEMATICS,
MYTHOLOGY,
SPORTS,
GEOGRAPHY,
HISTORY,
POLITICS,
ART,
CELEBRITIES,
ANIMALS,
VEHICLES,
ENTERTAINMENT_COMICS,
SCIENCE_GADGETS,
ENTERTAINMENT_JAPANESE_ANIME_AND_MANGA,
ENTERTAINMENT_CARTOON_AND_ANIMATIONS,

Use the Category class to navigate these categories. You'll receive intellisense on the 23 categories when using Category.allNames

import { Category } from 'easy-trivia';

Category.allNames. // ANIMALS, ART ...

Initiating a category can be done by passing a CategoryResolvable into the constructor

import { Category } from 'easy-trivia';


let myCategory = new Category(9);

myCategory = new Category('GENERAL_KNOWLEDGE');

myCategory = new Category(Category.allNames.GENERAL_KNOWLEDGE);

Once you have an instance, you get retrieve the category's API data in real time.

const data = await myCategory.getData();

Sessions

OpenTDB API sessions track the questions it has served you and allows for prevention of duplicate questions throughout multiple API calls.

import { Session, getQuestions } from 'easy-trivia';

const session = new Session();

async function sessionCalls() {
   await session.start();

   const batch1 = await getQuestions({
      amount: 10,
      difficulty: 'hard',
      session
   });

   const batch2 = await getQuestions({
     amount: 10,
     difficulty: 'hard',
     session
   });


   const completeBatch = [...batch1, ...batch2]; // All unique!
}

session.end();

NPM: https://www.npmjs.com/package/easy-trivia
GitHub: https://github.com/Elitezen/easy-trivia
Documentation: https://github.com/Elitezen/easy-trivia/wiki/Documentation


This content originally appeared on DEV Community and was authored by Elitezen


Print Share Comment Cite Upload Translate Updates
APA

Elitezen | Sciencx (2022-04-19T01:34:35+00:00) Retrieve Trivia Questions With easy-trivia. Retrieved from https://www.scien.cx/2022/04/19/retrieve-trivia-questions-with-easy-trivia/

MLA
" » Retrieve Trivia Questions With easy-trivia." Elitezen | Sciencx - Tuesday April 19, 2022, https://www.scien.cx/2022/04/19/retrieve-trivia-questions-with-easy-trivia/
HARVARD
Elitezen | Sciencx Tuesday April 19, 2022 » Retrieve Trivia Questions With easy-trivia., viewed ,<https://www.scien.cx/2022/04/19/retrieve-trivia-questions-with-easy-trivia/>
VANCOUVER
Elitezen | Sciencx - » Retrieve Trivia Questions With easy-trivia. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/19/retrieve-trivia-questions-with-easy-trivia/
CHICAGO
" » Retrieve Trivia Questions With easy-trivia." Elitezen | Sciencx - Accessed . https://www.scien.cx/2022/04/19/retrieve-trivia-questions-with-easy-trivia/
IEEE
" » Retrieve Trivia Questions With easy-trivia." Elitezen | Sciencx [Online]. Available: https://www.scien.cx/2022/04/19/retrieve-trivia-questions-with-easy-trivia/. [Accessed: ]
rf:citation
» Retrieve Trivia Questions With easy-trivia | Elitezen | Sciencx | https://www.scien.cx/2022/04/19/retrieve-trivia-questions-with-easy-trivia/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.