CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/examples/multiple_from_file.js
Views: 789
1
/*
2
* This example is for people with multiple (mojang) minecraft accounts,
3
* in a file in the format "username:password" on each line,
4
* change the file property of config to set your .txt file location
5
*/
6
7
const fs = require('fs')
8
const util = require('util')
9
const mineflayer = require('mineflayer')
10
const readFile = (fileName) => util.promisify(fs.readFile)(fileName, 'utf8')
11
12
const config = {
13
host: 'localhost',
14
port: 25565,
15
file: './accounts.txt',
16
interval: 500 // cooldown between joining server too prevent joining too quickly
17
}
18
19
function makeBot ([_u, _p], ix) {
20
return new Promise((resolve, reject) => {
21
setTimeout(() => {
22
const bot = mineflayer.createBot({
23
username: _u,
24
password: _p,
25
host: config.host,
26
port: config.port
27
})
28
bot.on('spawn', () => resolve(bot))
29
bot.on('error', (err) => reject(err))
30
setTimeout(() => reject(Error('Took too long to spawn.')), 5000) // 5 sec
31
}, config.interval * ix)
32
})
33
}
34
35
async function main () {
36
// convert accounts.txt => array
37
const file = await readFile(config.file)
38
const accounts = file.split(/\r?\n/).map(login => login.split(':'))
39
const botProms = accounts.map(makeBot)
40
// const bots = await Promise.allSettled(botProms)
41
const bots = (await Promise.allSettled(botProms)).map(({ value, reason }) => value || reason).filter(value => !(value instanceof Error))
42
console.log(`Bots (${bots.length} / ${accounts.length}) successfully logged in.`)
43
}
44
45
main()
46
47