From 2c180f39b706d532072e5d2f76e630ac906b5ba7 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Wed, 20 Mar 2024 19:59:57 -0400 Subject: [PATCH] Fix would you rather --- .../games-sp/will-you-press-the-button.js | 72 ------------------- commands/games-sp/would-you-rather.js | 50 +++++++------ 2 files changed, 27 insertions(+), 95 deletions(-) delete mode 100644 commands/games-sp/will-you-press-the-button.js diff --git a/commands/games-sp/will-you-press-the-button.js b/commands/games-sp/will-you-press-the-button.js deleted file mode 100644 index 5371a518..00000000 --- a/commands/games-sp/will-you-press-the-button.js +++ /dev/null @@ -1,72 +0,0 @@ -const Command = require('../../framework/Command'); -const request = require('node-superfetch'); -const { decode: decodeHTML } = require('html-entities'); -const { stripIndents } = require('common-tags'); -const { verify, formatNumber } = require('../../util/Util'); - -module.exports = class WillYouPressTheButtonCommand extends Command { - constructor(client) { - super(client, { - name: 'will-you-press-the-button', - aliases: ['press-the-button', 'button', 'wyptb', 'press-button'], - group: 'games-sp', - memberName: 'will-you-press-the-button', - description: 'Responds with a random "Will You Press The Button?" dilemma.', - credit: [ - { - name: 'Will You Press The Button?', - url: 'https://willyoupressthebutton.com/', - 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 dilemma = await this.fetchDilemma(); - await msg.reply(stripIndents` - **${decodeHTML(dilemma.txt1)}** but **${decodeHTML(dilemma.txt2)}** - - Will you press the button? - _Respond with **[y]es** or **[n]o** to continue._ - `); - const verification = await verify(msg.channel, msg.author); - if (verification === 0) { - this.client.games.delete(msg.channel.id); - return msg.reply(stripIndents` - No response? Too bad. - Yes ${formatNumber(dilemma.yes)} - ${formatNumber(dilemma.no)} No - `); - } - await this.postResponse(dilemma.id, verification); - const totalVotes = dilemma.yes + dilemma.no; - this.client.games.delete(msg.channel.id); - return msg.reply(stripIndents` - **${Math.round(((verification ? dilemma.yes : dilemma.no) / totalVotes) * 100)}%** of people agree! - Yes ${formatNumber(dilemma.yes)} - ${formatNumber(dilemma.no)} No - `); - } catch (err) { - this.client.games.delete(msg.channel.id); - return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); - } - } - - async fetchDilemma() { - const { body } = await request.post('https://api2.willyoupressthebutton.com/api/v2/dilemma/'); - return body.dilemma; - } - - async postResponse(id, bool) { - try { - const { body } = await request - .post(`https://api2.willyoupressthebutton.com/api/v2/dilemma/${id}/${bool ? 'yes' : 'no'}`); - return body.success; - } catch { - return null; - } - } -}; diff --git a/commands/games-sp/would-you-rather.js b/commands/games-sp/would-you-rather.js index 1f4911a6..4f9e6907 100644 --- a/commands/games-sp/would-you-rather.js +++ b/commands/games-sp/would-you-rather.js @@ -14,12 +14,14 @@ module.exports = class WouldYouRatherCommand extends Command { description: 'Responds with a random "Would you rather ...?" question.', credit: [ { - name: 'either', - url: 'http://either.io', + name: 'wouldurather.io', + url: 'https://wouldurather.io', reason: 'API' } ] }); + + this.availableQuestions = null; } async run(msg) { @@ -27,11 +29,12 @@ module.exports = class WouldYouRatherCommand extends Command { 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(); + if (!this.availableQuestions) await this.fetchAvailableQuestions(); + const data = await this.fetchRandomQuestion(); await msg.say(stripIndents` - ${data.prefix ? `${data.prefix}, would you rather...` : 'Would you rather...'} - **1.** ${data.option_1} - **2.** ${data.option_2} + Would you rather... + **1.** ${data.option1} + **2.** ${data.option2} _Respond with either **1** or **2** to continue._ `); @@ -45,17 +48,16 @@ module.exports = class WouldYouRatherCommand extends Command { this.client.games.delete(msg.channel.id); return msg.reply(stripIndents` No response? Too bad. - ${formatNumber(data.option1_total)} - ${formatNumber(data.option2_total)} + ${formatNumber(data.option1Votes)} - ${formatNumber(data.option2Votes)} `); } const option1 = msgs.first().content.toLowerCase() === '1'; - await this.postResponse(data.id, option1); - 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); + const totalVotes = Number.parseInt(data.option1Votes, 10) + Number.parseInt(data.option2Votes, 10); + const numToUse = option1 ? Number.parseInt(data.option1Votes, 10) : Number.parseInt(data.option2Votes, 10); this.client.games.delete(msg.channel.id); return msg.reply(stripIndents` **${Math.round((numToUse / totalVotes) * 100)}%** of people agree! - ${formatNumber(data.option1_total)} - ${formatNumber(data.option2_total)} + ${formatNumber(data.option1Votes)} - ${formatNumber(data.option2Votes)} `); } catch (err) { this.client.games.delete(msg.channel.id); @@ -63,19 +65,21 @@ module.exports = class WouldYouRatherCommand extends Command { } } - async fetchScenario() { - const { text } = await request.get('http://either.io/questions/next/1'); - return JSON.parse(text).questions[0]; + async fetchAvailableQuestions() { + const { body } = await request.get('https://wouldurather.io/api/availableQuestions'); + this.availableQuestions = body.question_array; + return this.availableQuestions; } - async postResponse(id, bool) { - try { - const { text } = await request - .get(`http://either.io/vote/${id}/${bool ? '1' : '2'}`) - .set({ 'X-Requested-With': 'XMLHttpRequest' }); - return JSON.parse(text).result; - } catch { - return false; - } + async fetchQuestion(id) { + const { body } = await request + .get('https://wouldurather.io/api/question') + .query({ id }); + return body; + } + + fetchRandomQuestion() { + const id = this.availableQuestions[Math.floor(Math.random() * this.availableQuestions.length)]; + return this.fetchQuestion(id); } };