chomens-bot-js/index.js

223 lines
5.9 KiB
JavaScript
Raw Normal View History

2022-11-09 05:15:59 -05:00
/* eslint-disable max-len */
2022-08-14 05:51:45 -04:00
/* eslint-disable no-var */
/* eslint-disable prefer-rest-params */
/* eslint-disable no-tabs */
/* eslint-disable no-undef */
const mc = require('minecraft-protocol');
2022-08-18 22:36:34 -04:00
const config = require('./config');
2022-08-14 05:51:45 -04:00
const crypto = require('crypto');
const colorConvert = require('color-convert');
const sleep = require('sleep-promise');
2022-08-16 07:38:50 -04:00
const generateEaglerUsername = require('./util/generateEaglerUsername');
2022-08-14 05:51:45 -04:00
const {EventEmitter} = require('events');
2022-11-08 05:43:24 -05:00
const {loadPlugins} = require('./util/loadPlugins');
2022-08-14 05:51:45 -04:00
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');
2022-08-22 06:16:36 -04:00
const cowsay = require('cowsay2');
2022-08-27 21:59:14 -04:00
const cows = require('cowsay2/cows');
2022-08-14 05:51:45 -04:00
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,
});
2022-11-11 07:03:44 -05:00
async function botThings() {
2022-08-14 05:51:45 -04:00
bot = new EventEmitter();
bot.options = {
2022-11-09 05:15:59 -05:00
username: process.argv[2] === 'mc.chomens41793.ga' ?
'ChomeNS_Bot' :
randomstring.generate(16),
2022-08-14 05:51:45 -04:00
host: process.argv[2],
port: process.argv[3] ? Number(process.argv[3]) : 25565,
version: config.version,
2022-08-14 05:51:45 -04:00
checkTimeoutInterval: '30000',
keepAlive: false,
hideErrors: true,
};
bot._client = mc.createClient(bot.options);
2022-10-18 21:37:33 -04:00
bot.version = bot._client.version;
2022-08-14 05:51:45 -04:00
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;
2022-08-14 05:51:45 -04:00
2022-11-11 07:03:44 -05:00
await sleep(500);
2022-11-09 07:04:14 -05:00
loadPlugins(bot, dcclient, config, rl);
2022-08-14 05:51:45 -04:00
}
/**
* Main bot function
*/
function main() {
2022-11-11 07:03:44 -05:00
const channel = dcclient.channels.cache.get(config.discord.servers[process.argv[2]]);
module.exports = bot;
2022-08-14 05:51:45 -04:00
bot.playersAddedPlayers = {};
bot.getplayerusername = {};
2022-10-22 01:58:07 -04:00
bot.once('end', (reason, event) => {
2022-11-09 05:15:59 -05:00
console.log(
`Disconnected from ${bot.options.host} (${event} event): ${util.inspect(reason)}`,
);
2022-10-24 21:43:51 -04:00
channel.send(`Disconnected: \`${util.inspect(reason)}\``);
2022-10-22 01:58:07 -04:00
2022-10-29 05:50:33 -04:00
let timeout = config.reconnectTimeout;
2022-10-22 01:58:07 -04:00
try {
if (reason.text) {
2022-11-09 05:15:59 -05:00
if (reason.text ===
'Wait 5 seconds before connecting, thanks! :)' ||
reason.text ===
'You are logging in too fast, try again later.'
) timeout = 1000 * 7;
}
2022-11-05 21:00:18 -04:00
} catch (e) {
console.log(e);
}
2022-10-22 01:58:07 -04:00
setTimeout(() => {
bot.end();
botThings();
main();
}, timeout);
});
2022-08-14 05:51:45 -04:00
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) {
2022-10-18 21:37:33 -04:00
bot.entityId = data.entityId;
2022-10-19 08:22:26 -04:00
bot.uuid = bot._client.uuid;
bot.username = bot._client.username;
2022-10-22 02:55:31 -04:00
chatMessage = require('prismarine-chat')(bot.version);
2022-10-18 20:59:58 -04:00
const mcData = require('minecraft-data')(bot.version);
2022-11-09 05:15:59 -05:00
console.log(
`Successfully logged in to: ${bot.options.host}:${bot.options.port}`,
);
channel.send(
`Successfully logged in to: \`${bot.options.host}:${bot.options.port}\``,
);
2022-08-14 05:51:45 -04:00
bot.eaglercrashstarted = false;
await sleep(1500);
bot.createCore();
bot.vmoptions = {
timeout: 2000,
sandbox: {
2022-08-17 07:59:26 -04:00
run: function(cmd) {
bot.core.run(cmd);
},
2022-10-24 05:42:18 -04:00
mc,
mineflayer,
2022-08-14 05:51:45 -04:00
chat: bot.chat,
2022-10-24 05:42:18 -04:00
moment,
randomstring,
uuid,
chatMessage,
crypto,
colorConvert,
2022-08-14 05:51:45 -04:00
bruhifyText: function(message) {
2022-11-09 05:15:59 -05:00
if (
typeof message !== 'string'
) throw new SyntaxError('message must be a string');
2022-08-14 05:51:45 -04:00
bot.bruhifyText = message.substring(0, 1000);
},
2022-10-24 05:42:18 -04:00
generateEaglerUsername,
cowsay,
cows,
mcData,
2022-08-14 05:51:45 -04:00
},
};
bot.vm = new VM(bot.vmoptions);
await sleep(1400);
2022-11-09 05:15:59 -05:00
bot.tellraw('@a', [
{
text: 'ChomeNS Bot',
color: 'yellow',
},
{
text: ' - ',
color: 'dark_gray',
},
{
text: 'made by ',
color: 'gray',
},
{
text: 'chayapak',
color: 'gold',
},
]);
2022-08-14 05:51:45 -04:00
});
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');
});
2022-11-09 05:15:59 -05:00
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});
});
2022-08-19 03:16:30 -04:00
2022-08-14 05:51:45 -04:00
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');
});
2022-10-26 08:53:12 -04:00
bot._client.on('error', function() {});
2022-08-14 05:51:45 -04:00
process.on('uncaughtException', (error) => {
console.log('uncaught ' + util.inspect(error));
channel.send('uncaught ```\n' + util.inspect(error) + '\n```');
2022-09-11 00:33:34 -04:00
bot.emit('end', 'uncaughtException', 'process: uncaughtException');
2022-08-14 05:51:45 -04:00
});
}
dcclient.on('ready', async () => {
botThings();
main();
});
dcclient.login(config.discord.token);