From 56bdbf033ac8df219848fff3ba1ccb7450f7d3e9 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sat, 7 Nov 2020 11:34:55 -0500 Subject: [PATCH] Would You Rather like WYPTB --- README.md | 2 +- commands/games-sp/would-you-rather.js | 67 +++++++++++++++++++++++++ commands/random-res/would-you-rather.js | 35 ------------- package.json | 2 +- 4 files changed, 69 insertions(+), 37 deletions(-) create mode 100644 commands/games-sp/would-you-rather.js delete mode 100644 commands/random-res/would-you-rather.js diff --git a/README.md b/README.md index fa83fade..f2773c0e 100644 --- a/README.md +++ b/README.md @@ -358,7 +358,6 @@ Total: 555 * **the-onion:** Responds with a random "The Onion" article. * **this-for-that:** So, basically, it's like a bot command for this dumb meme. * **word:** Responds with a random word. -* **would-you-rather:** Responds with a random "Would you rather ...?" question. * **xiao-fact:** Responds with a random fact about Xiao. ### Random Image: @@ -580,6 +579,7 @@ Total: 555 * **whos-that-pokemon-cry:** Guess who that Pokémon is, based on their cry. * **whos-that-pokemon:** Guess who that Pokémon is, based on their silhouette. * **will-you-press-the-button:** Responds with a random "Will You Press The Button?" dilemma. +* **would-you-rather:** Responds with a random "Would you rather ...?" question. ### Multi-Player Games: diff --git a/commands/games-sp/would-you-rather.js b/commands/games-sp/would-you-rather.js new file mode 100644 index 00000000..0d2b6e24 --- /dev/null +++ b/commands/games-sp/would-you-rather.js @@ -0,0 +1,67 @@ +const Command = require('../../structures/Command'); +const request = require('node-superfetch'); +const { stripIndents } = require('common-tags'); +const choices = ['1', '2']; + +module.exports = class WouldYouRatherCommand extends Command { + constructor(client) { + super(client, { + name: 'would-you-rather', + aliases: ['wy-rather', 'wyr'], + group: 'games-sp', + memberName: 'would-you-rather', + description: 'Responds with a random "Would you rather ...?" question.', + credit: [ + { + name: 'either', + url: 'http://either.io', + reason: 'API' + } + ] + }); + } + + async run(msg) { + const current = this.client.games.get(msg.channel.id); + if (current) return msg.reply(`Please wait until the current game of \`${current.name}\` is finished.`); + this.client.games.set(msg.channel.id, { name: this.name }); + try { + const data = await this.fetchScenario(); + await msg.say(stripIndents` + ${data.prefix ? `${data.prefix}, would you rather...` : 'Would you rather...'} + **1.** ${data.option_1} + **2.** ${data.option_2} + + _Respond with either **1** or **2** to continue._ + `); + const filter = res => res.author.id === msg.author.id && choices.includes(res.content.toLowerCase()); + const msgs = await msg.channel.awaitMessages(filter, { + time: 30000, + max: 1 + }); + if (!msgs.size) { + this.client.games.delete(msg.channel.id); + return msg.reply(stripIndents` + No response? Too bad. + 1 ${formatNumber(data.option1_total)} - ${formatNumber(data.option2_total)} 2 + `); + } + const option1 = msgs.first().content.toLowerCase() === '1'; + const totalVotes = Number.parseInt(data.option1_total, 10) + Number.parseInt(data.option2_total, 10); + const numToUse = option1 ? Number.parseInt(data.option1_total, 10) : Number.parseInt(data.option2_total, 10); + this.client.games.delete(msg.channel.id); + return msg.reply(stripIndents` + **${Math.round((numToUse / totalVotes) * 100)}%** of people agree! + 1 ${formatNumber(data.option1_total)} - ${formatNumber(data.option2_total)} 2 + `); + } catch (err) { + this.client.games.delete(msg.channel.id); + return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } + + async fetchScenario() { + const { text } = await request.get('http://either.io/'); + return JSON.parse(text.match(/window.initial_question = (\{.+\})/)[1]).question; + } +}; diff --git a/commands/random-res/would-you-rather.js b/commands/random-res/would-you-rather.js deleted file mode 100644 index 0d5390db..00000000 --- a/commands/random-res/would-you-rather.js +++ /dev/null @@ -1,35 +0,0 @@ -const Command = require('../../structures/Command'); -const request = require('node-superfetch'); -const { stripIndents } = require('common-tags'); - -module.exports = class WouldYouRatherCommand extends Command { - constructor(client) { - super(client, { - name: 'would-you-rather', - aliases: ['wy-rather', 'wyr'], - group: 'random-res', - memberName: 'would-you-rather', - description: 'Responds with a random "Would you rather ...?" question.', - credit: [ - { - name: 'either', - url: 'http://either.io', - reason: 'API' - } - ] - }); - } - - async run(msg) { - const data = await this.fetchScenario(); - return msg.say(stripIndents` - ${data.prefix ? `${data.prefix}, would you rather...` : 'Would you rather...'} - **${data.option_1}** or **${data.option_2}** - `); - } - - async fetchScenario() { - const { text } = await request.get('http://either.io/'); - return JSON.parse(text.match(/window.initial_question = (\{.+\})/)[1]).question; - } -}; diff --git a/package.json b/package.json index c859e314..4b285b54 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "119.38.2", + "version": "119.39.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {