chipmunkbot-archive/bot.js
2023-06-26 23:26:15 -04:00

65 lines
No EOL
2.2 KiB
JavaScript

const mc = require('minecraft-protocol')
const { EventEmitter } = require('events')
const parseText = require('./util/text_parser.js')
const fs = require('fs')
const path = require('path')
function createBot (options = {}) {
options.username = options.username ?? 'Bot'
//options.password = options.password ?? null
options.prefix = options.prefix ?? '!'
options.brand = options.brand ?? 'vanilla' //found that mineflayer has this so i added it here lol
options.colors = options.colors ?? { primary: 'white', secondary: 'gray', error: 'red' }
options.autoReconnect = options.autoReconnect ?? true
options.randomizeUsername = options.randomizeUsername ?? true;
if (options.randomizeUsername)
options.username += `§${String.fromCharCode(Math.floor(Math.random() * 65535))}`
let bot = new EventEmitter()
bot._client = options.client ?? mc.createClient(options)
//bot.host = function () { return bot._client.host }
//bot.username = function () { return bot._client.username }
//bot.uuid = function () { return bot._client.uuid }
bot.prefix = options.prefix
bot.colors = options.colors
bot.autoReconnect = options.autoReconnect
bot._client.on('connect', () => bot.emit('connect'))
bot._client.on('error', (err) => bot.emit('error', err))
bot.on('error', () => {}) // idc about errors lol
bot.end = () => bot._client.end()
bot._client.on('end', (reason) => {
bot.emit('end', reason)
setTimeout(() => bot = createBot(options), 6400)
})
//amogus
bot._client.on('login', () => bot.emit('login'))
bot.position = { x: null, y: null, z: null } //to prevent errors i guess
bot._client.on('position', (position) => bot.position = position)
bot.sendChat = (message) => bot._client.write('chat', { message })
const plugins = []
fs.readdirSync(
path.join(__dirname, 'plugins')
).forEach((file) => { // populate plugins array
if (file.endsWith(".js")) {
plugins.push(path.join(__dirname, 'plugins', file));
}
})
plugins.forEach((plugin) => {
try {
require(plugin)(bot)
} catch (e) {
console.log(`Error loading ${plugin}:`)
console.log(require('util').inspect(e))
}
})
}
module.exports = createBot