forked from COVID-Response-Collective/covid-data-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
212 lines (190 loc) · 7.68 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import Eris from 'eris';
import v from 'voca';
import { join, pick } from 'lodash';
import moment from 'moment';
import covid from 'novelcovid';
import schedule from 'node-schedule';
import { token, channels } from './config.json';
import { formatQuery, formatCountry, formatState } from './format';
const COMMAND = {
HELP: 'help',
SHOW: 'show',
};
const SCOPE = {
WORLD: 'world',
COUNTRY: 'country',
STATE: 'state',
};
const BOT_NAME = 'cbot';
const bot = new Eris(token);
function getHelp(channelId) {
const helpMessage = 'How to speak to me:\n\n'
+ `\`!${BOT_NAME} {command} {your query}\`\n\n`
+ 'Commands:\n\n'
+ `1. \`!${BOT_NAME} show world\`\n`
+ ' Gets latest case numbers worldwide.\n\n'
+ `2. \`!${BOT_NAME} show country [country name]\`\n`
+ ' Gets latest case numbers for the specified country.\n\n'
+ ' Example:\n'
+ ` \`!${BOT_NAME} show country us\`\n\n`
+ `3. \`!${BOT_NAME} show state [state name]\`\n`
+ ' Gets latest case numbers for the specified US state.\n\n'
+ ' Example:\n'
+ ` \`!${BOT_NAME} show state oregon\`\n`
+ ' OR\n'
+ ` \`!${BOT_NAME} show state or\`\n\n`
+ `3. \`!${BOT_NAME} help\`\n`
+ ' Prints this help message.\n\n'
+ 'Stay healthy! This bot loves you very much.';
bot.createMessage(channelId, helpMessage);
}
function getUnknown(channelId) {
bot.createMessage(channelId, `Unknown command or command missing. Type \`!${BOT_NAME} help\` for a list of commands.`);
}
async function getWorldData(channelId) {
try {
const {
cases, deaths, recovered, updated,
} = await covid.getAll();
const active = cases - deaths - recovered;
const dateLastUpdated = moment(updated).fromNow();
const message = `As of ${dateLastUpdated}, the current worldwide COVID-19 numbers are:\n\n`
+ `Total Cases: ${cases.toLocaleString('en')}\n`
+ `Deaths: ${deaths.toLocaleString('en')}\n`
+ `Recovered: ${recovered.toLocaleString('en')}\n`
+ `Active Cases: ${active.toLocaleString('en')}`;
bot.createMessage(channelId, message);
} catch (error) {
bot.createMessage(channelId, 'My apologies. I wasn\'t able to get the worldwide numbers.');
}
}
async function getCountryData(channelId, query) {
const country = formatQuery(join(query, ' '));
try {
const data = await covid.getCountry(country);
const {
cases,
todayCases,
deaths,
todayDeaths,
recovered,
active,
critical,
casesPerOneMillion,
} = pick(data, ['cases', 'todayCases', 'deaths', 'todayDeaths', 'recovered', 'active', 'critical', 'casesPerOneMillion']);
const formattedCountry = formatCountry(country);
const message = `As of the latest update, the current COVID-19 numbers in ${formattedCountry} are:\n\n`
+ `Total Cases: ${cases.toLocaleString('en')}\n`
+ `Deaths: ${deaths.toLocaleString('en')}\n`
+ `Critical Condition: ${critical.toLocaleString('en')}\n`
+ `Recovered: ${recovered.toLocaleString('en')}\n`
+ `Active Cases: ${active.toLocaleString('en')}\n`
+ `Cases Per Million: ${casesPerOneMillion.toLocaleString('en')}\n\n`
+ `New Cases Today: ${todayCases.toLocaleString('en')}\n`
+ `New Deaths Today: ${todayDeaths.toLocaleString('en')}`;
bot.createMessage(channelId, message);
} catch (error) {
bot.createMessage(channelId, `My apologies. I wasn't able to get the numbers for ${country}.`);
}
}
async function getStateData(channelId, query) {
const state = v.titleCase(formatState(join(query, ' ')));
try {
const data = await covid.getState(state);
const {
cases,
todayCases,
deaths,
todayDeaths,
active,
} = pick(data, ['cases', 'todayCases', 'deaths', 'todayDeaths', 'active']);
const recovered = cases - deaths - active;
const message = `As of the latest update, the current COVID-19 numbers in ${state} are:\n\n`
+ `Total Cases: ${cases.toLocaleString('en')}\n`
+ `Deaths: ${deaths.toLocaleString('en')}\n`
+ `Recovered: ${recovered.toLocaleString('en')}\n`
+ `Active Cases: ${active.toLocaleString('en')}\n\n`
+ `New Cases Today: ${todayCases.toLocaleString('en')}\n`
+ `New Deaths Today: ${todayDeaths.toLocaleString('en')}`;
bot.createMessage(channelId, message);
} catch (err) {
bot.createMessage(channelId, `My apologies. I wasn't able to get the numbers for ${state}.`);
}
}
async function update(channelId) {
try {
const or = await covid.getState('Oregon');
or.recovered = or.cases - or.deaths - or.active;
const oregonMessage = 'Oregon:\n\n'
+ `Total Cases: ${or.cases.toLocaleString('en')}\n`
+ `Deaths: ${or.deaths.toLocaleString('en')}\n`
+ `Recovered: ${or.recovered.toLocaleString('en')}\n`
+ `Active Cases: ${or.active.toLocaleString('en')}\n\n`
+ `New Cases Today: ${or.todayCases.toLocaleString('en')}\n`
+ `New Deaths Today: ${or.todayDeaths.toLocaleString('en')}\n\n`;
const wa = await covid.getState('Washington');
wa.recovered = wa.cases - wa.deaths - wa.active;
const washingtonMessage = 'Washington:\n\n'
+ `Total Cases: ${wa.cases.toLocaleString('en')}\n`
+ `Deaths: ${wa.deaths.toLocaleString('en')}\n`
+ `Recovered: ${wa.recovered.toLocaleString('en')}\n`
+ `Active Cases: ${wa.active.toLocaleString('en')}\n\n`
+ `New Cases Today: ${wa.todayCases.toLocaleString('en')}\n`
+ `New Deaths Today: ${wa.todayDeaths.toLocaleString('en')}`;
const fullMessage = oregonMessage + washingtonMessage;
bot.createMessage(channelId, fullMessage);
} catch (err) {
bot.createMessage(channelId, 'My apologies. I wasn\'t able to get the latest numbers for Oregon and Washington.');
}
}
bot.on('ready', () => { // When the bot is ready
console.log('Ready!'); // Log "Ready!"
});
bot.on('messageCreate', async (msg) => { // When a message is created
const message = v.lowerCase(msg.content);
if (v.startsWith(message, `!${BOT_NAME}`)) {
const messageWords = v.words(message).slice(1);
const [command, scope, ...query] = messageWords;
if (channels.allowed.includes(msg.channel.id)) {
switch (command) {
case COMMAND.SHOW:
switch (scope) {
case SCOPE.WORLD:
getWorldData(msg.channel.id, query);
break;
case SCOPE.COUNTRY:
getCountryData(msg.channel.id, query);
break;
case SCOPE.STATE:
getStateData(msg.channel.id, query);
break;
default:
getUnknown(msg.channel.id);
}
break;
case COMMAND.HELP:
getHelp(msg.channel.id);
break;
default:
getUnknown(msg.channel.id);
}
}
}
});
bot.connect(); // Get the bot to connect to Discord
schedule.scheduleJob('0 15 * * *', () => {
const updateMessage = 'This is the CRC COVID-19 Data Bot.\n'
+ `Here are the latest COVID-19 numbers in the Pacific Northwest this morning (${moment().format('D/MM/YYYY')}).`;
// bot.createMessage(channels.pnw, updateMessage);
// update(channels.pnw); // Oregon and Washington update
bot.createMessage(channels.test, updateMessage);
update(channels.test); // test Oregon and Washington update
});
schedule.scheduleJob('0 23 * * *', () => {
const updateMessage = 'This is the CRC COVID-19 Data Bot.\n'
+ `Here are the latest COVID-19 numbers in the Pacific Northwest this afternoon (${moment().format('D/MM/YYYY')}).`;
// bot.createMessage(channels.pnw, updateMessage);
// update(channels.pnw); // Oregon and Washington update
bot.createMessage(channels.test, updateMessage);
update(channels.test); // test Oregon and Washington update
});