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 ( ) )
. executes ( this . seenCommand )
)
)
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-04-02 17:53:10 -04:00
{ text : first , ... bot . styles . secondary } ,
2024-02-11 21:23:41 -05:00
' and last seen on ' ,
2024-04-02 17:53:10 -04:00
{ text : last , ... bot . styles . secondary }
] , false )
}
2024-02-11 21:23:41 -05:00
}