mirror of
https://github.com/ChomeNS/chomens-bot-mc.git
synced 2024-11-14 10:44:55 -05:00
518 lines
14 KiB
JavaScript
518 lines
14 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 {containsIllegalCharacters} = require('./util/containsIllegalCharacters');
|
||
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 {escapeMarkdown} = require('./util/escapeMarkdown');
|
||
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,
|
||
});
|
||
|
||
const ayunboomchannel = config.discord.servers['sus.shhnowisnottheti.me:25565'];
|
||
const dinboomchannel = config.discord.servers['129.159.58.114:25565'];
|
||
const kaboomchannel = config.discord.servers['kaboom.pw:25565'];
|
||
const localclonechannel = config.discord.servers['192.168.1.103:25565'];
|
||
const kitsunechannel = config.discord.servers['kitsune.icu:25565'];
|
||
const chomensserverchannel = config.discord.servers['mc.chomens41793.ga:25565'];
|
||
let chomenschannel = '969773424387981356';
|
||
const hashchannel = '980438368422871151';
|
||
const ownerhashchannel = '980786390247833620';
|
||
|
||
/**
|
||
* empty function for discord message
|
||
* @return {String}
|
||
*/
|
||
function dcmsg() {
|
||
return 'this does nothing...';
|
||
}
|
||
|
||
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.queue = [];
|
||
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 = {};
|
||
|
||
loadPlugins(bot, dcclient, config);
|
||
}
|
||
|
||
let messageloggingEnabled = true;
|
||
|
||
/**
|
||
* Main bot function
|
||
*/
|
||
function main() {
|
||
// allink's chat queue
|
||
chatQueue = setInterval(function() {
|
||
try {
|
||
if (bot.queue[0] || bot.queue[0] === '') {
|
||
try {
|
||
if (containsIllegalCharacters(bot.queue[0])) {
|
||
bot.queue.shift();
|
||
return;
|
||
};
|
||
bot.write('chat', {message: bot.queue[0].substring(0, 256)});
|
||
bot.queue.shift();
|
||
} catch (e) {
|
||
console.log(e.message);
|
||
}
|
||
}
|
||
} catch (e) {
|
||
return;
|
||
}
|
||
}, 450);
|
||
|
||
module.exports = function() {
|
||
return bot;
|
||
};
|
||
|
||
dcmsg.queue = '';
|
||
consoleQueue = [];
|
||
|
||
bot.playersAddedPlayers = {};
|
||
bot.getplayerusername = {};
|
||
|
||
bot.hash = '';
|
||
|
||
bot.chat = (message) => {
|
||
bot.queue.push(String(message));
|
||
};
|
||
|
||
discordQueue = setInterval(function() {
|
||
if (dcmsg.queue!='') {
|
||
channel.send({
|
||
content: '```ansi\n' + dcmsg.queue.substring(0, 1986) + '\n```',
|
||
allowedMentions: {
|
||
parse: [],
|
||
},
|
||
});
|
||
dcmsg.queue = '';
|
||
}
|
||
}, 1000);
|
||
|
||
consoleQueueInterval = setInterval(function() {
|
||
if (!messageloggingEnabled) return;
|
||
if (consoleQueue.length > 50) consoleQueue = [];
|
||
if (consoleQueue[0] || consoleQueue[0] === '') {
|
||
console.log(
|
||
'\u001b[48:5:208m' +
|
||
bot.options.host +
|
||
'\u001b[0m' +
|
||
': ' +
|
||
consoleQueue[0].substring(0, 10000),
|
||
);
|
||
consoleQueue.shift();
|
||
}
|
||
}, 100);
|
||
|
||
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);
|
||
}
|
||
|
||
try {
|
||
clearInterval(notonline);
|
||
clearInterval(discordQueue);
|
||
clearInterval(chatQueue);
|
||
clearInterval(consoleQueueInterval);
|
||
} catch (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}\``,
|
||
);
|
||
previusMessage = undefined;
|
||
loginfinish = false;
|
||
started = false;
|
||
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);
|
||
|
||
loginfinish = true;
|
||
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('parsed_chat', async function(message, data) {
|
||
// try catch prevents hi % exploit (it uses prismarine-chat)
|
||
// and try catch also prevents json parse error
|
||
try {
|
||
if (previusMessage === message.toString()) return;
|
||
previusMessage = message.toString();
|
||
|
||
const parsedMessage = JSON.parse(data.message);
|
||
if (parsedMessage.translate === 'translation.test.invalid') return;
|
||
// down here it prevents command set message
|
||
if (parsedMessage.translate === 'advMode.setCommand.success') return;
|
||
if (parsedMessage.extra !== undefined) {
|
||
if (parsedMessage.extra[0].text === 'Command set: ') return;
|
||
}
|
||
// prevent braille cuz it CRASHES THE ENTIRE LAPTOP
|
||
if (message.toString().includes('⣿')) return;
|
||
|
||
const cleanMessage = escapeMarkdown(message.toAnsi(), true);
|
||
discordMsg = cleanMessage
|
||
.replaceAll('@', '@\u200b')
|
||
.replaceAll('http', 'http\u200b')
|
||
.replaceAll('\u001b[9', '\u001b[3');
|
||
|
||
consoleQueue.push(message.toAnsi());
|
||
|
||
if (message.toMotd().startsWith('§8[§eChomeNS §9Discord§8] §c')) return;
|
||
dcmsg.queue += '\n' + discordMsg;
|
||
} catch (e) {
|
||
return;
|
||
}
|
||
});
|
||
|
||
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();
|
||
// Find the Discord channel messages will be sent to
|
||
if (process.argv[2] === 'sus.shhnowisnottheti.me') {
|
||
channel = dcclient.channels.cache.get(ayunboomchannel);
|
||
bot.channel = dcclient.channels.cache.get(ayunboomchannel);
|
||
}
|
||
if (process.argv[2] === '129.159.58.114') {
|
||
channel = dcclient.channels.cache.get(dinboomchannel);
|
||
bot.channel = dcclient.channels.cache.get(dinboomchannel);
|
||
}
|
||
if (process.argv[2] === 'kaboom.pw') {
|
||
channel = dcclient.channels.cache.get(kaboomchannel);
|
||
bot.channel = dcclient.channels.cache.get(kaboomchannel);
|
||
}
|
||
if (process.argv[2] === '192.168.1.103') {
|
||
channel = dcclient.channels.cache.get(localclonechannel);
|
||
bot.channel = dcclient.channels.cache.get(localclonechannel);
|
||
}
|
||
if (process.argv[2] === 'kitsune.icu') {
|
||
channel = dcclient.channels.cache.get(kitsunechannel);
|
||
bot.channel = dcclient.channels.cache.get(kitsunechannel);
|
||
}
|
||
if (process.argv[2] === 'mc.chomens41793.ga') {
|
||
channel = dcclient.channels.cache.get(chomensserverchannel);
|
||
bot.channel = dcclient.channels.cache.get(chomensserverchannel);
|
||
}
|
||
await sleep(200);
|
||
main();
|
||
chomenschannel = dcclient.channels.cache.get(chomenschannel);
|
||
bot.hashchannel = dcclient.channels.cache.get(hashchannel);
|
||
bot.ownerhashchannel = dcclient.channels.cache.get(ownerhashchannel);
|
||
bot.channelId = channel.id;
|
||
attachmentlink = '';
|
||
|
||
// Console COMMANDS
|
||
rl.on('line', function(line) {
|
||
try {
|
||
if (line.toLowerCase() === '' ||
|
||
line.toLowerCase().startsWith(' ')) return;
|
||
if (line.toLowerCase() === '.exit' || line.toLowerCase() === '.end') {
|
||
bot.emit('end', 'end command');
|
||
return;
|
||
}
|
||
if (line.toLowerCase().startsWith('.servereval ')) {
|
||
try {
|
||
bot.tellraw('@a', {
|
||
text: `${util.inspect(eval(`${line.substring(12)}`))}`,
|
||
color: 'green',
|
||
});
|
||
return;
|
||
} catch (err) {
|
||
bot.tellraw('@a', {text: `${util.inspect(err)}`, color: 'red'});
|
||
return;
|
||
}
|
||
}
|
||
if (line === '.clearconsolequeue') {
|
||
consoleQueue = [];
|
||
return;
|
||
}
|
||
if (line === '.messagelogging on') {
|
||
messageloggingEnabled = true;
|
||
return;
|
||
}
|
||
if (line === '.messagelogging off') {
|
||
messageloggingEnabled = false;
|
||
return;
|
||
}
|
||
if (line === '.kill') process.exit();
|
||
if (line.startsWith('.')) {
|
||
return bot.command_handler.run(
|
||
'Console',
|
||
'§a§lConsole§r',
|
||
'*' + line.substring(1),
|
||
'c0ns0le-uuid',
|
||
);
|
||
}
|
||
bot.tellraw('@a', [
|
||
{
|
||
text: '[',
|
||
color: 'dark_gray',
|
||
},
|
||
{
|
||
text: `${bot.username} Console`,
|
||
color: 'gray',
|
||
},
|
||
{
|
||
text: '] ',
|
||
color: 'dark_gray',
|
||
},
|
||
{
|
||
text: 'chayapak ',
|
||
color: 'green',
|
||
},
|
||
{
|
||
text: '\u203a ',
|
||
color: 'dark_gray',
|
||
},
|
||
chatMessage.MessageBuilder.fromString('&7' + line),
|
||
]);
|
||
} catch (e) {
|
||
console.log(e);
|
||
}
|
||
});
|
||
});
|
||
|
||
// Redirect Discord messages to in-game chat
|
||
dcclient.on('messageCreate', async (message) => {
|
||
// Ignore messages from the bot itself
|
||
if (message.author.id === dcclient.user.id) return;
|
||
|
||
// Only handle messages in specified channel
|
||
if (message.channel.id != channel.id) return;
|
||
if (message.content.startsWith(config.discord.prefix)) return;
|
||
|
||
try {
|
||
const attachmentsComponent = [];
|
||
if (message.attachments) {
|
||
message.attachments.forEach((value) => {
|
||
attachmentsComponent.push({
|
||
text: ' [Attachment]',
|
||
color: 'green',
|
||
clickEvent: {
|
||
action: 'open_url',
|
||
value: value.proxyURL,
|
||
},
|
||
});
|
||
});
|
||
}
|
||
const component = [
|
||
{text: '[', color: 'dark_gray'},
|
||
{text: 'ChomeNS ', color: 'yellow',
|
||
clickEvent: {
|
||
action: 'open_url',
|
||
value: 'https://discord.gg/xdgCkUyaA4',
|
||
},
|
||
},
|
||
{text: 'Discord', color: 'blue',
|
||
clickEvent: {
|
||
action: 'open_url',
|
||
value: 'https://discord.gg/xdgCkUyaA4',
|
||
},
|
||
},
|
||
{text: '] ', color: 'dark_gray'},
|
||
{text: `${message.member.displayName}`, color: 'red',
|
||
clickEvent: {
|
||
action: 'copy_to_clipboard',
|
||
value: `${message.author.username}#${message.author.discriminator}`,
|
||
},
|
||
hoverEvent: {
|
||
action: 'show_text',
|
||
value: [
|
||
{
|
||
text: message.author.username,
|
||
color: 'white',
|
||
},
|
||
{
|
||
text: '#',
|
||
color: 'dark_gray',
|
||
},
|
||
{
|
||
text: message.author.discriminator,
|
||
color: 'gray',
|
||
},
|
||
'\n',
|
||
{
|
||
text: 'Click here to copy the tag to your clipboard',
|
||
color: 'green',
|
||
},
|
||
],
|
||
},
|
||
},
|
||
{text: ' › ', color: 'dark_gray'},
|
||
chatMessage.MessageBuilder.fromString('&7' + message.content),
|
||
attachmentsComponent.length === 0 ? '' : attachmentsComponent,
|
||
];
|
||
bot.tellraw('@a', component);
|
||
} catch (e) {
|
||
return;
|
||
}
|
||
});
|
||
|
||
dcclient.login(config.discord.token);
|