-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
96 lines (82 loc) · 2.69 KB
/
main.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 { getProgrammingLanguage } from "aoc-dailies/lib/extra/languages.ts";
import { executeWebhook } from "aoc-dailies/lib/discord/webhook.ts";
import { getAoCDaily } from "aoc-dailies/lib/aoc/mod.ts";
import { load, retry } from "aoc-dailies/deps.ts";
import { getRandomMessage } from "aoc-dailies/lib/extra/messages.ts";
/**
* Executes the AOC daily Webhook message and sends it as an embed
*/
async function executeDaily() {
const discordWebhookURL = Deno.env.get("DISCORD_WEBHOOK_URL")!;
const date = new Date();
const year = date.getFullYear();
const day = date.getDate();
const { title, description, url } = await getAoCDaily({ year, day });
await executeWebhook({
url: discordWebhookURL,
data: {
embeds: [{
color: 0x00cc00,
url,
title: `Advent of Code ${year} Day ${day}: ${title}`,
fields: [{
name: "Puzzle Description:",
value: description.length > 500
? description.substring(0, 499) + "…"
: description,
}, {
name: "Fun Challenge - Complete the program using:",
value: getProgrammingLanguage(day - 1),
}, {
name: "Message of the day:",
value: getRandomMessage(),
}],
}],
},
});
}
/**
* Executes the AOC daily reminder (5 minutes before)
*/
async function executeReminder() {
const discordWebhookURL = Deno.env.get("DISCORD_WEBHOOK_URL")!;
const ROLE_ID = Deno.env.get("DISCORD_ROLE_ID") ?? "";
const now = new Date();
const utcYear = now.getUTCFullYear();
const utcMonth = now.getUTCMonth(); // 0-based
const utcDate = now.getUTCDate();
// 5:00 AM UTC
const reminderTime = new Date(Date.UTC(utcYear, utcMonth, utcDate, 5, 0, 0));
// Generate UNIX timestamp for Discord to render it
const unixTimestamp = Math.floor(reminderTime.getTime() / 1000);
const message =
`**<@&${ROLE_ID}> Advent Of Code ${utcYear} Day ${utcDate} releases <t:${unixTimestamp}:R>!**`;
await executeWebhook({
url: discordWebhookURL,
data: { content: message },
});
}
/**
* retryPromise will attempt to execute the callback until success or after 60 seconds
*/
async function retryPromise(callback: () => Promise<void>) {
await retry(callback);
}
async function main() {
await load({ export: true });
// Execute daily reminder 5 minutes before 5:00 AM UTC
Deno.cron(
"Execute AOC daily reminder via retryPromise",
"55 4 1-25 12 *",
async () => await retryPromise(executeReminder),
);
// Execute daily message at 5:00 AM UTC
Deno.cron(
"Executing AOC message via retryPromise",
"0 5 1-25 12 *",
async () => await retryPromise(executeDaily),
);
}
if (import.meta.main) {
await main();
}