From e084efc3693e9ae6c9c584c706d9697d7ea26c45 Mon Sep 17 00:00:00 2001 From: Hiroyuki Date: Fri, 5 Feb 2021 23:01:46 -0400 Subject: [PATCH] Executive Orders --- src/api/board.ins/main.ts | 3 + src/api/board.ins/routes/index.ts | 1 + src/api/board.ins/routes/root.ts | 72 +++++++++++++++++++ .../comm.libraryofcode.org/routes/report.ts | 31 ++++---- src/class/Client.ts | 5 +- src/events/guildMemberAdd.ts | 2 +- src/models/ExecutiveOrder.ts | 17 +++++ src/models/index.ts | 1 + 8 files changed, 112 insertions(+), 20 deletions(-) create mode 100644 src/api/board.ins/main.ts create mode 100644 src/api/board.ins/routes/index.ts create mode 100644 src/api/board.ins/routes/root.ts create mode 100644 src/models/ExecutiveOrder.ts diff --git a/src/api/board.ins/main.ts b/src/api/board.ins/main.ts new file mode 100644 index 0000000..999fd95 --- /dev/null +++ b/src/api/board.ins/main.ts @@ -0,0 +1,3 @@ +import { Server, ServerManagement } from '../../class'; + +export default (management: ServerManagement) => new Server(management, 3892, `${__dirname}/routes`); diff --git a/src/api/board.ins/routes/index.ts b/src/api/board.ins/routes/index.ts new file mode 100644 index 0000000..fb1d2b2 --- /dev/null +++ b/src/api/board.ins/routes/index.ts @@ -0,0 +1 @@ +export { default as Root } from './root'; diff --git a/src/api/board.ins/routes/root.ts b/src/api/board.ins/routes/root.ts new file mode 100644 index 0000000..e908f7d --- /dev/null +++ b/src/api/board.ins/routes/root.ts @@ -0,0 +1,72 @@ +import { TextChannel } from 'eris'; +import { RichEmbed, Route, Server } from '../../../class'; + +export default class Root extends Route { + constructor(server: Server) { + super(server); + this.conf = { + path: '/', + }; + } + + public bind() { + this.router.post('/executive-orders/new', async (req, res) => { + if (!req.body.pin) { + return res.status(401).json({ + code: this.constants.codes.UNAUTHORIZED, + message: this.constants.messages.UNAUTHORIZED, + }); + } + + const director = await this.server.client.db.Score.findOne({ pin: req.body.pin }); + + if (!director) { + return res.status(401).json({ + code: this.constants.codes.UNAUTHORIZED, + message: this.constants.messages.UNAUTHORIZED, + }); + } + + if (!req.body.subject) { + return res.status(401).json({ + code: this.constants.codes.CLIENT_ERROR, + message: this.constants.messages.CLIENT_ERROR, + }); + } + + if (!req.body.body) { + return res.status(401).json({ + code: this.constants.codes.CLIENT_ERROR, + message: this.constants.messages.CLIENT_ERROR, + }); + } + + const executiveOrder = await this.server.client.db.ExecutiveOrder.create({ + issuedBy: director.userID, + subject: req.body.subject, + body: req.body.body, + at: new Date(), + }); + + const staffInformation = await this.server.client.db.Staff.findOne({ userID: director.userID }); + const staffDiscord = this.server.client.users.get(director.userID) || await this.server.client.getRESTUser(director.userID); + + const embed = new RichEmbed(); + embed.setTitle('Executive Order'); + embed.setAuthor(`${staffDiscord.username}#${staffDiscord.discriminator}, ${staffInformation.pn.join(', ')}`, staffDiscord.avatarURL); + embed.setColor('#dd3acd'); + embed.addField('Subject', req.body.subject); + embed.addField('Body', req.body.body); + embed.addField('ID', executiveOrder._id); + embed.setTimestamp(new Date()); + + const channel = this.server.client.getChannel('807444198969835550'); + await channel.createMessage({ embed }); + + return res.status(200).json({ + code: this.constants.codes.SUCCESS, + message: `Created new Executive Order with ID ${executiveOrder._id} by the ${staffDiscord.username}#${staffDiscord.discriminator}, ${staffInformation.pn.join(', ')}.`, + }); + }); + } +} diff --git a/src/api/comm.libraryofcode.org/routes/report.ts b/src/api/comm.libraryofcode.org/routes/report.ts index 706bc7b..780801b 100644 --- a/src/api/comm.libraryofcode.org/routes/report.ts +++ b/src/api/comm.libraryofcode.org/routes/report.ts @@ -77,7 +77,6 @@ export default class Report extends Route { return res.status(403).json({ code: this.constants.codes.PERMISSION_DENIED, message: this.constants.messages.PERMISSION_DENIED }); } - const flags = []; if (mem.user.publicFlags) { if ((mem.user.publicFlags & (1 << 0)) === 1 << 0) flags.push('DISCORD_EMPLOYEE'); @@ -196,8 +195,8 @@ export default class Report extends Route { embed.addField('Reason', req.body.reason ?? 'N/A', true); embed.setTimestamp(); embed.setFooter(this.server.client.user.username, this.server.client.user.avatarURL); - const chan = this.server.client.guilds.get(this.server.client.config.guildID).channels.get('611584771356622849'); - chan.createMessage({ embed }).catch(() => {}); + const chan = this.server.client.guilds.get(this.server.client.config.guildID).channels.get('611584771356622849'); + chan.createMessage({ embed }).catch(() => { }); return res.status(200).json({ code: this.constants.codes.SUCCESS, @@ -259,8 +258,8 @@ export default class Report extends Route { embed.addField('Department/Service', merchant.name.toUpperCase(), true); embed.setTimestamp(); embed.setFooter(this.server.client.user.username, this.server.client.user.avatarURL); - const chan = this.server.client.guilds.get(this.server.client.config.guildID).channels.get('611584771356622849'); - chan.createMessage({ embed }).catch(() => {}); + const chan = this.server.client.guilds.get(this.server.client.config.guildID).channels.get('611584771356622849'); + chan.createMessage({ embed }).catch(() => { }); if (!merchant.privileged) { return res.status(200).json({ @@ -355,7 +354,6 @@ export default class Report extends Route { array.push(data); } - return res.status(200).json({ code: this.constants.codes.SUCCESS, message: { @@ -414,8 +412,8 @@ export default class Report extends Route { embed.addField('Department/Service', merchant.name.toUpperCase(), true); embed.setTimestamp(); embed.setFooter(this.server.client.user.username, this.server.client.user.avatarURL); - const chan = this.server.client.guilds.get(this.server.client.config.guildID).channels.get('611584771356622849'); - chan.createMessage({ embed }).catch(() => {}); + const chan = this.server.client.guilds.get(this.server.client.config.guildID).channels.get('611584771356622849'); + chan.createMessage({ embed }).catch(() => { }); if (!merchant.privileged) { return res.status(200).json({ @@ -448,7 +446,6 @@ export default class Report extends Route { else if (member.cloudServices > 10) cloudServicesScore = 10; else cloudServicesScore = Math.round(member.cloudServices); - return res.status(200).json({ code: this.constants.codes.SUCCESS, message: { @@ -498,8 +495,8 @@ export default class Report extends Route { embed.addField('Department/Service', `${member.username} via report.libraryofcode.org @ IP ${req.ip}`.toUpperCase(), true); embed.setTimestamp(); embed.setFooter(this.server.client.user.username, this.server.client.user.avatarURL); - const chan = this.server.client.guilds.get(this.server.client.config.guildID).channels.get('611584771356622849'); - chan.createMessage({ embed }).catch(() => {}); + const chan = this.server.client.guilds.get(this.server.client.config.guildID).channels.get('611584771356622849'); + chan.createMessage({ embed }).catch(() => { }); } else { await this.server.client.db.Score.updateOne({ userID: score.userID }, { $addToSet: { softInquiries: { name: 'Library of Code sp-us | Staff Team via report.libraryofcode.org', date: new Date() } } }); const embed = new RichEmbed(); @@ -510,8 +507,8 @@ export default class Report extends Route { embed.addField('Department/Service', 'Library of Code sp-us | Staff Team via report.libraryofcode.org'.toUpperCase(), true); embed.setTimestamp(); embed.setFooter(this.server.client.user.username, this.server.client.user.avatarURL); - const chan = this.server.client.guilds.get(this.server.client.config.guildID).channels.get('611584771356622849'); - chan.createMessage({ embed }).catch(() => {}); + const chan = this.server.client.guilds.get(this.server.client.config.guildID).channels.get('611584771356622849'); + chan.createMessage({ embed }).catch(() => { }); } } else if (!updated) { await this.server.client.db.Score.updateOne({ userID: score.userID }, { $addToSet: { softInquiries: { name: `${member.username} via report.libraryofcode.org @ IP ${req.ip}`, date: new Date() } } }); @@ -523,8 +520,8 @@ export default class Report extends Route { embed.addField('Department/Service', `${member.username} via report.libraryofcode.org @ IP ${req.ip}`.toUpperCase(), true); embed.setTimestamp(); embed.setFooter(this.server.client.user.username, this.server.client.user.avatarURL); - const chan = this.server.client.guilds.get(this.server.client.config.guildID).channels.get('611584771356622849'); - chan.createMessage({ embed }).catch(() => {}); + const chan = this.server.client.guilds.get(this.server.client.config.guildID).channels.get('611584771356622849'); + chan.createMessage({ embed }).catch(() => { }); } score = await this.server.client.db.Score.findOne({ pin: [Number(args.split('-')[0]), Number(args.split('-')[1]), Number(args.split('-')[2])] }).lean().exec(); @@ -646,11 +643,11 @@ export default class Report extends Route { name?: string, department?: string, date?: Date, - }> jwt.verify(req.query.code.toString(), this.server.client.config.internalKey); + }>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); + 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 = []; diff --git a/src/class/Client.ts b/src/class/Client.ts index efa6b4c..d6b1a93 100644 --- a/src/class/Client.ts +++ b/src/class/Client.ts @@ -7,6 +7,7 @@ import { Collection, Command, LocalStorage, Queue, Util, ServerManagement, Event import { Customer, CustomerInterface, CustomerPortal, CustomerPortalInterface, + ExecutiveOrder, ExecutiveOrderInterface, File, FileInterface, Member, MemberInterface, Merchant, MerchantInterface, @@ -43,7 +44,7 @@ export default class Client extends eris.Client { public stripe: Stripe; - public db: { Customer: mongoose.Model, CustomerPortal: mongoose.Model, File: mongoose.Model, Member: mongoose.Model, Merchant: mongoose.Model, Moderation: mongoose.Model, NNTrainingData: mongoose.Model, Note: mongoose.Model, PagerNumber: mongoose.Model, Promo: mongoose.Model, Rank: mongoose.Model, Redirect: mongoose.Model, Score: mongoose.Model, ScoreHistorical: mongoose.Model, Staff: mongoose.Model, Stat: mongoose.Model, local: { muted: LocalStorage } }; + public db: { Customer: mongoose.Model, CustomerPortal: mongoose.Model, ExecutiveOrder: mongoose.Model, File: mongoose.Model, Member: mongoose.Model, Merchant: mongoose.Model, Moderation: mongoose.Model, NNTrainingData: mongoose.Model, Note: mongoose.Model, PagerNumber: mongoose.Model, Promo: mongoose.Model, Rank: mongoose.Model, Redirect: mongoose.Model, Score: mongoose.Model, ScoreHistorical: mongoose.Model, Staff: mongoose.Model, Stat: mongoose.Model, local: { muted: LocalStorage } }; constructor(token: string, options?: eris.ClientOptions) { super(token, options); @@ -51,7 +52,7 @@ export default class Client extends eris.Client { this.events = new Collection(); this.intervals = new Collection(); this.queue = new Queue(this); - this.db = { Customer, CustomerPortal, File, Member, Merchant, Moderation, NNTrainingData, Note, PagerNumber, Promo, Rank, Redirect, Score, ScoreHistorical, Staff, Stat, local: { muted: new LocalStorage('muted') } }; + this.db = { Customer, CustomerPortal, ExecutiveOrder, File, Member, Merchant, Moderation, NNTrainingData, Note, PagerNumber, Promo, Rank, Redirect, Score, ScoreHistorical, Staff, Stat, local: { muted: new LocalStorage('muted') } }; } diff --git a/src/events/guildMemberAdd.ts b/src/events/guildMemberAdd.ts index 13b167c..0d750f0 100644 --- a/src/events/guildMemberAdd.ts +++ b/src/events/guildMemberAdd.ts @@ -12,7 +12,7 @@ export default class GuildMemberAdd extends Event { public async run(_, member: Member) { try { const search = await this.client.db.local.muted.get(`muted-${member.user.id}`); - if (search === true) { + if (search) { member.addRole('478373942638149643'); } } catch (err) { diff --git a/src/models/ExecutiveOrder.ts b/src/models/ExecutiveOrder.ts new file mode 100644 index 0000000..a096f84 --- /dev/null +++ b/src/models/ExecutiveOrder.ts @@ -0,0 +1,17 @@ +import { Document, model, Schema } from 'mongoose'; + +export interface ExecutiveOrderInterface extends Document { + issuedBy: string; + subject: string; + body: string; + at: Date; +} + +const ExecutiveOrder = new Schema({ + issuedBy: { type: String, required: true }, + subject: { type: String, required: true }, + body: { type: String, required: true }, + at: { type: Date, required: true }, +}); + +export default model('ExecutiveOrders', ExecutiveOrder); diff --git a/src/models/index.ts b/src/models/index.ts index d3cc016..f6f0541 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -1,5 +1,6 @@ export { default as Customer, CustomerInterface } from './Customer'; export { default as CustomerPortal, CustomerPortalInterface } from './CustomerPortal'; +export { default as ExecutiveOrder, ExecutiveOrderInterface } from './ExecutiveOrder'; export { default as File, FileInterface } from './File'; export { default as Member, MemberInterface } from './Member'; export { default as Merchant, MerchantInterface } from './Merchant';