owobot/index.js

101 lines
2.7 KiB
JavaScript
Raw Normal View History

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. It is also recommended to remove permissions of other users to read from this file, for example, by giving it 600 permissions if running on a Unix or Unix-like OS.')
2024-08-22 13:50:44 -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
const botplug = []
const bpl = fs.readdirSync('plugins')
for (const plugin of bpl) {
if (!plugin.endsWith('.js')) {
continue
}
try {
botplug.push(require(`./plugins/${plugin}`))
} catch (e) { console.log(e) }
}
const loadplug = (botno) => {
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])
}
} catch (e) { console.log(e) }
})
2024-07-27 02:39:18 -04:00
}
const createBot = function createBot (host, oldId) {
if (host.options.disabled) {
return
}
2024-09-02 16:41:07 -04:00
const options = {
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)
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]
bot.id = oldId
2024-09-11 23:44:17 -04:00
module.exports.bots[oldId] = bot
} 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-27 02:39:18 -04:00
bot.host = host
2024-09-19 22:21:01 -04:00
if (bot.host.host.includes(':')) {
2024-09-13 23:24:48 -04:00
bot.host.options.displayAsIPv6 = true
}
bot.interval = {}
2024-07-27 02:39:18 -04:00
bot.info = (msg) => {
console.log(`[${bot.id}] [info] ${msg}`)
}
2024-08-12 05:13:32 -04:00
bot.displayChat = (type, subtype, msg) => {
2024-09-19 22:21:01 -04:00
if (settings.displaySubtypesToConsole) {
console.log(`[${bot.id}] [${type}] [${subtype}] ${msg}`)
} else {
console.log(`[${bot.id}] [${type}] ${msg}`)
}
2024-08-12 05:13:32 -04:00
}
loadplug(bot.id)
bot._client.on('error', (err) => {
console.log(err)
})
2024-07-27 02:39:18 -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
module.exports.createBot = createBot