chipmunkbot3/commands/mail.js

93 lines
2.9 KiB
JavaScript
Raw Normal View History

const { literal, argument, string, greedyString, DynamicCommandExceptionType } = require('brigadier-commands')
2024-04-02 17:53:10 -04:00
const { createUuidSelector } = require('../util/command/utility')
const UNABLE_TO_LOAD_PLAYER_DATA_ERROR = new DynamicCommandExceptionType(error => new TextMessage([{ text: 'An unexpected error occurred trying to send that message', hoverEvent: { action: 'show_text', contents: error.stack } }]))
2024-04-02 17:53:10 -04:00
module.exports = {
register (dispatcher) {
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())
.executes(c => this.sendCommand(c))
2024-04-02 17:53:10 -04:00
)
)
)
.then(
listNode
)
.then(
literal('read')
.executes(c => this.listCommand(c))
2024-04-02 17:53:10 -04:00
.redirect(listNode)
)
.then(
literal('clear')
.executes(c => this.clearCommand(c))
2024-04-02 17:53:10 -04:00
)
)
node.description = 'Sends and receives mail from players'
node.permissionLevel = 0
},
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')
try {
await bot.sendMail(player.username, username, message)
} catch (error) {
throw UNABLE_TO_SEND_MAIL_ERROR.create(error)
}
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()
const playerData = bot.playerData[player.uuid]
2024-04-02 17:53:10 -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) => {
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))
},
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))
2024-02-11 21:23:41 -05:00
}
}