chipmunkbot3/util/command/command_source.js

24 lines
830 B
JavaScript

const { 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 = () => {}, displayName = { text: '' } } = {}) {
this.bot = bot
this.player = player
this.permissionLevel = permissionLevel
this.sendFeedback = sendFeedback
this.displayName = displayName
}
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