access individual bot from from multiple_from_file.js #1924
-
const fs = require('fs')
const util = require('util')
const mineflayer = require('mineflayer')
const readFile = (fileName) => util.promisify(fs.readFile)(fileName, 'utf8')
const config = {
host: 'localhost',
port: 25565,
file: './accounts.txt',
interval: 500 // cooldown between joining server too prevent joining too quickly
}
function makeBot ([_u, _p], ix) {
return new Promise((resolve, reject) => {
setTimeout(() => {
const bot = mineflayer.createBot({
username: _u,
//password: _p,
host: config.host,
port: config.port
})
bot.on('spawn', () => resolve(bot))
bot.on('error', (err) => reject(err))
setTimeout(() => reject(Error('Took too long to spawn.')), 5000) // 5 sec
}, config.interval * ix)
})
}
async function main () {
// convert accounts.txt => array
const file = await readFile(config.file)
const accounts = file.split(/\r?\n/).map(login => login.split(':'))
const botProms = accounts.map(makeBot)
const bots = await Promise.allSettled(botProms)
// const bots = (await Promise.allSettled(botProms)).map(({ value, reason }) => value || reason).filter(value => !(value instanceof Error))
console.log(`Bots (${bots.length} / ${accounts.length}) successfully logged in.`)
}
main()Hey, this is the example code for loading multiple bots from a file, but how would you access an individual bot? Suppose I load in 5 bots, and I want to make the 3 one say something. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
let bot
function makeBot ([_u, _p], ix) {
return new Promise((resolve, reject) => {
setTimeout(() => {
bot = mineflayer.createBot(
setTimeout(() => reject(Error('Took too long to spawn.')), 5000) // 5 sec
}, config.interval * ix)
})
}
async function main () {
const bots = await Promise.allSettled(botProms)
bot.chat('hello')
}I've tried out something like this, but that only makes the last bot from the text file to say it. is there a way to access the bot from the 'bots' object |
Beta Was this translation helpful? Give feedback.
-
|
solved it. bots[2].value.chat('lmfao')
bots[0].value.chat('test')that code should be there in the main() function if you try to console.log(bots) then you'll see a javascript object, and you can access each bot from that. It's a little confusing, but you'll get the hand of it if you copy-paste it onto another file |
Beta Was this translation helpful? Give feedback.
solved it.
that code should be there in the main() function
if you try to console.log(bots) then you'll see a javascript object, and you can access each bot from that. It's a little confusing, but you'll get the hand of it if you copy-paste it onto another file