file description module import type { EmbedOptions , Guild , Invite , Member , TextChannel } from "eris" ; import { Logger } from "../classes/Logger" ; import { defaultAvatar } from "../utils/constants" ; import { dateFormat } from "../utils/format" ; const TYPE = "memberLogging" ; export class MemberUpdate extends Logger { events = [ "guildMemberAdd" , "guildMemberRemove" , "loggingMemberAdd" , "loggingMemberRemove" ] ; async run ( event , guild , member ) { function compareInvites ( current , saved ) { for ( let i = 0 ; i < current ?. length ; i ++ ) { if ( current [ i ] && saved [ i ] && current [ i ] ?. uses > saved [ i ] ?. uses ) return current [ i ] ; } return null ; } switch ( event ) { case "guildMemberAdd" : { if ( ! member ) return ; this . bot . emit ( "loggingMemberAdd" , guild , member ) ; const guildconfig = await this . bot . db . getGuildConfig ( guild . id ) ; const string = this . bot . localeSystem . getLocaleFunction ( guildconfig ?. guildLocale ? guildconfig ?. guildLocale : this . bot . config . defaultLocale ) ; const muted = await this . bot . db . getGuildMuteCache ( guild . id ) ; const mute = muted . find ( ( m ) => m . member === member . id && m . guild === guild . id ) ; if ( mute && guildconfig ?. mutedRole ) await member . addRole ( guildconfig . mutedRole , string ( "logger.JOINED_AFTER_MUTED" ) ) . catch ( ( ) => { } ) ; if ( ! guildconfig ?. leaveJoin ) return ; const leavejoinchannel = guild . channels . find ( ( c ) => c . id === guildconfig ?. leaveJoin ) as TextChannel ; if ( ! leavejoinchannel ) return ; let joinMessage = string ( "logger.JOIN_MESSAGE" , { guild : guild . name , member : member . user . username } ) ; let joinTitle = `🎉 ${ string ( "logger.NEW_MEMBER" ) }` ; let greetingFooter = string ( "logger.MEMBER_COUNT" , { guild : guild . name , members : guild . memberCount } ) ; if ( guildconfig ?. joinMessage && guildconfig ?. joinMessage ?. length < 256 ) { joinMessage = guildconfig . joinMessage ; joinMessage = joinMessage . replace ( "{member}" , `**${ member . user . username }**` ) ; joinMessage = joinMessage . replace ( "{membercount}" , `**${ guild . memberCount }**` ) ; joinMessage = joinMessage . replace ( "{servername}" , `**${ guild . name }**` ) ; } if ( guildconfig ?. joinTitle && guildconfig ?. joinTitle ?. length < 100 ) { joinTitle = guildconfig . joinTitle ; joinTitle = joinTitle . replace ( "{member}" , `${ member . user . username }` ) ; joinTitle = joinTitle . replace ( "{membercount}" , `${ guild . memberCount }` ) ; joinTitle = joinTitle . replace ( "{servername}" , `${ guild . name }` ) ; } if ( guildconfig ?. greetingFooter && guildconfig ?. greetingFooter ?. length < 64 ) { greetingFooter = guildconfig . greetingFooter ; greetingFooter = greetingFooter . replace ( "{member}" , `${ member . user . username }` ) ; greetingFooter = greetingFooter . replace ( "{membercount}" , `${ guild . memberCount }` ) ; greetingFooter = greetingFooter . replace ( "{servername}" , `${ guild . name }` ) ; } this . bot . createMessage ( leavejoinchannel . id , { embed : { title : joinTitle , description : joinMessage , color : this . convertHex ( "success" ) , footer : { text : greetingFooter , icon_url : this . bot . user . dynamicAvatarURL ( ) , } , } , } ) ; break ; } case "guildMemberRemove" : { if ( ! member ) return ; this . bot . emit ( "loggingMemberRemove" , guild , member ) ; const guildconfig = await this . bot . db . getGuildConfig ( guild . id ) ; if ( ! guildconfig ?. leaveJoin ) return ; const leavejoinchannel = guild . channels . find ( ( c ) => c . id === guildconfig ?. leaveJoin ) as TextChannel ; if ( ! leavejoinchannel ) return ; const string = this . bot . localeSystem . getLocaleFunction ( guildconfig ?. guildLocale ? guildconfig ?. guildLocale : this . bot . config . defaultLocale ) ; let leaveMessage = string ( "logger.LEAVE_MESSAGE" , { member : member . user . username } ) ; let leaveTitle = `👋 ${ string ( "logger.MEMBER_LEFT" ) } ` ; let greetingFooter = string ( "logger.MEMBER_COUNT" , { guild : guild . name , members : guild . memberCount } ) ; if ( guildconfig ?. leaveMessage && guildconfig ?. leaveMessage ?. length < 256 ) { leaveMessage = guildconfig . leaveMessage ; leaveMessage = leaveMessage . replace ( "{member}" , `**${ member . user . username }**` ) ; leaveMessage = leaveMessage . replace ( "{membercount}" , `**${ guild . memberCount }**` ) ; leaveMessage = leaveMessage . replace ( "{servername}" , `**${ guild . name }**` ) ; } if ( guildconfig ?. leaveTitle && guildconfig ?. leaveTitle ?. length < 100 ) { leaveTitle = guildconfig . leaveTitle ; leaveTitle = leaveTitle . replace ( "{member}" , `**${ member . user . username }**` ) ; leaveTitle = leaveTitle . replace ( "{membercount}" , `**${ guild . memberCount }**` ) ; leaveTitle = leaveTitle . replace ( "{servername}" , `**${ guild . name }**` ) ; } if ( guildconfig ?. greetingFooter && guildconfig ?. greetingFooter ?. length < 64 ) { greetingFooter = guildconfig . greetingFooter ; greetingFooter = greetingFooter . replace ( "{member}" , `${ member . user . username }` ) ; greetingFooter = greetingFooter . replace ( "{membercount}" , `${ guild . memberCount }` ) ; greetingFooter = greetingFooter . replace ( "{servername}" , `${ guild . name }` ) ; } this . bot . createMessage ( leavejoinchannel . id , { embed : { title : leaveTitle , description : leaveMessage , color : this . convertHex ( "error" ) , footer : { text : greetingFooter , icon_url : this . bot . user . dynamicAvatarURL ( ) , } , } , } ) ; break ; } case "loggingMemberAdd" : { const guildconfig = await this . bot . db . getGuildConfig ( guild . id ) ; const channel = await this . getChannel ( guild , TYPE , event , guildconfig ) ; if ( ! channel ) return ; const string = this . bot . localeSystem . getLocaleFunction ( guildconfig ?. guildLocale ? guildconfig ?. guildLocale : this . bot . config . defaultLocale ) ; const embed = { color : this . convertHex ( "success" ) , fields : [ { name : string ( "global.ID" ) , value : member . id , inline : false , } , { name : string ( "global.CREATED" ) , value : dateFormat ( member . user . createdAt , string ) , inline : true , } , { name : string ( "global.ACCOUNT_AGE" ) , value : `**${ Math . floor ( ( Date . now ( ) - member . user . createdAt ) / 86400000 ) }** ${ string ( "global.DAYS_OLD" ) }` , inline : true , } , ] , author : { name : `${ this . tagUser ( member . user ) } ${ string ( "global.JOINED" ) }` , icon_url : member . user . dynamicAvatarURL ( ) , } , thumbnail : { url : member . user ? member . user . dynamicAvatarURL ( null , 1024 ) : defaultAvatar , } , footer : { icon_url : guild . iconURL || defaultAvatar , text : string ( "logger.MEMBER_COUNT" , { guild : guild . name , members : guild . memberCount } ) , } , } as EmbedOptions ; if ( member . joinedAt ) { embed . fields . push ( { name : string ( "global.JOINED_ON" ) , value : dateFormat ( member . joinedAt , string ) , inline : true , } ) ; } setTimeout ( async ( ) => { if ( this . bot . config . inviteLogs && guildconfig ?. inviteOptOut !== true ) { const guildInvites = await guild . getInvites ( ) . catch ( ( ) => { } ) ; const cachedInvites = this . bot . inviteHandler . inviteCache [ guild . id ] ; if ( guildInvites && cachedInvites ) { const invite = compareInvites ( guildInvites , cachedInvites ) ; if ( ! invite ) { if ( guild . features . includes ( "VANITY_URL" ) ) { embed . fields . push ( { name : string ( "logger.INVITE_USED" ) , value : string ( "general.SERVER_FEATURE_VANITY" ) , inline : true , } ) ; } } if ( invite ) { embed . fields . push ( { name : string ( "logger.INVITE_USED" ) , value : `${ member . bot ? string ( "logger.INVITE_OAUTH" ) : invite . code || string ( "global.UNKNOWN" ) }` , inline : true , } ) ; if ( invite . uses ) { embed . fields . push ( { name : string ( "logger.INVITE_USES" ) , value : `${ invite . maxUses ? `${ invite . uses }/${ invite . maxUses }` : invite . uses }` , inline : true , } ) ; } if ( invite . channel ) { embed . fields . push ( { name : string ( "global.CHANNEL" ) , value : guild . channels . get ( invite . channel . id ) ? guild . channels . get ( invite . channel . id ) . mention : invite . channel . name , inline : true , } ) ; } if ( invite . inviter ) { embed . fields . push ( { name : string ( "utility.INVITER" ) , value : `${ this . tagUser ( invite . inviter ) } (${ invite . inviter . id })` , inline : false , } ) ; } } this . bot . inviteHandler . inviteCache [ guild . id ] = guildInvites ; } } this . bot . createMessage ( channel , { embed : embed } ) ; } , 5000 ) ; break ; } case "loggingMemberRemove" : { const guildconfig = await this . bot . db . getGuildConfig ( guild . id ) ; const channel = await this . getChannel ( guild , TYPE , event , guildconfig ) ; if ( ! channel ) return ; const string = this . bot . localeSystem . getLocaleFunction ( guildconfig ?. guildLocale ? guildconfig ?. guildLocale : this . bot . config . defaultLocale ) ; const embed = { color : this . convertHex ( "error" ) , author : { name : `${ this . tagUser ( member . user ) } ${ string ( "global.LEFT" ) }` , icon_url : member . user . dynamicAvatarURL ( ) , } , thumbnail : { url : member . user ? member . user . dynamicAvatarURL ( null , 1024 ) : defaultAvatar , } , fields : [ { name : string ( "global.ID" ) , value : member . id , } , { name : string ( "global.CREATED_AT" ) , value : dateFormat ( member . user . createdAt , string ) , } , { name : string ( "global.ACCOUNT_AGE" ) , value : `**${ Math . floor ( ( Date . now ( ) - member . user . createdAt ) / 86400000 ) }** ${ string ( "global.DAYS_OLD" ) }` , } , ] , footer : { icon_url : guild . iconURL || defaultAvatar , text : string ( "logger.MEMBER_COUNT" , { guild : guild . name , members : guild . memberCount } ) , } , } ; if ( member . joinedAt ) { embed . fields . push ( { name : string ( "global.JOINED_ON" ) , value : dateFormat ( member . joinedAt , string ) , } ) ; } await this . bot . createMessage ( channel , { embed : embed } ) ; break ; } } } }