-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
56 lines (51 loc) · 1.43 KB
/
index.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
import cron from "node-cron";
import { send } from "./discord";
import { load, scrap } from "./haapi";
import config from "./config.toml";
console.info("Dragoturkey booting up");
setTimezone(config.timezone);
await load();
setScheduler(config.schedule);
if (config.debug) {
console.info("[DEBUG] Running tasks immediately");
await run();
}
async function run() {
const items = await scrap();
for (const item of items) {
console.info(
`[${item.template_key}][${item.sites}] ${item.name} (${item.id})`,
);
for (const server of config.servers) {
let site = item.sites.find((site) => server[site]);
// Needed hack cause DOFUS_RETRO also has DOFUS in his sites
if (item.sites.includes("DOFUS_RETRO")) {
site = "DOFUS_RETRO"
}
if (site) {
await send(item, server.webhook, server[site]);
} else if (server.others) {
await send(item, server.webhook, server.others);
} else {
if (config.debug) {
console.debug(`No channel set in ${server.name} for ${item.sites}`);
}
}
}
if (items.length === 0 && config.debug) {
console.debug("No new items");
}
}
}
function setTimezone(timezone = "Europe/Paris") {
process.env.TZ = timezone;
}
function setScheduler(schedule: { name: string; cron: string }[]) {
console.info("Setting up scheduler");
for (const task of schedule) {
console.info(`Task ${task.name} scheduled`);
cron.schedule(task.cron, async () => {
await run();
});
}
}