/* eslint-disable no-await-in-loop */ import { Client } from '../class'; let interval: NodeJS.Timeout; async function setupDepartmentCodes(client: Client): Promise { 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(); } } 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(); for (const member of members.values()) { 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); if (!acknowledgement || !acknowledgement.emailAddress) return; const newNumber = new client.db.PagerNumber({ num: randomPagerNumber, individualAssignID: member.id, emailAddresses: [acknowledgement.emailAddress], discordIDs: [member.id], }); if (await client.db.PagerNumber.findOne({ num: randomPagerNumber })) return; 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('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 || (check?.num !== randomPagerNumber)) status = true; } 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], }); if (await client.db.PagerNumber.findOne({ num: randomPagerNumber })) return; 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 || (check?.num !== randomPagerNumber)) status = true; } 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], }); if (await client.db.PagerNumber.findOne({ num: randomPagerNumber })) return; 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 || (check?.num !== randomPagerNumber)) status = true; } 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], }); if (await client.db.PagerNumber.findOne({ num: randomPagerNumber })) return; 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 || (check?.num !== randomPagerNumber)) status = true; } 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], }); if (await client.db.PagerNumber.findOne({ num: randomPagerNumber })) return; 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 || (check?.num !== randomPagerNumber)) status = true; } 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], }); if (await client.db.PagerNumber.findOne({ num: randomPagerNumber })) return; 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' }); for (const member of members.values()) { 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' }); for (const member of members.values()) { 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' }); for (const member of members.values()) { 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' }); for (const member of members.values()) { 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' }); for (const member of members.values()) { 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' }); for (const member of members.values()) { 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 } }); } } }, 1200000); return interval; }