chipmunkbot3/plugins/commands.js

117 lines
3.2 KiB
JavaScript
Raw Normal View History

2024-02-11 21:23:41 -05:00
const fs = require('fs')
const path = require('path')
2024-04-02 17:53:10 -04:00
const { CommandDispatcher, literal, argument, greedyString, CommandSyntaxException } = require('brigadier-commands')
const CommandSource = require('../util/command/command_source')
const TextMessage = require('../util/command/text_message')
const colorCodeStringify = require('../util/chat/stringify/color_code')
2024-02-11 21:23:41 -05:00
2024-04-02 17:53:10 -04:00
let commands
function loadCommands () {
commands = []
2024-04-04 22:00:51 -04:00
for (const filename of fs.readdirSync(path.resolve(__dirname, '..', 'commands'))) {
const filepath = path.resolve(__dirname, '..', 'commands', filename)
2024-04-02 17:53:10 -04:00
if (!filepath.endsWith('.js') || !fs.statSync(filepath).isFile()) return
try {
delete require.cache[require.resolve(filepath)]
commands.push(require(filepath))
} catch (error) {
console.error('Error loading command', filepath, ':', error)
}
2024-04-04 22:00:51 -04:00
}
2024-04-02 17:53:10 -04:00
}
loadCommands()
2024-02-11 21:23:41 -05:00
function inject (bot) {
bot.commands = {
2024-04-02 17:53:10 -04:00
dispatcher: null,
2024-02-11 21:23:41 -05:00
execute,
2024-04-02 17:53:10 -04:00
reload,
2024-02-11 21:23:41 -05:00
}
bot.on('message', ({ sender, plain }) => {
if (!plain.startsWith(bot.prefix)) return
2024-02-11 21:23:41 -05:00
2024-02-16 21:37:11 -05:00
function sendFeedback (message) {
bot.tellraw(message, '@a')
2024-02-16 21:37:11 -05:00
}
2024-04-02 17:53:10 -04:00
const displayName = {
insertion: sender.username,
clickEvent: { action: 'suggest_command', value: `/tell ${sender.username} ` },
hoverEvent: {
action: 'show_entity',
contents: {
type: 'minecraft:player',
id: sender.uuid,
name: sender.username
2024-02-16 21:37:11 -05:00
}
2024-04-02 17:53:10 -04:00
},
text: sender.username
}
2024-04-02 17:53:10 -04:00
bot.commands.execute(plain.substring(bot.prefix.length), new CommandSource({ bot, player: sender, sendFeedback, displayName }))
})
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
}
}
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
}
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
}
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
}
2024-04-02 17:53:10 -04:00
function reload (loadFiles = true) {
bot.commands.dispatcher = new CommandDispatcher()
if (loadFiles) loadCommands()
for (const command of commands) {
try {
command.register(bot.commands.dispatcher)
} catch (error) {
console.error('Unable to register command:', error)
}
}
bot.emit('commands_loaded')
}
reload(false)
2024-02-11 21:23:41 -05:00
}
2024-02-29 20:39:21 -05:00
module.exports = inject