From c126fc99d1cc6d71f5a0750bc07c773ae2932d29 Mon Sep 17 00:00:00 2001 From: Matthew R Date: Mon, 15 Jun 2020 21:34:11 -0400 Subject: [PATCH 1/4] add new deps --- package.json | 2 ++ yarn.lock | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/package.json b/package.json index 568e1cb..c2a1b87 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "@types/helmet": "^0.0.47", "@types/mongoose": "^5.7.19", "@types/node": "^14.0.1", + "@types/nodemailer": "^6.4.0", "@types/signale": "^1.4.1", "@types/uuid": "^7.0.3", "@typescript-eslint/eslint-plugin": "^2.33.0", @@ -32,6 +33,7 @@ "helmet": "^3.22.0", "moment": "^2.25.3", "mongoose": "^5.9.13", + "nodemailer": "^6.4.8", "signale": "^1.4.0", "uuid": "^8.0.0", "yaml": "^1.9.2" diff --git a/yarn.lock b/yarn.lock index 0fa9251..a86aef4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -119,6 +119,13 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-14.0.1.tgz#5d93e0a099cd0acd5ef3d5bde3c086e1f49ff68c" integrity sha512-FAYBGwC+W6F9+huFIDtn43cpy7+SzG+atzRiTfdp3inUKL2hXnd4rG8hylJLIh4+hqrQy1P17kvJByE/z825hA== +"@types/nodemailer@^6.4.0": + version "6.4.0" + resolved "https://registry.yarnpkg.com/@types/nodemailer/-/nodemailer-6.4.0.tgz#d8c039be3ed685c4719a026455555be82c124b74" + integrity sha512-KY7bFWB0MahRZvVW4CuW83qcCDny59pJJ0MQ5ifvfcjNwPlIT0vW4uARO4u1gtkYnWdhSvURegecY/tzcukJcA== + dependencies: + "@types/node" "*" + "@types/qs@*": version "6.9.2" resolved "https://registry.yarnpkg.com/@types/qs/-/qs-6.9.2.tgz#faab98ec4f96ee72c829b7ec0983af4f4d343113" @@ -1460,6 +1467,11 @@ nocache@2.1.0: resolved "https://registry.yarnpkg.com/nocache/-/nocache-2.1.0.tgz#120c9ffec43b5729b1d5de88cd71aa75a0ba491f" integrity sha512-0L9FvHG3nfnnmaEQPjT9xhfN4ISk0A8/2j4M37Np4mcDesJjHgEUfgPhdCyZuFI954tjokaIj/A3NdpFNdEh4Q== +nodemailer@^6.4.8: + version "6.4.8" + resolved "https://registry.yarnpkg.com/nodemailer/-/nodemailer-6.4.8.tgz#aca52886e4e56f71f6b8a65f5ca6b767ca751fc7" + integrity sha512-UbJD0+g5e2H20bWv7Rpj3B+N3TMMJ0MLoLwaGVJ0k3Vo8upq0UltwHJ5BJfrpST1vFa91JQ8cf7cICK5DSIo1Q== + normalize-package-data@^2.3.2: version "2.5.0" resolved "https://registry.yarnpkg.com/normalize-package-data/-/normalize-package-data-2.5.0.tgz#e66db1838b200c1dfc233225d12cb36520e234a8" From 826f28556e5e2d988db6d147856aa32cfe9de2c1 Mon Sep 17 00:00:00 2001 From: Matthew R Date: Mon, 15 Jun 2020 21:34:41 -0400 Subject: [PATCH 2/4] new config options --- src/class/Client.ts | 8 ++++---- src/main.ts | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/class/Client.ts b/src/class/Client.ts index 886c53c..2d25fb8 100644 --- a/src/class/Client.ts +++ b/src/class/Client.ts @@ -2,10 +2,10 @@ import eris from 'eris'; import mongoose from 'mongoose'; import { promises as fs } from 'fs'; import { Collection, Command, Util, ServerManagement, Event } from '.'; -import { Member, MemberInterface, Moderation, ModerationInterface, Redirect, RedirectInterface } from '../models'; +import { Member, MemberInterface, Moderation, ModerationInterface, PagerNumber, PagerNumberInterface, Redirect, RedirectInterface } from '../models'; export default class Client extends eris.Client { - public config: { token: string, prefix: string, guildID: string, mongoDB: string }; + public config: { token: string, prefix: string, guildID: string, mongoDB: string, emailPass: string, }; public commands: Collection; @@ -17,14 +17,14 @@ export default class Client extends eris.Client { public serverManagement: ServerManagement; - public db: { Member: mongoose.Model, Moderation: mongoose.Model, Redirect: mongoose.Model }; + public db: { Member: mongoose.Model, Moderation: mongoose.Model, PagerNumber: mongoose.Model, Redirect: mongoose.Model }; constructor(token: string, options?: eris.ClientOptions) { super(token, options); this.commands = new Collection(); this.events = new Collection(); this.intervals = new Collection(); - this.db = { Member, Moderation, Redirect }; + this.db = { Member, Moderation, PagerNumber, Redirect }; } public async loadDatabase() { diff --git a/src/main.ts b/src/main.ts index c3682ad..4677d4b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,7 +9,7 @@ import * as commandFiles from './commands'; async function main(): Promise { const read = await fs.readFile('../config.yaml', 'utf8'); - const config: { token: string, prefix: string, guildID: string, mongoDB: string } = parse(read); + const config: { token: string, prefix: string, guildID: string, mongoDB: string, emailPass: string } = parse(read); const client = new Client(config.token, { defaultImageFormat: 'png', restMode: true }); client.config = config; await client.loadDatabase(); From 1d581a86394ac3e3e8c7e6c93b65fe5739c50aff Mon Sep 17 00:00:00 2001 From: Matthew R Date: Mon, 15 Jun 2020 21:34:54 -0400 Subject: [PATCH 3/4] add email transporter to util --- src/class/Util.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/class/Util.ts b/src/class/Util.ts index fce0037..0baf1d7 100644 --- a/src/class/Util.ts +++ b/src/class/Util.ts @@ -1,4 +1,5 @@ /* eslint-disable no-bitwise */ +import nodemailer from 'nodemailer'; import signale from 'signale'; import { Member, Message, Guild, PrivateChannel, GroupChannel, Role, AnyGuildChannel } from 'eris'; import { Client, Command, Moderation, RichEmbed } from '.'; @@ -11,6 +12,8 @@ export default class Util { public signale: signale.Signale; + public transporter: nodemailer.Transporter; + constructor(client: Client) { this.client = client; this.moderation = new Moderation(this.client); @@ -20,6 +23,11 @@ export default class Util { displayTimestamp: true, displayFilename: true, }); + this.transporter = nodemailer.createTransport({ + host: 'staff.libraryofcode.org', + port: 587, + auth: { user: 'internal', pass: this.client.config.emailPass }, + }); } get emojis() { From 211a82974ab16f5c4143a21c60ebc65ae73de05d Mon Sep 17 00:00:00 2001 From: Matthew R Date: Mon, 15 Jun 2020 21:35:40 -0400 Subject: [PATCH 4/4] add paging system --- src/commands/index.ts | 1 + src/commands/page.ts | 120 ++++++++++++ src/intervals/departmentPager.ts | 315 +++++++++++++++++++++++++++++++ src/models/PagerNumber.ts | 26 +++ src/models/index.ts | 1 + 5 files changed, 463 insertions(+) create mode 100644 src/commands/page.ts create mode 100644 src/intervals/departmentPager.ts create mode 100644 src/models/PagerNumber.ts diff --git a/src/commands/index.ts b/src/commands/index.ts index b319ac8..313b0dc 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -11,6 +11,7 @@ export { default as info } from './info'; export { default as kick } from './kick'; export { default as listredirects } from './listredirects'; export { default as npm } from './npm'; +export { default as page } from './page'; export { default as ping } from './ping'; export { default as roleinfo } from './roleinfo'; export { default as unban } from './unban'; diff --git a/src/commands/page.ts b/src/commands/page.ts new file mode 100644 index 0000000..05d6b84 --- /dev/null +++ b/src/commands/page.ts @@ -0,0 +1,120 @@ +/* eslint-disable no-case-declarations */ +/* eslint-disable no-continue */ +/* eslint-disable no-await-in-loop */ +import { Message } from 'eris'; +import { Client, Command, RichEmbed } from '../class'; + +export default class Page extends Command { + public local: { emergencyNumbers: string[], departmentNumbers: string[], validPagerCodes: string[] }; + + constructor(client: Client) { + super(client); + this.name = 'page'; + this.description = 'Pages the specified emergency number, department number, or individual number with the specified pager code.'; + this.usage = 'page '; + this.permissions = 1; + this.enabled = true; + this.guildOnly = true; + this.local = { + emergencyNumbers: ['#0', '#1', '#2', '#3'], + departmentNumbers: ['00', '01', '10', '20', '21', '22'], + validPagerCodes: ['911', '811', '210', '265', '411', '419', '555'], + }; + } + + public async run(message: Message, args: string[]) { + try { + if (!args[0]) { + this.client.commands.get('help').run(message, [this.name]); + const embed = new RichEmbed(); + embed.setTitle('Special Emergency/Department Numbers & Pager Codes'); + embed.addField('Special Emergency Numbers', '`#0` | Broadcast - all Staff/Associates\n`#1` | Authoritative Broadcast - all Directors, Supervisors, Technicians, and Moderators\n`#2` | Systems Administrators/Technicians Broadcast - Matthew, Bsian, NightRaven, and all Technicians\n`#3` | Community/Moderation Team Broadcast - all Directors, Supervisors, Moderators, and Core Team'); + embed.addField('Department Numbers', '`00` | Board of Directors\n`01` | Supervisors\n`10` | Technicians\n`20` | Moderators\n`21` | Core Team\n`22` | Associates'); + embed.addField('Pager Codes', '"Pager" term in this field refers to the Staff member that initially paged. This is a list of valid codes you can send via a page.\n\n`911` - Pager is requesting EMERGENCY assistance\n`811` - Pager is requesting immediate/ASAP assistance\n`210` - Pager is informing you they acknowledged your request, usually sent in response to OK the initial page.\n`265` - Pager is requesting that you check your email\n`411` - Pager is requesting information/counsel from you\n`419` - Pager didn\'t recognize your request\n`555` - Pager is requesting that you contact them'); + embed.setFooter(this.client.user.username, this.client.user.avatarURL); + embed.setTimestamp(); + return message.channel.createMessage({ embed }); + } + message.delete(); + const loading = await this.loading(message.channel, 'Paging...'); + const sender = await this.client.db.PagerNumber.findOne({ individualAssignID: message.author.id }); + const page = await this.page(args[0], sender.num, args[1]); + if (page.status === true) { + loading.delete(); + return this.success(message.channel, page.message); + } + loading.delete(); + return this.error(message.channel, page.message); + } catch (err) { + return this.client.util.handleError(err, message, this); + } + } + + public async page(recipientNumber: string, senderNumber: string, code: string): Promise<{status: boolean, message: string}> { + try { + const senderEntry = await this.client.db.PagerNumber.findOne({ num: senderNumber }); + if (!senderEntry) { + return { status: false, message: 'You do not have a Pager Number.' }; + } + if (this.local.emergencyNumbers.includes(recipientNumber)) { + switch (recipientNumber) { + case '#0': + this.local.departmentNumbers.forEach(async (num) => { + await this.page(num, '#0', code); + }); + break; + case '#1': + await this.page('00', '#1', code); + await this.page('01', '#1', code); + await this.page('10', '#1', code); + await this.page('20', '#1', code); + break; + case '#2': + const matthew = await this.client.db.PagerNumber.findOne({ individualAssignID: '278620217221971968' }); + const bsian = await this.client.db.PagerNumber.findOne({ individualAssignID: '253600545972027394' }); + const nightraven = await this.client.db.PagerNumber.findOne({ individualAssignID: '239261547959025665' }); + await this.page(matthew?.num, '#2', code); + await this.page(bsian?.num, '#2', code); + await this.page(nightraven?.num, '#2', code); + await this.page('10', '#2', code); + break; + case '#3': + await this.page('00', '#3', code); + await this.page('01', '#3', code); + await this.page('20', '#3', code); + await this.page('21', '#3', code); + break; + default: + break; + } + return { status: true, message: `Page to \`${recipientNumber}\` sent.` }; + } + const recipientEntry = await this.client.db.PagerNumber.findOne({ num: recipientNumber }); + if (!recipientEntry) { + return { status: false, message: `Pager Number \`${recipientNumber}\` does not exist.` }; + } + if (!this.local.validPagerCodes.includes(code)) { + return { status: false, message: 'The Pager Code you provided is invalid.' }; + } + + for (const id of recipientEntry.discordIDs) { + const sender = this.client.guilds.get(this.client.config.guildID).members.get(senderEntry.individualAssignID); + const chan = await this.client.getDMChannel(id); + if (!chan) continue; + chan.createMessage(`__**Page**__\n**Recipient PN:** ${recipientNumber}\n**Sender PN:** ${senderNumber} (${sender ? `${sender.username}${sender.discriminator}` : ''})\n\n**Pager Code:** ${code}`); + } + for (const email of recipientEntry.emailAddresses) { + const sender = this.client.guilds.get(this.client.config.guildID).members.get(senderEntry.individualAssignID); + await this.client.util.transporter.sendMail({ + from: '"LOC Paging System" ', + to: email, + subject: `PAGE FROM ${recipientNumber}`, + html: `

