From ffeaa3f4f511c22cb152ea12a08f01095115309c Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Wed, 30 Sep 2020 13:07:32 -0400 Subject: [PATCH] US Election Command --- README.md | 5 ++- commands/events/us-election.js | 59 ++++++++++++++++++++++++++++++++++ package.json | 2 +- 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 commands/events/us-election.js diff --git a/README.md b/README.md index 6a348bbe..b8082c3d 100644 --- a/README.md +++ b/README.md @@ -257,7 +257,7 @@ in the appropriate channel's topic to use it. ## Commands -Total: 547 +Total: 548 ### Utility: @@ -447,6 +447,7 @@ Total: 547 * **people-in-space:** Responds with the people currently in space. * **time:** Responds with the current time in a particular location. * **today-in-history:** Responds with an event that occurred today in history. +* **us-election:** Responds with the odds of each canidate winning the presidential election, according to 538. ### Search: @@ -1075,6 +1076,8 @@ here. * wikia ([API](https://www.wikia.com/api/v1/)) - [festivalclaca.cat](https://www.festivalclaca.cat/) * hat ([Soviet Hat Image](https://www.festivalclaca.cat/maxvi/mmbwJ/)) +- [FiveThirtyEight](https://fivethirtyeight.com/) + * us-election ([API](https://projects.fivethirtyeight.com/2020-election-forecast/)) - [Flickr](https://www.flickr.com/) * flickr ([API](https://www.flickr.com/services/api/)) - [FML](https://www.fmylife.com/) diff --git a/commands/events/us-election.js b/commands/events/us-election.js new file mode 100644 index 00000000..6e621efe --- /dev/null +++ b/commands/events/us-election.js @@ -0,0 +1,59 @@ +const Command = require('../../structures/Command'); +const request = require('node-superfetch'); +const { stripIndents } = require('common-tags'); +const year = 2020; + +module.exports = class UsElectionCommand extends Command { + constructor(client) { + super(client, { + name: 'us-election', + aliases: ['election', 'usa-election', 'presidential-election', 'president-election', `${year}-election`], + group: 'events', + memberName: 'us-election', + description: 'Responds with the odds of each canidate winning the presidential election, according to 538.', + credit: [ + { + name: 'FiveThirtyEight', + url: 'https://fivethirtyeight.com/', + reason: 'API', + reasonURL: 'https://projects.fivethirtyeight.com/2020-election-forecast/' + } + ] + }); + } + + async run(msg) { + const currentYear = new Date().getFullYear(); + if (year !== currentYear) { + return msg.reply(`This command has not been updated to reflect the ${currentYear} election season.`); + } + try { + const { winners, simulations } = await this.getList(); + const chances = Object.entries(winners) + .map(([canidate, chances]) => `**${canidate}:** ${chances} in ${simulations}`); + return msg.say(stripIndents` + **Chances of Winning the 2020 US Presidential Election (Accoring to FiveThirtyEight):** + ${chances.join('\n')} + + _More detailed information is available at ._ + `); + } catch (err) { + return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } + + async getList() { + const { body } = await request + .get('https://projects.fivethirtyeight.com/2020-election-forecast/us_simulations.json'); + const winners = {}; + for (const simulation of body[0].simulations) { + const existing = winners[simulation.winner]; + if (!existing) { + winners[simulation.winner] = 1; + continue; + } + winners[simulation.winner] += 1; + } + return { winners, simulations: body[0].simulations.length }; + } +}; diff --git a/package.json b/package.json index 07ecacf6..8f33adc3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "119.28.0", + "version": "119.29.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {