Files
NitoriBot/src/bot.js
T

64 lines
2.0 KiB
JavaScript
Raw Normal View History

2024-07-07 15:44:16 -04:00
const mc = require('minecraft-protocol')
const { EventEmitter } = require('events')
const fs = require('fs')
const path = require('path')
require("events").EventEmitter.defaultMaxListeners = Infinity;
function createBot(options = {}) {
const bot = new EventEmitter()
bot.options = {
// Set some default values in options
host: options.host ??= 'localhost',
username: options.username ??= 'Player',
hideErrors: options.hideErrors ??= true, // HACK: Hide errors by default as a lazy fix to console being spammed with them
};
bot.options = options
2024-08-09 22:08:42 -04:00
const ChatMessage = require('prismarine-chat')(bot.options.version);
2024-07-07 15:44:16 -04:00
// Create our client object, put it on the bot, and register some events
bot.on('init_client', client => {
client.on('packet', (data, meta) => {
bot.emit('packet', data, meta)
bot.emit('packet.' + meta.name, data)
})
client.on('login', () => {
bot.uuid = client.uuid
bot.username = client.username
})
client.on('disconnect', (data) => {
bot.emit("disconnect", data.reason)
})
client.on('end', reason => {
bot.emit('end', reason);
})
client.on('error', error => {
// bot.emit('error', error)
2024-08-09 22:08:42 -04:00
// console.log(error.toString())
bot.console.logs(ChatMessage.fromNotch('§8[§bClient Reconnect§8]§r ')?.toAnsi() + error.toString())
2024-08-04 22:59:52 -04:00
bot?.discord?.channel?.send(error.toString())
2024-07-07 15:44:16 -04:00
})
client.on("keep_alive", ({ keepAliveId }) => {
bot.emit("keep_alive", { keepAliveId })
})
client.on('kick_disconnect', (data) => {
bot.emit("kick_disconnect", data.reason)
2024-08-09 22:08:42 -04:00
bot.console.logs(ChatMessage.fromNotch('§8[§bClient Reconnect§8]§r ')?.toAnsi() + data.toString())
2024-07-07 15:44:16 -04:00
})
process.on("uncaughtException", (e) => {
2024-08-09 22:08:42 -04:00
// console?.warn(e.stack)
2024-07-07 15:44:16 -04:00
});
})
const client = options.client ?? mc.createClient(options)
bot._client = client
bot.emit('init_client', client)
bot.bots = options.bots ?? [bot]
return bot
}
module.exports = createBot