2024-08-22 13:50:44 -04:00
|
|
|
const fs = require('fs')
|
|
|
|
|
|
|
|
if (!fs.readdirSync('.').includes('settings.json')) {
|
2024-08-24 10:15:39 -04:00
|
|
|
console.log('Settings file is missing, using defaults.')
|
|
|
|
fs.copyFileSync('settings_example.json', 'settings.json')
|
2024-08-22 13:50:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!fs.readdirSync('.').includes('secret.json')) {
|
2024-08-24 10:15:39 -04:00
|
|
|
console.log('Secrets file is missing, using defaults.')
|
|
|
|
fs.copyFileSync('secret_example.json', 'secret.json')
|
|
|
|
console.log('Please change the hashing keys in the secrets file.')
|
2024-08-22 13:50:44 -04:00
|
|
|
}
|
|
|
|
|
2024-07-28 02:37:31 -04:00
|
|
|
const m = require('minecraft-protocol')
|
|
|
|
const generateUser = require('./util/usergen.js')
|
|
|
|
const EventEmitter = require('node:events')
|
2024-08-22 13:50:44 -04:00
|
|
|
const settings = require('./settings.json')
|
2024-09-02 16:41:07 -04:00
|
|
|
const secret = require('./secret.json')
|
2024-09-11 23:44:17 -04:00
|
|
|
module.exports.bots = []
|
2024-07-27 02:39:18 -04:00
|
|
|
|
2024-08-22 07:34:39 -04:00
|
|
|
const botplug = []
|
|
|
|
const bpl = fs.readdirSync('plugins')
|
|
|
|
for (const plugin of bpl) {
|
|
|
|
if (!plugin.endsWith('.js')) {
|
|
|
|
continue
|
2024-07-28 02:37:31 -04:00
|
|
|
}
|
2024-08-22 07:34:39 -04:00
|
|
|
try {
|
|
|
|
botplug.push(require(`./plugins/${plugin}`))
|
|
|
|
} catch (e) { console.log(e) }
|
|
|
|
}
|
|
|
|
|
|
|
|
const loadplug = (botno) => {
|
2024-07-28 02:37:31 -04:00
|
|
|
botplug.forEach((plug) => {
|
|
|
|
try {
|
2024-08-12 05:13:32 -04:00
|
|
|
if (plug.load) {
|
2024-09-11 23:57:32 -04:00
|
|
|
plug.load(module.exports.bots[botno])
|
2024-07-28 02:37:31 -04:00
|
|
|
}
|
|
|
|
} catch (e) { console.log(e) }
|
|
|
|
})
|
2024-07-27 02:39:18 -04:00
|
|
|
}
|
|
|
|
|
2024-07-28 02:37:31 -04:00
|
|
|
const createBot = function createBot (host, oldId) {
|
|
|
|
if (host.options.disabled) {
|
|
|
|
return
|
|
|
|
}
|
2024-09-02 16:41:07 -04:00
|
|
|
const options = {
|
2024-07-28 02:37:31 -04:00
|
|
|
host: host.host,
|
|
|
|
port: host.port ? host.port : 25565,
|
2024-08-12 05:13:32 -04:00
|
|
|
version: host.version ? host.version : settings.version_mc
|
2024-09-02 16:41:07 -04:00
|
|
|
}
|
2024-09-12 00:26:36 -04:00
|
|
|
if (host.options.online) {
|
2024-09-02 16:41:07 -04:00
|
|
|
options.username = secret.onlineEmail
|
|
|
|
options.password = secret.onlinePass
|
|
|
|
options.auth = 'microsoft'
|
|
|
|
} else {
|
|
|
|
options.username = generateUser(host.options.legalName)
|
|
|
|
}
|
|
|
|
const bot = new EventEmitter()
|
|
|
|
bot._client = m.createClient(options)
|
2024-07-28 02:37:31 -04:00
|
|
|
if (typeof oldId !== 'undefined') {
|
2024-09-11 23:57:32 -04:00
|
|
|
for (const i in module.exports.bots[oldId].interval) {
|
|
|
|
clearInterval(module.exports.bots[oldId].interval[i])
|
2024-07-27 02:39:18 -04:00
|
|
|
}
|
2024-09-11 23:57:32 -04:00
|
|
|
delete module.exports.bots[oldId]
|
2024-07-28 02:37:31 -04:00
|
|
|
bot.id = oldId
|
2024-09-11 23:44:17 -04:00
|
|
|
module.exports.bots[oldId] = bot
|
2024-07-28 02:37:31 -04:00
|
|
|
} else {
|
2024-09-11 23:57:32 -04:00
|
|
|
bot.id = module.exports.bots.length
|
2024-09-11 23:58:16 -04:00
|
|
|
module.exports.bots.push(bot)
|
2024-07-28 02:37:31 -04:00
|
|
|
}
|
2024-07-27 02:39:18 -04:00
|
|
|
|
2024-07-28 02:37:31 -04:00
|
|
|
bot.host = host
|
|
|
|
bot.interval = {}
|
2024-07-27 02:39:18 -04:00
|
|
|
|
2024-07-28 02:37:31 -04:00
|
|
|
bot.info = (msg) => {
|
|
|
|
console.log(`[${bot.id}] [info] ${msg}`)
|
|
|
|
}
|
2024-08-12 05:13:32 -04:00
|
|
|
|
|
|
|
bot.displayChat = (type, msg) => {
|
|
|
|
console.log(`[${bot.id}] [${type}] ${msg}`)
|
|
|
|
}
|
|
|
|
|
2024-07-28 02:37:31 -04:00
|
|
|
loadplug(bot.id)
|
|
|
|
bot._client.on('error', (err) => {
|
|
|
|
console.log(err)
|
|
|
|
})
|
2024-07-27 02:39:18 -04:00
|
|
|
}
|
|
|
|
|
2024-08-22 07:34:39 -04:00
|
|
|
for (const server of settings.servers) {
|
|
|
|
createBot(server)
|
2024-07-27 02:39:18 -04:00
|
|
|
}
|
2024-08-12 05:13:32 -04:00
|
|
|
|
2024-07-28 02:37:31 -04:00
|
|
|
module.exports.createBot = createBot
|