A simple command manager and handler for Mineflayer plugins.
This plugin is built using Node and can be installed using:
npm install --save mineflayer-cmd
Loading the plugin can be done the same as any other plugin. Commands can also be loaded at any time.
// Create your bot
const mineflayer = require("mineflayer");
const bot = mineflayer.createBot({ username: "Player" });
// Load the cmd plugin
const cmd = require('mineflayer-cmd').plugin
cmd.allowConsoleInput = true // Optional config argument
bot.loadPlugin(cmd)
// Register your custom command handlers, if desired (plugins can load them too)
function sayCommand(sender, flags, args): Promise<void> {
return new Promise((resolve, reject) => {
let message = ''
if (flags.showsender) message += sender + ": "
if (flags.color) message += '&' + flags.color[0]
message += args.join(' ')
bot.chat(message)
resolve()
})
}
bot.once('cmd_ready', () => {
bot.cmd.registerCommand('say', sayCommand, // Create a new command called 'say' and set the executor function
'make me say something', // help text
'say <message>') // usage text
// Add a flag called 'color' that expects 1 input
.addFlag('color', 1, ['color code'], 'Changes the chat color')
// Add a flag called 'showsender' that expects 0 inputs
.addFlag('showsender', 0, [], 'If present, displays the sender who sent this message')
})
// And listen for command inputs from any source
// Let's listen for chat events that start with "!"
bot.on('chat', (username, message) => {
if (message.startsWith('!')) {
const command = message.substring(1)
bot.cmd.run(username, command) // Run with the sender and the command itself
}
})
This project uses the MIT license.
This project is accepting PRs and Issues. See something you think can be improved? Go for it! Any and all help is highly appreciated!
For larger changes, it is recommended to discuss these changes in the issues tab before writing any code. It's also preferred to make many smaller PRs than one large one, where applicable.