-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReady.ts
58 lines (51 loc) · 2.42 KB
/
Ready.ts
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
import type { BotClient } from "@models/discord/BotClient";
import dayjs from "dayjs";
import { CronJob } from 'cron';
import { Event } from '@models/discord/Event'
import { DateService } from "@services/DateService";
import { PlanningService } from "@services/PlanningService";
import { Constants } from "@constants";
export = new Event(
'ready',
true,
async (client) => {
console.info('[INFO] Connected to Discord\'s server');
// During the first seconds, the Node.js process is using the double of RAM
// Delay the initialization of the planning service to avoid overuse of CPU/RAM and so delay cron jobs
// initialization
setTimeout( () => {
PlanningService.getInstance().initialize( () => initializeCronJobsForAllConfigurations(client) );
}, 5_000);
console.info('[INFO] Ready to be used');
}
);
function initializeCronJobsForAllConfigurations(client: BotClient) {
Constants.CONFIGURATIONS.map( (conf, i) => {
// Plan a cron task to be executed the sunday, monday, tuesday, wednesday and thursday at 20:00 Europe/Paris
conf.cron.daily?.stop();
conf.cron.daily = new CronJob(i + " 20 * * 0,1,2,3,4", async () => {
const datePlanning = dayjs().tz(Constants.TIMEZONE).add(1, 'day');
if (!DateService.getInstance().isWorkDay(datePlanning)) {
// If it's the weekend, do nothing
return;
}
// Retrieve timetable for a specific day
return PlanningService.getInstance()
.getDailyPlanning(conf, datePlanning)
.then( async planning => {
if (planning.isDuringEnterprisePeriod()) {
return;
}
// Send an embed with planning for the next day
return planning.publish(client);
});
}, undefined, true, Constants.TIMEZONE, undefined, false);
// Plan a cron task to be executed the saturday at 20:00 Europe/Paris
conf.cron.weekly?.stop();
conf.cron.weekly = new CronJob(i + " 20 * * 6", async () => {
return PlanningService.getInstance()
.getBufferOfScreenWeeklyPlanning(conf, DateService.getInstance().getNextWeekIndex())
.then( async planning => planning.publish(client) )
}, undefined, true, Constants.TIMEZONE, undefined, false);
});
}