diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..ae79d13 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + "configurations": [ + { + "name": "Miniflare", + "type": "node", + "request": "attach", + "port": 9229, + "cwd": "/", + "resolveSourceMapLocations": null, + "attachExistingChildren": false, + "autoAttachChildProcesses": false, + } + ] + } \ No newline at end of file diff --git a/src/worker.js b/src/worker.js index 94e1652..25b7d10 100644 --- a/src/worker.js +++ b/src/worker.js @@ -1,4 +1,4 @@ -async function cron(env) { +async function cron(env, sendMsg) { console.log("Cron func is running..."); const KV = env.KV; const webhookURL = env.url; @@ -6,6 +6,7 @@ async function cron(env) { const list = await KV.list(); console.log("Key list aquired"); + var total = 0; for (const { name, expiration } of list.keys) { console.log("Getting value for " + name); const value = await KV.get(name); @@ -13,14 +14,18 @@ async function cron(env) { console.log("Invalid Repo, skipping"); } else { repos.push({ name: name, count: parseInt(value) }); + total += parseInt(value); } } repos.sort((a, b) => b.count - a.count); // Sort repos by count in descending order let currentDate = new Date(); + var message = "# Downloads as of " + currentDate.toUTCString() + "\n\n" + repos.map(repo => `Repo: ${repo.name}, Count: ${repo.count}\n`).join(""); + var message = message + "\n\nTotal downloads: " + total; console.log("Message generated. Sending to Discord..."); console.log(message); + if (sendMsg == false) { await fetch(webhookURL, { method: "POST", headers: { @@ -33,7 +38,11 @@ async function cron(env) { .then((response) => console.log(response)) .catch((error) => console.error(error)); console.log("Message sent to Discord"); - return "complete\n"; + return "Complete! Message sent."; +} else { + console.log("Returning mesage"); + return message; +} } @@ -42,13 +51,13 @@ export default { console.log("Worker is running..."); console.log("Processing request for " + request.url) const url = request.url; - if (url.includes("testCron")) { console.log("testing cron func"); return new Response(await cron(env));} + if (url.includes("testCron")) { console.log("testing cron func"); return new Response(await cron(env, true));} if (url.includes("favicon.ico")) { return new Response(null, { status: 404 }); } - if (url.length < 1 | url == undefined | url == "http://127.0.0.1:8787/") { + if (url.length < 1 | url == undefined | url == "http://127.0.0.1:8787/") { //for local testing console.log("Invalid request, redirecting to 404") - return await response.redirect( + return await Response.redirect( "https://thealiendoctor.com/404", 301 ); } @@ -59,7 +68,7 @@ export default { var packName = packNameRaw.split("?file=")[0]; //e.g niceygylive/example if (packName == undefined | fileName == undefined) { console.log("Invalid request, redirecting to 404") - return await response.redirect( + return await Response.redirect( "https://thealiendoctor.com/404", 301 ); @@ -94,6 +103,6 @@ export default { ); }, async scheduled(event, env, ctx) { - ctx.waitUntil(cron(env)); + ctx.waitUntil(cron(env, false)); }, };