Add endpoint to get Discord Username/Avatar

This commit is contained in:
Aadi Desai 2023-09-08 00:31:00 +01:00
parent c09a55465d
commit e56fbc5e41
Signed by: supleed2
SSH key fingerprint: SHA256:CkbNRs0yVzXEiUp2zd0PSxsfRUMFF9bLlKXtE1xEbKM

21
html/functions/discord.ts Normal file
View file

@ -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", },
});
}
}