OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Electron JS - Methods from the preload library do not work

  • Thread starter Thread starter Lotti
  • Start date Start date
L

Lotti

Guest
I have my own desktop application, it should launch a Minecraft bot for a local server. In my preload.js I add mineflayer (library) Thus

Code:
const { contextBridge } = require("electron");
const mineflayers = require("mineflayer");

contextBridge.exposeInMainWorld('libs', {
    mineflayer: mineflayers,
});

In main.js

Code:
const path = require('path');
const url = require('url');
const {app, BrowserWindow, ipcMain} = require('electron');

let win;

function createWindow() {
    win = new BrowserWindow({
        resizable: false,
        width: 900,
        height: 700,
        autoHideMenuBar: true,
        webPreferences: {
            preload: path.join(__dirname, 'preload.js'),
            nodeIntegration: true
        }
    });

    win.loadURL(url.format({
        pathname: path.join(__dirname, './html/index.html'),
        protocol: 'file:',
        slashes: true
    }));

    win.webContents.openDevTools();

    win.on('closed', () => {
        win = null;
    });
}

app.on('ready', createWindow);

app.on('window-all-closed', () => {
    app.quit();
});

When I try to connect the bot to the server it fails. As soon as I try to use any mentod like bot.chat("message") I'm getting the error chat is not a function

Code:
$(".connect-button").click(() => {
    let date = new Date()

    $(".console-log").append(`<br>[${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}] Запуск...`);
    const client = libs.mineflayer.createBot({
        username: "testBot",
        host: "ip",
        port: "port",
        versions: "1.16.5"
    });

    setTimeout(() => {
        client.chat("10 sec");
    }, 10000);
});

How do I use the library's functions?

I tried to use sandbox but realized that it was removed in version 1.20 I also tried adding the chat method directly to the bot’s connect function - similar

Code:
    const client = libs.mineflayer.createBot({
        username: "testBot",
        host: "ip",
        port: "port",
        versions: "1.16.5"
    }).on("messagestr", msg => {console.log(msg)});

<p>I have my own desktop application, it should launch a Minecraft bot for a local server. In my preload.js I add mineflayer (library)
Thus</p>
<pre><code>const { contextBridge } = require("electron");
const mineflayers = require("mineflayer");

contextBridge.exposeInMainWorld('libs', {
mineflayer: mineflayers,
});
</code></pre>
<p>In main.js</p>
<pre><code>const path = require('path');
const url = require('url');
const {app, BrowserWindow, ipcMain} = require('electron');

let win;

function createWindow() {
win = new BrowserWindow({
resizable: false,
width: 900,
height: 700,
autoHideMenuBar: true,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true
}
});

win.loadURL(url.format({
pathname: path.join(__dirname, './html/index.html'),
protocol: 'file:',
slashes: true
}));

win.webContents.openDevTools();

win.on('closed', () => {
win = null;
});
}

app.on('ready', createWindow);

app.on('window-all-closed', () => {
app.quit();
});
</code></pre>
<p>When I try to connect the bot to the server it fails. As soon as I try to use any mentod like bot.chat("message")
I'm getting the error chat is not a function</p>
<pre><code>$(".connect-button").click(() => {
let date = new Date()

$(".console-log").append(`<br>[${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}] Запуск...`);
const client = libs.mineflayer.createBot({
username: "testBot",
host: "ip",
port: "port",
versions: "1.16.5"
});

setTimeout(() => {
client.chat("10 sec");
}, 10000);
});
</code></pre>
<p>How do I use the library's functions?</p>
<p>I tried to use sandbox but realized that it was removed in version 1.20
I also tried adding the chat method directly to the bot’s connect function - similar</p>
<pre><code> const client = libs.mineflayer.createBot({
username: "testBot",
host: "ip",
port: "port",
versions: "1.16.5"
}).on("messagestr", msg => {console.log(msg)});
</code></pre>
 

Latest posts

Online statistics

Members online
0
Guests online
4
Total visitors
4
Top