chipmunkbot3/bot.js

154 lines
4.4 KiB
JavaScript

const mc = require('minecraft-protocol')
const nbt = require('prismarine-nbt')
const { EventEmitter } = require('events')
const fs = require('fs')
const path = require('path')
const fileExists = require('./util/file_exists')
const crypto = require('crypto')
const plugins = []
for (const filename of fs.readdirSync(path.resolve(__dirname, 'plugins'))) {
if (!filename.endsWith('.js')) return
plugins.push(require(path.resolve(__dirname, 'plugins', filename)))
}
const usernameSchemes = {
async random_playerdata (bot, options) {
const playerDataDir = await path.join(bot.paths.persistent, 'playerdata')
if (!await fileExists(playerDataDir)) return undefined
let subdir
try {
const subdirs = (await fs.promises.readdir(playerDataDir)).filter(filename => !filename.endsWith('_old'))
subdir = path.join(playerDataDir, subdirs[crypto.randomInt(0, subdirs.length)])
} catch {
return undefined
}
let data
try {
const files = await fs.promises.readdir(subdir)
const raw = await fs.promises.readFile(path.join(subdir, files[crypto.randomInt(0, files.length)]))
data = await nbt.parse(raw)
} catch {
return undefined
}
return data?.parsed?.value?.username?.value
}
}
function createBot (options = {}) {
// defaults
options.username ??= 'Bot'
// options.password = options.password ?? null
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
options.paths ??= {}
options.paths.logs ??= 'logs'
options.paths.music ??= 'music'
options.paths.images ??= 'images'
options.paths.persistent ??= 'persistent'
// 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 ?? 25565
bot.hostPortPair = bot.port !== 25565 ? bot.host + ':' + bot.port : bot.host
bot.brand = options.brand
bot.styles = options.styles
bot.paths = options.paths
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 && !options.client) {
setTimeout(() => {
bot._initClient()
}, 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')
})
bot._client.on('packet', (data, meta) => {
bot.emit('packet', data, meta);
bot.emit('packet.' + meta.name, data);
})
})
async function _initClient () {
const usernameScheme = typeof options.usernameScheme === 'function' ? options.usernameScheme : usernameSchemes[options.usernameScheme]
const username = await usernameScheme?.(bot, options)
if (username) options.username = username
bot._client = mc.createClient(options)
bot.emit('set_client', bot._client)
}
bot._initClient = _initClient
for (const plugin of plugins) plugin(bot, options)
if (options.client) {
bot._client = options.client
bot.emit('set_client', bot._client)
} else {
bot._initClient()
}
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