liamcottle / rustplus.js

Unofficial NodeJS library for controlling Smart Switches in the PC game Rust
224 stars 46 forks source link

TypeError: Cannot read properties of undefined (reading 'fromObject') #42

Closed AsutoraGG closed 2 years ago

AsutoraGG commented 2 years ago

I'm making a private BOT now. The code is this When I run it I get an error like this :

D:\ProjectFile\bot\node_modules\@liamcottle\rustplus.js\rustplus.js:138
        let request = this.AppRequest.fromObject({
                                      ^

TypeError: Cannot read properties of undefined (reading 'fromObject')
    at RustPlus.sendRequest (D:\ProjectFile\bot\node_modules\@liamcottle\rustplus.js\rustplus.js:138:39)
    at RustPlus.sendTeamMessage (D:\ProjectFile\bot\node_modules\@liamcottle\rustplus.js\rustplus.js:240:14)
    at print (file:///D:/ProjectFile/bot/tool/print.mjs:18:18)
    at RustPlus.<anonymous> (file:///D:/ProjectFile/bpt/index.mjs:102:5)
    at RustPlus.emit (node:events:394:28)
    at WebSocket.<anonymous> (D:\ProjectFile\bot\node_modules\@liamcottle\rustplus.js\rustplus.js:66:22)
    at WebSocket.emit (node:events:394:28)
    at WebSocket.setSocket (D:\ProjectFile\bot\node_modules\ws\lib\websocket.js:237:10)
    at ClientRequest.<anonymous> (D:\ProjectFile\bot\node_modules\ws\lib\websocket.js:974:15)
    at ClientRequest.emit (node:events:394:28)
liamcottle commented 2 years ago

Your code doesn't appear to be connecting to the server at all.

You need to connect to the server before you can send any requests like sendTeamMessage.

The docs show an example on how to do this properly.

const RustPlus = require('@liamcottle/rustplus.js');
var rustplus = new RustPlus('ip', 'port', 'playerId', 'playerToken');

// wait until connected before sending commands
rustplus.on('connected', () => {

    // ready to send requests
    rustplus.sendTeamMessage('Hello from rustplus.js!');

});

// connect to rust server
rustplus.connect();