Categories > Coding > Javascript >
[REL] Minecraft botting couldn't be easier with Mineflayer
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!
Random quote here...
Did I mention I use arch btw?
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)
}
})
Cancel
Post
"Questionable intellegence, but I like the mystery" - CubeFaces
https://cdn.discordapp.com/attachments/1136067487847415848/1138948596679589898/sig.png
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 :)
Cancel
Post
Random quote here...
Did I mention I use arch btw?
Replied
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.
Cancel
Post
https://media.discordapp.net/attachments/1044764388546068510/1051935933836050482/Signature_4.png
Replied
Cool thread Vouch!
Cancel
Post
Replied
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
Cancel
Post
Replied
@isaacxsx the hell is the purpose of a switch with one case?? Use switches only if you have like 3+ different conditions
Cancel
Post
"Questionable intellegence, but I like the mystery" - CubeFaces
https://cdn.discordapp.com/attachments/1136067487847415848/1138948596679589898/sig.png
Replied
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.
Cancel
Post
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
Cancel
Post
"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 )
Cancel
Post