Page

Recipient PN: ${recipientNumber}
Sender PN: ${senderNumber} (${sender ? `${sender.username}${sender.discriminator}` : ''})

Pager Code: ${code}`, + }); + } + return { status: true, message: `Page to \`${recipientNumber}\` sent.` }; + } catch (err) { + return { status: false, message: `Error during Processing: ${err}` }; + } + } +} diff --git a/src/intervals/departmentPager.ts b/src/intervals/departmentPager.ts new file mode 100644 index 0000000..bbf3f1f --- /dev/null +++ b/src/intervals/departmentPager.ts @@ -0,0 +1,315 @@ +/* eslint-disable no-await-in-loop */ +import { Client } from '../class'; +import { PagerNumberInterface } from '../models'; + +let interval: NodeJS.Timeout; + +async function setupDepartmentCodes(client: Client): Promise { + const directorPagers = await client.db.PagerNumber.findOne({ num: '00' }); + const supervisorPagers = await client.db.PagerNumber.findOne({ num: '01' }); + const technicianPagers = await client.db.PagerNumber.findOne({ num: '10' }); + const moderatorPagers = await client.db.PagerNumber.findOne({ num: '20' }); + const coreTeamPagers = await client.db.PagerNumber.findOne({ num: '21' }); + const associatePagers = await client.db.PagerNumber.findOne({ num: '22' }); + + if (!directorPagers) { + const setup = new client.db.PagerNumber({ + num: '00', + individualAssignID: '', + emailAddresses: [], + discordIDs: [], + }); + await setup.save(); + } + if (!supervisorPagers) { + const setup = new client.db.PagerNumber({ + num: '01', + individualAssignID: '', + emailAddresses: [], + discordIDs: [], + }); + await setup.save(); + } + if (!technicianPagers) { + const setup = new client.db.PagerNumber({ + num: '10', + individualAssignID: '', + emailAddresses: [], + discordIDs: [], + }); + setup.save(); + } + if (!moderatorPagers) { + const setup = new client.db.PagerNumber({ + num: '20', + individualAssignID: '', + emailAddresses: [], + discordIDs: [], + }); + await setup.save(); + } + if (!coreTeamPagers) { + const setup = new client.db.PagerNumber({ + num: '21', + individualAssignID: '', + emailAddresses: [], + discordIDs: [], + }); + await setup.save(); + } + if (!associatePagers) { + const setup = new client.db.PagerNumber({ + num: '22', + individualAssignID: '', + emailAddresses: [], + discordIDs: [], + }); + await setup.save(); + } +} + +export default function departmentPager(client: Client): NodeJS.Timeout { + setupDepartmentCodes(client); + interval = setInterval(async () => { + const acknowledgements = require(`${__dirname}/../configs/acknowledgements.json`); + function resolveStaffInformation(id: string) { + return acknowledgements.find((m) => m.id === id); + } + await client.guilds.get(client.config.guildID).fetchAllMembers(); + const { members } = client.guilds.get(client.config.guildID); + + // const takenPagers = new Set(); + + members.forEach(async (member) => { + let pager = await client.db.PagerNumber.findOne({ individualAssignID: member.id }); + // Directors + if (!pager && member.roles.includes('662163685439045632')) { + let randomPagerNumber: string; + let status = true; + // eslint-disable-next-line no-constant-condition + while (status) { + randomPagerNumber = `00${String(Math.floor(Math.random() * 9) + 1)}`; + const check = await client.db.PagerNumber.findOne({ num: randomPagerNumber }); + if (check?.num !== randomPagerNumber) status = false; + } + const acknowledgement = resolveStaffInformation(member.id); + if (!acknowledgement || !acknowledgement.emailAddress) return; + const newNumber = new client.db.PagerNumber({ + num: randomPagerNumber, + individualAssignID: member.id, + emailAddresses: [acknowledgement.emailAddress], + discordIDs: [member.id], + }); + pager = await newNumber.save(); + client.getDMChannel(member.id).then((chan) => { + chan.createMessage(`__**Pager Number Creation**__\nYour individual pager number has been automatically created. Your number (PN) is ${randomPagerNumber}.`); + }); + } + // Supervisors + if (!pager && member.roles.includes('701454855952138300')) { + let randomPagerNumber: string; + let status = true; + // eslint-disable-next-line no-constant-condition + while (status) { + randomPagerNumber = `01${String(Math.floor(Math.random() * 9) + 1)}`; + const check = await client.db.PagerNumber.findOne({ num: randomPagerNumber }); + if (check?.num !== randomPagerNumber) status = false; + } + const acknowledgement = resolveStaffInformation(member.id); + if (!acknowledgement || !acknowledgement.emailAddress) return; + const newNumber = new client.db.PagerNumber({ + num: randomPagerNumber, + individualAssignID: member.id, + emailAddresses: [acknowledgement.emailAddress], + discordIDs: [member.id], + }); + pager = await newNumber.save(); + client.getDMChannel(member.id).then((chan) => { + chan.createMessage(`__**Pager Number Creation**__\nYour individual pager number has been automatically created. Your number (PN) is ${randomPagerNumber}.`); + }); + } + // Technicians + if (!pager && member.roles.includes('701454780828221450')) { + let randomPagerNumber: string; + let status = true; + // eslint-disable-next-line no-constant-condition + while (status) { + randomPagerNumber = `10${String(Math.floor(Math.random() * 99) + 1)}`; + if (randomPagerNumber.length === 3) randomPagerNumber = `${randomPagerNumber}0`; + const check = await client.db.PagerNumber.findOne({ num: randomPagerNumber }); + if (check?.num !== randomPagerNumber) status = false; + } + const acknowledgement = resolveStaffInformation(member.id); + if (!acknowledgement || !acknowledgement.emailAddress) return; + const newNumber = new client.db.PagerNumber({ + num: randomPagerNumber, + individualAssignID: member.id, + emailAddresses: [acknowledgement.emailAddress], + discordIDs: [member.id], + }); + pager = await newNumber.save(); + client.getDMChannel(member.id).then((chan) => { + chan.createMessage(`__**Pager Number Creation**__\nYour individual pager number has been automatically created. Your number (PN) is ${randomPagerNumber}.`); + }); + } + // Moderators + if (!pager && member.roles.includes('455972169449734144')) { + let randomPagerNumber: string; + let status = true; + // eslint-disable-next-line no-constant-condition + while (status) { + randomPagerNumber = `20${String(Math.floor(Math.random() * 99) + 1)}`; + if (randomPagerNumber.length === 3) randomPagerNumber = `${randomPagerNumber}0`; + const check = await client.db.PagerNumber.findOne({ num: randomPagerNumber }); + if (check?.num !== randomPagerNumber) status = false; + } + const acknowledgement = resolveStaffInformation(member.id); + if (!acknowledgement || !acknowledgement.emailAddress) return; + const newNumber = new client.db.PagerNumber({ + num: randomPagerNumber, + individualAssignID: member.id, + emailAddresses: [acknowledgement.emailAddress], + discordIDs: [member.id], + }); + pager = await newNumber.save(); + client.getDMChannel(member.id).then((chan) => { + chan.createMessage(`__**Pager Number Creation**__\nYour individual pager number has been automatically created. Your number (PN) is ${randomPagerNumber}.`); + }); + } + // Core Team + if (!pager && member.roles.includes('453689940140883988')) { + let randomPagerNumber: string; + let status = true; + // eslint-disable-next-line no-constant-condition + while (status) { + randomPagerNumber = `21${String(Math.floor(Math.random() * 999) + 1)}`; + if (randomPagerNumber.length === 4) randomPagerNumber = `${randomPagerNumber}0`; + const check = await client.db.PagerNumber.findOne({ num: randomPagerNumber }); + if (check?.num !== randomPagerNumber) status = false; + } + const acknowledgement = resolveStaffInformation(member.id); + if (!acknowledgement || !acknowledgement.emailAddress) return; + const newNumber = new client.db.PagerNumber({ + num: randomPagerNumber, + individualAssignID: member.id, + emailAddresses: [acknowledgement.emailAddress], + discordIDs: [member.id], + }); + pager = await newNumber.save(); + client.getDMChannel(member.id).then((chan) => { + chan.createMessage(`__**Pager Number Creation**__\nYour individual pager number has been automatically created. Your number (PN) is ${randomPagerNumber}.`); + }); + } + // Associates + if (!pager && member.roles.includes('701481967149121627')) { + let randomPagerNumber: string; + let status = true; + // eslint-disable-next-line no-constant-condition + while (status) { + randomPagerNumber = `22${String(Math.floor(Math.random() * 999) + 1)}`; + if (randomPagerNumber.length === 4) randomPagerNumber = `${randomPagerNumber}0`; + const check = await client.db.PagerNumber.findOne({ num: randomPagerNumber }); + if (check?.num !== randomPagerNumber) status = false; + } + const acknowledgement = resolveStaffInformation(member.id); + if (!acknowledgement || !acknowledgement.emailAddress) return; + const newNumber = new client.db.PagerNumber({ + num: randomPagerNumber, + individualAssignID: member.id, + emailAddresses: [acknowledgement.emailAddress], + discordIDs: [member.id], + }); + pager = await newNumber.save(); + client.getDMChannel(member.id).then((chan) => { + chan.createMessage(`__**Pager Number Creation**__\nYour individual pager number has been automatically created. Your number (PN) is ${randomPagerNumber}.`); + }); + } + }); + + // Associates + const associatePagers = await client.db.PagerNumber.findOne({ num: '22' }); + members.forEach(async (member) => { + if (member.roles.includes('701481967149121627') && !associatePagers.discordIDs.includes(member.id)) { + await associatePagers.updateOne({ $addToSet: { discordIDs: member.id } }); + const acknowledgement = resolveStaffInformation(member.id); + if (acknowledgement?.emailAddress) await associatePagers.updateOne({ $addToSet: { emailAddresses: acknowledgement.emailAddress } }); + } + if (!member.roles.includes('701481967149121627') && associatePagers.discordIDs.includes(member.id)) { + await associatePagers.updateOne({ $pull: { discordIDs: member.id } }); + const acknowledgement = resolveStaffInformation(member.id); + if (acknowledgement?.emailAddress) await associatePagers.updateOne({ $pull: { emailAddresses: acknowledgement.emailAddress } }); + } + }); + // Core Team + const coreTeamPagers = await client.db.PagerNumber.findOne({ num: '21' }); + members.forEach(async (member) => { + if (member.roles.includes('453689940140883988') && !coreTeamPagers.discordIDs.includes(member.id)) { + await coreTeamPagers.updateOne({ $addToSet: { discordIDs: member.id } }); + const acknowledgement = resolveStaffInformation(member.id); + if (acknowledgement?.emailAddress) await coreTeamPagers.updateOne({ $addToSet: { emailAddresses: acknowledgement.emailAddress } }); + } + if (!member.roles.includes('453689940140883988') && coreTeamPagers.discordIDs.includes(member.id)) { + await coreTeamPagers.updateOne({ $pull: { discordIDs: member.id } }); + const acknowledgement = resolveStaffInformation(member.id); + if (acknowledgement?.emailAddress) await coreTeamPagers.updateOne({ $pull: { emailAddresses: acknowledgement.emailAddress } }); + } + }); + // Moderator + const moderatorPagers = await client.db.PagerNumber.findOne({ num: '20' }); + members.forEach(async (member) => { + if (member.roles.includes('455972169449734144') && !moderatorPagers.discordIDs.includes(member.id)) { + await moderatorPagers.updateOne({ $addToSet: { discordIDs: member.id } }); + const acknowledgement = resolveStaffInformation(member.id); + if (acknowledgement?.emailAddress) await moderatorPagers.updateOne({ $addToSet: { emailAddresses: acknowledgement.emailAddress } }); + } + if (!member.roles.includes('455972169449734144') && moderatorPagers.discordIDs.includes(member.id)) { + await moderatorPagers.updateOne({ $pull: { discordIDs: member.id } }); + const acknowledgement = resolveStaffInformation(member.id); + if (acknowledgement?.emailAddress) await moderatorPagers.updateOne({ $pull: { emailAddresses: acknowledgement.emailAddress } }); + } + }); + // Technician + const technicianPagers = await client.db.PagerNumber.findOne({ num: '10' }); + members.forEach(async (member) => { + if (member.roles.includes('701454780828221450') && !technicianPagers.discordIDs.includes(member.id)) { + await technicianPagers.updateOne({ $addToSet: { discordIDs: member.id } }); + const acknowledgement = resolveStaffInformation(member.id); + if (acknowledgement?.emailAddress) await technicianPagers.updateOne({ $addToSet: { emailAddresses: acknowledgement.emailAddress } }); + } + if (!member.roles.includes('701454780828221450') && technicianPagers.discordIDs.includes(member.id)) { + await technicianPagers.updateOne({ $pull: { discordIDs: member.id } }); + const acknowledgement = resolveStaffInformation(member.id); + if (acknowledgement?.emailAddress) await technicianPagers.updateOne({ $pull: { emailAddresses: acknowledgement.emailAddress } }); + } + }); + // Supervisor + const supervisorPagers = await client.db.PagerNumber.findOne({ num: '01' }); + members.forEach(async (member) => { + if (member.roles.includes('701454855952138300') && !supervisorPagers.discordIDs.includes(member.id)) { + await supervisorPagers.updateOne({ $addToSet: { discordIDs: member.id } }); + const acknowledgement = resolveStaffInformation(member.id); + if (acknowledgement?.emailAddress) await supervisorPagers.updateOne({ $addToSet: { emailAddresses: acknowledgement.emailAddress } }); + } + if (!member.roles.includes('701454855952138300') && supervisorPagers.discordIDs.includes(member.id)) { + await supervisorPagers.updateOne({ $pull: { discordIDs: member.id } }); + const acknowledgement = resolveStaffInformation(member.id); + if (acknowledgement?.emailAddress) await supervisorPagers.updateOne({ $pull: { emailAddresses: acknowledgement.emailAddress } }); + } + }); + // Board of Directors + const directorPagers = await client.db.PagerNumber.findOne({ num: '00' }); + members.forEach(async (member) => { + if (member.roles.includes('662163685439045632') && !directorPagers.discordIDs.includes(member.id)) { + await directorPagers.updateOne({ $addToSet: { discordIDs: member.id } }); + const acknowledgement = resolveStaffInformation(member.id); + if (acknowledgement?.emailAddress) await directorPagers.updateOne({ $addToSet: { emailAddresses: acknowledgement.emailAddress } }); + } + if (!member.roles.includes('662163685439045632') && directorPagers.discordIDs.includes(member.id)) { + await directorPagers.updateOne({ $pull: { discordIDs: member.id } }); + const acknowledgement = resolveStaffInformation(member.id); + if (acknowledgement?.emailAddress) await directorPagers.updateOne({ $pull: { emailAddresses: acknowledgement.emailAddress } }); + } + }); + }, 10000); + return interval; +} diff --git a/src/models/PagerNumber.ts b/src/models/PagerNumber.ts new file mode 100644 index 0000000..e9ef603 --- /dev/null +++ b/src/models/PagerNumber.ts @@ -0,0 +1,26 @@ +import { Document, Schema, model } from 'mongoose'; + +export interface PagerNumberRaw { + num: string, + // This field will be "" if the pager number doesn't belong to an individual user + individualAssignID: string, + emailAddresses: string[], + discordIDs: string[], +} + +export interface PagerNumberInterface extends Document { + num: string, + // This field will be "" if the pager number doesn't belong to an individual user + individualAssignID: string, + emailAddresses: string[], + discordIDs: string[], +} + +const PagerNumber: Schema = new Schema({ + num: String, + individualAssignID: String, + emailAddresses: Array, + discordIDs: Array, +}); + +export default model('PagerNumber', PagerNumber); diff --git a/src/models/index.ts b/src/models/index.ts index 1351809..1b78410 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -1,3 +1,4 @@ export { default as Member, MemberInterface } from './Member'; export { default as Moderation, ModerationInterface } from './Moderation'; +export { default as PagerNumber, PagerNumberInterface, PagerNumberRaw } from './PagerNumber'; export { default as Redirect, RedirectInterface, RedirectRaw } from './Redirect';