From bb511abc03bb66848947e37a999502b813c77269 Mon Sep 17 00:00:00 2001 From: 8cy <50817549+8cy@users.noreply.github.com> Date: Thu, 23 Jul 2020 23:24:17 -0700 Subject: goodbye old uwufier :cry: --- server/src/structures/Interfaces.ts | 16 ++++++++++ server/src/structures/OAuth2.ts | 62 +++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 server/src/structures/Interfaces.ts create mode 100644 server/src/structures/OAuth2.ts (limited to 'server/src/structures') diff --git a/server/src/structures/Interfaces.ts b/server/src/structures/Interfaces.ts new file mode 100644 index 0000000..4ee09f4 --- /dev/null +++ b/server/src/structures/Interfaces.ts @@ -0,0 +1,16 @@ +export interface APIUser { + id: string; + username: string; + discriminator: string; + avatar: string; + guilds: APIGuildMin[]; + admin: boolean; +} + +export interface APIGuildMin { + id: string; + name: string; + icon: string; + admin: boolean; + invited: boolean; +} \ No newline at end of file diff --git a/server/src/structures/OAuth2.ts b/server/src/structures/OAuth2.ts new file mode 100644 index 0000000..379cf4c --- /dev/null +++ b/server/src/structures/OAuth2.ts @@ -0,0 +1,62 @@ +import { Request } from 'express'; +import { AkairoClient } from 'discord-akairo'; +import { Guild } from 'discord.js'; +import fetch from 'node-fetch'; +import { owners } from '../Config'; +import { APIUser, APIGuildMin } from './Interfaces'; + +export default class OAuth2 { + protected client: AkairoClient; + protected guilds: object; + + public constructor(client: AkairoClient) { + this.client = client; + this.guilds = new Object(); + } + + public async resolveInformation(req: Request): Promise { + if (!req.session.token) return null; + + const userReq = await fetch('https://discord.com/api/users/@me', { + headers: { + 'Authorization': `Bearer ${req.session.token}` + } + }); + + const user = await userReq.json(); + if (!user.id) return null; + + if (!this.guilds[user.id]) { + const guildsReq = await fetch('https://discord.com/api/users/@me/guilds', { + headers: { + 'Authorization': `Bearer ${req.session.token}` + } + }); + + const guildsRes = await guildsReq.json(); + + this.guilds[user.id] = guildsRes; + setTimeout(() => { + delete this.guilds[user.id]; + }, 3e5); + } + + return { + id: user.id, + username: user.username, + discriminator: user.discriminator, + avatar: user.avatar, + guilds: this.guilds[user.id].map((guild): APIGuildMin => { + const g: Guild = this.client.guilds.cache.get(guild.id); + return { + id: guild.id, + name: guild.name, + icon: guild.icon, + admin: g ? g.members.cache.get(user.id).permissions.has('MANAGE_GUILD') : guild.owner, + invited: g ? true : false + } + }), + admin: owners.includes(user.id) + } + } +} \ No newline at end of file -- cgit v1.2.3