-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker-all-in-one.js
62 lines (48 loc) · 1.51 KB
/
worker-all-in-one.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
const STEAM_API_URL = "https://api.steampowered.com/ISteamApps/UpToDateCheck/v1/?appid=440&version=0"
const headers = {
headers: {
"Content-Type": "application/json",
},
}
async function updateVersionInKv(newVersion) {
await TF2_UPDATE.put("VERSION", newVersion)
}
async function sendWebhook() {
// Edit to hearts content
const formBody = {
embeds: [{
description: "A TF2 update has been released.",
color: 15105570,
timestamp: new Date(),
footer: {
text: "TF2 Update Worker"
}
}]
}
// Edit DISCORD_WEBHOOK_1 with whatever your ENV variable is
await fetch(DISCORD_WEBHOOK_1, {
body: JSON.stringify(formBody),
method: "POST",
headers
})
}
async function testForUpdate() {
const constKvVersion = await TF2_UPDATE.get("VERSION")
const resFromTf2Api = await fetch(STEAM_API_URL, headers)
const { response } = await resFromTf2Api.json()
const isNewUpdate = response.required_version.toString() !== constKvVersion.toString()
if (isNewUpdate) {
await Promise.all([
updateVersionInKv(response.required_version), sendWebhook()
])
return true
}
return false
}
async function handleRequest() {
const hasUpdate = await testForUpdate()
if (!hasUpdate) return new Response(JSON.stringify({ update: false }), headers)
return new Response(JSON.stringify({ update: true }), headers)
}
addEventListener("fetch", event => event.respondWith(handleRequest()))
addEventListener("scheduled", event => event.waitUntil(handleRequest()))