diff --git a/commands/random-res/will-you-press-the-button.js b/commands/random-res/will-you-press-the-button.js index 006ef5e4..2f3b712d 100644 --- a/commands/random-res/will-you-press-the-button.js +++ b/commands/random-res/will-you-press-the-button.js @@ -1,6 +1,7 @@ const Command = require('../../structures/Command'); const request = require('node-superfetch'); -const cheerio = require('cheerio'); +const { stripIndents } = require('common-tags'); +const { verify } = require('../../util/Util'); module.exports = class WillYouPressTheButtonCommand extends Command { constructor(client) { @@ -14,7 +15,7 @@ module.exports = class WillYouPressTheButtonCommand extends Command { { name: 'Will You Press The Button?', url: 'https://willyoupressthebutton.com/', - reason: 'Dilemma Data' + reason: 'API' } ] }); @@ -22,13 +23,36 @@ module.exports = class WillYouPressTheButtonCommand extends Command { async run(msg) { try { - const { text } = await request.get('https://willyoupressthebutton.com/'); - const $ = cheerio.load(text, { normalizeWhitespace: true }); - const cond = $('div[id="cond"]').first().text().trim(); - const res = $('div[id="res"]').first().text().trim(); - return msg.say(`**${cond}** but **${res}**`); + const dilemma = await this.fetchDilemma(); + await msg.reply(stripIndents` + **${dilemma.txt1}** but **${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) return msg.reply('No response? Too bad.'); + await this.postResponse(dilemma.id, verification); + const totalVotes = dilemma.yes + dilemma.no; + return msg.reply(stripIndents` + **${Math.round(((verification ? dilemma.yes : dilemma.no) / totalVotes) * 100)}%** of people agree! + Yes ${dilemma.yes} - ${dilemma.no} No + `); } catch (err) { 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/package.json b/package.json index 684a6c16..bb08a7d7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "119.29.7", + "version": "119.29.8", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {