FridayNightFunkinBoyfriendBot/src/bot.js

70 lines
3 KiB
JavaScript
Raw Normal View History

2024-07-07 15:44:16 -04:00
const mc = require('minecraft-protocol')
const { EventEmitter } = require('events')
require("events").EventEmitter.defaultMaxListeners = Infinity;
const util = require('util');
function createBot(options = {}, config) {
2024-07-07 15:44:16 -04:00
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", JSON.stringify(data.reason))
bot?.discord?.channel?.send(util.inspect(data.reason))
if (config.console.filelogger) {
bot?.console?.filelogging(`[${new Date().toLocaleTimeString("en-US", { timeZone: "America/CHICAGO", })} ${new Date().toLocaleDateString("en-US", { timeZone: "America/CHICAGO", })} logs] [${options.serverName}] ` + '[Client Reconnect] ' + util.inspect(data.reason))
}
2024-07-07 15:44:16 -04:00
})
client.on('end', reason => {
bot.emit('end', reason);
})
client.on('error', error => {
bot.console.logs(ChatMessage.fromNotch('§8[§bClient Reconnect§8]§r ')?.toAnsi() + util.inspect(error.toString()))
2024-08-04 22:59:52 -04:00
bot?.discord?.channel?.send(error.toString())
if (config.console.filelogger) {
bot?.console?.filelogging(`[${new Date().toLocaleTimeString("en-US", { timeZone: "America/CHICAGO", })} ${new Date().toLocaleDateString("en-US", { timeZone: "America/CHICAGO", })} logs] [${options.serverName}] ` + '[Client Reconnect] ' + util.inspect(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)
bot.console.logs(ChatMessage.fromNotch('§8[§bClient Reconnect§8]§r ')?.toAnsi() + util.inspect(data.reason))
bot?.discord?.channel?.send(util.inspect(data.reason))
if (config.console.filelogger) {
bot?.console?.filelogging(`[${new Date().toLocaleTimeString("en-US", { timeZone: "America/CHICAGO", })} ${new Date().toLocaleDateString("en-US", { timeZone: "America/CHICAGO", })} logs] [${options.serverName}] ` + '[Client Reconnect] ' + util.inspect(data.reason))
}
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