Example with discord.js

Discord is a communication tool like Skype and TeamSpeak, most used by gamers. You can create bots in Discord that will perform tasks for you. This is an example on how to use the package for a quiz.

The example below isn't correct anymore and outdated, I'll edit this soon!

DMQuizDiscord.js
const Discord = require('discord.js');
const Quizy = require('quizy');

const Client = new Discord.Client();

var myQuestion1 = new Quizy.Question("1 + 2 = ?", ['1', '2', '3'], '3');
var myQuestion2 = new Quizy.Question("2 - 1 = ?", ['1', '2', '3'], '1');

const myQuiz = new Quizy.Quiz([myQuestion1, myQuestion2])

Client.on("message", (message) => {
    if (message.content.startsWith("I want to do the quiz!")) {
        myQuiz.addUserIfNotSet(message.author)
        message.author.send(myQuiz.getQuestion(myQuiz.getUser(message.author).getCurrentQuestion()).question);
        message.author.send(myQuiz.getQuestion(myQuiz.getUser(message.author).getCurrentQuestion()).answers.join('\n'))
    } else {
        myQuiz.getUser(message.author).letAnswer(message.content,
            function () {
                message.author.send('Congratulations, you answered the question correct!')
            },
            function () {
                message.author.send('Your answer was wrong, I\'m sorry!')
            })
        if (!myQuiz.getUser(message.author).isFinished) {
            message.author.send(myQuiz.getQuestion(myQuiz.getUser(message.author).getCurrentQuestion()).question);
            message.author.send(myQuiz.getQuestion(myQuiz.getUser(message.author).getCurrentQuestion()).answers.join('\n'))
        } else {
            message.author.send(`You ended the quiz with a score of ${myQuiz.getUser(message.author).getScore()}`)
        }
    }
})

Last updated