91 lines
2.6 KiB
JavaScript
91 lines
2.6 KiB
JavaScript
const { literal, argument, string, greedyString } = require('brigadier-commands')
|
|
const { createUuidSelector } = require('../util/command/utility')
|
|
|
|
module.exports = {
|
|
register (dispatcher) {
|
|
const listNode = literal('list').executes(this.listCommand).build()
|
|
|
|
const node = dispatcher.register(
|
|
literal('mail')
|
|
.then(
|
|
literal('send')
|
|
.then(
|
|
argument('username', string())
|
|
.then(
|
|
argument('message', greedyString())
|
|
.executes(this.sendCommand)
|
|
)
|
|
)
|
|
)
|
|
.then(
|
|
listNode
|
|
)
|
|
.then(
|
|
literal('read')
|
|
.executes(this.listCommand)
|
|
.redirect(listNode)
|
|
)
|
|
.then(
|
|
literal('clear')
|
|
.executes(this.clearCommand)
|
|
)
|
|
)
|
|
|
|
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) {
|
|
source.sendError('Unable to send mail: ' + error)
|
|
return
|
|
}
|
|
|
|
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))
|
|
},
|
|
|
|
clearCommand (context) {
|
|
const source = context.source
|
|
const bot = source.bot
|
|
const player = source.getPlayerOrThrow()
|
|
|
|
delete bot.mail[player.username]
|
|
bot.tellraw({ text: 'Your mail has been cleared', ...bot.styles.primary }, createUuidSelector(player.uuid))
|
|
}
|
|
}
|