mirror of
https://github.com/ChomeNS/chomens-bot-mc.git
synced 2024-11-14 10:44:55 -05:00
222 lines
5.9 KiB
JavaScript
222 lines
5.9 KiB
JavaScript
/* eslint-disable max-len */
|
|
/* eslint-disable no-var */
|
|
/* eslint-disable prefer-rest-params */
|
|
/* eslint-disable no-tabs */
|
|
/* eslint-disable no-undef */
|
|
const mc = require('minecraft-protocol');
|
|
const config = require('./config');
|
|
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 readline = require('node:readline');
|
|
const {stdin: input, stdout: output} = require('node:process');
|
|
const rl = readline.createInterface({input, output});
|
|
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');
|
|
// readline > fix on console.log
|
|
const log = console.log;
|
|
console.log = function() {
|
|
rl.output.write('\x1b[2K\r');
|
|
log.apply(console, Array.prototype.slice.call(arguments));
|
|
rl._refreshLine();
|
|
};
|
|
|
|
// Load discord.js
|
|
const {
|
|
Client,
|
|
Intents,
|
|
} = require('discord.js');
|
|
// Create Discord intentions, required in v13
|
|
const intents = new Intents(['GUILDS', 'GUILD_MESSAGES']);
|
|
// Create Discord client
|
|
const dcclient = new Client({
|
|
intents,
|
|
});
|
|
|
|
async function botThings() {
|
|
bot = new EventEmitter();
|
|
bot.options = {
|
|
username: process.argv[2] === 'mc.chomens41793.ga' ?
|
|
'ChomeNS_Bot' :
|
|
randomstring.generate(16),
|
|
host: process.argv[2],
|
|
port: process.argv[3] ? Number(process.argv[3]) : 25565,
|
|
version: config.version,
|
|
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.getplayerusername = {};
|
|
if (typeof bot.messageLogging === 'undefined') bot.messageLogging = true;
|
|
|
|
await sleep(500);
|
|
|
|
loadPlugins(bot, dcclient, config, rl);
|
|
}
|
|
|
|
/**
|
|
* Main bot function
|
|
*/
|
|
function main() {
|
|
const channel = dcclient.channels.cache.get(config.discord.servers[process.argv[2]]);
|
|
|
|
module.exports = bot;
|
|
|
|
bot.playersAddedPlayers = {};
|
|
bot.getplayerusername = {};
|
|
|
|
bot.once('end', (reason, event) => {
|
|
console.log(
|
|
`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) {
|
|
console.log(e);
|
|
}
|
|
|
|
setTimeout(() => {
|
|
bot.end();
|
|
botThings();
|
|
main();
|
|
}, timeout);
|
|
});
|
|
|
|
console.log(`Connecting to: ${bot.options.host}:${bot.options.port}...`);
|
|
channel.send(`Connecting to: \`${bot.options.host}:${bot.options.port}\`...`);
|
|
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);
|
|
console.log(
|
|
`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._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() {});
|
|
|
|
process.on('uncaughtException', (error) => {
|
|
console.log('uncaught ' + util.inspect(error));
|
|
channel.send('uncaught ```\n' + util.inspect(error) + '\n```');
|
|
bot.emit('end', 'uncaughtException', 'process: uncaughtException');
|
|
});
|
|
}
|
|
|
|
dcclient.on('ready', async () => {
|
|
botThings();
|
|
main();
|
|
});
|
|
|
|
dcclient.login(config.discord.token);
|