chomens-bot-js/bot.js

187 lines
4.9 KiB
JavaScript
Raw Normal View History

2022-11-14 05:45:49 -05:00
/* 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
2022-11-14 05:45:49 -05:00
* @param {Class} dcclient discord client
2022-11-20 02:29:11 -05:00
* @param {object} rl readline.
* @return {object} the bot object
2022-11-14 05:45:49 -05:00
*/
2022-11-20 02:29:11 -05:00
async function createBot(server, config, getBots, dcclient, rl) {
2022-11-14 05:45:49 -05:00
const bot = new EventEmitter();
bot.options = {
2022-11-25 08:05:01 -05:00
username: !server.kaboom ?
2022-11-14 05:45:49 -05:00
'ChomeNS_Bot' :
randomstring.generate(8),
2022-11-25 08:05:01 -05:00
host: server.host,
port: server.port,
2022-11-14 05:45:49 -05:00
version: config.version,
kaboom: server.kaboom,
2022-11-14 05:45:49 -05:00
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);
2022-11-24 08:23:04 -05:00
bot.end = (reason = 'end', event) => {
bot.emit('end', reason, event);
2022-11-14 05:45:49 -05:00
bot.removeAllListeners();
bot._client.end();
bot._client.removeAllListeners();
};
bot.visibility = false;
bot.getBots = getBots;
2022-11-14 05:45:49 -05:00
bot.getplayerusername = {};
await sleep(200);
2022-11-20 02:29:11 -05:00
await loadPlugins(bot, dcclient, config, rl);
2022-11-14 05:45:49 -05:00
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;
bot.vmoptions = {
timeout: 2000,
sandbox: {
2022-11-24 05:21:20 -05:00
run(cmd) {
2022-11-14 05:45:49 -05:00
bot.core.run(cmd);
},
mc,
mineflayer,
chat: bot.chat,
moment,
randomstring,
uuid,
chatMessage,
crypto,
colorConvert,
2022-11-24 05:21:20 -05:00
bruhifyText(message) {
2022-11-14 05:45:49 -05:00
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) {
2022-11-24 08:23:04 -05:00
bot.end(reason, 'end');
2022-11-14 05:45:49 -05:00
});
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();
2022-11-20 02:29:11 -05:00
createBot(server, config, getBots, dcclient, rl);
2022-11-14 05:45:49 -05:00
}, timeout);
});
bot._client.on('keep_alive', (packet) => {
bot.write('keep_alive', {keepAliveId: packet.keepAliveId});
});
2022-11-21 07:50:02 -05:00
2022-11-14 05:45:49 -05:00
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);
2022-11-24 08:23:04 -05:00
bot.end(parsed, 'kick_disconnect');
2022-11-14 05:45:49 -05:00
});
bot._client.on('disconnect', function(data) {
const parsed = JSON.parse(data.reason);
2022-11-24 08:23:04 -05:00
bot.end(parsed, 'disconnect');
2022-11-14 05:45:49 -05:00
});
bot._client.on('error', function() {});
return bot;
2022-11-14 05:45:49 -05:00
};
module.exports = {createBot};