-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.ts
96 lines (90 loc) · 2.84 KB
/
scheduler.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
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
import {truncateKeystones} from "./db";
import schedule = require('node-schedule');
import {dadjoke} from "./dadjoke";
import {config} from "./config";
import {client, myLog} from "./index";
import {ForumChannel} from "discord.js";
import dayjs = require("dayjs");
// returns epoch time of last server reset
// optional parameter to specify a date
export function getLastReset(current: Date = new Date()) {
// if it's before 1500 UTC set hours to 0
current.setUTCHours((current.getUTCHours() < 15) ? 0 : 16)
// 2 = Tuesday
current.setDate(current.getDate() + (2 - current.getDay() - 7) % 7);
// create a new date
const tues = new Date(current.getFullYear(), current.getMonth(), current.getDate())
tues.setUTCHours(15)
return tues.getTime() / 1000;
}
// schedule a truncate job for each weekly reset
const job = schedule.scheduleJob(
"truncateKeystones",
{
minute: 1,
hour: 15,
dayOfWeek: 2,
tz: 'Etc/UTC'
},
() => {
truncateKeystones(getLastReset());
myLog('Truncating keystones table from database.');
});
// in case we weren't online during last reset, run once on startup
truncateKeystones(getLastReset());
myLog(`Scheduled job: ${job.name}`);
const dadjokejob = schedule.scheduleJob(
"dadjokes",
{
hour: 13,
minute: 15,
tz: 'Etc/UTC'
},
() => {
dadjoke().catch(myLog);
}
);
myLog(`Scheduled job: ${dadjokejob.name}`);
function cleanupAttendancePosts() {
const teams: any[] = [];
const teamInfo: any[] = [];
const guild = client.guilds.cache.get(config.guildID);
if (typeof (config.absences) !== undefined) {
for (const element of Object.entries(config.absences)) {
// @ts-ignore
teams.push([element[1].team, element[0]]);
// @ts-ignore
teamInfo[element[0]] = {
// @ts-ignore
teamName: element[1].team,
// @ts-ignore
channel: element[1].channel,
}
}
teamInfo.forEach(team => {
const posts = (guild?.channels.cache.get(team.channel) as ForumChannel).threads.cache;
posts.forEach(post => {
const date = dayjs(post.name);
const now = dayjs();
if (date.isBefore(now) && !post.locked && !post.archived) {
post.setLocked(true, 'Past Event').catch(myLog);
post.setArchived(true, 'Past Event').catch(myLog);
}
})
})
}
}
const attendancePostsCleanup = schedule.scheduleJob(
"cleanupAttendancePosts",
{
hour: 9,
minute: 0,
tz: 'Etc/UTC'
},
() => {
// cleanup function
cleanupAttendancePosts();
}
);
myLog(`Scheduled job: ${attendancePostsCleanup.name}`);
cleanupAttendancePosts();