From 846ed719821a76b41ff2b573f18a2ad3f5d6094c Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Thu, 8 Sep 2022 23:54:23 +0200 Subject: [PATCH] Created the discord controller --- controller/discord.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 controller/discord.ts diff --git a/controller/discord.ts b/controller/discord.ts new file mode 100644 index 0000000..935fa4b --- /dev/null +++ b/controller/discord.ts @@ -0,0 +1,21 @@ +import DiscordOauth2 from 'discord-oauth2'; +const oauth = new DiscordOauth2(); + +/** + * Retrieves an access & refresh token from the discord api + * @param code The authorization code from the callback + */ +export const createTokenByCode = (code: string) => oauth.tokenRequest({ + grantType: "authorization_code", + scope: "identify guilds", + clientId: process.env.DC_CLIENT, + clientSecret: process.env.DC_SECRET, + redirectUri: process.env.DC_REDIRECT_URI, + code +}); + +/** + * Gets user information from the discord api + * @param token The access token of the user + */ +export const getUser = (token: string) => oauth.getUser(token); \ No newline at end of file