mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-04 15:56:52 +02:00
59 lines
2.3 KiB
JavaScript
59 lines
2.3 KiB
JavaScript
const commando = require('discord.js-commando');
|
|
const Discord = require('discord.js');
|
|
const request = require('superagent');
|
|
|
|
module.exports = class QuizCommand extends commando.Command {
|
|
constructor(Client) {
|
|
super(Client, {
|
|
name: 'quiz',
|
|
aliases: [
|
|
'jeopardy'
|
|
],
|
|
group: 'games',
|
|
memberName: 'quiz',
|
|
description: 'Answer a quiz question. (;quiz)',
|
|
examples: [';quiz']
|
|
});
|
|
}
|
|
|
|
async run(message) {
|
|
if (message.channel.type !== 'dm') {
|
|
if (!message.channel.permissionsFor(this.client.user).hasPermission(['SEND_MESSAGES', 'READ_MESSAGES', 'EMBED_LINKS'])) return;
|
|
}
|
|
console.log("[Command] " + message.content);
|
|
try {
|
|
const response = await request
|
|
.get('http://jservice.io/api/random')
|
|
.query({
|
|
count: 1
|
|
});
|
|
const data = response.body[0];
|
|
const answer = data.answer.toLowerCase().split("<i>").join("").split("</i>").join("");
|
|
const embed = new Discord.RichEmbed()
|
|
.setTitle('You have **fifteen** seconds to answer this question:')
|
|
.setDescription(`**Category: ${data.category.title}**\n${data.question}`);
|
|
const embedMsg = await message.embed(embed);
|
|
try {
|
|
const collected = await message.channel.awaitMessages(res => res.author.id === message.author.id, {
|
|
max: 1,
|
|
time: 15000,
|
|
errors: ['time']
|
|
});
|
|
if (collected.first().content.toLowerCase() !== answer) {
|
|
const loseMsg = await message.say(`The correct answer is: ${answer}`);
|
|
return [embedMsg, loseMsg];
|
|
}
|
|
const victoryMsg = await message.say(`The correct answer is: ${answer}`);
|
|
return [embedMsg, victoryMsg];
|
|
}
|
|
catch (err) {
|
|
const loseMsg = await message.say(`Aw... Too bad, try again next time!\nThe Correct Answer was: ${answer}`);
|
|
return [embedMsg, loseMsg];
|
|
}
|
|
}
|
|
catch (err) {
|
|
return message.say(":x: Error! Something went wrong!");
|
|
}
|
|
}
|
|
};
|