From 415d1540974f2517510b6f974c674f88a93de845 Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Tue, 3 Oct 2017 18:24:24 +0000 Subject: [PATCH] Quizlet Commands --- commands/games/quizlet-game.js | 68 ++++++++++++++++++++++++++++++++++ commands/search/quizlet.js | 51 +++++++++++++++++++++++++ package.json | 2 +- 3 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 commands/games/quizlet-game.js create mode 100644 commands/search/quizlet.js diff --git a/commands/games/quizlet-game.js b/commands/games/quizlet-game.js new file mode 100644 index 00000000..86cc9125 --- /dev/null +++ b/commands/games/quizlet-game.js @@ -0,0 +1,68 @@ +const { Command } = require('discord.js-commando'); +const { stripIndents } = require('common-tags'); +const snekfetch = require('snekfetch'); +const { shuffle } = require('../../structures/Util'); +const { QUIZLET_KEY } = process.env; + +module.exports = class QuizletGameCommand extends Command { + constructor(client) { + super(client, { + name: 'quizlet-game', + aliases: ['quizlet-quiz', 'quizlet-test'], + group: 'games', + memberName: 'quizlet-game', + description: 'Shuffle a Quizlet study deck 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 fight 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. (type "end game" to end the game)** + ${term.definition}${term.image ? `\n${term.image.url}` : ''} + `); + const msgs = await msg.channel.awaitMessages(res => res.author.id === msg.author.id, { + max: 1, + time: 30000 + }); + if (!msgs.size || msgs.first().content.toLowerCase() !== term.term.toLowerCase()) { + if (msgs.first().content.toLowerCase() === 'end game') break; + 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 terms.push(term); + } + if (!seen.has(term.term)) seen.add(term.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!`); + } + } +}; diff --git a/commands/search/quizlet.js b/commands/search/quizlet.js new file mode 100644 index 00000000..4f3f82fe --- /dev/null +++ b/commands/search/quizlet.js @@ -0,0 +1,51 @@ +const { Command } = require('discord.js-commando'); +const { MessageEmbed } = require('discord.js'); +const snekfetch = require('snekfetch'); +const { QUIZLET_KEY } = process.env; + +module.exports = class QuizletCommand extends Command { + constructor(client) { + super(client, { + name: 'quizlet', + aliases: ['quizlet-set'], + group: 'search', + memberName: 'quizlet', + description: 'Searches Quizlet for study sets.', + clientPermissions: ['EMBED_LINKS'], + args: [ + { + key: 'query', + prompt: 'What study set would you like to search for?', + type: 'string' + } + ] + }); + } + + async run(msg, { query }) { + try { + const { body } = await snekfetch + .get('https://api.quizlet.com/2.0/search/sets') + .query({ + q: query, + client_id: QUIZLET_KEY + }); + if (!body.sets.length) return msg.say('Could not find any results.'); + const data = body.sets[0]; + const embed = new MessageEmbed() + .setAuthor('Quizlet', 'https://i.imgur.com/mUvSPJn.png') + .setColor(0x4257B2) + .setURL(data.url) + .setTitle(data.title) + .addField('❯ Creator', + data.created_by, true) + .addField('❯ ID', + data.id, true) + .addField('❯ Term Count', + data.term_count, true); + return msg.embed(embed); + } catch (err) { + return msg.say('Could not find any results.'); + } + } +}; diff --git a/package.json b/package.json index f7d1a29c..35b0e166 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiaobot", - "version": "44.3.1", + "version": "44.4.0", "description": "Your personal server companion.", "main": "Shard.js", "scripts": {