community-relations/src/intervals/departmentPager.ts

373 lines
19 KiB
TypeScript
Raw Normal View History

2020-07-06 03:15:06 -04:00
/* eslint-disable no-continue */
/* eslint-disable no-await-in-loop */
import { Member, TextableChannel } from 'eris';
import { Client } from '../class';
let interval: NodeJS.Timeout;
async function setupDepartmentCodes(client: Client): Promise<void> {
const directorPagers = await client.db.PagerNumber.findOne({ num: '00' }).lean().exec();
const supervisorPagers = await client.db.PagerNumber.findOne({ num: '01' }).lean().exec();
const technicianPagers = await client.db.PagerNumber.findOne({ num: '10' }).lean().exec();
const moderatorPagers = await client.db.PagerNumber.findOne({ num: '20' }).lean().exec();
const coreTeamPagers = await client.db.PagerNumber.findOne({ num: '21' }).lean().exec();
const associatePagers = await client.db.PagerNumber.findOne({ num: '22' }).lean().exec();
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();
}
}
function logNewPager(client: Client, num: string, member: Member): void {
2020-07-12 01:08:04 -04:00
client.util.signale.log(`Pager Number '${num}' created for '${member.user.username}#${member.user.discriminator}'.`);
2020-07-06 03:15:06 -04:00
const channel = <TextableChannel> client.guilds.get(client.config.guildID).channels.get('722636436716781619');
channel.createMessage(`__**'${member.user.username}#${member.user.discriminator}' assigned to pager number '${num}'.**__`);
}
2020-07-19 20:08:55 -04:00
export default async function departmentPager(client: Client): Promise<NodeJS.Timeout> {
setupDepartmentCodes(client);
// eslint-disable-next-line no-shadow
2020-07-19 18:42:17 -04:00
async function start(client: Client) {
2020-07-06 03:15:06 -04:00
const acknowledgements = require(`${__dirname}/../configs/acknowledgements.json`);
function resolveStaffInformation(id: string) {
return acknowledgements.find((m) => m.id === id);
}
2020-07-19 20:08:55 -04:00
await client.guilds.get(client.config.guildID).fetchAllMembers();
const members = client.guilds.get(client.config.guildID).members.values();
2020-07-06 03:15:06 -04:00
2020-07-19 20:08:55 -04:00
for (const member of members) {
2020-07-06 03:15:06 -04:00
const pager = await client.db.PagerNumber.findOne({ individualAssignID: member.id }).lean().exec();
if (!pager) continue;
if (pager.num.startsWith('00') && !member.roles.includes('662163685439045632')) {
2020-07-12 01:08:04 -04:00
await client.db.PagerNumber.deleteOne({ num: pager.num });
2020-07-06 03:15:06 -04:00
client.util.signale.log(`Pager Number '${pager.num}' has been deleted.`);
2020-07-12 01:08:04 -04:00
}
if (pager.num.startsWith('01') && !member.roles.includes('701454855952138300')) {
await client.db.PagerNumber.deleteOne({ num: pager.num });
2020-07-06 03:15:06 -04:00
client.util.signale.log(`Pager Number '${pager.num}' has been deleted.`);
2020-07-12 01:08:04 -04:00
}
if (pager.num.startsWith('10') && !member.roles.includes('701454780828221450')) {
await client.db.PagerNumber.deleteOne({ num: pager.num });
2020-07-06 03:15:06 -04:00
client.util.signale.log(`Pager Number '${pager.num}' has been deleted.`);
2020-07-12 01:08:04 -04:00
}
if (pager.num.startsWith('20') && !member.roles.includes('455972169449734144')) {
await client.db.PagerNumber.deleteOne({ num: pager.num });
2020-07-06 03:15:06 -04:00
client.util.signale.log(`Pager Number '${pager.num}' has been deleted.`);
2020-07-12 01:08:04 -04:00
}
if (pager.num.startsWith('21') && !member.roles.includes('453689940140883988')) {
await client.db.PagerNumber.deleteOne({ num: pager.num });
2020-07-06 03:15:06 -04:00
client.util.signale.log(`Pager Number '${pager.num}' has been deleted.`);
2020-07-12 01:08:04 -04:00
}
if (pager.num.startsWith('22') && !member.roles.includes('701481967149121627')) {
await client.db.PagerNumber.deleteOne({ num: pager.num });
2020-07-06 03:15:06 -04:00
client.util.signale.log(`Pager Number '${pager.num}' has been deleted.`);
}
}
// const takenPagers = new Set<string>();
2020-07-19 20:08:55 -04:00
for (const member of members) {
2020-07-06 03:15:06 -04:00
let pager = await client.db.PagerNumber.findOne({ individualAssignID: member.id }).lean().exec();
// 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) status = false;
if (check?.num !== randomPagerNumber) status = false;
}
const acknowledgement = resolveStaffInformation(member.id);
2020-07-19 20:08:55 -04:00
if (!acknowledgement || !acknowledgement.emailAddress) continue;
2020-07-06 03:15:06 -04:00
const newNumber = new client.db.PagerNumber({
num: randomPagerNumber,
individualAssignID: member.id,
emailAddresses: [acknowledgement.emailAddress],
discordIDs: [member.id],
});
2020-07-19 20:08:55 -04:00
if (await client.db.PagerNumber.findOne({ num: randomPagerNumber })) continue;
2020-07-06 03:15:06 -04:00
pager = await newNumber.save();
logNewPager(client, randomPagerNumber, member);
client.getDMChannel(member.id).then((chan) => {
chan.createMessage(`__**Pager Number Creation**__\nYour individual pager number has been automatically created. Your number (PN) is ${randomPagerNumber}.`);
});
} else if (!pager && member.roles.includes('701454855952138300')) {
// Supervisors
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) status = false;
if (check?.num !== randomPagerNumber) status = false;
}
const acknowledgement = resolveStaffInformation(member.id);
2020-07-19 20:08:55 -04:00
if (!acknowledgement || !acknowledgement.emailAddress) continue;
2020-07-06 03:15:06 -04:00
const newNumber = new client.db.PagerNumber({
num: randomPagerNumber,
individualAssignID: member.id,
emailAddresses: [acknowledgement.emailAddress],
discordIDs: [member.id],
});
2020-07-19 20:08:55 -04:00
if (await client.db.PagerNumber.findOne({ num: randomPagerNumber })) continue;
2020-07-06 03:15:06 -04:00
logNewPager(client, randomPagerNumber, member);
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}.`);
});
} else if (!pager && member.roles.includes('701454780828221450')) {
// Technicians
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) status = false;
if (check?.num !== randomPagerNumber) status = false;
}
const acknowledgement = resolveStaffInformation(member.id);
2020-07-19 20:08:55 -04:00
if (!acknowledgement || !acknowledgement.emailAddress) continue;
2020-07-06 03:15:06 -04:00
const newNumber = new client.db.PagerNumber({
num: randomPagerNumber,
individualAssignID: member.id,
emailAddresses: [acknowledgement.emailAddress],
discordIDs: [member.id],
});
2020-07-19 20:08:55 -04:00
if (await client.db.PagerNumber.findOne({ num: randomPagerNumber })) continue;
2020-07-06 03:15:06 -04:00
logNewPager(client, randomPagerNumber, member);
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}.`);
});
} else if (!pager && member.roles.includes('455972169449734144')) {
// Moderators
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) status = false;
if (check?.num !== randomPagerNumber) status = false;
}
const acknowledgement = resolveStaffInformation(member.id);
2020-07-19 20:08:55 -04:00
if (!acknowledgement || !acknowledgement.emailAddress) continue;
2020-07-06 03:15:06 -04:00
const newNumber = new client.db.PagerNumber({
num: randomPagerNumber,
individualAssignID: member.id,
emailAddresses: [acknowledgement.emailAddress],
discordIDs: [member.id],
});
2020-07-19 20:08:55 -04:00
if (await client.db.PagerNumber.findOne({ num: randomPagerNumber })) continue;
2020-07-06 03:15:06 -04:00
logNewPager(client, randomPagerNumber, member);
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}.`);
});
} else if (!pager && member.roles.includes('453689940140883988')) {
// Core Team
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) status = false;
if (check?.num !== randomPagerNumber) status = false;
}
const acknowledgement = resolveStaffInformation(member.id);
2020-07-19 20:08:55 -04:00
if (!acknowledgement || !acknowledgement.emailAddress) continue;
2020-07-06 03:15:06 -04:00
const newNumber = new client.db.PagerNumber({
num: randomPagerNumber,
individualAssignID: member.id,
emailAddresses: [acknowledgement.emailAddress],
discordIDs: [member.id],
});
2020-07-19 20:08:55 -04:00
if (await client.db.PagerNumber.findOne({ num: randomPagerNumber })) continue;
2020-07-06 03:15:06 -04:00
logNewPager(client, randomPagerNumber, member);
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}.`);
});
} else if (!pager && member.roles.includes('701481967149121627')) {
// Associates
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) status = false;
if (check?.num !== randomPagerNumber) status = false;
}
const acknowledgement = resolveStaffInformation(member.id);
2020-07-19 20:08:55 -04:00
if (!acknowledgement || !acknowledgement.emailAddress) continue;
2020-07-06 03:15:06 -04:00
const newNumber = new client.db.PagerNumber({
num: randomPagerNumber,
individualAssignID: member.id,
emailAddresses: [acknowledgement.emailAddress],
discordIDs: [member.id],
});
2020-07-19 20:08:55 -04:00
if (await client.db.PagerNumber.findOne({ num: randomPagerNumber })) continue;
2020-07-06 03:15:06 -04:00
logNewPager(client, randomPagerNumber, member);
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' });
2020-07-19 20:08:55 -04:00
for (const member of members) {
2020-07-06 03:15:06 -04:00
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' });
2020-07-19 20:08:55 -04:00
for (const member of members) {
2020-07-06 03:15:06 -04:00
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' });
2020-07-19 20:08:55 -04:00
for (const member of members) {
2020-07-06 03:15:06 -04:00
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' });
2020-07-19 20:08:55 -04:00
for (const member of members) {
2020-07-06 03:15:06 -04:00
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' });
2020-07-19 20:08:55 -04:00
for (const member of members) {
2020-07-06 03:15:06 -04:00
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' });
2020-07-19 20:08:55 -04:00
for (const member of members) {
2020-07-06 03:15:06 -04:00
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 } });
}
}
2020-07-19 18:20:56 -04:00
}
2020-07-19 20:08:55 -04:00
client.util.signale.time('pagerint');
await start(client);
client.util.signale.timeEnd('pagerint');
2020-07-19 18:20:56 -04:00
interval = setInterval(async () => {
2020-07-19 20:08:55 -04:00
client.util.signale.time('pagerint');
await start(client);
client.util.signale.timeEnd('pagerint');
2020-07-12 01:09:09 -04:00
}, 300000);
2020-07-06 03:15:06 -04:00
return interval;
}