chipmunkbot3/util/command_source.js
2024-02-16 22:59:09 -05:00

23 lines
783 B
JavaScript

const { exceptions: { SimpleCommandExceptionType }, LiteralMessage } = require('brigadier-commands')
const COMMAND_MUST_BE_EXECUTED_BY_A_PLAYER_EXCEPTION = new SimpleCommandExceptionType(new LiteralMessage('Command must be executed by a player')) // TODO: Translations
class CommandSource {
constructor ({ bot, player = null, permissionLevel = 0, sendFeedback = () => {} } = {}) {
this.bot = bot
this.player = player
this.permissionLevel = permissionLevel
this.sendFeedback = sendFeedback
}
sendError (error) {
this.sendFeedback([{ text: '', color: 'red' }, error], false)
}
getPlayerOrThrow () {
if (this.player == null) throw COMMAND_MUST_BE_EXECUTED_BY_A_PLAYER_EXCEPTION.create()
return this.player
}
}
module.exports = CommandSource