From 211a82974ab16f5c4143a21c60ebc65ae73de05d Mon Sep 17 00:00:00 2001 From: Matthew R Date: Mon, 15 Jun 2020 21:35:40 -0400 Subject: [PATCH] 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';