From 78939e24f1934b890b669669ccf45457a32b8dc5 Mon Sep 17 00:00:00 2001 From: RedStone576 Date: Sun, 23 Aug 2026 09:22:35 +0700 Subject: [PATCH] big bang --- .gitignore | 8 ++++++++ readme.md | 20 ++++++++++++++++++++ src/discord/mod.ts | 40 ++++++++++++++++++++++++++++++++++++++++ src/env.ts | 6 ++++++ src/feat/birthdays.ts | 25 +++++++++++++++++++++++++ src/index.ts | 41 +++++++++++++++++++++++++++++++++++++++++ src/utils/date.ts | 12 ++++++++++++ wrangler.toml.example | 11 +++++++++++ 8 files changed, 163 insertions(+) create mode 100644 .gitignore create mode 100644 readme.md create mode 100644 src/discord/mod.ts create mode 100644 src/env.ts create mode 100644 src/feat/birthdays.ts create mode 100644 src/index.ts create mode 100644 src/utils/date.ts create mode 100644 wrangler.toml.example diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7175c4e --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +node_modules/ + +.env.vars +.env +wrangler.toml + +src/feat/birthdays.json + diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..2e4d35c --- /dev/null +++ b/readme.md @@ -0,0 +1,20 @@ +``` +┌─ minute 0 +│ ┌─ hour 17 +│ │ ┌─ day * +│ │ │ ┌─ month * +│ │ │ │ ┌─ weekday * +│ │ │ │ │ +0 17 * * * +``` + +its in UTC+00, so +``` +UTC cron time = UTC+7 desired time − 7 hours +00:00 − 07:00 = 17:00 UTC + +17:00 UTC ++ 7 hours +---------- +00:00 UTC+7 +``` diff --git a/src/discord/mod.ts b/src/discord/mod.ts new file mode 100644 index 0000000..b4e44a9 --- /dev/null +++ b/src/discord/mod.ts @@ -0,0 +1,40 @@ +import type { Env } from "../env" + +const DISCORD_API = "https://discord.com/api/v10" + +// no embedbuilder cuz fuck you +export interface DiscordEmbed +{ + title?: string + description?: string + fields?: Array<{ + name: string + value: string + inline?: boolean + }> + footer?: { + text: string + } +} + +export async function editMessage(env: Env, content: string | null, embeds: DiscordEmbed[] = []): Promise +{ + const response = await fetch(`${DISCORD_API}/channels/${env.DISCORD_CHANNEL_ID}/messages/${env.DISCORD_MESSAGE_ID}`, { + method: "PATCH", + headers: { + "Authorization": `Bot ${env.DISCORD_BOT_TOKEN}`, + "Content-Type": "application/json" + }, + body: JSON.stringify({ + content, + embeds + }) + }) + + if (!response.ok) + { + const body = await response.text() + + throw new Error(`fuckass discord returned ${response.status}: ${body}`) + } +} diff --git a/src/env.ts b/src/env.ts new file mode 100644 index 0000000..429b243 --- /dev/null +++ b/src/env.ts @@ -0,0 +1,6 @@ +export interface Env +{ + DISCORD_BOT_TOKEN: string + DISCORD_CHANNEL_ID: string + DISCORD_MESSAGE_ID: string +} diff --git a/src/feat/birthdays.ts b/src/feat/birthdays.ts new file mode 100644 index 0000000..9300ecf --- /dev/null +++ b/src/feat/birthdays.ts @@ -0,0 +1,25 @@ +import birthdays from "./birthdays.json" +import type { DiscordEmbed } from "../discord/mod" + +interface Birthdays +{ + [date: string]: string[] +} + +export function getBirthdays(date: string): string[] +{ + return (birthdays as Birthdays)[date] ?? [] +} + +export function createBirthdayEmbed(date: string): DiscordEmbed +{ + const names = getBirthdays(date) + + return { + fields: [{ + name: "Today's Birthdays", + value: names.length > 0 ? names.map(name => `- ${name}`).join("\n") : "No birthdays today.", + inline: false + }] + } +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..b653405 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,41 @@ +import type { Env } from "./env" + +import { editMessage } from "./discord/mod" + +import { getToday } from "./utils/date" +import { createBirthdayEmbed } from "./feat/birthdays" + +async function updateMessage(env: Env): Promise +{ + await editMessage( + env, + `Last updated: `, + [ // embeds + createBirthdayEmbed(getToday("Asia/Jakarta")) + ] + ) +} + +export default +{ + async fetch(request: Request, env: Env): Promise + { + const url = new URL(request.url) + + if (url.pathname === "/invoke") + { + await updateMessage(env) + + return new Response("Discord message updated") + } + + return new Response("Not Found", { + status: 404 + }) + }, + + async scheduled(_event: ScheduledEvent, env: Env, _ctx: ExecutionContext): Promise + { + await updateMessage(env) + } +} diff --git a/src/utils/date.ts b/src/utils/date.ts new file mode 100644 index 0000000..0c28b0b --- /dev/null +++ b/src/utils/date.ts @@ -0,0 +1,12 @@ +export function getToday(timeZone = "Asia/Jakarta"): string +{ + /* + * Intl API cuz i can :P + */ + return new Intl.DateTimeFormat("en-US", { + timeZone, + month: "2-digit", + day: "2-digit" + }) + .format(new Date()) +} diff --git a/wrangler.toml.example b/wrangler.toml.example new file mode 100644 index 0000000..36e4df5 --- /dev/null +++ b/wrangler.toml.example @@ -0,0 +1,11 @@ +name = "meowl-cron" +main = "src/index.ts" +compatibility_date = "2025-07-18" + +[triggers] +crons = ["0 17 * * *"] + +[vars] +DISCORD_CHANNEL_ID = "" +DISCORD_MESSAGE_ID = "" +DISCORD_BOT_TOKEN = ""