community-relations/src/commands/page.ts

134 lines
7.7 KiB
TypeScript

/* 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[], codeDict: Map<string, 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 <pager number> <pager code>';
this.aliases = ['p'];
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'],
codeDict: new Map(),
};
this.init();
}
public init() {
this.local.codeDict.set('911', 'Sender is requesting EMERGENCY assistance.');
this.local.codeDict.set('811', 'Sender is requesting immediate/ASAP assistance.');
this.local.codeDict.set('210', 'Sender is informing you they acknowledged your request, usually sent in response to OK the initial page. (10-4)');
this.local.codeDict.set('265', 'Sender is requesting that you check your email.');
this.local.codeDict.set('411', 'Sender is requesting information/counsel from you.');
this.local.codeDict.set('419', 'Sender didn\'t recognize your request.');
this.local.codeDict.set('555', 'Sender is requesting that you contact them.');
}
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], message);
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, message: Message): 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, message);
});
break;
case '#1':
await this.page('00', '#1', code, message);
await this.page('01', '#1', code, message);
await this.page('10', '#1', code, message);
await this.page('20', '#1', code, message);
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, message);
await this.page(bsian?.num, '#2', code, message);
await this.page(nightraven?.num, '#2', code, message);
await this.page('10', '#2', code, message);
break;
case '#3':
await this.page('00', '#3', code, message);
await this.page('01', '#3', code, message);
await this.page('20', '#3', code, message);
await this.page('21', '#3', code, message);
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**Initial Command:** https://discordapp.com/channels/${message.guild.id}/${message.channel.id}/${message.id} (<#${message.channel.id}>)\n\n**Pager Code:** ${code} (${this.local.codeDict.get(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" <internal@libraryofcode.org>',
to: email,
subject: `PAGE FROM ${recipientNumber}`,
html: `<h1>Page</h1><strong>Recipient PN:</strong> ${recipientNumber}<br><strong>Sender PN:</strong> ${senderNumber} (${sender ? `${sender.username}#${sender.discriminator}` : ''})<br><strong>Initial Command:</strong> https://discordapp.com/channels/${message.guild.id}/${message.channel.id}/${message.id} (<#${message.channel.id}>)<br><br><strong>Pager Code:</strong> ${code} (${this.local.codeDict.get(code)})`,
});
}
return { status: true, message: `Page to \`${recipientNumber}\` sent.` };
} catch (err) {
return { status: false, message: `Error during Processing: ${err}` };
}
}
}