PrismarineJS / mineflayer

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

bot.chat(message) not working #1452

Closed ghost closed 3 years ago

ghost commented 3 years ago

Hello, the function bot.chat(message) isn't working. Please help. Thank you.

amoraschi commented 3 years ago

Fill out the template, or paste the code please

ghost commented 3 years ago

So I am saving the bots in an array with bots.push(bot); and looping throught them with for(let b of bots) { b.chat("hi"); }

ghost commented 3 years ago

But it isn't working only with one bot

ghost commented 3 years ago

If I create a bot and chatting with him it don't do anything.

TheBlueBurger commented 3 years ago

Could you send the code where you push bots to the array?

ghost commented 3 years ago

let botsArray = []; function initBot(options) { let bot = mineflayer.createBot(options); bot.on('chat', function (username, message) { console.log("[CHAT/" + bot._client.username + "] <" + username + "> " + message) })

    bot.on('connect', function () {
        console.info("[CONNECTION/" + bot._client.username + "] Connected!");
    });

    bot.on('kicked', async (reason, loggedIn) =>  {onKick(reason, loggedIn)})
    bot.on('error', () =>  {setTimeout(() => {bot.connect(options)})})

    function onKick(reason, loggedIn) {
        console.log("[KICK/]", reason, "\nLoggedIn: ", loggedIn)
        if (json.modules.reconnectOnKick.enabled)
            bot.connect(options)
    }

    bots.push(bot);
    return bot;
}
ghost commented 3 years ago

And how can I get the raw message of bot.on('chat')?

ghost commented 3 years ago

So do you know how to fix this?

d-513 commented 3 years ago

Show your full code in a code block (```) but most likely you are using bot.chat before the "spawn" event

ghost commented 3 years ago

no I am not

ghost commented 3 years ago

I added a command for this

ghost commented 3 years ago

I can walk with them etc.

ghost commented 3 years ago

Now its working?!!!

rom1504 commented 3 years ago

Great, next time follow the template, it's very hard to help if you provide almost no information

ghost commented 3 years ago

I think npm install minecraft-protocl fixes it

abitalipovsanjar commented 1 year ago

Hello! Can I help you?

const mineflayer = require('mineflayer');

const bot = mineflayer.createBot({ host: 'mc.masedworld.net', port: 25565, username: 'milfoochkaa', version: '1.16' });

bot.once('spawn', () => { bot.on('chat', (username, message) => { if (username === bot.username) return; // игнорируем свои сообщения if (message.startsWith('!invite')) { const playerName = message.split(' ')[1]; inviteToClan(playerName); } });

bot.navigate.on('pathPartFound', function (path) { bot.chat(Going ${path.length} meters in the general direction for now.); });

bot.navigate.on('cannotFind', function (closestPath) { bot.chat(Unable to find path. Getting as close as possible.); bot.navigate.walk(closestPath); });

bot.navigate.on('arrived', function () { bot.chat('I have arrived'); });

bot.setControlState('forward', true);

setTimeout(() => { bot.setControlState('forward', false); bot.setControlState('right', true);

setTimeout(() => {
  bot.setControlState('right', false);
  bot.setControlState('forward', true);

  setTimeout(() => {
    bot.setControlState('forward', false);

    setInterval(() => {
      checkNearbyPlayers();
    }, 10000);
  }, 5000);
}, 800);

}, 2000); });

const inviteCooldown = 10 * 1000; let inviteCount = 0;

function inviteToClan(playerName) { bot.chat(/c invite ${playerName}); inviteCount++; setTimeout(() => { inviteCount--; }, inviteCooldown); }

function checkNearbyPlayers() { const players = bot.players; for (const playerName in players) { const player = players[playerName].entity; if (player && bot.entity.position.distanceTo(player.position) <= 50 && inviteCount < 2) { inviteToClan(playerName); } } }

const adMessages = [ "!&8&l&oПрисоединяйся к клану &4&l&oEndlessSeas&8&l&o и получай невероятное удовольствие! &f&o&n/warp EndLessSe", "!&8&l&oПрисоединяйся к клану &4&l&oEndlessSeas&8&l&o и получай невероятное удовольствие! &f&o&n/warp EndLessSe", "!&8&l&oИщешь клан для игры? &4&l&oEndlessSeas&8&l&o ждет тебя! Присоединяйся и стань частью команды! &f&o&n/warp EndLessSe", "!&4&l&oEndlessSeas&8&l&o - это не просто клан, это сплоченная команда единомышленников! &f&o&n/warp EndLessSe", "!&8&l&oВступай в кланс и получай поддержку и помощь в игре! &f&o&n/warp EndLessSe", "!&8&l&oХочешь играть в команде профессионалов? Тогда присоединяйся к &4&l&oEndlessSeas&8&l&o! &f&o&n/warp EndLessSe", "!&8&l&o&4&l&oEndlessSeas&8&l&o - это клан, который всегда поддержит своих участников! Присоединяйся! &f&o&n/warp EndLessSe", "!&8&l&oУстал играть один? В клане &4&l&oEndlessSeas&8&l&o ты найдешь новых друзей и единомышленников! &f&o&n/warp EndLessSe", "!&8&l&o&4&l&oEndlessSeas&8&l&o - это клан, где каждый может проявить свои навыки и достичь высот в игре! &f&o&n/warp EndLessSe", "!&8&l&oХочешь быть ебанутьм? Тогда не присоединяйся к &4&l&oEndlessSeas&8&l&o! &f&o&n/warp EndLessSe" ];

setInterval(() => { const message = adMessages[Math.floor(Math.random() * adMessages.length)]; bot.chat(message); }, 80000);

setInterval(() => { bot.chat('/warp qdwqvdusасiuvausi_zxов'); }, 5000);

Error

bot._client.chat(smallMsg) ^

TypeError: bot._client.chat is not a function at c:\Users\Acer\Desktop\stock\node_modules\mineflayer\lib\plugins\chat.js:177:21 at Array.forEach () at chatWithHeader (c:\Users\Acer\Desktop\stock\node_modules\mineflayer\lib\plugins\chat.js:171:25) at bot.chat (c:\Users\Acer\Desktop\stock\node_modules\mineflayer\lib\plugins\chat.js:207:5) at Timeout._onTimeout (c:\Users\Acer\Desktop\stock\stock.js:89:7) at listOnTimeout (node:internal/timers:569:17) at process.processTimers (node:internal/timers:512:7)

Node.js v18.16.0