Rewrite index.js and the username generator

This commit is contained in:
7cc5c4f330d47060 2024-08-19 14:07:17 -04:00
parent 977ed314ac
commit e8cb2cea8d
Signed by: 7cc5c4f330d47060
SSH key fingerprint: SHA256:e+4tcZut1nBpe10PqjaO+Rvie0Q7W4qIvFzcUw+7riA
2 changed files with 25 additions and 24 deletions

View file

@ -1,30 +1,30 @@
const m = require('minecraft-protocol') import * as m from 'minecraft-protocol'
const settings = require('./settings.json') import * as settings from './settings.json' with {type: "json"}
const generateUser = require('./util/usergen.js') import * as generateUser from './util/usergen.js'
const EventEmitter = require('node:events') import EventEmitter from 'node:events'
const fs = require('fs') import * as fs from 'fs'
module.exports.bot = [] let botArray = []
const botplug = [] const botplug = []
const bpl = fs.readdirSync('plugins') const bpl = fs.readdirSync('plugins')
for (const i in bpl) { for (const i in bpl) {
if (!bpl[i].endsWith('.js')) { /*if (!bpl[i].endsWith('.js')) {
continue continue
} }
try { try {
botplug.push(require(`./plugins/${bpl[i]}`)) botplug.push(require(`./plugins/${bpl[i]}`))
} catch (e) { console.log(e) } } catch (e) { console.log(e) }*/
} }
const loadplug = (botno) => { const loadplug = (botno) => {
botplug.forEach((plug) => { /*botplug.forEach((plug) => {
try { try {
if (plug.load) { if (plug.load) {
plug.load(module.exports.bot[botno]) plug.load(botArray[botno])
} }
} catch (e) { console.log(e) } } catch (e) { console.log(e) }
}) })*/
} }
const createBot = function createBot (host, oldId) { const createBot = function createBot (host, oldId) {
@ -35,19 +35,19 @@ const createBot = function createBot (host, oldId) {
bot._client = m.createClient({ bot._client = m.createClient({
host: host.host, host: host.host,
port: host.port ? host.port : 25565, port: host.port ? host.port : 25565,
username: generateUser(host.options.legalName), username: generateUser.generateUser(host.options.legalName),
version: host.version ? host.version : settings.version_mc version: host.version ? host.version : settings.version_mc
}) })
if (typeof oldId !== 'undefined') { if (typeof oldId !== 'undefined') {
for (const i in module.exports.bot[oldId].interval) { for (const i in botArray[oldId].interval) {
clearInterval(module.exports.bot[oldId].interval[i]) clearInterval(botArray[oldId].interval[i])
} }
delete module.exports.bot[oldId] delete botArray[oldId]
bot.id = oldId bot.id = oldId
module.exports.bot[oldId] = bot botArray[oldId] = bot
} else { } else {
bot.id = module.exports.bot.length bot.id = botArray.length
module.exports.bot.push(bot) botArray.push(bot)
} }
bot.host = host bot.host = host
@ -61,14 +61,14 @@ const createBot = function createBot (host, oldId) {
console.log(`[${bot.id}] [${type}] ${msg}`) console.log(`[${bot.id}] [${type}] ${msg}`)
} }
loadplug(bot.id) //loadplug(bot.id)
bot._client.on('error', (err) => { bot._client.on('error', (err) => {
console.log(err) console.log(err)
}) })
} }
for (const i in settings.servers) { for (const i in settings.default.servers) {
createBot(settings.servers[i]) createBot(settings.default.servers[i])
} }
module.exports.createBot = createBot //module.exports.createBot = createBot

View file

@ -29,10 +29,11 @@ const rsg = function (count) {
} }
return output return output
} }
module.exports = function (legal) { const generateUser = function (legal) {
if (legal) { if (legal) {
return Math.floor(Math.random() * 1000000).toString() return Math.floor(Math.random() * 1000000).toString()
} else { } else {
return rsg(6 + Math.floor(Math.random() * 3)) return rsg(6 + Math.floor(Math.random() * 3))
} }
} }
export { generateUser }