file description module import type { EmbedOptions , Guild , Role } from "eris" ; import { Logger } from "../classes/Logger" ; import { dateFormat } from "../utils/format" ; const TYPE = "eventLogging" ; export class GuildRoleUpdate extends Logger { events = [ "guildRoleCreate" , "guildRoleDelete" , "guildRoleUpdate" ] ; async run ( event , guild , role , oldrole ) { switch ( event ) { case "guildRoleCreate" : { if ( role . managed ) return ; const guildconfig = await this . bot . db . getGuildConfig ( guild . id ) ; const loggingChannel = await this . getChannel ( guild , TYPE , event , guildconfig ) ; if ( ! loggingChannel ) return ; const string = this . bot . localeSystem . getLocaleFunction ( guildconfig ?. guildLocale ? guildconfig ?. guildLocale : this . bot . config . defaultLocale ) ; const embed = { color : this . convertHex ( "general" ) , author : { icon_url : this . bot . user . dynamicAvatarURL ( ) , name : string ( "logger.ROLE_CREATED" , { role : role . name } ) , } , fields : [ { name : string ( "global.NAME" ) , value : `${ role . name || string ( "global.UNKNOWN" ) }` , inline : false , } , { name : string ( "global.ID" ) , value : role . id , inline : true , } , { name : string ( "global.CREATED" ) , value : `${ dateFormat ( role . createdAt , string ) || string ( "global.UNKNOWN" ) }` , inline : true , } , { name : string ( "global.PERMISSIONS" ) , value : `${ Object . keys ( role . json ) . map ( ( p ) => `\`${ p }\`` ) . join ( " " ) || string ( "global.UNKNOWN" ) }` , } , ] , } as EmbedOptions ; const logs = await guild . getAuditLogs ( 1 , null , 30 ) . catch ( ( ) => { } ) ; if ( logs ) { const log = logs ?. entries ?. [ 0 ] ; const user = logs ?. users ?. [ 0 ] ; if ( log && new Date ( ) . getTime ( ) - new Date ( parseInt ( log . id ) / 4194304 + 1420070400000 ) . getTime ( ) < 3000 ) { embed . author . name = string ( "logger.ROLE_CREATEDBY" , { user : this . tagUser ( user ) } ) ; embed . author . icon_url = user ?. dynamicAvatarURL ( ) ; } } this . bot . createMessage ( loggingChannel , { embed : embed } ) ; break ; } case "guildRoleDelete" : { if ( role . managed ) return ; const guildconfig = await this . bot . db . getGuildConfig ( guild . id ) ; const loggingChannel = await this . getChannel ( guild , TYPE , event , guildconfig ) ; if ( ! loggingChannel ) return ; const string = this . bot . localeSystem . getLocaleFunction ( guildconfig ?. guildLocale ? guildconfig ?. guildLocale : this . bot . config . defaultLocale ) ; const embed = { color : this . convertHex ( "error" ) , author : { icon_url : this . bot . user . dynamicAvatarURL ( ) , name : string ( "logger.ROLE_DELETED" , { role : role . name } ) , } , fields : [ { name : string ( "global.NAME" ) , value : `${ role . name || string ( "global.UNKNOWN" ) }` , inline : false , } , { name : string ( "global.ID" ) , value : role . id , inline : true , } , { name : string ( "global.CREATED" ) , value : `${ dateFormat ( role . createdAt , string ) || string ( "global.UNKNOWN" ) }` , inline : true , } , { name : string ( "global.PERMISSIONS" ) , value : `${ Object . keys ( role . json ) . map ( ( p ) => `\`${ p }\`` ) . join ( " " ) || string ( "global.UNKNOWN" ) }` , } , ] , } as EmbedOptions ; const logs = await guild . getAuditLogs ( 1 , null , 32 ) . catch ( ( ) => { } ) ; if ( logs ) { const log = logs ?. entries ?. [ 0 ] ; const user = logs ?. users ?. [ 0 ] ; if ( log && new Date ( ) . getTime ( ) - new Date ( parseInt ( log . id ) / 4194304 + 1420070400000 ) . getTime ( ) < 3000 ) { embed . author . name = string ( "logger.ROLE_DELETEDBY" , { user : this . tagUser ( user ) } ) ; embed . author . icon_url = user ?. dynamicAvatarURL ( ) ; } } this . bot . createMessage ( loggingChannel , { embed : embed } ) ; break ; } case "guildRoleUpdate" : { const guildconfig = await this . bot . db . getGuildConfig ( guild . id ) ; const loggingChannel = await this . getChannel ( guild , TYPE , event , guildconfig ) ; if ( ! loggingChannel ) return ; const string = this . bot . localeSystem . getLocaleFunction ( guildconfig ?. guildLocale ? guildconfig ?. guildLocale : this . bot . config . defaultLocale ) ; const embed = { color : this . convertHex ( "general" ) , fields : [ ] , author : { icon_url : this . bot . user . dynamicAvatarURL ( ) , name : string ( "logger.ROLE_EDITED" , { role : oldrole . name } ) , } , } as EmbedOptions ; if ( role . name !== oldrole . name ) { embed . fields . push ( { name : string ( "global.NAME" ) , value : `${ oldrole . name || string ( "global.NONE" ) } ➜ ${ role . name || string ( "global.NONE" ) }` , } ) ; } if ( role . color !== oldrole . color ) { embed . fields . push ( { name : string ( "global.COLOR" ) , value : `${ oldrole . color ? `${ oldrole . color . toString ( 16 ) }` : "000000" } ➜ ${ role . color ? `${ role . color . toString ( 16 ) }` : "000000" }` , } ) ; embed . color = role . color ; } if ( role . hoist && oldrole . hoist && role . hoist !== oldrole . hoist ) { embed . fields . push ( { name : string ( "logger.VISIBILITY" ) , value : role . hoist ? `${ string ( "logger.NOT_SHOWING_SEPARATELY" ) } ➜ ${ string ( "logger.SHOWING_SEPARATELY" ) }` : `${ string ( "logger.SHOWING_SEPARATELY" ) } ➜ ${ string ( "logger.NOT_SHOWING_SEPARATELY" ) }` , } ) ; } if ( role . mentionable !== oldrole . mentionable ) { embed . fields . push ( { name : string ( "logger.MENTIONABILITY" ) , value : role . mentionable ? `${ string ( "logger.UNMENTIONABLE" ) } ➜ ${ string ( "logger.MENTIONABLE" ) }` : `${ string ( "logger.MENTIONABLE" ) } ➜ ${ string ( "logger.UNMENTIONABLE" ) }` , } ) ; } if ( ! embed . fields . length ) return ; const logs = await guild . getAuditLogs ( 1 , null , 31 ) . catch ( ( ) => { } ) ; if ( logs ) { const log = logs ?. entries ?. [ 0 ] ; const user = logs ?. users ?. [ 0 ] ; if ( log && new Date ( ) . getTime ( ) - new Date ( parseInt ( log . id ) / 4194304 + 1420070400000 ) . getTime ( ) < 3000 ) { embed . author . name = string ( "logger.ROLE_EDITEDBY" , { user : this . tagUser ( user ) , role : oldrole . name } ) ; embed . author . icon_url = user . dynamicAvatarURL ( ) ; } } this . bot . createMessage ( loggingChannel , { embed : embed } ) ; break ; } } } }