From 679dd7a8328261aab48121d8b397f7064f3753e8 Mon Sep 17 00:00:00 2001 From: "Jill \"oatmealine\" Monoids" Date: Mon, 20 Nov 2023 00:16:55 +0300 Subject: [PATCH] get user guilds on /profile --- src/web.ts | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/web.ts b/src/web.ts index 7ae4a6b..66f3a7d 100644 --- a/src/web.ts +++ b/src/web.ts @@ -3,7 +3,7 @@ import { engine } from 'express-handlebars'; import * as log from './lib/log'; import { CustomItem, Session, db } from './lib/db'; import { defaultItems } from './lib/rpg/items'; -import { APIUser, CDN, Client, RESTPostOAuth2AccessTokenResult, RESTPostOAuth2AccessTokenURLEncodedData, RESTPostOAuth2RefreshTokenURLEncodedData, Routes } from 'discord.js'; +import { APIPartialGuild, APIUser, CDN, Client, RESTPostOAuth2AccessTokenResult, RESTPostOAuth2AccessTokenURLEncodedData, RESTPostOAuth2RefreshTokenURLEncodedData, Routes } from 'discord.js'; import got from 'got'; import uid from 'uid-safe'; import { Cookie, parse } from 'tough-cookie'; @@ -80,6 +80,19 @@ export async function getUser(session: Session | undefined) { return null; } } +export async function getGuilds(session: Session | undefined) { + if (!session) return null; + try { + return await got('https://discord.com/api/users/@me/guilds', { + headers: { + authorization: `${session.tokenType} ${session.accessToken}` + } + }).json() as APIPartialGuild[]; + } catch(err) { + log.error(err); + return null; + } +} export async function startServer(bot: Client, port: number) { const app = express(); @@ -183,9 +196,13 @@ export async function startServer(bot: Client, port: number) { if (!session) return res.redirect(`https://discord.com/api/oauth2/authorize?client_id=${bot.config.clientId}&redirect_uri=${encodeURIComponent(bot.config.siteURL)}&response_type=code&scope=identify%20guilds`); const user = await getUser(session); + const guilds = await getGuilds(session); //res.sendFile('profile/index.html', { root: 'static/' }); - res.json(user); + res.json({ + user, + guilds + }); }); app.use(express.static('static/'));