115 lines
3.4 KiB
JavaScript
115 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.styles ??= {}
|
|
options.styles.primary ??= { color: 'white' }
|
|
options.styles.secondary ??= { color: '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
|
|
|
|
// properties
|
|
bot.host = options.host
|
|
bot.port = options.port
|
|
|
|
bot.prefix = options.prefix
|
|
bot.brand = options.brand
|
|
bot.styles = options.styles
|
|
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.username = bot._client.username
|
|
bot.uuid = bot._client.uuid
|
|
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)
|
|
|
|
for (const plugin of plugins) plugin(bot, options) // Load plugins before emitting set_client so that plugins can listen for the event
|
|
|
|
bot.emit('set_client', bot._client)
|
|
|
|
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
|