mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-14 00:08:06 +02:00
US Election Command
This commit is contained in:
@@ -257,7 +257,7 @@ in the appropriate channel's topic to use it.
|
|||||||
|
|
||||||
## Commands
|
## Commands
|
||||||
|
|
||||||
Total: 547
|
Total: 548
|
||||||
|
|
||||||
### Utility:
|
### Utility:
|
||||||
|
|
||||||
@@ -447,6 +447,7 @@ Total: 547
|
|||||||
* **people-in-space:** Responds with the people currently in space.
|
* **people-in-space:** Responds with the people currently in space.
|
||||||
* **time:** Responds with the current time in a particular location.
|
* **time:** Responds with the current time in a particular location.
|
||||||
* **today-in-history:** Responds with an event that occurred today in history.
|
* **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:
|
### Search:
|
||||||
|
|
||||||
@@ -1075,6 +1076,8 @@ here.
|
|||||||
* wikia ([API](https://www.wikia.com/api/v1/))
|
* wikia ([API](https://www.wikia.com/api/v1/))
|
||||||
- [festivalclaca.cat](https://www.festivalclaca.cat/)
|
- [festivalclaca.cat](https://www.festivalclaca.cat/)
|
||||||
* hat ([Soviet Hat Image](https://www.festivalclaca.cat/maxvi/mmbwJ/))
|
* 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](https://www.flickr.com/)
|
||||||
* flickr ([API](https://www.flickr.com/services/api/))
|
* flickr ([API](https://www.flickr.com/services/api/))
|
||||||
- [FML](https://www.fmylife.com/)
|
- [FML](https://www.fmylife.com/)
|
||||||
|
|||||||
@@ -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
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "xiao",
|
"name": "xiao",
|
||||||
"version": "119.28.0",
|
"version": "119.29.0",
|
||||||
"description": "Your personal server companion.",
|
"description": "Your personal server companion.",
|
||||||
"main": "Xiao.js",
|
"main": "Xiao.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
Reference in New Issue
Block a user