US Election Command

This commit is contained in:
Dragon Fire
2020-09-30 13:07:32 -04:00
parent 762d4e49d6
commit ffeaa3f4f5
3 changed files with 64 additions and 2 deletions
+4 -1
View File
@@ -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/)
+59
View File
@@ -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 <https://projects.fivethirtyeight.com/2020-election-forecast/>._
`);
} 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 };
}
};
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "119.28.0",
"version": "119.29.0",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {