summaryrefslogtreecommitdiff
path: root/server/src/structures
diff options
context:
space:
mode:
author8cy <[email protected]>2020-07-23 23:24:17 -0700
committer8cy <[email protected]>2020-07-23 23:24:17 -0700
commitbb511abc03bb66848947e37a999502b813c77269 (patch)
tree612c010fc8317e1cdf11471a18aad0270819d33e /server/src/structures
parentfix: if clear amount equal or over 100, round down to 99 (diff)
downloaddep-core-bb511abc03bb66848947e37a999502b813c77269.tar.xz
dep-core-bb511abc03bb66848947e37a999502b813c77269.zip
goodbye old uwufier :cry:
Diffstat (limited to 'server/src/structures')
-rw-r--r--server/src/structures/Interfaces.ts16
-rw-r--r--server/src/structures/OAuth2.ts62
2 files changed, 78 insertions, 0 deletions
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<APIUser | null> {
+ 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