PrismarineJS / mineflayer

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

bot.chat() isn't working #2839

Closed D4rknezSl1me closed 1 year ago

D4rknezSl1me commented 1 year ago

bot.chat() isn't working, i tried using await and not using it, it says Cannot read properties of undefined. here is my code and error log.

mineflayer = require('mineflayer');
let requirements = {
    'host':'Enrico8401.aternos.me',
    'port':'24712',
    'version':'1.19.2'
};

class MCBot{
    // Constructor
    constructor(username){
        this.username = username;
        this.host = requirements['host'];
        this.port = requirements['port'];
        this.version = requirements['version'];

        this.initBot()
    };
    // Init Bot Instance
    initBot(){
        this.bot = mineflayer.createBot({
            'username': this.username,
            'host': this.host,
            'port': this.port,
            'version': this.version
        });
        this.initEvents()
    }
    // Init Bot Events
    initEvents(){
        // Login Event
        this.bot.on('login', ()=>{
            let botSocket = this.bot._client.socket;
            console.log(`[${this.username}] Logged in to ${botSocket.server ? botSocket.server : botSocket.host}`);
        });
        // Disconnect Event
        this.bot.on('end', ()=>{
            console.log(`[${this.username}] Disconnected`);
            // Reconnection Attempt
            setTimeout(()=> this.initBot, 2000);
        });
        // Error Event
        this.bot.on('error', (err)=>{
            if(err.code==='ECONNREFUSED'){
                console.log(`[${this.username}] Failed to Reconnect to ${err.address}:${err.port}`);
            }else{
                console.log(`[${this.username}] Unhandled Error: ${err}`);
            }
        });
        // Spawn Event
        this.bot.on('spawn', async ()=>{
            console.log(`[${this.username}]  Spawned in`);
            await this.bot.waitForTicks(0)
            this.bot.chat(`${this.username}'s Ready!`);
            /*
            await this.bot.waitForTicks(60);
            this.bot.chat("Goodbye!");
            this.bot.quit();
            */
        });
    }
}
new MCBot("Bob")
// let bots = ["Bob"]
// for(var i = 0; i<bots.length; i++){
//     bots.push(new MCBot(bots[i]))
//     console.log("Bot added")
// }

This is the error

[Bob] Logged in to undefined
[Bob]  Spawned in
TypeError: Serialization error for play.toServer : SizeOf error for undefined : Cannot read properties of undefined (reading 'length')
    at Object.previousMessages (eval at compile (C:\Users\xianc\Desktop\CHRIS\Minecraft\Bots\node_modules\protodef\src\compiler.js:258:12), <anonymous>:175:37)
    at Object.packet_chat_message (eval at compile (C:\Users\xianc\Desktop\CHRIS\Minecraft\Bots\node_modules\protodef\src\compiler.js:258:12), <anonymous>:599:37)
    at eval (eval at compile (C:\Users\xianc\Desktop\CHRIS\Minecraft\Bots\node_modules\protodef\src\compiler.js:258:12), <anonymous>:1222:64)
    at packet (eval at compile (C:\Users\xianc\Desktop\CHRIS\Minecraft\Bots\node_modules\protodef\src\compiler.js:258:12), <anonymous>:1270:9)
    at CompiledProtodef.sizeOf (C:\Users\xianc\Desktop\CHRIS\Minecraft\Bots\node_modules\protodef\src\compiler.js:89:14)
    at e.message (C:\Users\xianc\Desktop\CHRIS\Minecraft\Bots\node_modules\protodef\src\compiler.js:96:40)
    at tryCatch (C:\Users\xianc\Desktop\CHRIS\Minecraft\Bots\node_modules\protodef\src\utils.js:50:16)
    at CompiledProtodef.createPacketBuffer (C:\Users\xianc\Desktop\CHRIS\Minecraft\Bots\node_modules\protodef\src\compiler.js:96:20)
    at Serializer.createPacketBuffer (C:\Users\xianc\Desktop\CHRIS\Minecraft\Bots\node_modules\protodef\src\serializer.js:12:23)
    at Serializer._transform (C:\Users\xianc\Desktop\CHRIS\Minecraft\Bots\node_modules\protodef\src\serializer.js:18:18) {
  field: 'play.toServer'
}
[Bob] Unhandled Error: TypeError: Serialization error for play.toServer : SizeOf error for undefined : Cannot read properties of undefined (reading 'length')
[Bob] Disconnected
banocean commented 1 year ago

Similar issue, but no error events were emitted

AshDoesStuf commented 1 year ago

mineflayer doesnt support 1.19.2 yet btw

snowcatman commented 1 year ago

your bot.chat is fine. though you might want to use > ${botSocket._host} To get the right server host info. Your using the wrong version 1.19.2 in mineflayer is not out yet.

extremeheat commented 1 year ago

2797