posts "question of the day" to a discord webhook

Initial commit

+70
+3
.gitignore
··· 1 + .env 2 + .vscode 3 + deno.lock
+13
deno.json
··· 1 + { 2 + "tasks": { 3 + "dev": "deno run --watch main.ts", 4 + "run": "deno run --unstable-cron --unstable-kv --allow-all main.ts" 5 + }, 6 + "imports": { 7 + "@std/assert": "jsr:@std/assert@1" 8 + }, 9 + "fmt": { 10 + "useTabs": true, 11 + "indentWidth": 4 12 + } 13 + }
+32
main.ts
··· 1 + import { sendDiscordNotification } from "./utils/discordUtils.ts"; 2 + import { load } from "jsr:@std/dotenv"; 3 + 4 + await load({ export: true }); 5 + 6 + const endpoint = Deno.env.get("SHEET_ENDPOINT"); 7 + if (!endpoint) { 8 + throw new Error("SHEET_ENDPOINT environment variable is not set"); 9 + } 10 + 11 + const response = await fetch(endpoint); 12 + const { data } = await response.json(); 13 + 14 + // @ts-ignore Deno.cron is unstable, run with --unstable-cron flag 15 + Deno.cron("QOTD", Deno.env.get("CRON_STRING"), async () => { 16 + // Open KV and get last index 17 + // @ts-ignore Deno.openKv is unstable, run with --unstable-kv flag 18 + const kv = await Deno.openKv( 19 + // `https://api.deno.com/databases/${ 20 + // Deno.env.get("DENO_KV_DATABASE_ID") 21 + // }/connect`, 22 + ); 23 + const result = await kv.get<number>(["lastIndex"]); 24 + let index = result.value ?? 0; 25 + 26 + await sendDiscordNotification(data[index].question); 27 + console.log(`Cron: posted question of index ${index} to Discord.`); 28 + 29 + // Update last index in KV 30 + index = (index + 1) % data.length; 31 + await kv.set(["lastIndex"], index); 32 + });
+22
utils/discordUtils.ts
··· 1 + import { load } from "jsr:@std/dotenv"; 2 + 3 + await load({ export: true }); 4 + 5 + export async function sendDiscordNotification(quote: string) { 6 + const embed = { 7 + title: "❓❔ Question of the Day ❓❔", 8 + color: 0xFEF250, // lemon yellow 9 + description: quote, 10 + //timestamp: new Date().toISOString(), 11 + }; 12 + 13 + await fetch(Deno.env.get("DISCORD_WEBHOOK_URL")!, { 14 + method: "POST", 15 + headers: { 16 + "Content-Type": "application/json", 17 + }, 18 + body: JSON.stringify({ 19 + embeds: [embed], 20 + }), 21 + }); 22 + }