96 lines
3 KiB
JavaScript
96 lines
3 KiB
JavaScript
const { literal, argument, string, greedyString, DynamicCommandExceptionType } = require('brigadier-commands')
|
|
const { createUuidSelector } = require('../util/command/utility')
|
|
|
|
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 } }]))
|
|
|
|
module.exports = {
|
|
register (dispatcher) {
|
|
const listNode = literal('list').executes(c => this.listCommand(c)).build()
|
|
|
|
const node = dispatcher.register(
|
|
literal('mail')
|
|
.then(
|
|
literal('send')
|
|
.then(
|
|
argument('username', string())
|
|
.then(
|
|
argument('message', greedyString())
|
|
.executes(c => this.sendCommand(c))
|
|
)
|
|
)
|
|
)
|
|
.then(
|
|
listNode
|
|
)
|
|
.then(
|
|
literal('read')
|
|
.executes(c => this.listCommand(c))
|
|
.redirect(listNode)
|
|
)
|
|
.then(
|
|
literal('clear')
|
|
.executes(c => this.clearCommand(c))
|
|
)
|
|
)
|
|
|
|
node.description = 'Sends and receives mail from players'
|
|
node.permissionLevel = 0
|
|
},
|
|
|
|
async sendCommand (context) {
|
|
const source = context.source
|
|
const bot = source.bot
|
|
const player = source.getPlayerOrThrow()
|
|
|
|
const username = context.getArgument('username')
|
|
const message = context.getArgument('message')
|
|
|
|
try {
|
|
await bot.sendMail(player.username, username, message)
|
|
} catch (error) {
|
|
bot.console.error(error.stack)
|
|
throw UNABLE_TO_SEND_MAIL_ERROR.create(error)
|
|
}
|
|
|
|
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()
|
|
const playerData = bot.playerData[player.uuid]
|
|
|
|
const messages = playerData.data?.mail
|
|
if (!messages || !messages.length) {
|
|
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) => {
|
|
msg.push(`${message.sender} (from ${message.host}:${message.port}): `)
|
|
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))
|
|
|
|
delete playerData.data.mailUnread
|
|
},
|
|
|
|
clearCommand (context) {
|
|
const source = context.source
|
|
const bot = source.bot
|
|
const player = source.getPlayerOrThrow()
|
|
const playerData = bot.playerData[player.uuid]
|
|
|
|
delete playerData.data.mail
|
|
bot.tellraw({ text: 'Your mail has been cleared', ...bot.styles.primary }, createUuidSelector(player.uuid))
|
|
}
|
|
}
|