From 97506cc750fc53861621c0c931daab4daf5f0162 Mon Sep 17 00:00:00 2001 From: Marcel <65048232+dromzeh@users.noreply.github.com> Date: Mon, 2 Sep 2024 00:45:54 +0100 Subject: [PATCH] feat: discord changelog endpoint --- src/handler.ts | 2 + src/lib/types/discord.ts | 75 +++++++++++++++++++++++++++++++++ src/routes/discord/changelog.ts | 29 +++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 src/routes/discord/changelog.ts diff --git a/src/handler.ts b/src/handler.ts index 39e2fd16..0131cbb1 100644 --- a/src/handler.ts +++ b/src/handler.ts @@ -7,6 +7,7 @@ import { index } from "@/routes/index"; import { getGameId } from "@/routes/games/get-game-id"; import { getAsset } from "@/routes/games/get-game-category"; import { getGames } from "@/routes/games/list-games"; +import { getChangelog } from "@/routes/discord/changelog"; const router = AutoRouter(); @@ -17,6 +18,7 @@ router .get("/game/:gameId/:asset", errorHandler(getAsset)) .get("/discord/contributors", errorHandler(getContributors)) .get("/discord/members", errorHandler(getMembers)) + .get("/discord/changelog", errorHandler(getChangelog)) .all("*", (): Response => { return new Response( JSON.stringify({ diff --git a/src/lib/types/discord.ts b/src/lib/types/discord.ts index a0bc932e..95d8a3a9 100644 --- a/src/lib/types/discord.ts +++ b/src/lib/types/discord.ts @@ -15,3 +15,78 @@ export interface GuildMember { avatar: string; }; } + +export interface DiscordAPIResponse { + success: boolean; + status: string; + path: string; + messages: DiscordMessage[]; +} +export interface DiscordMessage { + type: number; + content: string; + mentions: DiscordUser[]; + mention_roles: string[]; + attachments: any[]; + embeds: any[]; + timestamp: string; + edited_timestamp: string | null; + flags: number; + components: any[]; + id: string; + channel_id: string; + author: DiscordUser; + pinned: boolean; + mention_everyone: boolean; + tts: boolean; + reactions?: DiscordReaction[]; + message_reference?: MessageReference; + referenced_message?: DiscordMessage; + position?: number; +} + +export interface DiscordUser { + id: string; + username: string; + avatar: string; + discriminator: string; + public_flags: number; + flags: number; + banner: string | null; + accent_color: number | null; + global_name: string | null; + avatar_decoration_data: AvatarDecorationData | null; + banner_color: string | null; + clan: any | null; +} + +export interface AvatarDecorationData { + asset: string; + sku_id: string; + expires_at: string | null; +} + +export interface DiscordReaction { + emoji: { + id: string | null; + name: string; + animated?: boolean; + }; + count: number; + count_details: { + burst: number; + normal: number; + }; + burst_colors: any[]; + me_burst: boolean; + burst_me: boolean; + me: boolean; + burst_count: number; +} + +export interface MessageReference { + type: number; + channel_id: string; + message_id: string; + guild_id: string; +} diff --git a/src/routes/discord/changelog.ts b/src/routes/discord/changelog.ts new file mode 100644 index 00000000..ab98be56 --- /dev/null +++ b/src/routes/discord/changelog.ts @@ -0,0 +1,29 @@ +import { responseHeaders } from "@/lib/responseHeaders"; +import { DiscordMessage } from "@/lib/types/discord"; + +export const getChangelog = async ( + request: Request, + env: Env, + ctx: ExecutionContext +): Promise => { + const CHANNEL_ID = "1112527121848483920"; + const API_ENDPOINT = `https://discord.com/api/v10/channels/${CHANNEL_ID}/messages?limit=15`; + + const res = await fetch(API_ENDPOINT, { + headers: { + Authorization: `Bot ${env.DISCORD_TOKEN}`, + }, + }); + + const messages = (await res.json()) as DiscordMessage[]; + + return new Response( + JSON.stringify({ + success: true, + status: "ok", + path: `/discord/changelog`, + messages, + }), + responseHeaders + ); +};