800 words
4 minutes
How to create a simple minecraft bot (NodeJS & Mineflayer) (🇬🇧)

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


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.


To start making your Minecraft bot, create a new folder, and enter it by running the below command:

mkdir minecraft-bot && cd minecraft-bot

Then, create a new npm project by running the command:

npm init -y

To install the needed packages to start creating the bot, run the below command:

npm install mineflayer

This will install all needed packages to start creating your new Minecraft bot!

If you want to edit the details, you can do so in the newly created package.json

Create a file, preferably called index.js. This file will be called main file in this guide

That should be all you need to create a basic Minecraft bot

To test/run your code, run in the main directory node . if your main files name is index.js

0. Creating a test server#

You will probably want to test your bot while you are creating it. This is very common practice, I expect. If you want to test your bot without actually having to give it an account yet, you can start by creating a lan server. To do this, start by creating an offline world, and pressing escape.

Go ahead and press the Open to LAN button, and selecting everything you want. Id recommend to enable cheats, so it is easier to switch gamemodes. When you press the Start button, you will see in chat appear a message, that says something along the lines of “Local game hosted on port #####“

1. Logging in#

Mineflayer is a great api to create your bot with. It is very stable, powerful and flexible, which is what we need!

To start, we will create a bot that just join our server. Start by requiring mineflayer at the top of your main file like this:

const mineflayer = require('mineflayer');

Now, we need to define the options with which the bot will be created and logged into the server with. We can do this by first defining the options we will be using, inside of a json, like this:

const options = {
  host: 'localhost',
  port: '25565',
  username: '',
  password: '12345678',

Most of this should be selfexplanatory, but incase you want to be completely sure, here is an explanation on all of these options

host is the ip that the bot will connect to. If you followed step 0, keep this as localhost.

port is the port where the minecraft server is using. If you followed step 0, set this to the port you got in chat

If you are going to use an actual account, username should be the accounts email. If you are following step 0, you can set this to a random username (like herobrine!)

password should be the password you use if you use an actual account

Now, we need to start the bot, by adding this like of code:

const bot = mineflayer.createBot(options)

This starts the new bot, and when you start the node project, you should see the bot connect to your server! Id also recommend to add these two lines to the very end of your file, incase any errors happen:

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

2. Adding features#

To start, lets add a feature that repeats every word that is sent in chat. We can do this by using the event chat. If you know how to make discord bots with d.js, you will notice that the events system is pratically the same. There are two kind of events, on and once. The on event can repeat itself multiple times, while the once event repeats itself only once.

To start logging the chat event, lets add this code:

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

What this does is log every message that is sent by a user. Your console should look like this:

Now, we want to be able to tell who sent these messages, so lets change the console.log line to this:

console.log(`<${username}> ${message}`)

Now your console should feel a little more like the minecraft chat! Now, lets start creating the copycat feature.

Before we start creating this feature, lets make sure that we ignore the bot, so that we dont enter an endless loop! Add this below the console.log line:

if (username === bot.username) return

This will check if the username of the author of the message is the same as the bots username, and if it is the same, to ignore the message. Now we can continue without the risk of a neverending loop. To send a message, lets use the chat function, like this:

This will repeat the message sent by you! Test it out! It should look something like this:

This is how you can create a simple feature!

How to create a simple minecraft bot (NodeJS & Mineflayer) (🇬🇧)
Published at