mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-03 23:36:43 +02:00
list in Util
This commit is contained in:
@@ -2,6 +2,7 @@ const Command = require('../../structures/Command');
|
||||
const { MessageEmbed } = require('discord.js');
|
||||
const { stripIndents } = require('common-tags');
|
||||
const snekfetch = require('snekfetch');
|
||||
const { shuffle, list } = require('../../structures/Util');
|
||||
|
||||
module.exports = class QuizCommand extends Command {
|
||||
constructor(client) {
|
||||
@@ -38,20 +39,23 @@ module.exports = class QuizCommand extends Command {
|
||||
encode: 'url3986'
|
||||
});
|
||||
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()
|
||||
.setTitle('You have 15 seconds to answer this question:')
|
||||
.setDescription(stripIndents`
|
||||
**${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);
|
||||
const msgs = await msg.channel.awaitMessages(res => res.author.id === msg.author.id, {
|
||||
max: 1,
|
||||
time: 15000
|
||||
});
|
||||
if (!msgs.size) return msg.say(`Time! It was ${answer}, sorry!`);
|
||||
if (msgs.first().content.toLowerCase() !== answer) return msg.say(`Nope, sorry, it's ${answer}.`);
|
||||
if (!msgs.size) return msg.say(`Time! It was ${correct}, sorry!`);
|
||||
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!');
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const Command = require('../../structures/Command');
|
||||
const { list } = require('../../structures/Util');
|
||||
|
||||
module.exports = class ShipCommand extends Command {
|
||||
constructor(client) {
|
||||
@@ -20,7 +21,6 @@ module.exports = class ShipCommand extends Command {
|
||||
|
||||
run(msg, 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} a ${Math.floor(Math.random() * 100) + 1}%!`);
|
||||
return msg.say(`I'd give ${list(things)} a ${Math.floor(Math.random() * 100) + 1}%!`);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -73,6 +73,10 @@ class Util {
|
||||
}
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user