const axios = require('axios'); const { MessageCollector } = require('eris-collector'); module.exports.run = async (client, message, args) => { axios.get('https://opentdb.com/api.php?amount=10&difficulty=easy&type=boolean').then(async res => { let score = 0 for (let qNumber = 0; qNumber < 10; qNumber++) { const getQuestion = function (number) { let question = res.data.results[number].question; question = question.replace(/"/g, '`'); question = question.replace(/'/g, '\''); return question; } client.createMessage(message.channel.id, { embed: { title: `${client.user.username} | Trivia`, color: 0x303136, description: `**Question ${qNumber + 1}:** ${getQuestion(qNumber)}` } }); let filter = (m) => m.author.id === message.author.id && new RegExp(`^(True|False|cancel)$`, "i").test(m.content); let collector = new MessageCollector(client, message.channel, filter, { time: 1000 * 30, max: 1 }); collector.on('collect', (m) => { if (/cancel/i.test(m.content)) return collector.stop("Cancelled.") if(/True/i.test(m.content) && res.data.results[0].correct_answer == 'True') { score++; qNumber++; return client.createMessage(message.channel.id, { embed: { title: `${client.user.username} | Trivia`, color: 0x32CD32, description: `The answer is correct!` } }); } if(/True/i.test(m.content) && res.data.results[0].correct_answer == 'False') { qNumber++; return client.createMessage(message.channel.id, { embed: { title: `${client.user.username} | Trivia`, color: 0xFF0000, description: `The answer is incorrect!` } }); } if(/False/i.test(m.content) && res.data.results[0].correct_answer == 'False') { score++; qNumber++; return client.createMessage(message.channel.id, { embed: { title: `${client.user.username} | Trivia`, color: 0x32CD32, description: `The answer is correct!` } }); } if(/False/i.test(m.content) && res.data.results[0].correct_answer == 'True') { qNumber++; return client.createMessage(message.channel.id, { embed: { title: `${client.user.username} | Trivia`, color: 0xFF0000, description: `The answer is incorrect!` } }); } }); collector.on('end', (_, reason) => { qNumber = 9 if (["time", "cancelled"].includes(reason)) return client.createMessage(message.channel.id, { embed: { title: `${client.user.username} | Trivia`, color: 0x303136, description: `Trivia cancelled.` } }); }); } if (qNumber === 10)return client.createMessage(message.channel.id, `Score: ${(score * 10)}%`); }); }; module.exports.help = { name: "trivia", aliases: ["triv"], syntax: "m!triva", description: "Play trivia!", category: "fun", permissions: ["No permissions required."] }