From 211cbc2cb8b433f6faff5a3a2a03c4a4ec13b39e Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Tue, 15 Sep 2020 15:49:00 -0400 Subject: [PATCH] True or False split from quiz --- README.md | 4 +- commands/games-sp/quiz.js | 18 ++------ commands/games-sp/true-or-false.js | 74 ++++++++++++++++++++++++++++++ package.json | 2 +- 4 files changed, 81 insertions(+), 17 deletions(-) create mode 100644 commands/games-sp/true-or-false.js diff --git a/README.md b/README.md index 7010ccbd..0b5cd419 100644 --- a/README.md +++ b/README.md @@ -256,7 +256,7 @@ in the appropriate channel's topic to use it. ## Commands -Total: 541 +Total: 542 ### Utility: @@ -564,6 +564,7 @@ Total: 541 * **slots:** Play a game of slots. * **sorting-hat:** Take a quiz to determine your Hogwarts house. * **the-game:** If you think about the game, you lose. +* **true-or-false:** Answer a true or false question. * **typing-test:** See how fast you can type a sentence in a given time limit. * **waldo:** Try to find Waldo with spoiler tags! * **whos-that-pokemon:** Guess who that Pokémon is. @@ -1413,6 +1414,7 @@ here. * lie-swatter ([API](https://opentdb.com/api_config.php)) * quiz ([API](https://opentdb.com/api_config.php)) * quiz-duel ([API](https://opentdb.com/api_config.php)) + * true-or-false ([API](https://opentdb.com/api_config.php)) - [OpenWeatherMap](https://openweathermap.org/) * weather ([API](https://openweathermap.org/api)) - [OPTIFONT](http://opti.netii.net/) diff --git a/commands/games-sp/quiz.js b/commands/games-sp/quiz.js index 0c879c65..27cbd54c 100644 --- a/commands/games-sp/quiz.js +++ b/commands/games-sp/quiz.js @@ -2,7 +2,6 @@ const Command = require('../../structures/Command'); const { stripIndents } = require('common-tags'); const request = require('node-superfetch'); const { shuffle, list } = require('../../util/Util'); -const types = ['multiple', 'boolean']; const difficulties = ['easy', 'medium', 'hard']; const choices = ['A', 'B', 'C', 'D']; @@ -14,10 +13,7 @@ module.exports = class QuizCommand extends Command { group: 'games-sp', memberName: 'quiz', description: 'Answer a quiz question.', - details: stripIndents` - **Types:** ${types.join(', ')} - **Difficulties:** ${difficulties.join(', ')} - `, + details: `**Difficulties:** ${difficulties.join(', ')}`, credit: [ { name: 'Open Trivia DB', @@ -27,14 +23,6 @@ module.exports = class QuizCommand extends Command { } ], args: [ - { - key: 'type', - prompt: `Which type of question would you like to have? Either ${list(types, 'or')}.`, - type: 'string', - default: 'multiple', - oneOf: types, - parse: type => type.toLowerCase() - }, { key: 'difficulty', prompt: `What should the difficulty of the game be? Either ${list(difficulties, 'or')}.`, @@ -47,13 +35,13 @@ module.exports = class QuizCommand extends Command { }); } - async run(msg, { type, difficulty }) { + async run(msg, { difficulty }) { try { const { body } = await request .get('https://opentdb.com/api.php') .query({ amount: 1, - type, + type: 'multiple', encode: 'url3986', difficulty }); diff --git a/commands/games-sp/true-or-false.js b/commands/games-sp/true-or-false.js new file mode 100644 index 00000000..475e545a --- /dev/null +++ b/commands/games-sp/true-or-false.js @@ -0,0 +1,74 @@ +const Command = require('../../structures/Command'); +const { stripIndents } = require('common-tags'); +const request = require('node-superfetch'); +const { list } = require('../../util/Util'); +const difficulties = ['easy', 'medium', 'hard']; +const trueAns = ['true', 't', 'tru', 'yes', 'y']; +const falseAns = ['false', 'f', 'no', 'n']; + +module.exports = class TrueOrFalseCommand extends Command { + constructor(client) { + super(client, { + name: 'true-or-false', + aliases: ['true-false', 'tf', 'quiz-boolean'], + group: 'games-sp', + memberName: 'true-or-false', + description: 'Answer a true or false question.', + details: `**Difficulties:** ${difficulties.join(', ')}`, + credit: [ + { + name: 'Open Trivia DB', + url: 'https://opentdb.com/', + reason: 'API', + reasonURL: 'https://opentdb.com/api_config.php' + } + ], + args: [ + { + key: 'difficulty', + prompt: `What should the difficulty of the game be? Either ${list(difficulties, 'or')}.`, + type: 'string', + default: '', + oneOf: difficulties, + parse: difficulty => difficulty.toLowerCase() + } + ] + }); + } + + async run(msg, { difficulty }) { + try { + const { body } = await request + .get('https://opentdb.com/api.php') + .query({ + amount: 1, + type: 'boolean', + encode: 'url3986', + difficulty + }); + if (!body.results) return msg.reply('Oh no, a question could not be fetched. Try again later!'); + const correct = decodeURIComponent(body.results[0].correct_answer.toLowerCase()); + const correctBool = correct === 'true'; + await msg.reply(stripIndents` + **You have 15 seconds to answer this question.** + ${decodeURIComponent(body.results[0].question)} + **[T]rue or [F]alse?** + `); + const filter = res => { + if (res.author.id !== msg.author.id) return false; + return trueAns.includes(res.content.toUpperCase()) || falseAns.includes(res.content.toUpperCase()); + } + const msgs = await msg.channel.awaitMessages(filter, { + max: 1, + time: 15000 + }); + if (!msgs.size) return msg.reply(`Sorry, time is up! It was ${correctBool}.`); + const ans = msgs.first().content.toLowerCase(); + const ansBool = trueAns.includes(ans); + if (correctBool !== ansBool) return msg.reply(`Nope, sorry, it's ${correctBool}.`); + return msg.reply('Nice job! 10/10! You deserve some cake!'); + } catch (err) { + return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } +}; diff --git a/package.json b/package.json index 3df9e8a4..e3d3a987 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "119.24.3", + "version": "119.25.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {