2024-08-12 04:33:43 -04:00
|
|
|
const m = require('minecraft-protocol')
|
|
|
|
const settings = require('./settings.json')
|
|
|
|
const generateUser = require('./util/usergen.js')
|
|
|
|
const EventEmitter = require('node:events')
|
|
|
|
const fs = require('fs')
|
|
|
|
|
|
|
|
module.exports.bot = []
|
|
|
|
|
2024-08-17 09:56:26 -04:00
|
|
|
const botplug = []
|
|
|
|
const bpl = fs.readdirSync('plugins')
|
2024-08-21 16:52:04 -04:00
|
|
|
for (const plugin of bpl) {
|
|
|
|
if (!plugin.endsWith('.js')) {
|
2024-08-17 09:56:26 -04:00
|
|
|
continue
|
2024-08-12 04:33:43 -04:00
|
|
|
}
|
2024-08-17 09:56:26 -04:00
|
|
|
try {
|
2024-08-21 16:52:04 -04:00
|
|
|
botplug.push(require(`./plugins/${plugin}`))
|
2024-08-17 09:56:26 -04:00
|
|
|
} catch (e) { console.log(e) }
|
|
|
|
}
|
|
|
|
|
|
|
|
const loadplug = (botno) => {
|
2024-08-12 04:33:43 -04:00
|
|
|
botplug.forEach((plug) => {
|
|
|
|
try {
|
|
|
|
if (plug.load) {
|
|
|
|
plug.load(module.exports.bot[botno])
|
|
|
|
}
|
|
|
|
} catch (e) { console.log(e) }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const createBot = function createBot (host, oldId) {
|
|
|
|
if (host.options.disabled) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const bot = new EventEmitter()
|
|
|
|
bot._client = m.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 module.exports.bot[oldId].interval) {
|
|
|
|
clearInterval(module.exports.bot[oldId].interval[i])
|
|
|
|
}
|
|
|
|
delete module.exports.bot[oldId]
|
|
|
|
bot.id = oldId
|
|
|
|
module.exports.bot[oldId] = bot
|
|
|
|
} else {
|
|
|
|
bot.id = module.exports.bot.length
|
|
|
|
module.exports.bot.push(bot)
|
|
|
|
}
|
|
|
|
|
|
|
|
bot.host = host
|
|
|
|
bot.interval = {}
|
|
|
|
|
|
|
|
bot.info = (msg) => {
|
|
|
|
console.log(`[${bot.id}] [info] ${msg}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
bot.displayChat = (type, msg) => {
|
|
|
|
console.log(`[${bot.id}] [${type}] ${msg}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
loadplug(bot.id)
|
|
|
|
bot._client.on('error', (err) => {
|
|
|
|
console.log(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-08-21 16:52:04 -04:00
|
|
|
for (const server of settings.servers) {
|
|
|
|
createBot(server)
|
2024-08-12 04:33:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.createBot = createBot
|