How to Setup a Basic Mineflayer Bot on Ubuntu 20.04

Updated on November 21, 2023
How to Setup a Basic Mineflayer Bot on Ubuntu 20.04 header image

Introduction

In this tutorial, you will be learning how to set up a basic Minecraft bot that responds to chat messages, utilizing Node.js and the Mineflayer library. So, without further ado, let's get started!

Brief Description of Mineflayer and Node.js

Mineflayer is a complex library that allows you to control Minecraft accounts through a powerful, stable, and high-level JavaScript API. It can do things such as digging and building, crafting, interacting with entities, inventory management, chat, and more.

Node.js is a fast, cross-platform JavaScript runtime based on Chrome's V8 engine that is used in both desktop and server environments. Node.js features a package registry called NPM, which hosts over 1.3 million open-source packages. NPM packages (or modules) are the backbone of Node.js, as they provide a way to utilize community-driven code that can be reused in any Node.js application. The library that you will be using in this tutorial, Mineflayer, is an NPM package.

Setting Up Your Environment

For this tutorial, you will need to deploy a Vultr Ubuntu 20.04 server. As with any Node.js library, you will need to install Node.js on your server. For the sake of this tutorial, we will assume that you already have Node.js installed on your server. If not, you can learn how to install Node Version Manager (NVM) and the latest version of Node.js here.

You will also need your own Minecraft server or a Minecraft server IP for your bot to connect to. If you want to create your own Minecraft server, please deploy a Vultr One-Click Minecraft installation, or install it on your current server by following this guide.

To connect to an 'online' Minecraft server, you will also need to own a Minecraft account.

Installing Mineflayer

First, SSH into the server as root, then navigate to a new folder. We will call it mineflayer-tutorial.

# mkdir mineflayer-tutorial
# cd mineflayer-tutorial

Now you can install the Mineflayer library using NPM.

# npm install mineflayer

Great! Now you have the Mineflayer library installed.

Setting Up the Mineflayer Bot

We can now create a new Mineflayer bot by creating a new JavaScript file called index.js and opening it in our text editor.

# nano index.js

Once you have opened the file, you can now insert the code that will create the bot.

const mineflayer = require('mineflayer');

const bot = mineflayer.createBot({
  host: 'YOUR_SERVER_IP_ADDRESS',
  username: 'YOUR_USERNAME',
  password: 'YOUR_PASSWORD'
});

bot.on('login', () => {
  console.log('Bot connected to server');
});

bot.on('chat', (username, message) => {
  if (message === 'hi') bot.chat(`${username} said hi`);
});

bot.on('end', () => {
  console.log('Bot disconnected');
});

This example will create a bot that will respond to the chat message 'hello' with the message 'hi'. It will also tell you when the bot connects to the server and when it disconnects in your terminal.

Configuring Your Mineflayer Bot

Using mineflayer.createBot(), we can specify options such as the server IP, account username, and account password. To connect to 'online' multiplayer servers, you will need to enter an email in the username field, and the password in the password field.

If you do not own a Minecraft account, your server will need to be in 'offline' mode, and you will only need to have the username field. Offline mode allows users who are not authenticated with Mojang servers to connect to your server. Please only use this option if you know what you are doing. For the sake of this tutorial, we will assume that you have bought a Minecraft account.

If your Minecraft account has been migrated to a Microsoft account, you will need to add the auth: 'microsoft' option.

If your server is hosted locally, use localhost for the host option. Otherwise, use the IP that you have assigned to your server or the IP of a server. Also, if your server uses a different port, specify it by using the port option.

Now, for example, let's say your account has been migrated, your server IP is localhost, and it uses port 1234. Your mineflayer.createBot() options should now look like this:

const bot = mineflayer.createBot({
  host: 'localhost',
  port: 1234,
  username: 'example@example.com',
  password: 'password',
  auth: 'microsoft',
});

Now, close your text editor by using Ctrl + X, then pressing Y, followed by Enter to confirm your changes.

Running Your Mineflayer Bot

Congratulations! Now you have set up a basic Mineflayer bot.

To run your bot, run the following command in your terminal:

# node index.js

To test if it's working, connect to the same server as your bot and type 'hi'. The bot should respond with '[your username] said hi'. It should also tell you when the bot connects to the server and when it disconnects from the server.

Additional Information

Mineflayer can do much more than chat. For a full list of features, visit the Mineflayer documentation.