2020-06-17 19:11:11 -04:00
/* eslint-disable no-continue */
2020-06-15 21:35:40 -04:00
/* eslint-disable no-await-in-loop */
2020-06-18 15:50:37 -04:00
import { Member , TextableChannel } from 'eris' ;
2020-06-15 21:35:40 -04:00
import { Client } from '../class' ;
let interval : NodeJS.Timeout ;
async function setupDepartmentCodes ( client : Client ) : Promise < void > {
2020-06-16 17:32:01 -04:00
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 ( ) ;
2020-06-15 21:35:40 -04:00
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 ( ) ;
}
}
2020-06-18 15:50:37 -04:00
function logNewPager ( client : Client , num : string , member : Member ) : void {
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-06-15 21:35:40 -04:00
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 ) ;
}
2020-06-16 12:50:07 -04:00
// await client.guilds.get(client.config.guildID).fetchAllMembers();
2020-06-15 21:35:40 -04:00
const { members } = client . guilds . get ( client . config . guildID ) ;
2020-06-17 19:11:11 -04:00
for ( const member of members . values ( ) ) {
const pager = await client . db . PagerNumber . findOne ( { individualAssignID : member.id } ) . lean ( ) . exec ( ) ;
if ( ! pager ) continue ;
if ( pager . num . startsWith ( '00' ) && ! member . roles . includes ( '662163685439045632' ) ) {
client . db . PagerNumber . deleteOne ( { num : pager.num } ) ;
client . util . signale . log ( ` Pager Number ' ${ pager . num } ' has been deleted. ` ) ;
} else if ( pager . num . startsWith ( '01' ) && ! member . roles . includes ( '701454855952138300' ) ) {
client . db . PagerNumber . deleteOne ( { num : pager.num } ) ;
client . util . signale . log ( ` Pager Number ' ${ pager . num } ' has been deleted. ` ) ;
} else if ( pager . num . startsWith ( '10' ) && ! member . roles . includes ( '701454780828221450' ) ) {
client . db . PagerNumber . deleteOne ( { num : pager.num } ) ;
client . util . signale . log ( ` Pager Number ' ${ pager . num } ' has been deleted. ` ) ;
} else if ( pager . num . startsWith ( '20' ) && ! member . roles . includes ( '455972169449734144' ) ) {
client . db . PagerNumber . deleteOne ( { num : pager.num } ) ;
client . util . signale . log ( ` Pager Number ' ${ pager . num } ' has been deleted. ` ) ;
} else if ( pager . num . startsWith ( '21' ) && ! member . roles . includes ( '453689940140883988' ) ) {
client . db . PagerNumber . deleteOne ( { num : pager.num } ) ;
client . util . signale . log ( ` Pager Number ' ${ pager . num } ' has been deleted. ` ) ;
} else if ( pager . num . startsWith ( '22' ) && ! member . roles . includes ( '701481967149121627' ) ) {
client . db . PagerNumber . deleteOne ( { num : pager.num } ) ;
client . util . signale . log ( ` Pager Number ' ${ pager . num } ' has been deleted. ` ) ;
}
}
2020-06-15 21:35:40 -04:00
// const takenPagers = new Set<string>();
2020-06-16 17:32:01 -04:00
for ( const member of members . values ( ) ) {
let pager = await client . db . PagerNumber . findOne ( { individualAssignID : member.id } ) . lean ( ) . exec ( ) ;
2020-06-15 21:35:40 -04:00
// 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 } ) ;
2020-06-15 23:01:03 -04:00
if ( check ) status = false ;
2020-06-15 21:35:40 -04:00
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 ] ,
} ) ;
2020-06-15 23:01:03 -04:00
if ( await client . db . PagerNumber . findOne ( { num : randomPagerNumber } ) ) return ;
2020-06-15 21:35:40 -04:00
pager = await newNumber . save ( ) ;
2020-06-18 15:50:37 -04:00
logNewPager ( client , randomPagerNumber , member ) ;
2020-06-15 21:35:40 -04:00
client . getDMChannel ( member . id ) . then ( ( chan ) = > {
chan . createMessage ( ` __**Pager Number Creation**__ \ nYour individual pager number has been automatically created. Your number (PN) is ${ randomPagerNumber } . ` ) ;
} ) ;
2020-06-16 17:32:01 -04:00
} else if ( ! pager && member . roles . includes ( '701454855952138300' ) ) {
// Supervisors
2020-06-15 21:35:40 -04:00
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 } ) ;
2020-06-18 15:50:37 -04:00
if ( check ) status = false ;
if ( check ? . num !== randomPagerNumber ) status = false ;
2020-06-15 21:35:40 -04:00
}
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 ] ,
} ) ;
2020-06-15 23:01:03 -04:00
if ( await client . db . PagerNumber . findOne ( { num : randomPagerNumber } ) ) return ;
2020-06-18 15:50:37 -04:00
logNewPager ( client , randomPagerNumber , member ) ;
2020-06-15 21:35:40 -04:00
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 } . ` ) ;
} ) ;
2020-06-16 17:32:01 -04:00
} else if ( ! pager && member . roles . includes ( '701454780828221450' ) ) {
// Technicians
2020-06-15 21:35:40 -04:00
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 } ) ;
2020-06-18 15:50:37 -04:00
if ( check ) status = false ;
if ( check ? . num !== randomPagerNumber ) status = false ;
2020-06-15 21:35:40 -04:00
}
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 ] ,
} ) ;
2020-06-15 23:01:03 -04:00
if ( await client . db . PagerNumber . findOne ( { num : randomPagerNumber } ) ) return ;
2020-06-18 15:50:37 -04:00
logNewPager ( client , randomPagerNumber , member ) ;
2020-06-15 21:35:40 -04:00
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 } . ` ) ;
} ) ;
2020-06-16 17:32:01 -04:00
} else if ( ! pager && member . roles . includes ( '455972169449734144' ) ) {
// Moderators
2020-06-15 21:35:40 -04:00
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 } ) ;
2020-06-18 15:50:37 -04:00
if ( check ) status = false ;
if ( check ? . num !== randomPagerNumber ) status = false ;
2020-06-15 21:35:40 -04:00
}
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 ] ,
} ) ;
2020-06-15 23:01:03 -04:00
if ( await client . db . PagerNumber . findOne ( { num : randomPagerNumber } ) ) return ;
2020-06-18 15:50:37 -04:00
logNewPager ( client , randomPagerNumber , member ) ;
2020-06-15 21:35:40 -04:00
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 } . ` ) ;
} ) ;
2020-06-16 17:32:01 -04:00
} else if ( ! pager && member . roles . includes ( '453689940140883988' ) ) {
// Core Team
2020-06-15 21:35:40 -04:00
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 } ) ;
2020-06-18 15:50:37 -04:00
if ( check ) status = false ;
if ( check ? . num !== randomPagerNumber ) status = false ;
2020-06-15 21:35:40 -04:00
}
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 ] ,
} ) ;
2020-06-15 23:01:03 -04:00
if ( await client . db . PagerNumber . findOne ( { num : randomPagerNumber } ) ) return ;
2020-06-18 15:50:37 -04:00
logNewPager ( client , randomPagerNumber , member ) ;
2020-06-15 21:35:40 -04:00
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 } . ` ) ;
} ) ;
2020-06-16 17:32:01 -04:00
} else if ( ! pager && member . roles . includes ( '701481967149121627' ) ) {
// Associates
2020-06-15 21:35:40 -04:00
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 } ) ;
2020-06-18 15:50:37 -04:00
if ( check ) status = false ;
if ( check ? . num !== randomPagerNumber ) status = false ;
2020-06-15 21:35:40 -04:00
}
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 ] ,
} ) ;
2020-06-15 23:01:03 -04:00
if ( await client . db . PagerNumber . findOne ( { num : randomPagerNumber } ) ) return ;
2020-06-18 15:50:37 -04:00
logNewPager ( client , randomPagerNumber , member ) ;
2020-06-15 21:35:40 -04:00
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 } . ` ) ;
} ) ;
}
2020-06-16 17:32:01 -04:00
}
2020-06-15 21:35:40 -04:00
// Associates
const associatePagers = await client . db . PagerNumber . findOne ( { num : '22' } ) ;
2020-06-16 17:32:01 -04:00
for ( const member of members . values ( ) ) {
2020-06-15 21:35:40 -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 } } ) ;
}
2020-06-16 17:32:01 -04:00
}
2020-06-15 21:35:40 -04:00
// Core Team
const coreTeamPagers = await client . db . PagerNumber . findOne ( { num : '21' } ) ;
2020-06-16 17:32:01 -04:00
for ( const member of members . values ( ) ) {
2020-06-15 21:35:40 -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 } } ) ;
}
2020-06-16 17:32:01 -04:00
}
2020-06-15 21:35:40 -04:00
// Moderator
const moderatorPagers = await client . db . PagerNumber . findOne ( { num : '20' } ) ;
2020-06-16 17:32:01 -04:00
for ( const member of members . values ( ) ) {
2020-06-15 21:35:40 -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 } } ) ;
}
2020-06-16 17:32:01 -04:00
}
2020-06-15 21:35:40 -04:00
// Technician
const technicianPagers = await client . db . PagerNumber . findOne ( { num : '10' } ) ;
2020-06-16 17:32:01 -04:00
for ( const member of members . values ( ) ) {
2020-06-15 21:35:40 -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 } } ) ;
}
2020-06-16 17:32:01 -04:00
}
2020-06-15 21:35:40 -04:00
// Supervisor
const supervisorPagers = await client . db . PagerNumber . findOne ( { num : '01' } ) ;
2020-06-16 17:32:01 -04:00
for ( const member of members . values ( ) ) {
2020-06-15 21:35:40 -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 } } ) ;
}
2020-06-16 17:32:01 -04:00
}
2020-06-15 21:35:40 -04:00
// Board of Directors
const directorPagers = await client . db . PagerNumber . findOne ( { num : '00' } ) ;
2020-06-16 17:32:01 -04:00
for ( const member of members . values ( ) ) {
2020-06-15 21:35:40 -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-06-16 17:32:01 -04:00
}
2020-06-18 15:50:37 -04:00
} , 300000 ) ;
2020-06-15 21:35:40 -04:00
return interval ;
}