Minecraft Blogs / Tutorial

Making a Minecraft Bot

  • 14,494 views, 6 today
  • 9
  • 7
  • 1
MoreTommy's Avatar MoreTommy
Level 28 : Expert Engineer
25
Hey, it's Tommy.

Do you want to make a bot but you have no idea how? Let me show you.

Requirements:
Newest installation of Node.JS
VSCode
Windows

Step 1:
Go to the command line and type "npm install mineflayer"
without quotations and hit enter. This will install the API we will use.

Step 2:
Open VSCode (Visual Studio Code), make a new file ending with ".js" and type this to get started. Replace the username in options with your email and add "password: [​my password without brackets]" if you want to connect to premium servers. Also replace the IP and port in options with your server IP. Here comes the code!

Setup Code
const mineflayer = require('mineflayer');
var
options = {
  host: 'MYSERVERIPHERE',
  port: myporthere (must be an integer),
  username: 'mynamehere',
};


Step 3:
Ok, now that you're done that, I will show you how to create the bot to connect to the server. Let's explain how the functions work first. Mineflayer has a function to create the bot, which is:
mineflayer.createBot()
This requires an argument in the function, between the two parentheses. So, let's set the variable to "bot" so we can control it with "bot.function" instead of not being able to control it. Type this code after the last option setup code:
Bot Code
var bot = mineflayer.createBot(options);

Step 4:
Easy, right? Now, let's make a function for when the bot joins.
This function can be made with the built-in function:
bot.on('spawn', () => {});
What's that arrow thing? It's called an arrow operator, and we can operate multiple commands with arguments with the arrow operator. So let's add this:
Spawn code
bot.on('spawn', () => { console.log("- Spawned -")
  console.log("- Listening for messages -")
  for (const player of Object.values(bot.players)) {
    console.log("- Player " + player.displayName + " connected -")
  }
});
What is bot.players? This loops through each player in your server and gives you a list of the connected players in the console.

Step 5:
Let's run the bot. Right click on the area you're working on coding in, and press "run code". Now, check in your server. Is it connected? If it's not, your login details may be incorrect.
This bot is ONLY made for servers that allow cracked bots (you do not want to pay for an alternate bot account most likely)
. Get the game.

Others:
Hey, hope you enjoyed my simple tutorial. If you want to check out the docs, try clicking on
this link and looking at the README.MD. That explains a lot more functions. Otherwise, try cloning the repository and looking through the many examples.
That's all, Tommy out. Peace~
CreditPrismarineJS and Mineflayer developers
Tags

1 Update Logs

v2 : by MoreTommy 02/21/2021 12:10:40 pmFeb 21st, 2021

Had to fix code formatting :))

Create an account or sign in to comment.

Madoda2s
04/09/2021 11:15 am
Level 9 : Apprentice Miner
history
Madoda2s's Avatar
nice
<h1>nice</h1>
1
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome