chipmunkbot3/bot.js

116 lines
3.4 KiB
JavaScript

const mc = require('minecraft-protocol')
const { EventEmitter } = require('events')
const fs = require('fs')
const path = require('path')
const plugins = []
for (const filename of fs.readdirSync('plugins')) {
if (!filename.endsWith('.js')) return
plugins.push(require(path.resolve('plugins', filename)))
}
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.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', (packet) => {
bot.position = packet
bot._client.write('teleport_confirm', { teleportId: packet.teleportId })
})
bot._client.on('packet', (data, meta) => {
bot.emit('packet', data, meta);
bot.emit('packet.' + meta.name, data);
})
})
bot._client = options.client ?? mc.createClient(options)
bot.emit('set_client', bot._client)
for (const plugin of plugins) plugin(bot, options)
function loadPlugin (plugin) {
try {
plugin(bot, options)
} catch (e) {
console.log(`Error loading ${plugin}:`)
console.log(require('util').inspect(e))
}
}
return bot
}
module.exports = createBot