Categories > Coding > Javascript >

[REL] Minecraft botting couldn't be easier with Mineflayer

Posts: 2016

Threads: 198

Joined: Apr, 2021

Reputation: 16

Posted

Hi!
Probably most of you knew that already, but here I come back after a while with a tutorial on how to make a Minecraft bot. You can make it chat, or kick other players, make it dig, etc. But in this part, I will tell you how to make the bot itself. So, let's get started.

 

First, we run this command to init a new Node.JS project:

npm init -y

 

It will quickly make a new project for us. Now, after it's done, we make a new file called index.js.

 

Our first lines of code are:

const mineflayer = require('mineflayer')

 

We now added our mineflayer "library". Now, we want to make options for the bot. This will be made like that:

const options = {
	host: "server.com",
	username: "Blume"
}

 

If we want the bot to actually join the server we want it to, you need to change the "host" value to the server's domain or address IP.

If you want you can also change the username to anything you want. My bot is going to be called "Blume". 

 

Now, we need to create our bot using the options. How do we do that?

The answer is simple: One line of code.

 

const bot = mineflayer.createBot(options)

Quick explanation:
We used the mineflayer package to create the bot using our options we made before.

 

Now, we'll type some events. One of them will be on spawn:

bot.on("spawn", () =>{
	console.log("[*] Bot connected!")
	bot.chat("Hello world!")
})

 

See? Now we have a bot that will say something when spawned / joined! Great work!

 

Now we will make it listen to a command, for example "!givemeop" that will give the player OP (keep in mind the bot has to be opped first to make the command executor opped)

 

bot.on("chat", (message, username) => {
	if(username != bot.username){
		if(message == "!givemeop"){
			bot.chat("/op" + username)
		}
	}
})

 

Another quick explanation what we just coded:

- We coded an event listener (if I called it properly lol), which will detect any new messages in chat.

If the message sender's username is the bot's username, ignore it, because we don't want the bot to talk to itself and repeat the message all over again, right?

 

So if the username is not the bot's username, we make the bot type in the command for opp'ing the message sender's username.

 

That's all for today!
I hope you understood all of the tutorial.

If you have any questions, feel free to ask them below.

Cya!

 

  • 1

Random quote here...

Cyros

Revision

Posts: 1057

Threads: 48

Joined: Feb, 2021

Reputation: 20

Replied

this is actually cool

  • 0

We Hate VOID

Posts: 2016

Threads: 198

Joined: Apr, 2021

Reputation: 16

Replied

@Cyros Thanks for your feedback!

  • 0

Random quote here...

Posts: 1433

Threads: 71

Joined: May, 2022

Reputation: 20

Replied

cool but where does it get the account from?..

  • 0

i use arch btw

SeizureSalad

i love femboys

Posts: 1159

Threads: 79

Joined: Mar, 2021

Reputation: 40

Replied

@Whoman probably spoofed

 

also bad js code moment smh

also i think you forgor the npm install commnad

bot.on('chat', (message, username) => {
	if (username !== bot.username && message === '!givemeop'){
               bot.chat("/op" + username)
	}
})

  • 0

"Questionable intellegence, but I like the mystery" - CubeFaces

https://cdn.discordapp.com/attachments/1136067487847415848/1138948596679589898/sig.png

Posts: 2016

Threads: 198

Joined: Apr, 2021

Reputation: 16

Replied

@SeizureSalad the code that you just made is the same but you made one if, and its overcomplicated for new users. Both codes are okay :) 

  • 0

Random quote here...

Cyros

Revision

Posts: 1057

Threads: 48

Joined: Feb, 2021

Reputation: 20

Replied

@Whoman Guessing it's a Cracked Account (You don't have to buy a account) for this

  • 0

We Hate VOID

Posts: 1433

Threads: 71

Joined: May, 2022

Reputation: 20

Replied

@VoidableMethod

just learn keywords

  • 0

i use arch btw

Posts: 283

Threads: 48

Joined: May, 2022

Reputation: -4

Replied

@VoidableMethod

lmao it's not overcomplicated? it's actually less complicated as it's less lines and more concise, it also teaches beginners to use !== and === when possible. I've had experience with JS myself and using strict comparisons is good practice.

  • 0

https://media.discordapp.net/attachments/1044764388546068510/1051935933836050482/Signature_4.png

Entity

Usability >> modern

vip

Posts: 416

Threads: 40

Joined: May, 2022

Reputation: 51

Replied

Cool thread Vouch!

  • 0

Posts: 19

Threads: 4

Joined: Jul, 2021

Reputation: 4

Replied

@SeizureSalad

idk, that still bad

bot.on('chat', (message, username) => {
    if (username == bot.username || !message.startsWith('!')) return;
    const command = message.toLowerCase().slice(1);

    switch (command) {
      case 'givememod': {
        return bot.chat('/op' + username);
      }
   }
});

ignore bad format

  • 0

SeizureSalad

i love femboys

Posts: 1159

Threads: 79

Joined: Mar, 2021

Reputation: 40

Replied

@isaacxsx the hell is the purpose of a switch with one case?? Use switches only if you have like 3+ different conditions

  • 0

"Questionable intellegence, but I like the mystery" - CubeFaces

https://cdn.discordapp.com/attachments/1136067487847415848/1138948596679589898/sig.png

Posts: 19

Threads: 4

Joined: Jul, 2021

Reputation: 4

Replied

@SeizureSalad

oh, I thought by the type of message filter I thought you would know it was for multiple commands, but it was just an example.

  • 0

SeizureSalad

i love femboys

Posts: 1159

Threads: 79

Joined: Mar, 2021

Reputation: 40

Replied

@isaacxsx actually you're right because in that case you should use a switch but I feel like the one command was just a single example

  • 0

"Questionable intellegence, but I like the mystery" - CubeFaces

https://cdn.discordapp.com/attachments/1136067487847415848/1138948596679589898/sig.png

Users viewing this thread:

( Members: 0, Guests: 1, Total: 1 )