2024-05-30 20:29:06 -04:00
const { literal , argument , string , greedyString , DynamicCommandExceptionType } = require ( 'brigadier-commands' )
2024-04-02 17:53:10 -04:00
const { createUuidSelector } = require ( '../util/command/utility' )
2024-09-30 20:56:05 -04:00
const UNABLE _TO _SEND _MAIL _ERROR = new DynamicCommandExceptionType ( error => new TextMessage ( [ { text : 'An unexpected error occurred trying to send that message' , hoverEvent : { action : 'show_text' , contents : error . stack } } ] ) )
2024-05-30 20:29:06 -04:00
2024-04-02 17:53:10 -04:00
module . exports = {
register ( dispatcher ) {
2024-07-31 22:34:19 -04:00
const listNode = literal ( 'list' ) . executes ( c => this . listCommand ( c ) ) . build ( )
2024-04-02 17:53:10 -04:00
const node = dispatcher . register (
literal ( 'mail' )
. then (
literal ( 'send' )
. then (
argument ( 'username' , string ( ) )
. then (
argument ( 'message' , greedyString ( ) )
2024-07-31 22:34:19 -04:00
. executes ( c => this . sendCommand ( c ) )
2024-04-02 17:53:10 -04:00
)
)
)
. then (
listNode
)
. then (
literal ( 'read' )
2024-07-31 22:34:19 -04:00
. executes ( c => this . listCommand ( c ) )
2024-04-02 17:53:10 -04:00
. redirect ( listNode )
)
. then (
literal ( 'clear' )
2024-07-31 22:34:19 -04:00
. executes ( c => this . clearCommand ( c ) )
2024-04-02 17:53:10 -04:00
)
)
node . description = 'Sends and receives mail from players'
node . permissionLevel = 0
} ,
2024-05-26 19:38:43 -04:00
async sendCommand ( context ) {
2024-04-02 17:53:10 -04:00
const source = context . source
const bot = source . bot
const player = source . getPlayerOrThrow ( )
const username = context . getArgument ( 'username' )
const message = context . getArgument ( 'message' )
2024-05-26 19:38:43 -04:00
try {
await bot . sendMail ( player . username , username , message )
} catch ( error ) {
2024-09-30 20:56:05 -04:00
bot . console . error ( error . stack )
2024-05-30 20:29:06 -04:00
throw UNABLE _TO _SEND _MAIL _ERROR . create ( error )
2024-05-26 19:38:43 -04:00
}
2024-04-02 17:53:10 -04:00
bot . tellraw ( [
{ text : 'Sent ' , ... bot . styles . primary } ,
{ text : message , ... bot . styles . secondary } ,
' to ' ,
{ text : username , ... bot . styles . secondary }
] , createUuidSelector ( player . uuid ) )
} ,
listCommand ( context ) {
const source = context . source
const bot = source . bot
const player = source . getPlayerOrThrow ( )
2024-05-26 19:38:43 -04:00
const playerData = bot . playerData [ player . uuid ]
2024-04-02 17:53:10 -04:00
2024-05-26 19:38:43 -04:00
const messages = playerData . data ? . mail
if ( ! messages || ! messages . length ) {
2024-04-02 17:53:10 -04:00
bot . tellraw ( { text : 'You have no mail' , ... bot . styles . primary } , createUuidSelector ( player . uuid ) )
return
}
const msg = [ { text : 'Mail:\n' , ... bot . styles . primary } ]
messages . forEach ( ( message ) => {
2024-05-26 19:38:43 -04:00
msg . push ( ` ${ message . sender } (from ${ message . host } : ${ message . port } ): ` )
2024-04-02 17:53:10 -04:00
msg . push ( { text : ` ${ message . message } \n ` , ... bot . styles . secondary } )
} )
msg [ msg . length - 1 ] . text = msg [ msg . length - 1 ] . text . slice ( 0 , - 1 )
bot . tellraw ( msg , createUuidSelector ( player . uuid ) )
2024-09-30 20:56:05 -04:00
delete playerData . data . mailUnread
2024-04-02 17:53:10 -04:00
} ,
clearCommand ( context ) {
const source = context . source
const bot = source . bot
const player = source . getPlayerOrThrow ( )
2024-09-08 23:43:54 -04:00
const playerData = bot . playerData [ player . uuid ]
2024-04-02 17:53:10 -04:00
2024-09-08 23:43:54 -04:00
delete playerData . data . mail
2024-04-02 17:53:10 -04:00
bot . tellraw ( { text : 'Your mail has been cleared' , ... bot . styles . primary } , createUuidSelector ( player . uuid ) )
2024-02-11 21:23:41 -05:00
}
}