list in Util

This commit is contained in:
Daniel Odendahl Jr
2017-08-17 21:55:11 +00:00
parent c532eb726c
commit 80d86b3411
3 changed files with 14 additions and 6 deletions
+8 -4
View File
@@ -2,6 +2,7 @@ const Command = require('../../structures/Command');
const { MessageEmbed } = require('discord.js'); const { MessageEmbed } = require('discord.js');
const { stripIndents } = require('common-tags'); const { stripIndents } = require('common-tags');
const snekfetch = require('snekfetch'); const snekfetch = require('snekfetch');
const { shuffle, list } = require('../../structures/Util');
module.exports = class QuizCommand extends Command { module.exports = class QuizCommand extends Command {
constructor(client) { constructor(client) {
@@ -38,20 +39,23 @@ module.exports = class QuizCommand extends Command {
encode: 'url3986' encode: 'url3986'
}); });
if (!body.results) return msg.say('Oh no, a question could not be fetched. Try again later!'); if (!body.results) return msg.say('Oh no, a question could not be fetched. Try again later!');
const answer = decodeURIComponent(body.results[0].correct_answer.toLowerCase()); const answers = body.results[0].incorrect_answers.map(answer => decodeURIComponent(answer.toLowerCase()));
const correct = decodeURIComponent(body.results[0].correct_answer.toLowerCase());
answers.push(correct);
const embed = new MessageEmbed() const embed = new MessageEmbed()
.setTitle('You have 15 seconds to answer this question:') .setTitle('You have 15 seconds to answer this question:')
.setDescription(stripIndents` .setDescription(stripIndents`
**${decodeURIComponent(body.results[0].category)}** **${decodeURIComponent(body.results[0].category)}**
${type === 'boolean' ? 'True or False: ' : ''}${decodeURIComponent(body.results[0].question)} ${type === 'boolean' ? '**True or False:** ' : ''}${decodeURIComponent(body.results[0].question)}
${type === 'multiple' ? `**Choices:** ${list(shuffle(answers), 'or')}` : ''}
`); `);
await msg.embed(embed); await msg.embed(embed);
const msgs = await msg.channel.awaitMessages(res => res.author.id === msg.author.id, { const msgs = await msg.channel.awaitMessages(res => res.author.id === msg.author.id, {
max: 1, max: 1,
time: 15000 time: 15000
}); });
if (!msgs.size) return msg.say(`Time! It was ${answer}, sorry!`); if (!msgs.size) return msg.say(`Time! It was ${correct}, sorry!`);
if (msgs.first().content.toLowerCase() !== answer) return msg.say(`Nope, sorry, it's ${answer}.`); if (msgs.first().content.toLowerCase() !== correct) return msg.say(`Nope, sorry, it's ${correct}.`);
return msg.say('Nice job! 10/10! You deserve some cake!'); return msg.say('Nice job! 10/10! You deserve some cake!');
} }
}; };
+2 -2
View File
@@ -1,4 +1,5 @@
const Command = require('../../structures/Command'); const Command = require('../../structures/Command');
const { list } = require('../../structures/Util');
module.exports = class ShipCommand extends Command { module.exports = class ShipCommand extends Command {
constructor(client) { constructor(client) {
@@ -20,7 +21,6 @@ module.exports = class ShipCommand extends Command {
run(msg, args) { run(msg, args) {
const { things } = args; const { things } = args;
const list = `${things.slice(0, -1).join(', ')}${things.length > 1 ? ' and ' : ''}${things.slice(-1)}`; return msg.say(`I'd give ${list(things)} a ${Math.floor(Math.random() * 100) + 1}%!`);
return msg.say(`I'd give ${list} a ${Math.floor(Math.random() * 100) + 1}%!`);
} }
}; };
+4
View File
@@ -73,6 +73,10 @@ class Util {
} }
return arr; return arr;
} }
static list(arr, conj = 'and') {
return `${arr.slice(0, -1).join(', ')}${arr.length > 1 ? `${arr.length > 2 ? ',' : ''} ${conj} ` : ''}${arr.slice(-1)}`; // eslint-disable-line max-len
}
} }
module.exports = Util; module.exports = Util;