chipmunkbot3/commands/seen.js

50 lines
1.7 KiB
JavaScript
Raw Normal View History

2024-04-02 17:53:10 -04:00
const { literal, argument, string, DynamicCommandExceptionType } = require('brigadier-commands')
const TextMessage = require('../util/command/text_message')
2024-04-02 17:53:10 -04:00
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 } }]))
2024-02-11 21:23:41 -05:00
2024-04-02 17:53:10 -04:00
module.exports = {
register (dispatcher) {
const node = dispatcher.register(
literal('seen')
.then(
argument('username', string())
.executes(this.seenCommand)
)
)
node.description = 'Shows when a player was first and last seen'
node.permissionLevel = 0
},
async seenCommand (context) {
2024-04-02 17:53:10 -04:00
const source = context.source
const bot = source.bot
const username = context.getArgument('username')
2024-02-11 21:23:41 -05:00
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
2024-04-02 17:53:10 -04:00
source.sendFeedback([
{ text: '', ...bot.styles.primary },
{ text: username, ...bot.styles.secondary },
2024-02-11 21:23:41 -05:00
' was first seen on ',
2024-04-02 17:53:10 -04:00
{ text: first, ...bot.styles.secondary },
2024-02-11 21:23:41 -05:00
' and last seen on ',
2024-04-02 17:53:10 -04:00
{ text: last, ...bot.styles.secondary }
], false)
}
2024-02-11 21:23:41 -05:00
}