get user guilds on /profile

This commit is contained in:
Jill 2023-11-20 00:16:55 +03:00
parent a487fc2f4c
commit 679dd7a832
Signed by: oat
GPG Key ID: 33489AA58A955108
1 changed files with 19 additions and 2 deletions

View File

@ -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/'));