DEV Community

Elitezen
Elitezen

Posted on • Updated on

Retrieve Trivia Questions With open-trivia-db

Easy Trivia

open-trivia-db 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 open-trivia-db
Enter fullscreen mode Exit fullscreen mode

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 'open-trivia-db';

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

   console.log(questions);
}

sample();
Enter fullscreen mode Exit fullscreen mode

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]
  },
...
]
Enter fullscreen mode Exit fullscreen mode

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,
Enter fullscreen mode Exit fullscreen mode

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

import { Category } from 'open-trivia-db';

Category.allNames. // ANIMALS, ART ...
Enter fullscreen mode Exit fullscreen mode

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

import { Category } from 'open-trivia-db';


let myCategory = new Category(9);

myCategory = new Category('GENERAL_KNOWLEDGE');

myCategory = new Category(Category.allNames.GENERAL_KNOWLEDGE);
Enter fullscreen mode Exit fullscreen mode

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

const data = await myCategory.getData();
Enter fullscreen mode Exit fullscreen mode

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 'open-trivia-db';

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();
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)