316 lines
15 KiB
TypeScript
316 lines
15 KiB
TypeScript
|
/* eslint-disable no-await-in-loop */
|
||
|
import { Client } from '../class';
|
||
|
import { PagerNumberInterface } from '../models';
|
||
|
|
||
|
let interval: NodeJS.Timeout;
|
||
|
|
||
|
async function setupDepartmentCodes(client: Client): Promise<void> {
|
||
|
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<string>();
|
||
|
|
||
|
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;
|
||
|
}
|