32 lines
893 B
TypeScript
32 lines
893 B
TypeScript
import { Session } from '../lib/db';
|
|
import * as log from '../lib/log';
|
|
import got from 'got';
|
|
import { APIPartialGuild, APIUser, Routes } from 'discord.js';
|
|
import { DISCORD_ENDPOINT } from './oauth2';
|
|
|
|
export async function getUser(session: Session | undefined) {
|
|
if (!session) return null;
|
|
try {
|
|
return await got(DISCORD_ENDPOINT + Routes.user(), {
|
|
headers: {
|
|
authorization: `${session.tokenType} ${session.accessToken}`
|
|
}
|
|
}).json() as APIUser;
|
|
} catch(err) {
|
|
log.error(err);
|
|
return null;
|
|
}
|
|
}
|
|
export async function getGuilds(session: Session | undefined) {
|
|
if (!session) return null;
|
|
try {
|
|
return await got(DISCORD_ENDPOINT + Routes.userGuilds(), {
|
|
headers: {
|
|
authorization: `${session.tokenType} ${session.accessToken}`
|
|
}
|
|
}).json() as APIPartialGuild[];
|
|
} catch(err) {
|
|
log.error(err);
|
|
return null;
|
|
}
|
|
} |