From e56fbc5e4143399a493d77a71cc30fb30c1e8355 Mon Sep 17 00:00:00 2001 From: Aadi Desai <21363892+supleed2@users.noreply.github.com> Date: Fri, 8 Sep 2023 00:31:00 +0100 Subject: [PATCH] Add endpoint to get Discord Username/Avatar --- html/functions/discord.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 html/functions/discord.ts diff --git a/html/functions/discord.ts b/html/functions/discord.ts new file mode 100644 index 0000000..0b302b6 --- /dev/null +++ b/html/functions/discord.ts @@ -0,0 +1,21 @@ +export async function onRequestGet({ request, env }) { + const id = new URL(request.url).searchParams.get("id"); + const { username, avatar } = await fetch(`https://discord.com/api/v9/users/${id}`, { + headers: { Authorization: `Bot ${env.DISCORD_TOKEN}`, }, + }).then((r) => r.json()).catch(console.error); + if (username == undefined) { + return new Response(JSON.stringify({ + username: "User not found.", + avatar_url: "https://cdn.discordapp.com/embed/avatars/0.png", + }), { + headers: { "content-type": "application/json;charset=UTF-8", }, + }); + } else { + return new Response(JSON.stringify({ + username: username, + avatar_url: `https://cdn.discordapp.com/avatars/${id}/${avatar}.png`, + }), { + headers: { "content-type": "application/json;charset=UTF-8", }, + }); + } +}