chipmunkbot3/bot.js
2024-02-11 21:23:41 -05:00

141 lines
4.1 KiB
JavaScript

const mc = require('minecraft-protocol')
const { EventEmitter } = require('events')
const fs = require('fs')
const path = require('path')
function createBots (servers = ['localhost'], options = {}) {
const bots = []
servers.forEach((server) => {
const a = server.split(':')
options.host = a[0]
options.port = parseInt(a[1])
options.brand = a[2]
const bot = createBot({ ...options })
bot.getBots = () => bots
bot.on('error', console.error) // "fix"
bots.push(bot)
})
return bots
}
function createBot (options = {}) {
// defaults
options.username ??= 'Bot'
// options.password = options.password ?? null
options.prefix ??= '!'
options.brand ??= 'vanilla' // found that mineflayer has this so i added it here lol
options.plugins ??= {}
fs.readdirSync(
'plugins'
).forEach((file) => { // populate plugins array
if (file.endsWith('.js') && options.plugins[file] == null) {
options.plugins[file] = require(path.resolve('plugins', file))
}
})
const plugins = []
Object.keys(options.plugins).forEach((key) => {
const plugin = options.plugins[key]
if (plugin) plugins.push(plugin)
})
options.colors ??= {}
options.colors.primary ??= 'white'
options.colors.secondary ??= 'green'
options.autoReconnect ??= false
options.randomizeUsername ??= false
options['online-mode'] ??= {}
options['online-mode'].enabled ??= false
options['online-mode'].username ??= null
options['online-mode'].password ??= null
// fard
if (options.randomizeUsername) {
options.username += '\u00a7' + String.fromCharCode(Math.floor(Math.random() * 65535))
}
if (options['online-mode'].enabled) {
options.username = options['online-mode'].username
options.password = options['online-mode'].password
}
// actually create the bot lol
const bot = new EventEmitter()
bot.plugins = plugins
bot.loadPlugin = loadPlugin
// add fards
bot.host = options.host
bot.port = options.port
/* bot._client.on('set_protocol', (packet) => {
bot.host = packet.serverHost
bot.port = packet.serverPort
}) */
// bot.username = () => bot._client.username
// bot.uuid = () => bot._client.uuid
bot.prefix = options.prefix
bot.brand = options.brand
bot.colors = options.colors
bot.autoReconnect = options.autoReconnect
bot.randomizeUsername = options.randomizeUsername
bot['online-mode'] = options['online-mode']
// set the client and add listeners
bot.on('set_client', (client) => {
client.on('connect', () => bot.emit('connect'))
client.on('error', (err) => bot.emit('error', err))
bot.end = (...r) => bot._client.end(...r)
client.on('end', (reason) => {
bot.loggedIn = false
bot.emit('end', reason)
// auto reconnect
if (bot.autoReconnect) {
setTimeout(() => {
if (bot.randomizeUsername && !bot['online-mode'].enabled) { options.username = options.username.slice(0, -2) + '\u00a7' + String.fromCharCode(Math.floor(Math.random() * 65535)) }
bot._client = mc.createClient(options)
bot.emit('set_client', bot._client)
}, 6000)
}
})
// more fard listeners
bot.loggedIn = false
bot._client.on('login', () => {
bot.loggedIn = true
bot.emit('login')
})
// position code
bot.position = { x: null, y: null, z: null } // to prevent errors i guess
bot._client.on('position', (position) => (bot.position = position))
// plugin injection
bot.plugins.forEach((plugin) => {
if (typeof plugin.client === 'function') plugin.client(bot, bot._client)
})
})
bot._client = options.client ?? mc.createClient(options)
bot.emit('set_client', bot._client)
bot.plugins.forEach((plugin) => {
if (typeof plugin.bot === 'function') plugin.bot(bot)
})
function loadPlugin (plugin) {
try {
if (typeof plugin.bot === 'function') plugin.bot(bot)
if (typeof plugin.client === 'function') plugin.client(bot, bot._client)
bot.plugins.push(plugin)
} catch (e) {
console.log(`Error loading ${plugin}:`)
console.log(require('util').inspect(e))
}
}
return bot
}
module.exports = { createBot, createBots }