2024-04-02 17:53:10 -04:00
const { literal , argument , string , DynamicCommandExceptionType } = require ( 'brigadier-commands' )
const TextMessage = require ( '../util/command/text_message' )
2024-05-26 19:38:43 -04:00
2024-04-02 17:53:10 -04:00
const NEVER _SEEN _ERROR = new DynamicCommandExceptionType ( username => new TextMessage ( [ username , ' was never seen' ] ) )
2024-05-30 20:29:06 -04:00
const UNABLE _TO _LOAD _PLAYER _DATA _ERROR = new DynamicCommandExceptionType ( error => new TextMessage ( [ { text : 'An unexpected error occurred trying to load that player\'s data' , hoverEvent : { action : 'show_text' , contents : error . stack } } ] ) )
2024-02-11 21:23:41 -05:00
2024-04-02 17:53:10 -04:00
module . exports = {
register ( dispatcher ) {
const node = dispatcher . register (
literal ( 'seen' )
. then (
argument ( 'username' , string ( ) )
2024-07-31 22:34:19 -04:00
. executes ( c => this . seenCommand ( c ) )
2024-04-02 17:53:10 -04:00
)
)
node . description = 'Shows when a player was first and last seen'
node . permissionLevel = 0
} ,
2024-05-26 19:38:43 -04:00
async seenCommand ( context ) {
2024-04-02 17:53:10 -04:00
const source = context . source
const bot = source . bot
const username = context . getArgument ( 'username' )
2024-02-11 21:23:41 -05:00
2024-05-26 19:38:43 -04:00
let playerData = Object . values ( bot . playerData ) . find ( playerData => playerData . data . username === username )
if ( ! playerData ) {
try {
playerData = await bot . loadPlayerData ( username )
} catch ( error ) {
2024-05-30 20:29:06 -04:00
throw UNABLE _TO _LOAD _PLAYER _DATA _ERROR . create ( error )
2024-05-26 19:38:43 -04:00
}
}
2024-05-30 20:29:06 -04:00
if ( ! playerData . data ? . seen ) throw NEVER _SEEN _ERROR . create ( username )
2024-05-26 19:38:43 -04:00
const { first , last } = playerData . data . seen
2024-04-02 17:53:10 -04:00
source . sendFeedback ( [
{ text : '' , ... bot . styles . primary } ,
{ text : username , ... bot . styles . secondary } ,
2024-02-11 21:23:41 -05:00
' was first seen on ' ,
2024-11-07 00:09:40 -05:00
{ text : first . date + '' , ... bot . styles . secondary } ,
' at ' ,
{ ... this . formatServer ( first ) , ... bot . styles . secondary } ,
2024-02-11 21:23:41 -05:00
' and last seen on ' ,
2024-11-07 00:09:40 -05:00
{ text : last . date + '' , ... bot . styles . secondary } ,
' at ' ,
{ ... this . formatServer ( last ) , ... bot . styles . secondary }
2024-04-02 17:53:10 -04:00
] , false )
2024-11-07 00:09:40 -05:00
} ,
formatServer ( value ) {
if ( ! value . host ) return { text : 'unknown server' , italic : true }
let text = value . host
if ( value . port && value . port !== 25565 ) text += ':' + value . port
return { text }
2024-04-02 17:53:10 -04:00
}
2024-02-11 21:23:41 -05:00
}