2024-10-23 10:33:14 -04:00
|
|
|
const mc = require('minecraft-protocol');
|
|
|
|
const { EventEmitter } = require('events');
|
2024-10-30 19:07:14 -04:00
|
|
|
EventEmitter.defaultMaxListeners = 5e6;
|
2024-08-21 12:20:11 -04:00
|
|
|
const util = require('util');
|
2024-10-23 10:33:14 -04:00
|
|
|
const ChatMessage = require('prismarine-chat');
|
2024-08-21 12:20:11 -04:00
|
|
|
function createBot(options = {}, config) {
|
2024-12-06 11:04:23 -05:00
|
|
|
let endCount = 0;
|
2024-09-13 08:23:42 -04:00
|
|
|
const bot = new EventEmitter();
|
2024-07-07 15:44:16 -04:00
|
|
|
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
|
|
|
|
};
|
2024-09-13 08:23:42 -04:00
|
|
|
bot.options = options;
|
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)
|
2024-12-06 11:04:23 -05:00
|
|
|
bot.emit('packet.' + meta.name, data);
|
2024-07-07 15:44:16 -04:00
|
|
|
})
|
|
|
|
|
2024-12-06 11:04:23 -05:00
|
|
|
client.on('login', (data) => {
|
2024-07-07 15:44:16 -04:00
|
|
|
bot.uuid = client.uuid
|
|
|
|
bot.username = client.username
|
2024-12-06 11:04:23 -05:00
|
|
|
bot.registry = require('prismarine-registry')(client.version);
|
2024-10-23 10:33:14 -04:00
|
|
|
bot.registry.language = require('./data/language.json');
|
2024-12-06 11:04:23 -05:00
|
|
|
bot.emit('registry_ready', bot.registry);
|
2024-07-07 15:44:16 -04:00
|
|
|
})
|
2024-10-30 19:07:14 -04:00
|
|
|
|
2024-10-23 10:33:14 -04:00
|
|
|
client.on('disconnect', data => {
|
|
|
|
bot.emit("disconnect", data);
|
|
|
|
bot.console.warn(`${ChatMessage(bot._client.version).fromNotch("§8[§bClient Reconnect§8]§r")?.toAnsi()} ${ChatMessage(bot._client.version).fromNotch(data.reason)?.toAnsi()}`)
|
2024-07-07 15:44:16 -04:00
|
|
|
})
|
2024-10-30 19:07:14 -04:00
|
|
|
|
2024-07-07 15:44:16 -04:00
|
|
|
client.on('end', reason => {
|
|
|
|
bot.emit('end', reason);
|
2024-10-23 10:33:14 -04:00
|
|
|
if (reason === "socketClosed") return;
|
2024-12-06 11:04:23 -05:00
|
|
|
bot.console.warn(ChatMessage(bot._client.version).fromNotch(`§8[§bClient Reconnect§8]§r ${reason}`)?.toAnsi());
|
2024-07-07 15:44:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
client.on('error', error => {
|
2024-12-06 11:04:23 -05:00
|
|
|
endCount++
|
|
|
|
if (endCount === 10) {
|
|
|
|
bot.console.info('stopped logging disconnect messages for now...');
|
|
|
|
bot?.discord?.channel?.send('stopped logging disconnect messages for now...');
|
|
|
|
return;
|
|
|
|
} else if (endCount > 10) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
bot.console.warn(ChatMessage(bot._client.version).fromNotch('§8[§bClient Reconnect§8]§r ')?.toAnsi() + util.inspect(error.toString()))
|
|
|
|
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-10-23 10:33:14 -04:00
|
|
|
bot.console?.warn(`${ChatMessage(bot._client.version).fromNotch("§8[§bClient Reconnect§8]§r")?.toAnsi()} ${ChatMessage(bot._client.version).fromNotch(data.reason)?.toAnsi()}`)
|
2024-08-21 12:20:11 -04:00
|
|
|
bot?.discord?.channel?.send(util.inspect(data.reason))
|
2024-07-07 15:44:16 -04:00
|
|
|
})
|
|
|
|
|
2024-12-06 11:04:23 -05:00
|
|
|
client.on('success', (data) => {
|
|
|
|
endCount = 0;
|
|
|
|
})
|
|
|
|
|
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
|
|
|
});
|
|
|
|
})
|
|
|
|
|
2024-10-23 10:33:14 -04:00
|
|
|
const client = options.client ?? new mc.createClient(bot.options)
|
2024-07-07 15:44:16 -04:00
|
|
|
bot._client = client
|
|
|
|
bot.emit('init_client', client)
|
|
|
|
bot.bots = options.bots ?? [bot]
|
|
|
|
return bot
|
|
|
|
}
|
2024-10-30 19:07:14 -04:00
|
|
|
module.exports = createBot;
|