summaryrefslogtreecommitdiff
path: root/server/src/API
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/API')
-rw-r--r--server/src/API/API.ts33
-rw-r--r--server/src/API/routers/GuildRouter.ts44
-rw-r--r--server/src/API/routers/OAuth2Router.ts71
3 files changed, 148 insertions, 0 deletions
diff --git a/server/src/API/API.ts b/server/src/API/API.ts
new file mode 100644
index 0000000..8a6eb0f
--- /dev/null
+++ b/server/src/API/API.ts
@@ -0,0 +1,33 @@
+import { AkairoClient } from 'discord-akairo';
+import express, { Application } from 'express';
+import { createServer } from 'http';
+import cors from 'cors';
+import OAuth2 from '../structures/OAuth2';
+
+import OAuth2Router from './routers/OAuth2Router';
+import GuildRouter from './routers/GuildRouter';
+
+export default class API {
+ protected client: AkairoClient;
+ protected server: Application;
+ protected oauth: OAuth2;
+
+ public constructor(client: AkairoClient) {
+ this.client = client;
+ this.oauth = new OAuth2(this.client);
+ }
+
+ public start(): void {
+ this.server = express();
+ this.server.use(express.json());
+ this.server.use(cors({
+ origin: true,
+ credentials: true
+ }));
+
+ new OAuth2Router(this.server, this.client, this.oauth);
+ new GuildRouter(this.server, this.client);
+
+ createServer(this.server).listen(8088, (): void => console.log('API is online.'));
+ }
+} \ No newline at end of file
diff --git a/server/src/API/routers/GuildRouter.ts b/server/src/API/routers/GuildRouter.ts
new file mode 100644
index 0000000..01a8a9b
--- /dev/null
+++ b/server/src/API/routers/GuildRouter.ts
@@ -0,0 +1,44 @@
+import { Router, Request, Response, Application } from 'express';
+import { AkairoClient } from 'discord-akairo';
+import { Guild } from 'discord.js';
+import { authorization } from '../../Config';
+
+export default class GuildRouter {
+ protected app: Application;
+ protected client: AkairoClient;
+ protected router: Router;
+
+ public constructor(app: Application, client: AkairoClient) {
+ this.app = app;
+ this.client = client;
+ this.router = Router();
+
+ this.app.use(this.router);
+
+ this.router.get('/v1/get/guild/:id', (req: Request, res: Response) => {
+ const guild: Guild = this.client.guilds.cache.get(req.params.id);
+ if (!guild) return res.status(404).send({ message: 'Guild Not Found' });
+
+ return res.status(200).send({
+ name: guild.name,
+ owner: guild.owner.user.tag,
+ members: guild.memberCount
+ });
+ });
+
+ this.router.post('/v1/post/guild-name/:id', (req: Request, res: Response) => {
+ if (req.headers.authorization !== authorization) return res.status(401).send({ message: 'Unauthorized' });
+
+ const guild: Guild = this.client.guilds.cache.get(req.params.id);
+ if (!guild) return res.status(404).send({ message: 'Guild Not Found' });
+
+ if (!req.body.name) return res.status(404).send({ message: 'No Guild Name Provided' });
+ if (req.body.name.length > 32) return res.status(400).send({ message: 'Guild Name Exceeds 32 Characters' });
+ if (!guild.me.permissions.has('MANAGE_GUILD')) return res.status(401).send({ message: 'Cannot Manage Guild' });
+
+ guild.setName(req.body.name);
+
+ return res.status(201).send(req.body);
+ });
+ }
+} \ No newline at end of file
diff --git a/server/src/API/routers/OAuth2Router.ts b/server/src/API/routers/OAuth2Router.ts
new file mode 100644
index 0000000..60b0410
--- /dev/null
+++ b/server/src/API/routers/OAuth2Router.ts
@@ -0,0 +1,71 @@
+import { Router, Request, Response, Application } from 'express';
+import { AkairoClient } from 'discord-akairo';
+import fetch from 'node-fetch';
+import session from 'express-session';
+import OAuth2 from '../../structures/OAuth2';
+import { callbackUrl, authorization, clientID, redirectUri, clientSecret } from '../../Config';
+
+export default class OAuth2Router {
+ protected app: Application;
+ protected client: AkairoClient;
+ protected router: Router;
+ protected oauth: OAuth2;
+
+ public constructor(app: Application, client: AkairoClient, oauth: OAuth2) {
+ this.app = app;
+ this.client = client;
+ this.router = Router();
+ this.oauth = oauth;
+
+ this.app.use(session({
+ secret: authorization,
+ resave: false,
+ saveUninitialized: false,
+ cookie: {
+ secure: 'auto',
+ sameSite: false,
+ httpOnly: false,
+ maxAge: 6048e5
+ }
+ }));
+
+ this.app.use(this.router);
+
+ this.router.get('/oauth/login', (req: Request, res: Response) => {
+ return res.redirect(`https://discord.com/api/oauth2/authorize?client_id=${clientID}&redirect_uri=${encodeURIComponent(callbackUrl)}&response_type=code&scope=${encodeURIComponent('identify guilds')}`);
+ });
+
+ this.router.get('/oauth/logout', (req: Request, res: Response) => {
+ req.session.destroy(null);
+ return res.redirect(redirectUri);
+ });
+
+ this.router.get('/oauth/callback', (req: Request, res: Response) => {
+ fetch('https://discord.com/api/oauth2/token', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/x-www-form-urlencoded'
+ },
+ //@ts-ignore
+ body: new URLSearchParams({
+ 'client_id': clientID,
+ 'client_secret': clientSecret,
+ 'grant_type': 'authorization_code',
+ 'code': req.query.code,
+ 'redirect_uri': callbackUrl,
+ 'scope': 'identify'
+ })
+ })
+ .then(response => response.json())
+ .then(response => {
+ req.session.token = response['access_token'];
+ res.redirect(redirectUri);
+ });
+ });
+
+ this.router.get('/oauth/details', async (req: Request, res: Response) => {
+ const details = await this.oauth.resolveInformation(req);
+ return res.status(200).send(details);
+ });
+ }
+} \ No newline at end of file