This repository has been archived by the owner on Aug 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
354 lines (317 loc) · 13.8 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
const request = require('request');
const chalk = require('chalk');
const fs = require('fs-extra');
const config = require('./config.json');
const SUCCESS = chalk.hex('#43B581');
const ERROR = chalk.hex('#F04747');
const WARN = chalk.hex('#FAA61A');
const PROCESS = chalk.hex('#F57731');
const INFO = chalk.hex('#FF73FA');
const LOG = chalk.hex('#44DDBF');
const URL = `https://discord.com/api/v10/users/@me/settings`;
/* Adds [LOG] and [dd/mm/yyyy | hh:mm:ss UTC] prefix to all console.log's */
let originalConsoleLog = console.log;
console.log = function () {
args = [];
let date = new Date();
let day = date.getDate();
let month = date.getMonth() + 1;
let year = date.getFullYear();
let hours = date.getUTCHours().toString().padStart(2, '0');
let minutes = date.getUTCMinutes().toString().padStart(2, '0');
let seconds = date.getUTCSeconds().toString().padStart(2, '0');
args.push(`${LOG(`[LOG]`)} ${INFO(`[${day}/${month}/${year} | ${hours}:${minutes}:${seconds} UTC]`)}`);
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
originalConsoleLog.apply(console, args);
}
/* Adds [ERROR] and [dd/mm/yyyy | hh:mm:ss UTC] prefix to all console.error's */
let originalConsoleError = console.error;
console.error = function () {
args = [];
let date = new Date();
let day = date.getDate();
let month = date.getMonth() + 1;
let year = date.getFullYear();
let hours = date.getUTCHours().toString().padStart(2, '0');
let minutes = date.getUTCMinutes().toString().padStart(2, '0');
let seconds = date.getUTCSeconds().toString().padStart(2, '0');
args.push(`${ERROR(`[ERROR]`)} ${INFO(`[${day}/${month}/${year} | ${hours}:${minutes}:${seconds} UTC]`)}`);
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
originalConsoleError.apply(console, args);
}
/* Format today's and yesterday's date in the following format: dd/mm/yyyy (e.g. 31/10/2020) */
let today = new Date();
today.setDate(today.getDate());
let yesterday = new Date(today);
yesterday.setDate(yesterday.getDate() - 1);
let dd = today.getDate();
let ydd = yesterday.getDate();
let mm = today.getMonth() + 1;
let ymm = yesterday.getMonth() + 1;
let yy = today.getFullYear();
let yyy = yesterday.getFullYear();
if (dd < 10) {
dd = `0${dd}`;
} else if (ydd < 10) {
ydd = `0${ydd}`;
}
if (mm < 10) {
mm = `0${mm}`;
} else if (ymm < 10) {
ymm = `0${ymm}`;
}
today = `${dd}/${mm}/${yy}`;
yesterday = `${ydd}/${ymm}/${yyy}`;
console.log(SUCCESS(`Successfully started the application...`));
console.log(PROCESS(`Fetching latest figures for ${today} (today) and ${yesterday} (yesterday)`));
let dataType;
let figureOne;
let figureTwo;
let todayHasChanged;
let yesterdayHasChanged;
let initialTasks = [compareStatus, fetchToday, fetchYesterday, handleFigures],
i = 0;
/* Consecutively execute initialTasks before handling the figures returned (onStartup only) */
onStartup();
function onStartup() {
initialTasks[i++]();
if (i < initialTasks.length) {
setTimeout(onStartup, 5000); // 5 seconds
}
}
/* Execute the tasks at a specified interval (in milliseconds) */
setInterval(function () {
let hour = new Date().getUTCHours();
let userPreference = config.showYesterday;
if (userPreference && hour >= 0 && hour < 3) {
y = 0;
todayHasChanged = 0;
yesterdayHasChanged = 0;
checkYesterday();
} else {
t = 0;
todayHasChanged = 0;
yesterdayHasChanged = 0;
checkToday();
}
}, 60000); // 60 seconds
/* Consecutively execute yesterdayTasks before handling the figures returned */
let yesterdayTasks = [fetchToday, fetchYesterday, handleFigures],
y = 0;
function checkYesterday() {
yesterdayTasks[y++]();
if (y < yesterdayTasks.length) {
setTimeout(checkYesterday, 5000); // 5 seconds
}
}
/* Consecutively execute todayTasks before handling the figures returned */
let todayTasks = [fetchToday, handleFigures],
t = 0;
function checkToday() {
todayTasks[t++]();
if (t < todayTasks.length) {
setTimeout(checkToday, 5000); // 5 seconds
}
}
/* Compare the user's Custom Status with the localData before attempting to make any changes */
function compareStatus() {
let localData = JSON.parse(fs.readFileSync('./data.json', 'utf8'));
return new Promise((resolve, reject) => {
request({
method: 'GET',
uri: URL,
headers: {
Authorization: config.token
},
json: true,
}, (err, res) => {
if (err) {
return reject(ERROR(`[ERROR] ${err}`));
}
if (res.statusCode !== 200) {
return reject(ERROR(`[ERROR] Invalid Status Code: ${res.statusCode}`));
}
resolve(true);
let customStatus = res.body.custom_status;
if (customStatus === null) {
console.log(WARN(`No already existing custom status was detected... setting user's status to the latest figures.`));
return resetLocalData();
}
let defaultString = {
'today': `Today: ${formatNumber(localData.today.todayCases)} Cases & ${formatNumber(localData.today.todayDeaths)} Deaths`,
'yesterday': `Yesterday: ${formatNumber(localData.yesterday.todayCases)} Cases & ${formatNumber(localData.yesterday.todayDeaths)} Deaths`
}
let suitableSuffix = config.suffix;
if (suitableSuffix && suitableSuffix.length > 75) {
throw new Error(ERROR(`Suffix provided exceeds the maximum character length of 75.`));
}
if ([defaultString.today, `${defaultString.today} ${config.suffix}`, defaultString.yesterday, `${defaultString.yesterday} ${config.suffix}`].indexOf(customStatus.text) < 0 ||
(customStatus.emoji_id !== config.emojiID) || (customStatus.emoji_name !== config.emojiName) || (localData.country !== config.country) || (localData.suffix !== config.suffix)) {
console.log(WARN(`User's custom status is not up-to-date... setting user's status to the latest figures.`));
return resetLocalData();
} else if (config.showYesterday && (customStatus.text === 'Today: 0 Cases & 0 Deaths')) {
console.log(WARN(`User's custom status is equal to today's figures, setting user's status to yesterday's figures.`));
return resetLocalData();
} else if (!config.showYesterday && ([defaultString.today, `${defaultString.today} ${config.suffix}`].indexOf(customStatus.text) < 0)) {
console.log(WARN(`User's custom status is not equal to today's figures, setting user's status to today's figures.`));
return resetLocalData();
} else {
console.log(WARN(`User's custom status is already up-to-date!`));
return;
}
});
});
}
/* Fetch the latest figures for today and write them to localData */
function fetchToday() {
let localData = JSON.parse(fs.readFileSync('./data.json', 'utf8'));
request({
url: `https://disease.sh/v3/covid-19/countries/${config.country}?yesterday=false&twoDaysAgo=false&strict=true&allowNull=false`,
json: true
}, function (err, data) {
if (err) {
console.error(err);
} else {
let apiData = data.body;
let newestDate = Math.max(localData.today.updated, apiData.updated);
if (newestDate <= localData.today.updated) {
return todayHasChanged = 0;
} else {
localData.today.updated = newestDate;
fs.writeFileSync('./data.json', JSON.stringify(localData, null, 4));
try {
if (localData.today.cases !== apiData.cases && apiData.cases !== undefined || localData.today.todayCases !== apiData.todayCases && apiData.todayCases !== undefined ||
localData.today.deaths !== apiData.deaths && apiData.deaths !== undefined || localData.today.todayDeaths !== apiData.todayDeaths && apiData.todayDeaths !== undefined ||
localData.today.recovered !== apiData.recovered && apiData.recovered !== undefined) {
for (let key in localData.today) {
localData.today[key] = apiData[key];
}
fs.writeFileSync('./data.json', JSON.stringify(localData, null, 4));
console.log(LOG('[TODAY] ' + WARN(`${formatNumber(apiData.todayCases)} Cases & ${formatNumber(apiData.todayDeaths)} Deaths`)));
return todayHasChanged = 1;
} else {
return todayHasChanged = 0;
}
} catch (err) {
console.error(err);
}
}
}
});
}
/* Fetch the latest figures for yesterday and write them to localData */
function fetchYesterday() {
let localData = JSON.parse(fs.readFileSync('./data.json', 'utf8'));
request({
url: `https://disease.sh/v3/covid-19/countries/${config.country}?yesterday=true&twoDaysAgo=false&strict=true&allowNull=false`,
json: true
}, function (err, data) {
if (err) {
console.error(err);
} else {
let apiData = data.body;
let newestDate = Math.max(localData.yesterday.updated, apiData.updated);
if (newestDate <= localData.yesterday.updated) {
return yesterdayHasChanged = 0;
} else {
localData.yesterday.updated = newestDate;
fs.writeFileSync('./data.json', JSON.stringify(localData, null, 4));
try {
if (localData.yesterday.cases !== apiData.cases && apiData.cases !== undefined || localData.yesterday.todayCases !== apiData.todayCases && apiData.todayCases !== undefined ||
localData.yesterday.deaths !== apiData.deaths && apiData.deaths !== undefined || localData.yesterday.todayDeaths !== apiData.todayDeaths && apiData.todayDeaths !== undefined ||
localData.yesterday.recovered !== apiData.recovered && apiData.recovered !== undefined) {
for (let key in localData.yesterday) {
localData.yesterday[key] = apiData[key];
}
fs.writeFileSync('./data.json', JSON.stringify(localData, null, 4));
console.log(LOG('[YESTERDAY] ' + WARN(`${formatNumber(apiData.todayCases)} Cases & ${formatNumber(apiData.todayDeaths)} Deaths`)));
return yesterdayHasChanged = 1;
} else {
return yesterdayHasChanged = 0;
}
} catch (err) {
console.error(err);
}
}
}
});
}
/* Handle localData before attempting to update the user's Custom Status */
function handleFigures() {
let localData = JSON.parse(fs.readFileSync('./data.json', 'utf8'));
let userPreference = config.showYesterday;
if (userPreference) {
if (yesterdayHasChanged && (localData.today.todayCases === 0 && localData.today.todayDeaths === 0) ||
todayHasChanged && (localData.today.todayCases === 0 && localData.today.todayDeaths === 0)) {
dataType = 'Yesterday';
figureOne = formatNumber(localData.yesterday.todayCases);
figureTwo = formatNumber(localData.yesterday.todayDeaths);
return updateStatus();
} else if (todayHasChanged && (localData.today.todayCases !== 0 || localData.today.todayDeaths !== 0)) {
dataType = 'Today';
figureOne = formatNumber(localData.today.todayCases);
figureTwo = formatNumber(localData.today.todayDeaths);
return updateStatus();
} else {
return;
}
} else {
if (todayHasChanged) {
dataType = 'Today';
figureOne = formatNumber(localData.today.todayCases);
figureTwo = formatNumber(localData.today.todayDeaths);
return updateStatus();
} else {
return;
}
}
}
/* Update user's Custom Status */
function updateStatus() {
return new Promise((resolve, reject) => {
request({
method: 'PATCH',
uri: URL,
headers: {
Authorization: config.token
},
json: {
custom_status: {
text: `${dataType}: ${figureOne} Cases & ${figureTwo} Deaths ${(config.suffix ? `${config.suffix}` : '')}`,
emoji_id: config.emojiID,
emoji_name: config.emojiName
}
}
}, (err, res) => {
if (err) {
return reject(ERROR(`[ERROR] ${err}`));
}
if (res.statusCode !== 200) {
return reject(ERROR(`[ERROR] Invalid Status Code: ${res.statusCode}`));
}
resolve(true);
console.log(LOG('[UPDATED STATUS] ' + WARN(`${dataType}: ${figureOne} Cases & ${figureTwo} Deaths ${(config.suffix ? `${config.suffix}` : '')}`)))
});
});
}
/* Reset localData */
function resetLocalData() {
let localData = JSON.parse(fs.readFileSync('./data.json', 'utf8'));
for (let key in localData.today) {
localData.today[key] = 0;
}
for (let key in localData.yesterday) {
localData.yesterday[key] = 0;
}
localData.country = config.country;
localData.suffix = config.suffix;
fs.writeFileSync('./data.json', JSON.stringify(localData, null, 4));
}
/* Format figureOne and figureTwo if they are > 999 (e.g. 23065 → 23,065) */
function formatNumber(figure) {
return figure.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');
}