import { GuildMember , Role , Snowflake } from 'discord.js' ; import ms from 'ms' ; import { Maybe } from '../common/types' ; import { GuildService } from './guild.service' ; export class UserService { public static readonly AGE_THRESHOLD = ms ( '2d' ) ; private _STRIP_NON_NUMERIC : RegExp = /^\d/g ; private _persistedRoles : Record < Snowflake , Role [ ] > = { } ; constructor ( private _guildService ) { } /**
     * Finds a user by tag, id, or username
     */ public getMember ( target ) { const strippedID = target . replace ( this . _STRIP_NON_NUMERIC , '' ) ; return this . _guildService . get ( ) . members . cache . filter ( ( member ) => { const { nickname } = member ; const { tag , username , id } = member . user ; return [ nickname , tag , username , id ] . some ( ( t ) => t === target || t === strippedID ) ; } ) . first ( ) ; } public shouldUnverify ( member ) { const creationDate = member . user . createdTimestamp ; const accountAge = Date . now ( ) - creationDate ; return accountAge <= UserService . AGE_THRESHOLD ; } public hasRole ( member , roleName ) { if ( typeof roleName === 'string' ) { const roleNameLower = roleName . toLowerCase ( ) ; return member . roles . cache . filter ( ( r ) => r . name . toLowerCase ( ) === roleNameLower ) . size !== 0 ; } return member . roles . cache . filter ( ( r ) => r === roleName ) . size !== 0 ; } public getPersistedRoles ( id ) { return this . _persistedRoles [ id ] ; } public setPersistedRoles ( id , roles ) { this . _persistedRoles [ id ] = roles ; } }