Added basic server joining functionality

This commit is contained in:
7cc5c4f330d47060 2024-10-20 14:39:25 -04:00
parent b59f0b9b07
commit 88c049b24f
Signed by: 7cc5c4f330d47060
SSH key fingerprint: SHA256:e+4tcZut1nBpe10PqjaO+Rvie0Q7W4qIvFzcUw+7riA
2 changed files with 48 additions and 0 deletions

43
index.js Normal file
View file

@ -0,0 +1,43 @@
import { createClient } from "minecraft-protocol";
import { default as settings } from './settings.json' with {type: "json"}
import { generateUser } from './util/usergen.js'
import EventEmitter from 'node:events'
const bots = [];
const createBot = function createBot (host, oldId) {
const bot = new EventEmitter()
bot._client = createClient({
host: host.host,
port: host.port ? host.port : 25565,
username: generateUser(host.options.legalName),
version: host.version ? host.version : settings.version_mc
})
if (typeof oldId !== 'undefined') {
for (const i in bots[oldId].interval) {
clearInterval(bots[oldId].interval[i])
}
delete bots[oldId]
bot.id = oldId
bots[oldId] = bot
} else {
bot.id = bots.length
bots.push(bot)
}
bot.host = host
bot.interval = {}
bot._client.on('error', (err) => {
console.log(err)
})
}
for (const i in settings.servers) {
createBot(settings.servers[i])
}
export {
bots,
createBot
}

5
util/usergen.js Normal file
View file

@ -0,0 +1,5 @@
import { randomBytes } from "crypto";
const generateUser = function (legal) {
return `colon3_${parseInt(randomBytes(3).toString("hex"),16)}`
}
export { generateUser }