# Introduction
Discord Trivia is a Discord.JS based library designed to implement fully-fledged trivia/quiz games through your Discord bot. This library enables developers to fetch API questions from +20 categories with support for custom questions, configurable game settings, configurable designs, and a built-in leaderboard system. Thanks to OpenTDB for the questions!
# Installation
Start by installing the package via the command line with NPM: -->
npm install discord-trivia
# Basic Setup
Begin by importing GameManager
from the package and assigning it to a variable. Your game manager will be responsible for creating and managing trivia games. You can have multiple trivia games simultaneously, but only one trivia manager.
import { GameManager } from 'discord-trivia';
const manager = new GameManager();
Define your first trivia game with manager.createGame(channel)
, where channel
is a TextBasedChannel from a Message, Command Interaction, or fetched channel.
To start the game, call game.startQueue()
. If your game was created via a command interaction, you can supply the interaction as a parameter to ensure it is replied to.
// Message Command
const game = manager.createGame(message.channel);
game.startQueue();
// Slash Command
const game = manager.createGame(interaction.channel);
// Automated interaction reply
game.startQueue(interaction);
// Or use your own interaction.reply()
game.startQueue();
interaction.reply(...);
# Error Handling
Handle errors that may occur with your game using a try...catch block or then()...catch() callbacks.
// Try...Catch
try {
await game.startQueue(interaction);
} catch (err) {
console.error(`An error occurred: ${err}`);
}
// Then...Catch
game.startQueue(interaction)
.catch(err => console.error(`An error occurred: ${err}`));
# Complete Basic Setup
Your final code will have this general structure:
import { GameManager } from 'discord-trivia';
const manager = new GameManager();
// -- Inside your command function --
const game = manager.createGame(interaction.channel);
try {
await game.startQueue(interaction);
} catch (err) {
console.error(`An error occurred: ${err}`);
}
That's it! Your bot will serve your channel with a complete trivia game experience. For further customization, continue reading to learn how to configure and customize your trivia games to your liking.