2024-02-11 21:23:41 -05:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const util = require('util')
|
2024-02-13 19:37:06 -05:00
|
|
|
const { CommandDispatcher, builder: { LiteralArgumentBuilder: { literal }, RequiredArgumentBuilder: { argument } }, arguments: { StringArgumentType: { greedyString } }, exceptions: { CommandSyntaxException } } = require('brigadier-commands')
|
|
|
|
const CommandSource = require('../util/command_source')
|
|
|
|
const TextMessage = require('../util/text_message')
|
2024-02-11 21:23:41 -05:00
|
|
|
|
|
|
|
function inject (bot) {
|
|
|
|
bot.commands = {
|
2024-02-13 19:37:06 -05:00
|
|
|
dispatcher: new CommandDispatcher(),
|
2024-02-11 21:23:41 -05:00
|
|
|
add,
|
|
|
|
execute,
|
|
|
|
info,
|
|
|
|
isCommand,
|
|
|
|
loadFromDir,
|
|
|
|
isValid
|
|
|
|
}
|
|
|
|
|
|
|
|
bot.on('message', (player, message) => {
|
2024-02-13 19:37:06 -05:00
|
|
|
if (!message.startsWith(bot.prefix)) return
|
2024-02-11 21:23:41 -05:00
|
|
|
|
2024-02-16 21:37:11 -05:00
|
|
|
function sendFeedback (message) {
|
|
|
|
bot.core.run('minecraft:tellraw @a ' + JSON.stringify(message))
|
|
|
|
}
|
2024-02-16 22:59:09 -05:00
|
|
|
bot.commands.execute(message.substring(bot.prefix.length), new CommandSource({ bot, player, sendFeedback }))
|
2024-02-11 21:23:41 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
function add (command) {
|
2024-02-13 19:37:06 -05:00
|
|
|
if (command.register) {
|
|
|
|
command.register(bot.commands.dispatcher)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isValid(command)) {
|
|
|
|
bot.console.warn(`Command '${command.aliases[0]}' is using the legacy command system!`)
|
|
|
|
|
2024-02-16 22:59:09 -05:00
|
|
|
const _execute = (c, args) => {
|
2024-02-16 21:37:11 -05:00
|
|
|
try {
|
2024-02-16 22:59:09 -05:00
|
|
|
const player = c.source.player
|
|
|
|
command.execute(bot, command.aliases[0], { UUID: player?.uuid, name: player?.username }, args)
|
2024-02-16 21:37:11 -05:00
|
|
|
} catch (error) {
|
|
|
|
bot.core.run('minecraft:tellraw @a ' + JSON.stringify({ text: error.toString(), color: 'red' }))
|
|
|
|
}
|
|
|
|
}
|
2024-02-13 19:37:06 -05:00
|
|
|
|
2024-02-29 20:39:21 -05:00
|
|
|
const requirement = source => source.permissionLevel >= command.permLevel
|
|
|
|
|
2024-02-13 19:37:06 -05:00
|
|
|
const node = bot.commands.dispatcher.register(
|
|
|
|
literal(command.aliases[0])
|
2024-02-16 22:59:09 -05:00
|
|
|
.executes(c => { _execute(c, []); return 0 })
|
2024-02-29 20:39:21 -05:00
|
|
|
.requires(requirement)
|
2024-02-13 19:37:06 -05:00
|
|
|
.then(
|
|
|
|
argument('args', greedyString())
|
2024-02-16 22:59:09 -05:00
|
|
|
.executes(c => { _execute(c, c.getArgument('args').split(' ')); return 0 })
|
2024-02-13 19:37:06 -05:00
|
|
|
)
|
|
|
|
)
|
|
|
|
for (let i = 1; i < command.aliases.length; i++) {
|
|
|
|
bot.commands.dispatcher.register(
|
|
|
|
literal(command.aliases[i])
|
|
|
|
.executes(context => { _execute([]); return 0 })
|
2024-02-29 20:39:21 -05:00
|
|
|
.requires(requirement)
|
2024-02-13 19:37:06 -05:00
|
|
|
.redirect(node)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add metadata for help command
|
|
|
|
node.description = command.description
|
|
|
|
node.permissionLevel = command.permLevel
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('Invalid command', 'invalid_command')
|
2024-02-11 21:23:41 -05:00
|
|
|
}
|
2024-02-13 19:37:06 -05:00
|
|
|
|
2024-02-11 21:23:41 -05:00
|
|
|
function loadFromDir (dirpath) {
|
|
|
|
fs.readdirSync(dirpath).forEach(filename => {
|
|
|
|
const filepath = path.resolve(dirpath, filename)
|
|
|
|
if (!filepath.endsWith('js') || !fs.statSync(filepath).isFile()) return
|
|
|
|
try {
|
|
|
|
bot.commands.add(require(filepath))
|
2024-02-13 19:37:06 -05:00
|
|
|
} catch (error) {
|
|
|
|
bot.console.error('Error loading command ' + filepath + ': ' + util.inspect(error))
|
2024-02-11 21:23:41 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2024-02-13 19:37:06 -05:00
|
|
|
|
2024-02-11 21:23:41 -05:00
|
|
|
function info (command) {
|
|
|
|
const info = bot.commands.commands[command] ?? command
|
|
|
|
if (isValid(info)) { return info }
|
|
|
|
}
|
2024-02-13 19:37:06 -05:00
|
|
|
|
|
|
|
function isCommand (command) { return true }
|
|
|
|
|
|
|
|
function execute (command, source) {
|
|
|
|
try {
|
|
|
|
bot.commands.dispatcher.execute(command, source)
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof CommandSyntaxException) {
|
|
|
|
const text = (error._message instanceof TextMessage) ? error._message.text : error._message.getString()
|
|
|
|
source.sendError(text)
|
|
|
|
const context = getContext(error)
|
|
|
|
if (context) source.sendError(context)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
source.sendError({ translate: 'command.failed', hoverEvent: { action: 'show_text', contents: error.stack } })
|
2024-02-11 21:23:41 -05:00
|
|
|
}
|
2024-02-13 19:37:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function getContext (error) {
|
|
|
|
const _cursor = error.cursor
|
|
|
|
const input = error.input
|
|
|
|
|
|
|
|
if (input == null || _cursor < 0) {
|
|
|
|
return null
|
2024-02-11 21:23:41 -05:00
|
|
|
}
|
2024-02-13 19:37:06 -05:00
|
|
|
|
|
|
|
const text = [{ text: '', color: 'gray', clickEvent: { action: 'suggest_command', value: bot.prefix + input } }]
|
|
|
|
|
|
|
|
const cursor = Math.min(input.length, _cursor)
|
|
|
|
|
|
|
|
if (cursor > CommandSyntaxException.CONTEXT_AMOUNT) {
|
|
|
|
text.push('...')
|
2024-02-11 21:23:41 -05:00
|
|
|
}
|
2024-02-13 19:37:06 -05:00
|
|
|
|
|
|
|
text.push(
|
|
|
|
input.substring(Math.max(0, cursor - CommandSyntaxException.CONTEXT_AMOUNT), cursor),
|
|
|
|
{ text: input.substring(cursor), color: 'red', underline: true },
|
|
|
|
{ translate: 'command.context.here', color: 'red', italic: true }
|
|
|
|
)
|
|
|
|
|
|
|
|
return text
|
2024-02-11 21:23:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function isValid (command) {
|
|
|
|
return command != null &&
|
|
|
|
typeof command.execute === 'function' &&
|
|
|
|
typeof command.name === 'string' &&
|
|
|
|
typeof command.description === 'string' &&
|
|
|
|
Array.isArray(command.usages) &&
|
|
|
|
Array.isArray(command.aliases) &&
|
|
|
|
typeof command.enabled === 'boolean' &&
|
|
|
|
command.aliases.length > 0 &&
|
|
|
|
typeof command.permLevel === 'number'
|
|
|
|
}
|
|
|
|
|
2024-02-29 20:39:21 -05:00
|
|
|
module.exports = inject
|