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.
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