npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

open-trivia-db

v2.1.6

Published

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

Downloads

122

Readme

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