mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-04 15:56:52 +02:00
78 lines
2.3 KiB
JavaScript
78 lines
2.3 KiB
JavaScript
const { Command } = require('discord.js-commando');
|
|
const { stripIndents } = require('common-tags');
|
|
const snekfetch = require('snekfetch');
|
|
const { shuffle } = require('../../util/Util');
|
|
const { QUIZLET_KEY } = process.env;
|
|
|
|
module.exports = class QuizletGameCommand extends Command {
|
|
constructor(client) {
|
|
super(client, {
|
|
name: 'quizlet',
|
|
aliases: ['quizlet-game', 'quizlet-quiz', 'quizlet-test'],
|
|
group: 'games',
|
|
memberName: 'quizlet',
|
|
description: 'Shuffle a Quizlet study set and play a game similar to a quiz.',
|
|
args: [
|
|
{
|
|
key: 'id',
|
|
prompt: 'What is the ID of the deck you wish to use?',
|
|
type: 'string',
|
|
parse: id => encodeURIComponent(id)
|
|
}
|
|
]
|
|
});
|
|
|
|
this.playing = new Set();
|
|
}
|
|
|
|
async run(msg, { id }) {
|
|
if (this.playing.has(msg.channel.id)) return msg.say('Only one game may be occurring per channel.');
|
|
this.playing.add(msg.channel.id);
|
|
try {
|
|
const { body } = await snekfetch
|
|
.get(`https://api.quizlet.com/2.0/sets/${id}/terms`)
|
|
.query({ client_id: QUIZLET_KEY });
|
|
const terms = shuffle(body);
|
|
const seen = new Set();
|
|
while (terms.length > 0) {
|
|
const term = terms[0];
|
|
await msg.say(stripIndents`
|
|
**You have 30 seconds to answer which word this is.**
|
|
${term.definition}
|
|
${term.image ? term.image.url : ''}
|
|
`);
|
|
const msgs = await msg.channel.awaitMessages(res => res.author.id === msg.author.id, {
|
|
max: 1,
|
|
time: 30000
|
|
});
|
|
if (!msgs.size) {
|
|
await msg.say('Time!');
|
|
continue;
|
|
}
|
|
const choice = msgs.first().content.toLowerCase();
|
|
if (choice === 'end') break;
|
|
if (choice !== term.term.toLowerCase()) {
|
|
await msg.say(`Nope, sorry, it was ${term.term}.`);
|
|
if (seen.has(term.term)) seen.delete(term.term);
|
|
terms.push(term);
|
|
} else {
|
|
await msg.say('Nice job! 10/10! You deserve some cake!');
|
|
if (seen.has(term.term)) {
|
|
seen.delete(term.term);
|
|
} else {
|
|
seen.add(term.term);
|
|
terms.push(term);
|
|
}
|
|
}
|
|
terms.shift();
|
|
}
|
|
this.playing.delete(msg.channel.id);
|
|
return msg.say('See you next time!');
|
|
} catch (err) {
|
|
this.playing.delete(msg.channel.id);
|
|
if (err.status === 404) return msg.say('Could not find any results.');
|
|
return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
|
}
|
|
}
|
|
};
|