mirror of
https://github.com/ChomeNS/chomens-bot-mc.git
synced 2024-11-14 10:44:55 -05:00
189 lines
5.2 KiB
JavaScript
189 lines
5.2 KiB
JavaScript
/* eslint-disable max-len */
|
|
|
|
const mc = require('minecraft-protocol');
|
|
const crypto = require('crypto');
|
|
const colorConvert = require('color-convert');
|
|
const sleep = require('sleep-promise');
|
|
const generateEaglerUsername = require('./util/generateEaglerUsername');
|
|
const {EventEmitter} = require('events');
|
|
const {loadPlugins} = require('./util/loadPlugins');
|
|
const uuid = require('uuid-by-string');
|
|
const moment = require('moment-timezone');
|
|
const cowsay = require('cowsay2');
|
|
const cows = require('cowsay2/cows');
|
|
const util = require('node:util');
|
|
const {VM} = require('vm2');
|
|
const randomstring = require('randomstring');
|
|
const mineflayer = require('mineflayer');
|
|
|
|
/**
|
|
* makes the bot
|
|
* @param {object} server the server object used in the config
|
|
* @param {object} config the config file
|
|
* @param {Function} getBots get bots function in index.js
|
|
* @param {Class} dcclient discord client
|
|
* @param {object} rl readline.
|
|
* @return {object} the bot object
|
|
*/
|
|
async function createBot(server, config, getBots, dcclient, rl) {
|
|
const bot = new EventEmitter();
|
|
bot.options = {
|
|
username: /* process.argv[2] */ server.host === 'mc.chomens41793.ga' ?
|
|
'ChomeNS_Bot' :
|
|
randomstring.generate(16),
|
|
host: /* process.argv[2] */ server.host,
|
|
port: /* process.argv[3] ? Number(process.argv[3]) : 25565*/ server.port,
|
|
version: config.version,
|
|
kaboom: server.kaboom,
|
|
checkTimeoutInterval: '30000',
|
|
keepAlive: false,
|
|
hideErrors: true,
|
|
};
|
|
bot._client = mc.createClient(bot.options);
|
|
bot.version = bot._client.version;
|
|
bot.write = (name, data) => bot._client.write(name, data);
|
|
bot.end = (reason = 'end') => {
|
|
bot.emit('end', reason);
|
|
bot.removeAllListeners();
|
|
bot._client.end();
|
|
bot._client.removeAllListeners();
|
|
};
|
|
bot.visibility = false;
|
|
bot.getBots = getBots;
|
|
bot.getplayerusername = {};
|
|
if (typeof bot.messageLogging === 'undefined') bot.messageLogging = true;
|
|
|
|
await sleep(200);
|
|
|
|
await loadPlugins(bot, dcclient, config, rl);
|
|
|
|
const channel = dcclient.channels.cache.get(config.discord.servers[bot.options.host]);
|
|
|
|
bot.playersAddedPlayers = {};
|
|
bot.getplayerusername = {};
|
|
|
|
bot._client.on('login', async function(data) {
|
|
bot.entityId = data.entityId;
|
|
bot.uuid = bot._client.uuid;
|
|
bot.username = bot._client.username;
|
|
chatMessage = require('prismarine-chat')(bot.version);
|
|
const mcData = require('minecraft-data')(bot.version);
|
|
bot.console.info(
|
|
`Successfully logged in to: ${bot.options.host}:${bot.options.port}`,
|
|
);
|
|
channel.send(
|
|
`Successfully logged in to: \`${bot.options.host}:${bot.options.port}\``,
|
|
);
|
|
bot.eaglercrashstarted = false;
|
|
// await sleep(1500);
|
|
// bot.createCore();
|
|
|
|
bot.vmoptions = {
|
|
timeout: 2000,
|
|
sandbox: {
|
|
run: function(cmd) {
|
|
bot.core.run(cmd);
|
|
},
|
|
mc,
|
|
mineflayer,
|
|
chat: bot.chat,
|
|
moment,
|
|
randomstring,
|
|
uuid,
|
|
chatMessage,
|
|
crypto,
|
|
colorConvert,
|
|
bruhifyText: function(message) {
|
|
if (
|
|
typeof message !== 'string'
|
|
) throw new SyntaxError('message must be a string');
|
|
bot.bruhifyText = message.substring(0, 1000);
|
|
},
|
|
generateEaglerUsername,
|
|
cowsay,
|
|
cows,
|
|
mcData,
|
|
},
|
|
};
|
|
bot.vm = new VM(bot.vmoptions);
|
|
|
|
// await sleep(1400);
|
|
// bot.tellraw('@a', [
|
|
// {
|
|
// text: 'ChomeNS Bot',
|
|
// color: 'yellow',
|
|
// },
|
|
// {
|
|
// text: ' - ',
|
|
// color: 'dark_gray',
|
|
// },
|
|
// {
|
|
// text: 'made by ',
|
|
// color: 'gray',
|
|
// },
|
|
// {
|
|
// text: 'chayapak',
|
|
// color: 'gold',
|
|
// },
|
|
// ]);
|
|
});
|
|
|
|
bot.on('player_added', (player) => {
|
|
bot.playersAddedPlayers[player.name] = player.UUID;
|
|
bot.getplayerusername[player.UUID] = player.name;
|
|
});
|
|
|
|
bot._client.on('end', function(reason) {
|
|
bot.emit('end', reason, 'end');
|
|
});
|
|
|
|
bot.once('end', (reason, event) => {
|
|
bot.console.info(
|
|
`Disconnected from ${bot.options.host} (${event} event): ${util.inspect(reason)}`,
|
|
);
|
|
channel.send(`Disconnected: \`${util.inspect(reason)}\``);
|
|
|
|
let timeout = config.reconnectTimeout;
|
|
|
|
try {
|
|
if (reason.text) {
|
|
if (reason.text ===
|
|
'Wait 5 seconds before connecting, thanks! :)' ||
|
|
reason.text ===
|
|
'You are logging in too fast, try again later.'
|
|
) timeout = 1000 * 7;
|
|
}
|
|
} catch (e) {
|
|
bot.console.error(e);
|
|
}
|
|
|
|
setTimeout(() => {
|
|
bot.end();
|
|
createBot(server, config, getBots, dcclient, rl);
|
|
}, timeout);
|
|
});
|
|
|
|
bot._client.on('keep_alive', (packet) => {
|
|
bot.write('keep_alive', {keepAliveId: packet.keepAliveId});
|
|
});
|
|
|
|
bot._client.on('game_state_change', () => {
|
|
bot.write('client_command', {payload: 0});
|
|
});
|
|
|
|
bot._client.on('kick_disconnect', function(data) {
|
|
const parsed = JSON.parse(data.reason);
|
|
bot.emit('end', parsed, 'kick_disconnect');
|
|
});
|
|
|
|
bot._client.on('disconnect', function(data) {
|
|
const parsed = JSON.parse(data.reason);
|
|
bot.emit('end', parsed, 'disconnect');
|
|
});
|
|
|
|
bot._client.on('error', function() {});
|
|
|
|
return bot;
|
|
};
|
|
|
|
module.exports = {createBot};
|