Files
xiao/commands/games/quiz.js
T
Daniel Odendahl Jr 6b4beb3bbe better errors
2017-05-16 17:35:53 +00:00

52 lines
1.9 KiB
JavaScript

const { Command } = require('discord.js-commando');
const { RichEmbed } = require('discord.js');
const { stripIndents } = require('common-tags');
const snekfetch = require('snekfetch');
module.exports = class QuizCommand extends Command {
constructor(client) {
super(client, {
name: 'quiz',
aliases: ['jeopardy'],
group: 'games',
memberName: 'quiz',
description: 'Answer a quiz question.'
});
}
async run(msg) {
if (msg.channel.type !== 'dm')
if (!msg.channel.permissionsFor(this.client.user).has('EMBED_LINKS'))
return msg.say('This Command requires the `Embed Links` Permission.');
try {
const { body } = await snekfetch
.get('http://jservice.io/api/random')
.query({
count: 1
});
const answer = body[0].answer.toLowerCase().replace(/(<i>|<\/i>)/g, '');
const embed = new RichEmbed()
.setTitle('You have **15** seconds to answer this question:')
.setDescription(stripIndents`
**Category: ${body[0].category.title}**
${body[0].question}
`);
msg.embed(embed);
try {
const collected = await msg.channel.awaitMessages(res => res.author.id === msg.author.id, {
max: 1,
time: 15000,
errors: ['time']
});
if (collected.first().content.toLowerCase() !== answer)
return msg.say(`The correct answer is: ${answer}.`);
return msg.say(`Perfect! The correct answer is: ${answer}.`);
} catch (err) {
return msg.say(`Time! The correct answer is: ${answer}`);
}
} catch (err) {
return msg.say(`${err.name}: ${err.message}`);
}
}
};