botvX_mjs/index.mjs

94 lines
2.5 KiB
JavaScript
Raw Normal View History

import * as m from 'minecraft-protocol'
2024-08-19 14:39:45 -04:00
import { default as settings } from './settings.json' with {type: "json"}
import { generateUser } from './util/usergen.mjs'
import EventEmitter from 'node:events'
import * as fs from 'fs'
2024-08-12 04:33:43 -04:00
2024-08-20 08:18:06 -04:00
let bots = []
2024-08-12 04:33:43 -04:00
2024-08-19 14:20:15 -04:00
const botplug = [];
2024-08-19 14:24:55 -04:00
import { default as botplug_test } from "./plugins/testing.mjs" // We have to load plugins manually, because auto-detection does not work in this format
import { default as botplug_chat } from "./plugins/cq.mjs"
2024-08-19 14:39:45 -04:00
import { default as botplug_chatParse } from "./plugins/!chat.mjs"
import { default as botplug_player } from "./plugins/player.mjs"
import { default as botplug_cmd } from "./plugins/command.mjs"
import { default as botplug_cmdCore } from "./plugins/commandblock.mjs"
import { default as botplug_selfCare } from "./plugins/selfcare.mjs"
2024-08-20 05:36:12 -04:00
import { default as botplug_rejoin } from "./plugins/rejoin.mjs"
2024-08-20 06:04:23 -04:00
import { default as botplug_console } from "./plugins/console.mjs"
2024-08-20 06:45:07 -04:00
import { default as botplug_chatlog } from "./plugins/chatlog.mjs"
import { default as botplug_cloop } from "./plugins/cloop.mjs"
2024-08-19 14:20:15 -04:00
2024-08-19 14:24:55 -04:00
botplug.push(botplug_test)
botplug.push(botplug_chat)
2024-08-19 14:39:45 -04:00
botplug.push(botplug_chatParse)
botplug.push(botplug_player)
botplug.push(botplug_cmd)
botplug.push(botplug_cmdCore)
botplug.push(botplug_selfCare)
2024-08-20 05:36:12 -04:00
botplug.push(botplug_rejoin)
2024-08-20 06:04:23 -04:00
botplug.push(botplug_console)
2024-08-20 06:45:07 -04:00
botplug.push(botplug_chatlog)
botplug.push(botplug_cloop)
2024-08-19 14:20:15 -04:00
2024-08-17 09:56:26 -04:00
const loadplug = (botno) => {
2024-08-19 14:20:15 -04:00
botplug.forEach((plug) => {
2024-08-12 04:33:43 -04:00
try {
if (plug.load) {
2024-08-20 08:18:06 -04:00
plug.load(bots[botno])
2024-08-12 04:33:43 -04:00
}
} catch (e) { console.log(e) }
2024-08-19 14:20:15 -04:00
})
2024-08-12 04:33:43 -04:00
}
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,
2024-08-19 14:20:15 -04:00
username: generateUser(host.options.legalName),
2024-08-12 04:33:43 -04:00
version: host.version ? host.version : settings.version_mc
})
if (typeof oldId !== 'undefined') {
2024-08-20 08:18:06 -04:00
for (const i in bots[oldId].interval) {
clearInterval(bots[oldId].interval[i])
2024-08-12 04:33:43 -04:00
}
2024-08-20 08:18:06 -04:00
delete bots[oldId]
2024-08-12 04:33:43 -04:00
bot.id = oldId
2024-08-20 08:18:06 -04:00
bots[oldId] = bot
2024-08-12 04:33:43 -04:00
} else {
2024-08-20 08:18:06 -04:00
bot.id = bots.length
bots.push(bot)
2024-08-12 04:33:43 -04:00
}
2024-08-12 04:33:43 -04:00
bot.host = host
bot.interval = {}
bot.info = (msg) => {
console.log(`[${bot.id}] [info] ${msg}`)
}
bot.displayChat = (type, msg) => {
console.log(`[${bot.id}] [${type}] ${msg}`)
}
2024-08-19 14:20:15 -04:00
loadplug(bot.id)
2024-08-12 04:33:43 -04:00
bot._client.on('error', (err) => {
console.log(err)
})
}
2024-08-19 14:39:45 -04:00
for (const i in settings.servers) {
createBot(settings.servers[i])
2024-08-12 04:33:43 -04:00
}
export {
2024-08-20 08:18:06 -04:00
bots,
createBot
}
//module.exports.createBot = createBot