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 { permToString , stringToMember } from '../../utils/parsers' ; import { getDayDiff , timeFormat } from '../../utils/time' ; import dateFormat = require ( 'dateformat' ) ; async function getJoinRank ( ID , guild ) { if ( ! guild . member ( ID ) ) return ; let arr = ( await guild . fetchMembers ( ) ) . members . array ( ) ; arr . sort ( ( a , b ) => a . joinedTimestamp - b . joinedTimestamp ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] . id == ID ) return i ; } } param { number } i returns function ordinalSuffixOf ( i ) { var j = i % 10 , k = i % 100 ; if ( j == 1 && k != 11 ) { return i + "st" ; } if ( j == 2 && k != 12 ) { return i + "nd" ; } if ( j == 3 && k != 13 ) { return i + "rd" ; } return i + "th" ; } function getPresenceColor ( member ) { switch ( member . user . presence . status ) { case "dnd" : return Bot . database . settingsDB . cache . embedColors . negative ; case "idle" : return Bot . database . settingsDB . cache . embedColors . warn ; case "offline" : return Bot . database . settingsDB . cache . embedColors . neutral ; case "online" : return Bot . database . settingsDB . cache . embedColors . positive ; } } param { GuildMember } member param { number } permLevel param { number } requesterPermLevel returns async function createMemberEmbed ( member , permLevel , requesterPermLevel ) { var date = new Date ( ) ; var roles = '' ; var roleArray = member . roles . array ( ) ; var roleCount = member . roles . array ( ) . length - 1 ; roleArray . shift ( ) ; for ( const role of roleArray . slice ( 0 , 40 ) ) { roles += role . toString ( ) + ' ' ; } if ( roleArray . length > 40 ) roles += `and ${ roleCount - 40 } more` ; if ( roles . length == 0 ) { roles = 'member has no roles' ; } var joinRank = ( await getJoinRank ( member . id , member . guild ) ) + 1 ; if ( joinRank == 1 ) { joinRank = 'oldest member' ; } else { joinRank = ordinalSuffixOf ( joinRank ) + ' oldest member' ; } let embed = { "embed" : { "description" : member . toString ( ) , "color" : getPresenceColor ( member ) , "timestamp" : date . toISOString ( ) , "footer" : { "text" : "ID: " + member . id } , "thumbnail" : { "url" : member . user . displayAvatarURL } , "author" : { "name" : `${ member . user . username } ${ member . nickname ? '(' + member . nickname + ')' : '' }` , "icon_url" : member . user . displayAvatarURL } , "fields" : [ { "name" : "Status" , "value" : member . user . presence . status , "inline" : true } , { "name" : "Join Rank" , "value" : joinRank , "inline" : true } , { "name" : "Joined" , "value" : dateFormat ( member . joinedAt , timeFormat ) + `\n (${ getDayDiff ( member . joinedTimestamp , date . getTime ( ) ) } days ago)` , "inline" : true } , { "name" : "Registered" , "value" : dateFormat ( member . user . createdAt , timeFormat ) + `\n (${ getDayDiff ( member . user . createdTimestamp , date . getTime ( ) ) } days ago)` , "inline" : true } , { "name" : "Join-Register Diff" , "value" : getDayDiff ( member . joinedTimestamp , member . user . createdTimestamp ) + ' days' , "inline" : true } , { "name" : "Title" , "value" : permToString ( permLevel ) , "inline" : true } , { "name" : `Roles [${ roleCount }]` , "value" : roles } ] } } ; if ( requesterPermLevel >= permLevels . mod ) { let caseDocs = await Bot . caseLogger . cases . find ( { user : member . id , guild : member . guild . id } , [ 'action' ] ) . exec ( ) ; let summary = { unmute : 0 , mute : 0 , unban : 0 , ban : 0 , kick : 0 , warn : 0 , softban : 0 } ; for ( const caseDoc of caseDocs ) summary [ caseDoc . action ] ++ ; embed . embed . fields . push ( { "name" : "Cases" , "value" : `Total: ${ caseDocs . length } | Warn: ${ summary . warn } | Mute: ${ summary . mute } | Kick: ${ summary . kick } | Softban: ${ summary . softban } | Ban: ${ summary . ban } | Unmute: ${ summary . unmute } | Unban: ${ summary . unban }` } ) ; } return embed ; } param { Message } message param { GuildMember } member param { number } permLevel param { number } requesterPermLevel param { [ number , number ] } requestTime async function sendMemberInfo ( message , member , permLevel , requesterPermLevel , requestTime ) { var embed = await createMemberEmbed ( member , permLevel , requesterPermLevel ) ; Bot . mStats . logResponseTime ( command . name , requestTime ) ; message . channel . send ( embed ) ; Bot . mStats . logCommandUsage ( command . name , 'self' ) ; Bot . mStats . logMessageSend ( ) ; } var command = { name : 'whois' , path : '' , dm : false , permLevel : permLevels . member , togglable : false , help : { shortDescription : 'Returns infos about a user' , longDescription : 'Returns infos about a user' , usages : [ '{command}' , '{command} [member]' ] , examples : [ '{command}' , '{command} @Bullet Bot#1234' ] } , run : async ( message , args , permLevel , dm , requestTime ) => { try { if ( args . length === 0 ) { await sendMemberInfo ( message , message . member , permLevel , permLevel , requestTime ) ; return ; } var member = await stringToMember ( message . guild , args ) ; if ( ! member ) { message . channel . send ( 'Couldn\'t member with that name/id' ) ; Bot . mStats . logMessageSend ( ) ; return false ; } await sendMemberInfo ( message , member , await getPermLevel ( member ) , permLevel , requestTime ) ; } catch ( e ) { sendError ( message . channel , e ) ; Bot . mStats . logError ( e , command . name ) ; } } } ; export default command ;