PrismarineJS / mineflayer

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

cant use if (message) #2090

Closed Torono909 closed 3 years ago

Torono909 commented 3 years ago

code: const mineflayer = require('mineflayer') const bot = mineflayer.createBot({ host: 'anarchit.mcpro.io', port: 25565, username: 'Chocolate_Twit' })

bot.on('login', function(username, message) { bot.chat("/gamemode creative") })

bot.on('chat', (username) => { if (message === '!help') { bot.chat('Hello console! this is currently in process! so please wait for the bot to be released into the public. the developers are currently working on this.') } if (message === '!kill') { bot.chat('/kill') } }) error: C:\Users\hi\Desktop\Bots\Artica\index.js:13 if (message === '!help') { ^

ReferenceError: message is not defined at EventEmitter. (C:\Users\hi\Desktop\Bots\Artica\index.js:13:2) at EventEmitter.emit (events.js:315:20) at EventEmitter. (C:\Users\hi\Desktop\Bots\Artica\node_modules\mineflayer\lib\plugins\chat.js:79:104) at EventEmitter.emit (events.js:315:20) at Client. (C:\Users\hi\Desktop\Bots\Artica\node_modules\mineflayer\lib\plugins\chat.js:122:9) at Client.emit (events.js:315:20) at FullPacketParser. (C:\Users\hi\Desktop\Bots\Artica\node_modules\minecraft-protocol\src\client.js:91:12) at FullPacketParser.emit (events.js:315:20) at addChunk (C:\Users\hi\Desktop\Bots\Artica\node_modules\readable-stream\lib_stream_readable.js:298:12) at readableAddChunk (C:\Users\hi\Desktop\Bots\Artica\node_modules\readable-stream\lib_stream_readable.js:280:11)

GroobleDierne commented 3 years ago

You do not have a 'message' variable that's why it's failing, use

bot.on("chat", (username, message) => {})

Instead.

Torono909 commented 3 years ago

i used bot.on("chat", (username, message) => {}) but my bot disconnects when i use !help or !kill

Torono909 commented 3 years ago

i need help for
if(message === '!help') { ^

ReferenceError: message is not defined at EventEmitter. (C:\Users\hi\Desktop\Bots\Artica\index.js:13:2) at EventEmitter.emit (events.js:327:22) at EventEmitter. (C:\Users\hi\Desktop\Bots\Artica\node_modules\mineflayer\lib\plugins\chat.js:79:104) at EventEmitter.emit (events.js:315:20) at Client. (C:\Users\hi\Desktop\Bots\Artica\node_modules\mineflayer\lib\plugins\chat.js:122:9) at Client.emit (events.js:315:20) at FullPacketParser. (C:\Users\hi\Desktop\Bots\Artica\node_modules\minecraft-protocol\src\client.js:91:12) at FullPacketParser.emit (events.js:315:20) at addChunk (C:\Users\hi\Desktop\Bots\Artica\node_modules\readable-stream\lib_stream_readable.js:298:12)

GroobleDierne commented 3 years ago

Send your actual code

Torono909 commented 3 years ago

k

Torono909 commented 3 years ago

i edited some code

Torono909 commented 3 years ago

`const mineflayer = require('mineflayer') const pvp = require('mineflayer-pvp').plugin const { pathfinder, Movements, goals } = require('mineflayer-pathfinder') const armorManager = require('mineflayer-armor-manager')

const bot = mineflayer.createBot({ host: '127.0.0.1', port: 25565, username: 'Source_', version: '1.12.2', logErrors: false })

bot.loadPlugin(pvp) bot.loadPlugin(armorManager) bot.loadPlugin(pathfinder)

bot.on('playerCollect', (collector, itemDrop) => { if (collector !== bot.entity) return

setTimeout(() => { const sword = bot.inventory.items().find(item => item.name.includes('sword')) if (sword) bot.equip(sword, 'hand') }, 150) })

bot.on('playerCollect', (collector, itemDrop) => { if (collector !== bot.entity) return

setTimeout(() => { const axe = bot.inventory.items().find(item => item.name.includes('sword')) if (axe) bot.equip(axe, 'hand') }, 150) })

bot.on('playerCollect', (collector, itemDrop) => { if (collector !== bot.entity) return

setTimeout(() => { const shield = bot.inventory.items().find(item => item.name.includes('shield')) if (shield) bot.equip(shield, 'off-hand') }, 250) })

let guardPos = null

function guardArea (pos) { guardPos = pos.clone()

if (!bot.pvp.target) { moveToGuardPos() } }

function stopGuarding () { guardPos = null bot.pvp.stop() bot.pathfinder.setGoal(null) }

function moveToGuardPos () { const mcData = require('minecraft-data')(bot.version) bot.pathfinder.setMovements(new Movements(bot, mcData)) bot.pathfinder.setGoal(new goals.GoalBlock(guardPos.x, guardPos.y, guardPos.z)) }

bot.on('stoppedAttacking', () => { if (guardPos) { moveToGuardPos() } })

bot.on('physicTick', () => { if (bot.pvp.target) return if (bot.pathfinder.isMoving()) return

const entity = bot.nearestEntity() if (entity) bot.lookAt(entity.position.offset(0, entity.height, 0)) })

bot.on('physicTick', () => { if (!guardPos) return

const filter = e => e.type === 'mob' && e.position.distanceTo(bot.entity.position) < 16 && e.mobType !== 'Armor Stand' // Mojang classifies armor stands as mobs for some reason?

const entity = bot.nearestEntity(filter) if (entity) { bot.pvp.attack(entity) } })

bot.on('chat', (username, message) => { if (message === '!guard') { const player = bot.players[username]

if (!player) {
  bot.chat("I can't see you.")
  return
}

bot.chat('I will guard that location.')
guardArea(player.entity.position)

}

if (message === '!fight-me') { const player = bot.players[username]

if (!player) {
  bot.chat("I can't see you.")
  return
}

bot.chat('Prepare to fight!')
bot.pvp.attack(player.entity)

}

if (message === '!stop') { bot.chat('I will no longer guard this area.') stopGuarding() } })`

Torono909 commented 3 years ago

`const mineflayer = require('mineflayer') const pvp = require('mineflayer-pvp').plugin const { pathfinder, Movements, goals } = require('mineflayer-pathfinder') const armorManager = require('mineflayer-armor-manager')

const bot = mineflayer.createBot({ host: '127.0.0.1', port: 25565, username: 'Source_', version: '1.12.2', logErrors: false })

bot.loadPlugin(pvp) bot.loadPlugin(armorManager) bot.loadPlugin(pathfinder)

bot.on('playerCollect', (collector, itemDrop) => { if (collector !== bot.entity) return

setTimeout(() => { const sword = bot.inventory.items().find(item => item.name.includes('sword')) if (sword) bot.equip(sword, 'hand') }, 150) })

bot.on('playerCollect', (collector, itemDrop) => { if (collector !== bot.entity) return

setTimeout(() => { const axe = bot.inventory.items().find(item => item.name.includes('sword')) if (axe) bot.equip(axe, 'hand') }, 150) })

bot.on('playerCollect', (collector, itemDrop) => { if (collector !== bot.entity) return

setTimeout(() => { const shield = bot.inventory.items().find(item => item.name.includes('shield')) if (shield) bot.equip(shield, 'off-hand') }, 250) })

let guardPos = null

function guardArea (pos) { guardPos = pos.clone()

if (!bot.pvp.target) { moveToGuardPos() } }

function stopGuarding () { guardPos = null bot.pvp.stop() bot.pathfinder.setGoal(null) }

function moveToGuardPos () { const mcData = require('minecraft-data')(bot.version) bot.pathfinder.setMovements(new Movements(bot, mcData)) bot.pathfinder.setGoal(new goals.GoalBlock(guardPos.x, guardPos.y, guardPos.z)) }

bot.on('stoppedAttacking', () => { if (guardPos) { moveToGuardPos() } })

bot.on('physicTick', () => { if (bot.pvp.target) return if (bot.pathfinder.isMoving()) return

const entity = bot.nearestEntity() if (entity) bot.lookAt(entity.position.offset(0, entity.height, 0)) })

bot.on('physicTick', () => { if (!guardPos) return

const filter = e => e.type === 'mob' && e.position.distanceTo(bot.entity.position) < 16 && e.mobType !== 'Armor Stand' // Mojang classifies armor stands as mobs for some reason?

const entity = bot.nearestEntity(filter) if (entity) { bot.pvp.attack(entity) } })

bot.on('chat', (username, message) => { if (message === '!guard') { const player = bot.players[username]

if (!player) {
  bot.chat("I can't see you.")
  return
}

bot.chat('I will guard that location.')
guardArea(player.entity.position)

}

if (message === '!fight-me') { const player = bot.players[username]

if (!player) {
  bot.chat("I can't see you.")
  return
}

bot.chat('Prepare to fight!')
bot.pvp.attack(player.entity)

}

if (message === '!stop') { bot.chat('I will no longer guard this area.') stopGuarding() } })`

u9g commented 3 years ago

Okay, this isn't an issue, if you want code help, please join our discord and ask in the #mineflayer-beginners channel: https://discord.gg/6jUDqUrejH

Torono909 commented 3 years ago

is there a way so the bot can automatically put items in the off-hand when items break