···11+import { sendDiscordNotification } from "./utils/discordUtils.ts";
22+import { load } from "jsr:@std/dotenv";
33+44+await load({ export: true });
55+66+const endpoint = Deno.env.get("SHEET_ENDPOINT");
77+if (!endpoint) {
88+ throw new Error("SHEET_ENDPOINT environment variable is not set");
99+}
1010+1111+const response = await fetch(endpoint);
1212+const { data } = await response.json();
1313+1414+// @ts-ignore Deno.cron is unstable, run with --unstable-cron flag
1515+Deno.cron("QOTD", Deno.env.get("CRON_STRING"), async () => {
1616+ // Open KV and get last index
1717+ // @ts-ignore Deno.openKv is unstable, run with --unstable-kv flag
1818+ const kv = await Deno.openKv(
1919+ // `https://api.deno.com/databases/${
2020+ // Deno.env.get("DENO_KV_DATABASE_ID")
2121+ // }/connect`,
2222+ );
2323+ const result = await kv.get<number>(["lastIndex"]);
2424+ let index = result.value ?? 0;
2525+2626+ await sendDiscordNotification(data[index].question);
2727+ console.log(`Cron: posted question of index ${index} to Discord.`);
2828+2929+ // Update last index in KV
3030+ index = (index + 1) % data.length;
3131+ await kv.set(["lastIndex"], index);
3232+});