Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

open-trivia-db

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

open-trivia-db

A small and simple wrapper for the Open Trivia Database API.

  • 2.1.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

OpenTriviaDB

version downloads minisize types

open-trivia-db is a small and simple library for interacting with the OpenTDB API.

Live Demo: https://replit.com/@Elitezenv/open-trivia-db-DEMO?v=1

Documentation: https://github.com/Elitezen/open-trivia-db-wrapper/wiki/Documentation

Discord.JS Add On

Planning a trivia command for your Discord bot? discord-trivia v2 is coming!: https://github.com/Elitezen/discord-trivia

2.1.5

  • Fixed the issue of the dist/ folder not being included.

Example Code

import { getQuestions, CategoryNames } from "open-trivia-db";

const questions = await getQuestions({
  amount: 10,
  category: CategoryNames.Animals,
  difficulty: QuestionDifficulties.Easy,
})

Result

[
  {
    value: 'How many teeth does an adult rabbit have?',
    category: { id: 27, name: 'Animals', getData: [Function: getData] },
    type: 'multiple',
    difficulty: 'easy',
    correctAnswer: '28',
    incorrectAnswers: [ '30', '26', '24' ],
    allAnswers: [ '24', '28', '30', '26' ],
    checkAnswer: [Function: checkAnswer]
  }
  ...
]

Guide

Getting Questions

Questions can be fetched via the getQuestions() function by supplying options such as amount, category, difficulty, type, session and encode.

type: The kind of questions, such as multiple choice ("multiple") or true/false ("boolean").

session: A session instance or session token. Learn about sessions

encode: The encoding of the questions such as base64, urlLegacy, url3968 or none which is the default.

You can apply options via their respective enums.

The result will be an array of questions.

import { 
  CategoryNames, 
  QuestionDifficulties, 
  QuestionTypes, 
  QuestionEncodings 
} from "open-trivia-db";

getQuestions({
  amount: 50,
  category: CategoryNames["Entertainment: Japanese Anime & Manga"],
  difficulty: QuestionDifficulties.Hard,
  type: QuestionTypes.Multiple,
  encode: QuestionEncodings.None
})

Getting Categories and Category Data


A category resolvable can either be a category name or id. Category id's range from 9-32 inclusive, for there are 23 categories.

To jump between resolvables, use Category.idByName() and Category.nameById().

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

Category.idByName('Art'); // 25
Category.nameById(25); // 'Art'

Getting a Category's Data

Use Category.getCategory() to get a category's data such as name, id, and question counts.

import { Category, CategoryNames } from "open-trivia-db"

Category.getCategory(CategoryNames.Geography)
  .then(console.log)
{
  id: 22,
  name: 'Geography',
  questionCount: { 
    total: 275, 
    easy: 80, medium: 139, hard: 56 
  }
}

You can also complete a category's data through a question via Question.category.getData()

const targetQuestion = questions[0] // from getQuestions()

targetQuestion.category.getData()
  .then(console.log)

Sessions

A session ensures you are not supplied a question more than once throughout it's lifetime.

Initialize a session and supply the instance into getQuestions(). Make sure to await or resolve Session.start().

import { Session } from "open-trivia-db"

const mySession = new Session();
await mySession.start();

getQuestions({
  session: mySession
})

getQuestions() will return an error once your session has served every single question in OpenTDB. Don't worry, theres thousands of questions! You will likely never come accross a session's end. However, if you wish to reset your session, use Session.reset().

await mySession.reset();

Documentation: https://github.com/Elitezen/open-trivia-db-wrapper/wiki/Documentation

Keywords

FAQs

Package last updated on 30 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc