From cffa0d1d30cbdac115be492bc95c0d7153705fa7 Mon Sep 17 00:00:00 2001 From: Matthew R Date: Wed, 21 Oct 2020 16:33:08 -0400 Subject: [PATCH] fixes --- .../comm.libraryofcode.org/routes/report.ts | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/api/comm.libraryofcode.org/routes/report.ts b/src/api/comm.libraryofcode.org/routes/report.ts index 459c4cd..102fd44 100644 --- a/src/api/comm.libraryofcode.org/routes/report.ts +++ b/src/api/comm.libraryofcode.org/routes/report.ts @@ -1,14 +1,18 @@ /* eslint-disable no-bitwise */ /* eslint-disable no-continue */ +import jwt from 'jsonwebtoken'; import { TextChannel } from 'eris'; -import { Route, Server, RichEmbed } from '../../../class'; +import { LocalStorage, Route, Server, RichEmbed } from '../../../class'; export default class Report extends Route { public timeout: Set; + public acceptedOffers: LocalStorage; + constructor(server: Server) { super(server); this.timeout = new Set(); + this.acceptedOffers = new LocalStorage('accepted-offers'); this.conf = { path: '/report', }; @@ -467,5 +471,49 @@ export default class Report extends Route { return this.handleError(err, res); } }); + + this.router.get('/offer', async (req, res) => { + try { + res.setHeader('Access-Control-Allow-Origin', '*'); + if (!req.query.code) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED }); + if (await this.acceptedOffers.get(req.query.code.toString())) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED }); + let offer: { + userID?: string, + staffID?: string, + channelID?: string, + messageID?: string, + pin?: string, + name?: string, + department?: string, + date?: Date, + }; + + try { + offer = <{ + userID?: string, + staffID?: string, + channelID?: string, + messageID?: string, + pin?: string, + name?: string, + department?: string, + date?: Date, + }> jwt.verify(req.query.code.toString(), this.server.client.config.internalKey); + } catch { + return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: this.constants.messages.UNAUTHORIZED }); + } + const chan = this.server.client.guilds.get(this.constants.discord.SERVER_ID).channels.get(offer.channelID); + await chan.createMessage(`__**PRE-APPROVED OFFER ACCEPTED**__\n<@${offer.staffID}>`); + const message = await chan.getMessage(offer.messageID); + const args = []; + args.push(offer.userID, 'hard'); + `${offer.department}:${offer.name}`.split(' ').forEach((item) => args.push(item)); + await this.server.client.commands.get('score').run(message, args); + await this.acceptedOffers.set(req.query.code.toString(), true); + return res.sendStatus(200); + } catch (err) { + return this.handleError(err, res); + } + }); } }