DEV Community

Elitezen
Elitezen

Posted on • Edited on

2 2

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!

If you use Discord.JS, checkout discord-trivia

Setup

Setup a JavaScript or TypeScript project and install open-trivia-db via NPM:

npm i open-trivia-db
Enter fullscreen mode Exit fullscreen mode

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 { getQuestions, CategoryNames } from "open-trivia-db";

const questions = await getQuestions({
  amount: 10,
  category: CategoryNames.Animals,
  difficulty: 'easy',
  type: 'multiple'
})
Enter fullscreen mode Exit fullscreen mode

The result will be an array of questions with the following shape:

[
{
    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 24 categories to choose from:

1. General Knowledge
2. Entertainment: Books
3. Entertainment: Film
4. Entertainment: Music
5. Entertainment: Musicals and Theatres
6. Entertainment: Television
7. Entertainment: Video Games
8. Entertainment: Board Games
9. Science and Nature
10. Science: Computers
11. Science Mathematics
12. Mythology
13. Sports
14. Geography
15. History
16. Politics
17. Art
18. Celebrities
19. Animals
20. Vehicles
21. Entertainment: Comics
22. Science: Gadgets
23. Entertainment: Japanese Anime and Manga
24. Entertainment: Cartoon and Animations
Enter fullscreen mode Exit fullscreen mode

Use the CategoryNames enum to choose categories.

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

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


let myCategory = Category.getCategory(9);

myCategory = await Category.getCategory(CategoryNames.Geography); // (9)

console.log(myCategory);

// {
//   id: 22,
//   name: 'Geography',
//   questionCount: { 
//     total: 275, 
//     easy: 80, medium: 139, hard: 56 
//   }
// }
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

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay