Samu
618 words
3 minutes
How to make a command handler (NodeJS & Mineflayer) (🇬🇧)

This guide is a way to learn how to create your own command handler for your minecraft bot with NodeJS and Mineflayer!

This guide is a continuation of my previous minecraft bot related guide, which you can find here!

We will be using this code as the base code for our bot:

const mineflayer = require('mineflayer');

const options = {
    host: 'localhost',
    port: '25565',
    username: 'herobrine'
}

const bot = mineflayer.createBot(options)

bot.on('chat', (username, message) => {
    
})

bot.on('kicked', console.log)
bot.on('error', console.log)

Warning#

This guide was made in 2021, my skills in coding and writing have probably improved, I haven’t actually bothered reading the guide in its entirety. Also, in the original post, there was a warning that this is only for Java Minecraft, so here you go.

0. Structure#

Before starting to create the handler, you may want to make a backup of your bot. So far, your projects folder should look something like this:

minecraft-bot/
├── node_modules
├── index.js
├── package-lock.json
└── package.json

In your projects main directory, create a new folder called commands. Here we will store all our commands!

1. Scanning for commands#

In your main file, start by adding these two lines to the top of your code:

const fs = require('fs')
const mineflayer = require('mineflayer');
const prefix = '!'

Note: fs is the file system for NodeJS, so there is no need to install it with npm

Next, lets add the code to find all the available commands. To do this, we will use fs.readdirSync which scans all files in a directory, and we will filter the files to only look for javascript files, like so:

const bot = mineflayer.createBot(options)

const commandfiles = fs.readdirSync(__dirname + '/commands').filter(file => file.endsWith('.js'));

Under this line, we will also start to log all commands, by creating a new array:

let commands = []

Now, we need to add all our available commands to this array, like so:

for (const val of commandfiles) {
    commands.push(val.replace('.js', ''))
}

With all this in place, we can start running these commands!

2. Parsing the message and args#

We need to make sure that any commands we read has the prefix we defined at the top, and isnt sent from our bot. Go inside the curly brackets for the event chat, and add this line of code, like so:

bot.on('chat', (username, message) => {
    if(!message.startsWith(prefix) || username === bot.username) return
})

Now that we are filtering messages that have our name in it and messages without a prefix, lets start parsing our arguments!

Note: All code from now on till when said will be inside the chat event

To accomplish this, lets remove the prefix, and break down the message into an array, like so:

const args = message.slice(prefix.length).trim().split(/ +/);

Now, when we log args into our console, it will return an array with all args we supply ingame. At this point, the bot doesnt know what command we want to run yet, so lets give it this info, with the following line:

const command = args.shift().toLowerCase();

This will get our arguments, get the first argument that is in the array, shift it to lowercase and remove it from our args so we wont have to deal with it in the future.

To prepare for the next step, we need to check if the command supplied in the message matches any of the commands in our command array. To do this, we can get away with this code:

if (!commands.includes(command)) return;

3. Running the command#

Now to run the command, its very easy. You can just add these two lines of code:

const torun = require(__dirname + '/commands/' + command + '.js')

torun.execute(bot, args)

And thats it! To create a command, use this as a template:

module.exports = {
    execute(bot, args) {
        // Start of where code can be placed

        console.log('This is an example command')
        
        bot.chat('This is an example command')

        // End of where code can be placed
    },
}
How to make a command handler (NodeJS & Mineflayer) (🇬🇧)
https://samu.lol/posts/createmineflayerhandler/
Author
Samu
Published at
2021-08-14