import mongoose = require ( 'mongoose' ) ; import { caseDoc , caseObject , caseSchema , guildDoc , guildSchema , caseActions } from './schemas' ; import { Guild , GuildMember , RichEmbed , User , } from 'discord.js' ; import { Bot } from '..' ; import { durationToString } from "../utils/parsers" ; export class export class CaseLogger { type { mongoose . Connection } memberof connection : mongoose . Connection ; type { mongoose . Model < caseDoc > } memberof guilds : mongoose . Model < guildDoc > ; type { mongoose . Model < caseDoc > } memberof cases : mongoose . Model < caseDoc > ; 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 ( 'caseLogger connected to /main database' ) ; Bot . caseLogger . guilds = mainCon . model ( 'guild' , guildSchema , 'guilds' ) ; Bot . caseLogger . cases = mainCon . model ( 'cases' , caseSchema , 'cases' ) ; } ) ; this . connection = mainCon ; } param { Guild } guild param { GuildMember } member param { GuildMember } mod param { string } reason async logKick ( guild , member , mod , reason ? ) { let color = Bot . database . settingsDB . cache . embedColors . negative ; return await this . logCase ( guild , member . user , mod , caseActions . kick , color , reason ) ; } param { Guild } guild param { User } user param { GuildMember } mod param { string } reason param { number } duration async logBan ( guild , user , mod , reason ? , duration ? ) { let color = Bot . database . settingsDB . cache . embedColors . negative ; return await this . logCase ( guild , user , mod , caseActions . ban , color , reason , duration ) ; } param { Guild } guild param { GuildMember } member param { GuildMember } mod param { string } reason async logSoftban ( guild , member , mod , reason ? ) { let color = Bot . database . settingsDB . cache . embedColors . negative ; return await this . logCase ( guild , member . user , mod , caseActions . softban , color , reason ) ; } param { Guild } guild param { GuildMember } user param { GuildMember } mod param { string } reason async logUnban ( guild , user , mod , reason ? ) { let color = Bot . database . settingsDB . cache . embedColors . positive ; return await this . logCase ( guild , user , mod , caseActions . unban , color , reason ) ; } param { Guild } guild param { GuildMember } member param { GuildMember } mod param { string } reason param { number } duration async logMute ( guild , member , mod , reason ? , duration ? ) { let color = Bot . database . settingsDB . cache . embedColors . negative ; return await this . logCase ( guild , member . user , mod , caseActions . mute , color , reason , duration ) ; } param { Guild } guild param { GuildMember } member param { GuildMember } mod param { string } reason async logUnmute ( guild , member , mod , reason ? ) { let color = Bot . database . settingsDB . cache . embedColors . positive ; return await this . logCase ( guild , member . user , mod , caseActions . unmute , color , reason ) ; } param { Guild } guild param { GuildMember } member param { GuildMember } mod param { string } reason async logWarn ( guild , member , mod , reason ) { let color = Bot . database . settingsDB . cache . embedColors . warn ; return await this . logCase ( guild , member . user , mod , caseActions . warn , color , reason ) ; } param guildID async findByGuild ( guildID ) { return await this . cases . find ( { guild : guildID } ) . exec ( ) ; } param guildID param caseID async findByCase ( guildID , caseID ) { return await this . cases . findOne ( { guild : guildID , caseID : Number ( caseID ) } ) . exec ( ) ; } param guildID param modID async findByMod ( guildID , modID ) { return await this . cases . find ( { guild : guildID , mod : modID } ) . exec ( ) ; } param guildID param userID async findByMember ( guildID , userID ) { return await this . cases . find ( { guild : guildID , user : userID } ) . exec ( ) ; } param guildID param caseID async deleteCase ( guildID , caseID ) { let cases = await this . cases . findOne ( { guild : guildID , caseID : caseID } ) . exec ( ) ; if ( cases ) { await this . cases . deleteOne ( { guild : guildID , caseID : caseID } ) . exec ( ) ; return true ; } return false ; } param { string } guildID param { string } caseID param { string } reason returns memberof async editReason ( guildID , caseID , reason ) { let success = false ; if ( ! isNaN ( Number ( caseID ) ) ) { let editCase = await this . cases . findOne ( { guild : guildID , caseID : Number ( caseID ) } ) . exec ( ) ; if ( editCase ) { editCase . reason = reason ; editCase . save ( ) ; success = true ; } } return success ; } param { Guild } guild param { GuildMember } user param { GuildMember } mod param action param color param { string } reason param { number } duration private async logCase ( guild , user , mod , action , color , reason ? , duration ? ) { let date = new Date ( ) ; let guildDoc = await this . guilds . findOne ( { guild : guild . id } ) . exec ( ) ; let caseChannel = guild . channels . get ( guildDoc . toObject ( ) . caseChannel ) ; let totalCases = guildDoc . totalCases ; if ( ! totalCases ) totalCases = 0 ; let caseObject = { guild : guild . id , caseID : totalCases + 1 , user : user . id , action : action , timestamp : date . getTime ( ) , duration : duration , mod : mod . id , reason : reason } ; let caseDoc = new this . cases ( caseObject ) ; await caseDoc . save ( ) ; guildDoc . totalCases = totalCases + 1 ; await guildDoc . save ( ) ; if ( caseChannel ) { let caseEmbed = this . createCaseEmbed ( user , mod , caseObject . caseID , action , color , duration , reason ) ; caseChannel . send ( caseEmbed ) ; Bot . mStats . logMessageSend ( ) ; } return caseObject ; } param { GuildMember } user param { GuildMember } mod param caseID param action param color param { number } duration param { string } reason createCaseEmbed ( user , mod , caseID , action , color , duration ? , reason ? ) { let date = new Date ( ) ; var embed = new RichEmbed ( ) ; embed . setAuthor ( `Case ${ caseID } | ${ action } | ${ user . tag }` , user . displayAvatarURL ) ; embed . setTimestamp ( date ) ; embed . setColor ( color ) ; embed . setFooter ( `User: ${ user . id } Mod: ${ mod . id }` ) ; embed . addField ( "Mod: " , mod , true ) ; embed . addField ( "User: " , user , true ) ; if ( duration ) embed . addField ( "Duration: " , durationToString ( duration ) , true ) ; if ( reason ) embed . addField ( "Reason: " , reason ) ; return embed ; } }