chipmunkbot3/commands/seen.js
2024-07-31 22:34:19 -04:00

49 lines
1.7 KiB
JavaScript

const { literal, argument, string, DynamicCommandExceptionType } = require('brigadier-commands')
const TextMessage = require('../util/command/text_message')
const NEVER_SEEN_ERROR = new DynamicCommandExceptionType(username => new TextMessage([username, ' was never seen']))
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 } }]))
module.exports = {
register (dispatcher) {
const node = dispatcher.register(
literal('seen')
.then(
argument('username', string())
.executes(c => this.seenCommand(c))
)
)
node.description = 'Shows when a player was first and last seen'
node.permissionLevel = 0
},
async seenCommand (context) {
const source = context.source
const bot = source.bot
const username = context.getArgument('username')
let playerData = Object.values(bot.playerData).find(playerData => playerData.data.username === username)
if (!playerData) {
try {
playerData = await bot.loadPlayerData(username)
} catch (error) {
throw UNABLE_TO_LOAD_PLAYER_DATA_ERROR.create(error)
}
}
if (!playerData.data?.seen) throw NEVER_SEEN_ERROR.create(username)
const { first, last } = playerData.data.seen
source.sendFeedback([
{ text: '', ...bot.styles.primary },
{ text: username, ...bot.styles.secondary },
' was first seen on ',
{ text: first, ...bot.styles.secondary },
' and last seen on ',
{ text: last, ...bot.styles.secondary }
], false)
}
}