My Mineflayer bot doesn't respond to my message

3k Views Asked by At

I'm using MineFlayer JS to make a basic bot that responds to a user's message. Here is the source code. The issue I'm having is that it doesn't respond to my chat message when I type /msg bot test in chat.

// Importing the necessary modules
const mineflayer = require('mineflayer')

// Options for the bot
const options = {
    host: 'localhost',
    port: 53115,
    username: 'bot'
}

// Creating the actual bot
const bot = mineflayer.createBot(options)

// Creating a function to say "Hey I am a bot." in minecraft chat and logs "I spawned." in console
function typeHiInChat() {
    bot.chat("Hey! I am a bot.")
    console.log('I spawned.')
}

// Creating a function to say "I have been kicked from the server!" open it getting kicked
function onKick() {
    console.log('I have been kicked from the server!')
}

// Setting up a listener that listens for the 'spawn' event
bot.once('spawn', typeHiInChat)

// Setting up a listener that listens for the 'kick' event
bot.once('kicked', onKick)

bot.on('message', (message, jsonMSG) => {
    msg = JSON.stringify(jsonMSG)
    if (msg == "test") {
        bot.chat("This Works!")
    }
})

It doesn't respond to my chat message when I type /msg test in my Minecraft chat. It does say "Hey! I am a bot." in chat, but not the actual thing.

2

There are 2 best solutions below

0
Michael Bauer On

I'd suggest to use the 'chat' event as it appears you're not using the 'message' event properly, where the first parameter is the jsonMSG and the 2nd is the position. Take a look here https://github.com/PrismarineJS/mineflayer/blob/master/docs/api.md#events

Based on this, this is an example that should work

bot.on('chat', (username, message) => {
  if (username === bot.username) return
  if(message === 'test') bot.chat("This Works!") 
  
})

0
MrFiend On

dont use any functions for chat just simply use bot.chat()bot.on('chat', (username,message)=>{if(message === 'hi'){bot.chat('Hi')}}) this automatically updates the bot on chat so u dont have to use settintervals and if u want the bot to respond to specific chat just use if of else if statements