Fix willyoupressthebutton

This commit is contained in:
Dragon Fire
2020-10-12 16:40:34 -04:00
parent ac60af0c0f
commit 12497d492f
2 changed files with 32 additions and 8 deletions
@@ -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;
}
}
};
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "119.29.7",
"version": "119.29.8",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {