import { Message , RichEmbed , Guild , GuildMember } from 'discord.js' ; import { commandInterface } from '../../commands' ; import { permLevels , getPermLevel } from '../../utils/permissions' ; import { Bot } from '../..' ; import { sendError } from '../../utils/messages' ; import { durationToString , permToString , stringToChannel , stringToMember , stringToRole } from '../../utils/parsers' ; import { caseActions , caseActionsArray , caseDoc } from '../../database/schemas' ; var command = { name : 'case' , path : '' , dm : false , permLevel : permLevels . mod , togglable : false , help : { shortDescription : 'Delete, list or view cases' , longDescription : 'Delete, list or view cases of a guild or member' , usages : [ '{command} list [member]' , '{command} view [caseID]' , '{command} delete [caseID]' , '{command} delete user [user]' , '{command} delete user [user] [type]' , '{command} delete [type]' ] , examples : [ '{command} list' , '{command} list @jeff#1234' , '{command} view 32' , '{command} delete 12' , '{command} delete user @jeff#1234' , '{command} delete user @jeff#1234 warn' , '{command} delete warn' ] , additionalFields : [ { name : 'Types:' , value : caseActionsArray . join ( ', ' ) } ] } , run : async ( message , args , permLevel , dm , requestTime ) => { try { if ( args . length === 0 ) { message . channel . send ( await Bot . commands . getHelpEmbed ( command , message . guild ) ) ; Bot . mStats . logMessageSend ( ) ; return false ; } let argIndex = 0 ; let argsArray = args . split ( ' ' ) . filter ( x => x . length != 0 ) ; if ( argsArray [ argIndex ] == 'list' ) { argIndex ++ ; if ( ! argsArray [ argIndex ] ) { let embed = await createTotalEmbed ( message . guild ) ; let detailEmbeds = await createDetailEmbeds ( message . guild ) ; Bot . mStats . logResponseTime ( command . name , requestTime ) ; message . channel . send ( embed ) ; for ( let i = 0 ; detailEmbeds . length > i ; i ++ ) { message . channel . send ( detailEmbeds [ i ] ) ; } Bot . mStats . logMessageSend ( ) ; Bot . mStats . logCommandUsage ( command . name ) ; return true ; } if ( argsArray [ argIndex ] ) { let caseMember = await stringToMember ( message . guild , argsArray [ argIndex ] ) ; if ( ! caseMember ) { message . channel . send ( `Cannot find user '${ argsArray [ argIndex ] . replace ( '@' , '' ) }'` ) ; Bot . mStats . logMessageSend ( ) ; return false ; } let totalEmbed = await createTotalEmbed ( message . guild , caseMember ) ; let detailEmbeds = await createDetailEmbeds ( message . guild , caseMember ) ; Bot . mStats . logResponseTime ( command . name , requestTime ) ; message . channel . send ( totalEmbed ) ; for ( let i = 0 ; detailEmbeds . length > i ; i ++ ) { message . channel . send ( detailEmbeds [ i ] ) ; } Bot . mStats . logMessageSend ( ) ; Bot . mStats . logCommandUsage ( command . name ) ; return true ; } } if ( argsArray [ argIndex ] == 'view' ) { argIndex ++ ; if ( ! argsArray [ argIndex ] || isNaN ( Number ( argsArray [ argIndex ] ) ) ) { message . channel . send ( 'Please provide a valid case ID' ) ; Bot . mStats . logMessageSend ( ) ; return false ; } let embed = await createSpecificEmbed ( message . guild , argsArray [ argIndex ] ) ; Bot . mStats . logResponseTime ( command . name , requestTime ) ; message . channel . send ( embed ) ; Bot . mStats . logMessageSend ( ) ; Bot . mStats . logCommandUsage ( command . name ) ; } if ( argsArray [ argIndex ] == 'delete' ) { argIndex ++ ; if ( ! isNaN ( parseInt ( argsArray [ argIndex ] ) ) ) { let caseID = parseInt ( argsArray [ argIndex ] ) ; if ( ! await Bot . caseLogger . deleteCase ( message . guild . id , caseID ) ) { message . channel . send ( 'Please provide a valid case ID' ) ; Bot . mStats . logMessageSend ( ) ; return false ; } Bot . mStats . logResponseTime ( command . name , requestTime ) ; message . channel . send ( `:white_check_mark: **Case ${ caseID } has been deleted**` ) ; Bot . mStats . logMessageSend ( ) ; Bot . mStats . logCommandUsage ( command . name , 'delete id' ) ; } else { let userID ; if ( argsArray [ argIndex ] == 'user' ) { argIndex ++ ; if ( ! argsArray [ argIndex ] ) { message . channel . send ( 'Please specify a user' ) ; Bot . mStats . logMessageSend ( ) ; return false ; } let member = await stringToMember ( message . guild , argsArray [ argIndex ] , false , false , false ) ; if ( member ) { userID = member . id ; } else { userID = argsArray [ argIndex ] ; } argIndex ++ ; } let type ; if ( argsArray [ argIndex ] ) { if ( ! caseActionsArray . includes ( argsArray [ argIndex ] ) ) { message . channel . send ( 'Invalid Type. Use one of the following:\n' + caseActionsArray . join ( ', ' ) ) ; Bot . mStats . logMessageSend ( ) ; return false ; } else { type = argsArray [ argIndex ] ; argIndex ++ ; } } else if ( ! userID ) { message . channel . send ( 'Please provide one of the following types:\n' + caseActionsArray . join ( ', ' ) ) ; Bot . mStats . logMessageSend ( ) ; return false ; } let query = { guild : message . guild . id } ; if ( userID ) query . user = userID ; if ( type ) query . action = type ; let result = await Bot . caseLogger . cases . deleteMany ( query ) . exec ( ) ; Bot . mStats . logResponseTime ( command . name , requestTime ) ; message . channel . send ( `:white_check_mark: **Successfully deleted ${ result . n } ${ type ? type + ' ' : '' }case${ result . n != 1 ? 's' : '' }${ userID ? ` from <@${ userID }>` : '' }**` ) ; Bot . mStats . logMessageSend ( ) ; Bot . mStats . logCommandUsage ( command . name , 'id' ) ; } return true ; } } catch ( e ) { sendError ( message . channel , e ) ; Bot . mStats . logError ( e , command . name ) ; return false ; } } } ; param { Guild } guild param { GuildMember } member returns async function createTotalEmbed ( guild , member ? ) { let subject ; let id ; let name ; let avatar ; let query ; let cases ; if ( member ) { subject = member ; id = member . id ; name = member . user . tag ; avatar = member . user . displayAvatarURL ; query = await Bot . caseLogger . findByMember ( guild . id , member . id ) ; cases = resolveTotalCases ( query ) ; } else { id = guild . id ; name = guild . name ; avatar = guild . iconURL ; query = await Bot . caseLogger . findByGuild ( guild . id ) ; cases = resolveTotalCases ( query ) ; subject = guild . name ; } let embed = new RichEmbed ( ) ; embed . setAuthor ( name , avatar ) ; embed . setColor ( Bot . database . settingsDB . cache . embedColors . default ) ; embed . setDescription ( `All cases for ${ subject }` ) ; embed . addField ( "Count" , `Total: ${ cases . total }\nWarn: ${ cases . warn }\n Mute: ${ cases . mute }\nKick: ${ cases . kick }\nSoftban: ${ cases . softban }\nBan: ${ cases . ban }\nUnmute: ${ cases . unmute }\nUnban: ${ cases . unban }` ) ; embed . setFooter ( `ID: ${ id }` ) ; embed . setTimestamp ( ) ; return embed ; } param { * } query returns function resolveTotalCases ( query ) { var caseResolved = { unmute : 0 , mute : 0 , unban : 0 , ban : 0 , kick : 0 , warn : 0 , softban : 0 , total : 0 } ; for ( let i = 0 ; query . length > i ; i ++ ) { caseResolved . total += 1 ; switch ( query [ i ] . action ) { case caseActions . ban : caseResolved . ban ++ ; break ; case caseActions . warn : caseResolved . warn ++ ; break ; case caseActions . mute : caseResolved . mute ++ ; break ; case caseActions . kick : caseResolved . kick ++ ; break ; case caseActions . softban : caseResolved . softban ++ ; break ; case caseActions . unmute : caseResolved . unmute ++ ; break ; case caseActions . unban : caseResolved . unban ++ ; break ; } } return caseResolved ; } param { Guild } guild param { GuildMember } member returns async function createDetailEmbeds ( guild , member ? ) { let cases ; if ( member ) { cases = await Bot . caseLogger . findByMember ( guild . id , member . id ) ; } else { cases = await Bot . caseLogger . findByGuild ( guild . id ) ; } let detailEmbedArray = [ ] ; let caseIndex = 0 ; let numOfCases = cases . length ; let tempCase ; let tempMod ; let tempMember ; let embed ; while ( ( cases . length - caseIndex ) > 0 ) { embed = new RichEmbed ( ) ; embed . setColor ( Bot . database . settingsDB . cache . embedColors . default ) ; for ( let i = 0 ; i < 10 && numOfCases > i ; i ++ ) { tempCase = cases [ caseIndex ] ; let date = new Date ( tempCase . timestamp ) ; tempMod = guild . members . get ( tempCase . mod ) ; if ( ! tempMod ) tempMod = cases [ caseIndex ] . mod ; if ( member ) { tempMember = member ; } else { tempMember = guild . members . get ( tempCase . user ) ; if ( ! tempMember ) tempMember = cases [ caseIndex ] . user ; } embed . addField ( `Case ${ tempCase . caseID } | ${ capitalizeFirstLetter ( tempCase . action ) } | ${ date . toDateString ( ) } ${ date . toTimeString ( ) . substr ( 0 , 8 ) }` , `**User:** ${ tempMember }\n**Mod:** ${ tempMod }\n**Reason:** ${ tempCase . reason }${ tempCase . duration ? `\n**Duration:** ${ durationToString ( tempCase . duration ) }` : '' }` ) ; caseIndex ++ ; } numOfCases -= 10 ; detailEmbedArray . push ( embed ) ; } return detailEmbedArray ; } param { Guild } guild param { string } caseID returns async function createSpecificEmbed ( guild , caseID ) { let tempCase = await Bot . caseLogger . findByCase ( guild . id , caseID ) ; if ( ! tempCase ) { return 'Case not found' ; } let embed = new RichEmbed ( ) ; let user ; user = await stringToMember ( guild , tempCase . user ) ; if ( ! user ) user = tempCase . user ; let mod ; mod = await stringToMember ( guild , tempCase . mod ) ; if ( ! mod ) mod = tempCase . mod ; let color = resolveColor ( tempCase . action ) ; let date = new Date ( tempCase . timestamp ) ; embed . setAuthor ( `Case ${ caseID } | ${ capitalizeFirstLetter ( tempCase . action ) } | ${ user instanceof GuildMember ? user . user . tag : user }` , user instanceof GuildMember ? user . user . displayAvatarURL : undefined ) ; embed . setTimestamp ( date ) ; embed . setColor ( color ) ; embed . addField ( "Mod: " , mod , true ) ; embed . addField ( "User: " , user , true ) ; if ( tempCase . duration ) embed . addField ( "Duration: " , durationToString ( tempCase . duration ) , true ) ; if ( tempCase . reason ) embed . addField ( "Reason: " , tempCase . reason ) ; return embed ; } param { * } string returns function capitalizeFirstLetter ( string ) { return string . charAt ( 0 ) . toUpperCase ( ) + string . slice ( 1 ) ; } param { string } action returns function resolveColor ( action ) { switch ( action ) { case caseActions . warn : return Bot . database . settingsDB . cache . embedColors . warn ; case caseActions . ban || caseActions . kick || caseActions . mute || caseActions . softban : return Bot . database . settingsDB . cache . embedColors . negative ; case caseActions . unmute || caseActions . unban : return Bot . database . settingsDB . cache . embedColors . positive ; default : return Bot . database . settingsDB . cache . embedColors . default ; } } export default command ;