PrismarineJS / mineflayer

Create Minecraft bots with a powerful, stable, and high level JavaScript API.
https://prismarinejs.github.io/mineflayer/
MIT License
4.8k stars 885 forks source link

On chat repeats with each death #2709

Open 112cxyz opened 2 years ago

112cxyz commented 2 years ago

Versions

Detailed description of a problem

A clear and concise description of what the problem is, with as much context as possible. What are you building? What problem are you trying to solve?

Needed a simple player bot for a server I made that just afk's at a farm we made. I made it have a simple !reboot command and I left overnight. the bot got killed 12 times so I ran !help which tells you what !reboot does and it spammed it and got kicked for spam

I recreated it by killing the bot myself and each time I kill it it adds another line to the chat

Your current code


const mineflayer = require('mineflayer')
const autoeat = require('mineflayer-auto-eat')

const bot = mineflayer.createBot({
    host: 'placeholder.example.com',
    port: 25565,
    auth:'microsoft',
    username: '@icloud.com',
  })  

  const { mineflayer: mineflayerViewer } = require('prismarine-viewer')
  bot.once('spawn', () => {
    mineflayerViewer(bot, { port: 3007, firstPerson: true }) // port is the minecraft server port, if first person is false, you get a bird's-eye view
  })
  // Load the plugin
  bot.loadPlugin(autoeat)

  bot.once('spawn', () => {
    bot.autoEat.options = {
      priority: 'foodPoints',
      startAt: 14,
      bannedFood: []
    }
  })
  // The bot eats food automatically and emits these events when it starts eating and stops eating.

  bot.on('autoeat_started', () => {
    console.log('Auto Eat started!')
  })

  bot.on('autoeat_stopped', () => {
    console.log('Auto Eat stopped!')
  })

  bot.on('health', () => {
    if (bot.food === 20) bot.autoEat.disable()
    // Disable the plugin if the bot is at 20 food points
    else bot.autoEat.enable() // Else enable the plugin again
  })

  const logger = (rank, username, message) => {
    console.log(`${username} said ${message}`)
  }

bot.on('spawn', () => {
  bot.on('chat', (username, message) => {
    if (message === '!reboot') exit() // using pm2 to keep bot alive
    else if (message === '!help') bot.chat('!reboot - Reboots Bot')
  })
})

bot.on('playerJoined', (player) => {
  if (player.username !== bot.username) {
    bot.chat(`Hello, ${player.username}! ive been expecting you traveler. Use !help for a list of commands. I am a bot!`)
  }
})

/*

Expected behavior

It to say the command once.

nildotdev commented 2 years ago

Place the chat event outside of the spawn event.

bot.on("spawn", () => {

})
bot.on('chat', (username, message) => {
    if (message === '!reboot') exit() // using pm2 to keep bot alive
    else if (message === '!help') bot.chat('!reboot - Reboots Bot')
  })