Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/elitezen/open-trivia-db-wrapper
A wrapper for the Open Trivia Database API. Built with TypeScript, works with VanillaJS.
https://github.com/elitezen/open-trivia-db-wrapper
api-wrapper game javascript library nodejs npm npm-module npm-package open-trivia-database opentdb opentdb-api quiz trivia trivia-api typescript
Last synced: about 1 month ago
JSON representation
A wrapper for the Open Trivia Database API. Built with TypeScript, works with VanillaJS.
- Host: GitHub
- URL: https://github.com/elitezen/open-trivia-db-wrapper
- Owner: Elitezen
- License: mit
- Created: 2022-05-14T00:28:49.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-05T17:42:58.000Z (7 months ago)
- Last Synced: 2024-11-01T21:36:43.158Z (about 2 months ago)
- Topics: api-wrapper, game, javascript, library, nodejs, npm, npm-module, npm-package, open-trivia-database, opentdb, opentdb-api, quiz, trivia, trivia-api, typescript
- Language: TypeScript
- Homepage: https://opentdb.com
- Size: 609 KB
- Stars: 6
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
![OpenTriviaDB](https://i.imgur.com/QBhF5aY.png)
![version](https://img.shields.io/npm/v/open-trivia-db)
![downloads](https://img.shields.io/npm/dm/open-trivia-db)
![minisize](https://img.shields.io/bundlephobia/min/open-trivia-db)
![types](https://img.shields.io/npm/types/open-trivia-db)`open-trivia-db` is a small and simple library for interacting with the [OpenTDB](https://opentdb.com/) API.
**Documentation**: https://github.com/Elitezen/open-trivia-db-wrapper/wiki/Documentation
## Discord.JS Add On
Checkout the powerful module `discord-trivia`: https://github.com/Elitezen/discord-trivia
# 2.1.6
- Added typescript as a developer depencency.
- Added safegaurd for unknown thrown exceptions from `OpenTDBError`.## Example Code
```js
import { getQuestions, CategoryNames, QuestionDifficulties } from "open-trivia-db";const questions = await getQuestions({
amount: 10,
category: CategoryNames.Animals,
difficulty: QuestionDifficulties.Easy,
})
```## Result
```js
[
{
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 QuestionsQuestions 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](#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.
```js
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()`.
```js
import { Category, CategoryNames } from "open-trivia-db";Category.idByName('Art'); // 25
Category.idByName(CategoryNames.Art); // 25Category.nameById(25); // 'Art'
```### Getting a Category's Data
Use `Category.getCategory()` to get a category's data such as name, id, and question counts.```js
import { Category, CategoryNames } from "open-trivia-db";Category.getCategory(CategoryNames.Geography)
.then(console.log);
``````js
{
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()`
```js
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()`.
```js
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, there are thousands of questions! You will likely never come accross a session's end. However, if you wish to reset your session, use `Session.reset()`.
```js
await mySession.reset();
```
**Documentation**: https://github.com/Elitezen/open-trivia-db-wrapper/wiki/Documentation