import mongoose = require ( 'mongoose' ) ; import { logDoc , logSchema , guildDoc , guildSchema , logObject , logActions , webhookDoc , logTypes , logMegalog } from './schemas' ; import { Guild , Role , User , GuildMember , Message , Channel , TextChannel , } from 'discord.js' ; import { commandInterface } from '../commands' ; import { Bot } from '..' ; import { filterInterface } from '../filters' ; import { filterAction , filterActions } from '../utils/filters' ; import { actionToString } from '../utils/parsers' ; import { youtube } from '../bot-config.json' ; export class export class Logger { type { mongoose . Connection } memberof connection : mongoose . Connection ; type { mongoose . Model < guildDoc > } memberof guilds : mongoose . Model < guildDoc > ; type { mongoose . Model < logDoc > } memberof logs : mongoose . Model < logDoc > ; param { { url : string , suffix : string } } clusterInfo memberof constructor ( clusterInfo ) { var mainCon = mongoose . createConnection ( clusterInfo . url + '/main' + clusterInfo . suffix , { useNewUrlParser : true } ) ; mainCon . on ( 'error' , error => { console . error ( 'connection error:' , error ) ; Bot . mStats . logError ( error ) ; } ) ; mainCon . once ( 'open' , function ( ) { console . log ( 'logger connected to /main database' ) ; Bot . logger . guilds = mainCon . model ( 'guild' , guildSchema , 'guilds' ) ; Bot . logger . logs = mainCon . model ( 'log' , logSchema , 'logs' ) ; } ) ; this . connection = mainCon ; } param { Guild } guild param { GuildMember } mod param { ( 0 | 1 ) } type param { ( 'admins' | 'mods' | 'immune' ) } rank param { Role } role param { User } user returns memberof async logStaff ( guild , mod , type , rank , role ? , user ? ) { var date = new Date ( ) ; var guildDoc = await this . guilds . findOne ( { guild : guild . id } ) . exec ( ) ; if ( ! guildDoc ) return ; var logObject = { guild : guild . id , action : logActions . staff , mod : mod . id , timestamp : date . getTime ( ) , info : { type : type , rank : rank , role : role ? role . id : undefined , user : user ? user . id : undefined } } ; var logDoc = new this . logs ( logObject ) ; await logDoc . save ( ) ; guildDoc . logs . push ( logDoc . id ) ; guildDoc . save ( ) ; Bot . mStats . logLog ( ) ; var logChannel = guild . channels . get ( guildDoc . toObject ( ) . logChannel ) ; if ( ! logChannel ) return ; var rankName = rank . charAt ( 0 ) . toUpperCase ( ) + rank . slice ( 1 ) ; var embed = { "embed" : { "description" : `${ role ? 'Role ' + role . toString ( ) : 'User' + user . toString ( ) } was ${ type ? 'removed from' : 'added to' } ${ rank } rank by ${ mod . toString ( ) }` , "color" : Bot . database . settingsDB . cache . embedColors . default , "timestamp" : date . toISOString ( ) , "author" : { "name" : rankName + " Rank:" } } } ; if ( user ) { embed . embed . thumbnail = { url : user . displayAvatarURL } ; } Bot . mStats . logMessageSend ( ) ; logChannel . send ( embed ) ; } param { Guild } guild param { GuildMember } mod param { string } service param { webhookDoc } webhookDoc param { ( 0 | 1 | 2 ) } type param { boolean } changedChannel param { boolean } changedMessage returns memberof async logWebhook ( guild , mod , service , webhookDoc , type , changedChannel ? , changedMessage ? ) { var date = new Date ( ) ; var guildDoc = await this . guilds . findOne ( { guild : guild . id } ) . exec ( ) ; if ( ! guildDoc ) return ; var logObject = { guild : guild . id , mod : mod . id , action : logActions . webhook , timestamp : date . getTime ( ) , info : { type : type , service : service , webhookID : webhookDoc . id , changedChannel : changedChannel , changedMessage : changedMessage } } ; var logDoc = new this . logs ( logObject ) ; await logDoc . save ( ) ; guildDoc . logs . push ( logDoc . id ) ; guildDoc . save ( ) ; Bot . mStats . logLog ( ) ; var logChannel = guild . channels . get ( guildDoc . toObject ( ) . logChannel ) ; if ( ! logChannel ) return ; Bot . mStats . logMessageSend ( ) ; var color = 0 ; var logo = '' ; var name = '' ; switch ( service ) { case 'youtube' : color = youtube . color ; logo = youtube . logo ; name = youtube . name ; break ; } var action = '' ; switch ( type ) { case logTypes . add : action = 'Created' ; break ; case logTypes . remove : action = 'Deleted' ; break ; case logTypes . change : action = 'Changed' ; break ; } logChannel . send ( { "embed" : { "description" : `${ mod . toString ( ) } ${ action . toLowerCase ( ) } a webhook` , "color" : color , "timestamp" : date . toISOString ( ) , "author" : { "name" : `${ name } Webhook ${ action }` , "icon_url" : logo } , "fields" : [ { "name" : "Feed" , "value" : 'https://youtube.com/channel/' + webhookDoc . toObject ( ) . feed } , { "name" : ( changedChannel ? 'New ' : '' ) + "Channel" , "value" : guild . channels . get ( webhookDoc . toObject ( ) . channel ) . toString ( ) , "inline" : true } , { "name" : ( changedMessage ? 'New ' : '' ) + "Message:" , "value" : webhookDoc . toObject ( ) . message , "inline" : true } ] } } ) ; } param { Message } message param { filterInterface } filter param { string } reason param { filterAction [ ] } actions returns memberof async logFilterCatch ( message , filter , reason , actions ) { var date = new Date ( ) ; var guildDoc = await this . guilds . findOne ( { guild : message . guild . id } ) . exec ( ) ; if ( ! guildDoc ) return ; var logChannel = message . guild . channels . get ( guildDoc . toObject ( ) . logChannel ) ; if ( ! logChannel ) return ; Bot . mStats . logMessageSend ( ) ; var actionsString = actionToString ( actions [ 0 ] ) ; var deleted = false ; if ( actions [ 0 ] . type == filterActions . delete ) deleted = true ; for ( var i = 1 ; i < actions . length ; i ++ ) { if ( actions [ i ] . type == filterActions . delete ) deleted = true ; actionsString += "\n" + actionToString ( actions [ i ] ) ; } var content = message . content ; if ( ! deleted ) { content = `[Jump to Message](${ message . url })\n` + content ; } logChannel . send ( { "embed" : { "description" : filter . shortHelp , "color" : Bot . database . settingsDB . cache . embedColors . default , "timestamp" : message . createdAt . toISOString ( ) , "thumbnail" : { "url" : message . member . user . displayAvatarURL } , "author" : { "name" : "Filter: " + filter . name } , "fields" : [ { "name" : "From:" , "value" : message . author . toString ( ) + " (" + message . author . id + ")" , "inline" : true } , { "name" : "Reason:" , "value" : reason , "inline" : true } , { "name" : "Message:" , "value" : content } , { "name" : "Actions" , "value" : actionsString } ] } } ) ; } param { Guild } guild param { GuildMember } mod param { filterInterface } filter param { ( 0 | 1 ) } type returns memberof async logFilter ( guild , mod , filter , type ) { var date = new Date ( ) ; var guildDoc = await this . guilds . findOne ( { guild : guild . id } ) . exec ( ) ; if ( ! guildDoc ) return ; var logObject = { guild : guild . id , mod : mod . id , action : logActions . filter , timestamp : date . getTime ( ) , info : { type : type , filter : filter . name } } ; var logDoc = new this . logs ( logObject ) ; await logDoc . save ( ) ; guildDoc . logs . push ( logDoc . id ) ; guildDoc . save ( ) ; Bot . mStats . logLog ( ) ; var logChannel = guild . channels . get ( guildDoc . toObject ( ) . logChannel ) ; if ( ! logChannel ) return ; Bot . mStats . logMessageSend ( ) ; logChannel . send ( { 'embed' : { 'description' : `Filter \`${ filter . name }\` was  ${ type ? 'disabled' : 'enabled' } by ${ mod . toString ( ) }` , 'color' : Bot . database . settingsDB . cache . embedColors . default , 'timestamp' : date . toISOString ( ) , 'author' : { 'name' : 'Filter Change:' } , 'fields' : [ { 'name' : 'Description:' , 'value' : filter . shortHelp } , { 'name' : `${ type ? 'Enable' : 'Disable' } Filter:` , 'value' : `${ await Bot . database . getPrefix ( guild ) }filters ${ type ? 'enable' : 'disable' } ${ filter . name }` } ] } } ) ; } param { Guild } guild param { GuildMember } mod param { commandInterface } command param { ( 0 | 1 ) } type returns memberof async logCommand ( guild , mod , command , type ) { var date = new Date ( ) ; var guildDoc = await this . guilds . findOne ( { guild : guild . id } ) . exec ( ) ; if ( ! guildDoc ) return ; var logObject = { guild : guild . id , mod : mod . id , action : logActions . command , timestamp : date . getTime ( ) , info : { type : type , command : command . name } } ; var logDoc = new this . logs ( logObject ) ; await logDoc . save ( ) ; guildDoc . logs . push ( logDoc . id ) ; guildDoc . save ( ) ; Bot . mStats . logLog ( ) ; var logChannel = guild . channels . get ( guildDoc . toObject ( ) . logChannel ) ; if ( ! logChannel ) return ; Bot . mStats . logMessageSend ( ) ; logChannel . send ( { 'embed' : { 'description' : `Command \`${ command . name }\` was  ${ type ? 'disabled' : 'enabled' } by ${ mod . toString ( ) }` , 'color' : Bot . database . settingsDB . cache . embedColors . default , 'timestamp' : date . toISOString ( ) , 'author' : { 'name' : 'Command Change:' , 'icon_url' : Bot . client . user . displayAvatarURL } , 'fields' : [ { 'name' : 'Description:' , 'value' : command . help . shortDescription } , { 'name' : `${ type ? 'Re-enable' : 'Disable' } Command:` , 'value' : `${ await Bot . database . getPrefix ( guild ) }commands ${ type ? 'enable' : 'disable' } ${ command . name }` } ] } } ) ; } param { Guild } guild param { GuildMember } mod param { string } oldPrefix param { string } newPrefix returns memberof async logPrefix ( guild , mod , oldPrefix , newPrefix ) { var date = new Date ( ) ; var guildDoc = await this . guilds . findOne ( { guild : guild . id } ) . exec ( ) ; if ( ! guildDoc ) return ; var logObject = { guild : guild . id , mod : mod . id , action : logActions . prefix , timestamp : date . getTime ( ) , info : { old : oldPrefix , new : newPrefix } } ; var logDoc = new this . logs ( logObject ) ; await logDoc . save ( ) ; guildDoc . logs . push ( logDoc . id ) ; guildDoc . save ( ) ; Bot . mStats . logLog ( ) ; var logChannel = guild . channels . get ( guildDoc . toObject ( ) . logChannel ) ; if ( ! logChannel ) return ; Bot . mStats . logMessageSend ( ) ; logChannel . send ( { 'embed' : { 'description' : `The prefix was changed by ${ mod . toString ( ) }` , 'color' : Bot . database . settingsDB . cache . embedColors . default , 'timestamp' : date . toISOString ( ) , 'author' : { 'name' : 'Changed Prefix:' } , 'fields' : [ { 'name' : 'New:' , 'value' : newPrefix , 'inline' : true } , { 'name' : 'Old:' , 'value' : oldPrefix , 'inline' : true } , { 'name' : 'Reset command:' , 'value' : Bot . database . settingsDB . cache . prefix + 'prefix reset' } ] } } ) ; } param { Guild } guild param { GuildMember } admin param { logTypes } type param { string [ ] } functions param { Channel } channel returns memberof async logMegalog ( guild , admin , type , functions , channel ? ) { var date = new Date ( ) ; var guildDoc = await this . guilds . findOne ( { guild : guild . id } ) . exec ( ) ; if ( ! guildDoc ) return ; var logObject = { guild : guild . id , mod : admin . id , action : logActions . megalog , timestamp : date . getTime ( ) , info : { type : type , functions : functions , channel : channel ? channel . id : undefined } } ; var logDoc = new this . logs ( logObject ) ; await logDoc . save ( ) ; guildDoc . logs . push ( logDoc . id ) ; guildDoc . save ( ) ; Bot . mStats . logLog ( ) ; let functionLength = functions . length ; var logChannel = guild . channels . get ( guildDoc . toObject ( ) . logChannel ) ; if ( ! logChannel ) return ; Bot . mStats . logMessageSend ( ) ; logChannel . send ( { 'embed' : { 'description' : `${ functionLength } ${ functionLength > 1 ? "functions were" : "function was" } ${ type . valueOf ( ) ? 'disabled' : 'enabled' } by ${ admin . toString ( ) }` , 'color' : type . valueOf ( ) ? Bot . database . settingsDB . cache . embedColors . negative : Bot . database . settingsDB . cache . embedColors . positive , 'timestamp' : date . toISOString ( ) , 'author' : { 'name' : 'Megalog Logging Change' , 'icon_url' : guild . iconURL } , 'fields' : [ { 'name' : `Functions disabled/enabled` , 'value' : functions . join ( '\n' ) } ] } } ) ; } param { Guild } guild param { GuildMember } admin param { ( logTypes . add | logTypes . remove ) } type param { TextChannel } channel returns memberof async logMegalogIgnore ( guild , admin , type , channel ) { var date = new Date ( ) ; var guildDoc = await this . guilds . findOne ( { guild : guild . id } ) . exec ( ) ; if ( ! guildDoc ) return ; var logObject = { guild : guild . id , mod : admin . id , action : logActions . megalogIgnore , timestamp : date . getTime ( ) , info : { type : type , channel : channel . id } } ; var logDoc = new this . logs ( logObject ) ; await logDoc . save ( ) ; guildDoc . logs . push ( logDoc . id ) ; guildDoc . save ( ) ; Bot . mStats . logLog ( ) ; var logChannel = guild . channels . get ( guildDoc . toObject ( ) . logChannel ) ; if ( ! logChannel ) return ; Bot . mStats . logMessageSend ( ) ; logChannel . send ( { 'embed' : { 'description' : `${ channel } has been ${ type . valueOf ( ) ? 'removed from' : 'added to' } ignored channels by ${ admin }` , 'color' : type . valueOf ( ) ? Bot . database . settingsDB . cache . embedColors . negative : Bot . database . settingsDB . cache . embedColors . positive , 'timestamp' : date . toISOString ( ) , 'author' : { 'name' : 'Megalog Logging Change' , 'icon_url' : guild . iconURL } } } ) ; } }