diff --git a/commands/cloop.js b/commands/cloop.js index 32d8479..5c6917a 100644 --- a/commands/cloop.js +++ b/commands/cloop.js @@ -6,12 +6,12 @@ function add(command, interval, bot) { bot.cloops.push({id, interval, command}); } -function remove(item) { +function remove(item, bot) { clearInterval(bot.cloops[item].id); bot.cloops.splice(item, 1); } -function clear() { +function clear(bot) { for (const interval of bot.cloops) clearInterval(interval.id); bot.cloops = []; @@ -77,10 +77,10 @@ module.exports = { } else if (args[1] === 'list') { list(bot, false, null, selector); } else if (args[1] === 'remove') { - remove(args[2]); + remove(args[2], bot); bot.tellraw(selector, [{text: 'Removed cloop '}, {text: args[2], color: 'aqua'}]); } else if (args[1] === 'removeall') { - clear(); + clear(bot); bot.tellraw(selector, [{text: 'Removed all looped commands', color: 'white'}]); } else { throw new SyntaxError('Invalid argument'); diff --git a/config.js b/config.js index 62a375a..11314d8 100644 --- a/config.js +++ b/config.js @@ -66,10 +66,10 @@ module.exports = { 'host': 'kitsune.icu', 'port': 25565, }, - { - 'host': '71.179.136.66', - 'port': 25565, - }, + // { + // 'host': '71.179.136.66', + // 'port': 25565, + // }, // { // 'host': 'mc.chomens41793.ga', // 'port': 25565, diff --git a/plugins/chat.js b/plugins/chat.js index d244709..a823bc6 100644 --- a/plugins/chat.js +++ b/plugins/chat.js @@ -3,97 +3,7 @@ // eslint-disable-next-line no-undef // const parse = require('../util/text_parser'); const {containsIllegalCharacters} = require('../util/containsIllegalCharacters'); -async function chatPacketListener(packet, ChatMessage, previousMessage, bot) { - // try catch prevents hi % exploit (it uses prismarine-chat) - // and try catch also prevents json parse error - try { - const parsedMessage = JSON.parse(packet.message); - if (parsedMessage.translate === 'translation.test.invalid' || - parsedMessage.translate === 'translation.test.invalid2') return; - - // down here it prevents command set message - - // for ayunboom cuz its 1.17.1 - // VVVVVVVVVVVVVVVVVVVVVVVVVVVV - if (parsedMessage.extra) { - if (parsedMessage.extra[0].text === 'Command set: ') return; - } - // for 1.18 or newer(?) - // VVVVVVVVVVVVVVVVVVVVV - if (parsedMessage.translate === 'advMode.setCommand.success') return; - - const message = ChatMessage.fromNotch(packet.message); - - // before emitting, prevent spam first!!! - if (previousMessage === message.toString()) return; - previousMessage = message.toString(); - - bot.emit('parsed_chat', message, packet); - } catch (e) { - return; - } -} - -async function parsedChatListener(message, packet, bot) { - try { - // prevent braille cuz it CRASHES THE ENTIRE LAPTOP - // but sometimes this may not work - if (message.toString().includes('⣿')) return; - - // then here is all the player message parsing thing - const raw = message.toMotd().substring(0, 32767); - if (raw.match(/.* .*: .*/g)) { - // if (packet.sender === '00000000-0000-0000-0000-000000000000') return; - const username = raw.replace(/.*?\[.*?\] /, '').replace(/:.*/g, '').replace(/§#....../gm, ''); - const message = raw.split(': ')[1]; - bot.emit('message', username, message, packet.sender); - } else if (raw.match(/.* .*\u203a .*/g)) { - // if (packet.sender === '00000000-0000-0000-0000-000000000000') return; - const username = raw.replace(/.*?\[.*?\] /g, '').replace(/\u203a.*/g, '').replace(/§#....../gm, '').split(' ')[0]; - const message = raw.split('\u203a ')[1].substring(2); - bot.emit('message', username, message, packet.sender); - } else if (raw.match(/.* .*\u00BB .*/g)) { - // if (packet.sender === '00000000-0000-0000-0000-000000000000') return; - const username = raw.replace(/.*?\[.*?\] /g, '').replace(/\u00BB.*/g, '').replace(/§#....../gm, '').split(' ')[0]; - const message = raw.split('\u00BB ')[1].substring(2); - bot.emit('message', username, message, packet.sender); - } else if (raw.match(/<.*§r> .*/g)) { - // if (packet.sender === '00000000-0000-0000-0000-000000000000') return; - - const username = raw.substring(3).split('§r>')[0]; - const message = raw.split('§r> ')[1]; - - bot.emit('message', username, message, packet.sender); - } else if (raw.match(/<.*> .*/g)) { - // if (packet.sender === '00000000-0000-0000-0000-000000000000') return; - - const username = raw.substring(3).split('>')[0]; - const message = raw.split('> ')[1]; - - bot.emit('message', username, message, packet.sender); - } else if (raw.match(/§.*§b: §b\/.*/g)) { - const username = raw.split('§b: §b')[0]; - const command = raw.split('§b: §b')[1]; - - bot.emit('cspy', username, command); - } else if (raw.match(/§.*§e: §e\/.*/g)) { - const username = raw.split('§e: §e')[0]; - const command = raw.split('§e: §e')[1]; - bot.emit('cspy', username, command); - } else if (raw.match(/§.*§b: \/.*/g)) { - const username = raw.split('§b: ')[0]; - const command = raw.split('§b: ')[1]; - - bot.emit('cspy', username, command); - } else if (raw.match(/§.*§e: \/.*/g)) { - const username = raw.split('§e: ')[0]; - const command = raw.split('§e: ')[1]; - bot.emit('cspy', username, command); - } - } catch (e) { - return; - } -}; +const {chatPacketListener, parsePlayerMessages} = require('../util/chat'); function inject(bot) { bot.chatQueue = []; @@ -130,15 +40,7 @@ function inject(bot) { bot._client.on('chat', (packet) => chatPacketListener(packet, ChatMessage, previousMessage, bot)); - bot.on('parsed_chat', (message, packet) => parsedChatListener(message, packet, bot)); + bot.on('parsed_chat', (message, packet) => parsePlayerMessages(message, packet, bot)); } -function proxy(bot, client, targetClient) { - const ChatMessage = require('prismarine-chat')(bot.version); - - targetClient.on('chat', (packet) => chatPacketListener(packet, ChatMessage, '', targetClient)); - - targetClient.on('parsed_chat', (message, packet) => parsedChatListener(message, packet, targetClient)); -}; - -module.exports = {inject, proxy}; +module.exports = {inject}; diff --git a/plugins/proxy.js b/plugins/proxy.js index 97d7a8f..432f90f 100644 --- a/plugins/proxy.js +++ b/plugins/proxy.js @@ -3,104 +3,108 @@ const mc = require('minecraft-protocol'); const {loadPlugins} = require('../util/loadPlugins'); function inject(bot, dcclient, config) { - config.servers.forEach((server, index) => { - if (server.host !== bot.options.host) return; + let index; + config.servers.forEach((server, _index) => { + if (bot.options.host !== server.host) return; + index = _index; + }); - const {MessageBuilder} = require('prismarine-chat')(bot.version); + const {MessageBuilder} = require('prismarine-chat')(bot.version); - const version = bot.version; - const srv = mc.createServer({ - 'online-mode': false, - 'port': 25566 + index, - 'keepAlive': false, + const version = bot.version; + const srv = mc.createServer({ + 'online-mode': false, + 'port': 25566 + index, + 'keepAlive': false, + version, + }); + + srv.on('login', function(client) { + bot.console.info(`[Proxy] ${client.username} connected to proxy`); + let endedClient = false; + let endedTarget = false; + + const targetClient = mc.createClient({ + username: client.username, + host: bot.options.host, version, }); - bot.once('end', () => srv.close()); + targetClient.chat = function(message) { + targetClient.write('chat', {message}); + }; - srv.on('login', function(client) { - bot.console.info(`[Proxy] ${client.username} connected to proxy`); - let endedClient = false; - let endedTarget = false; - - const targetClient = mc.createClient({ - username: client.username, - host: bot.options.host, - version, - }); - - targetClient.chat = function(message) { - targetClient.write('chat', {message}); - }; - - targetClient.on('login', (packet) => { - targetClient.entityId = packet.entityId; - loadPlugins(bot, null, config, null, targetClient, client, true); - }); - - targetClient.on('packet', (data, meta) => { - if (!endedClient && - meta.state === mc.states.PLAY && - client.state === mc.states.PLAY - ) client.write(meta.name, data); - }); - - targetClient.on('error', () => {}); - - targetClient.on('end', () => { - targetClient.end(); - endedTarget = true; - }); - - client.on('end', function() { - endedClient = true; - targetClient.end(); - bot.console.info(`[Proxy] ${client.username} ended`); - }); - - client.on('error', function() { - endedClient = true; - bot.console.info(`[Proxy] ${client.username} got error`); - }); - - client.on('packet', (data, meta) => { - if (meta.name === 'chat' && !data.message?.startsWith('/')) { - if (data.message.startsWith('.')) { - return bot.command_handler.run( - client.username, - client.username, - '*' + data.message.substring(1), - client.uuid, - null, - 'h', - 'o', - client.username, - ); - } - bot.tellraw('@a', { - color: 'dark_gray', - translate: '[%s] [%s] %s \u203a %s', - with: [ - { - text: 'Chat', - color: 'gray', - }, - { - text: 'Proxy', - color: 'gray', - }, - { - text: client.username, - color: 'green', - }, - MessageBuilder.fromString('&7' + data.message), - ], - }); - return; - } - targetClient.write(meta.name, data); - }); + targetClient.on('login', (packet) => { + targetClient.entityId = packet.entityId; + loadPlugins(bot, null, config, null, targetClient, client, true); }); + + targetClient.on('packet', (data, meta) => { + if (!endedClient && + meta.state === mc.states.PLAY && + client.state === mc.states.PLAY + ) client.write(meta.name, data); + }); + + targetClient.on('error', () => {}); + + targetClient.on('end', () => { + targetClient.end(); + endedTarget = true; + }); + + client.on('end', function() { + endedClient = true; + targetClient.end(); + bot.console.info(`[Proxy] ${client.username} ended`); + }); + + client.on('error', function() { + endedClient = true; + bot.console.info(`[Proxy] ${client.username} got error`); + }); + + client.on('packet', (data, meta) => { + if (meta.name === 'chat' && !data.message?.startsWith('/')) { + if (data.message.startsWith('.')) { + return bot.command_handler.run( + client.username, + client.username, + '*' + data.message.substring(1), + client.uuid, + null, + 'h', + 'o', + client.username, + ); + } + bot.tellraw('@a', { + color: 'dark_gray', + translate: '[%s] [%s] %s \u203a %s', + with: [ + { + text: 'Chat', + color: 'gray', + }, + { + text: 'Proxy', + color: 'gray', + }, + { + text: client.username, + color: 'green', + }, + MessageBuilder.fromString('&7' + data.message), + ], + }); + return; + } + targetClient.write(meta.name, data); + }); + }); + bot.once('end', () => { + srv.close(); + srv.removeAllListeners(); }); }; diff --git a/plugins/proxy/chat.js b/plugins/proxy/chat.js new file mode 100644 index 0000000..2fcfd8e --- /dev/null +++ b/plugins/proxy/chat.js @@ -0,0 +1,15 @@ +/* eslint-disable require-jsdoc */ +const {chatPacketListener, parsePlayerMessages} = require('../../util/chat'); +function inject(bot, client, targetClient) { + const ChatMessage = require('prismarine-chat')(bot.version); + + targetClient.on('chat', (packet) => { + chatPacketListener(packet, ChatMessage, '', targetClient); + }); + + targetClient.on('parsed_chat', (message, packet) => { + parsePlayerMessages(message, packet, targetClient); + }); +}; + +module.exports = {inject}; diff --git a/plugins/proxy/self_care.js b/plugins/proxy/self_care.js new file mode 100644 index 0000000..2de88e7 --- /dev/null +++ b/plugins/proxy/self_care.js @@ -0,0 +1,49 @@ +/* eslint-disable max-len */ +/* eslint-disable require-jsdoc */ +function inject(bot, client, targetClient, config) { + let cspy = false; + let op = true; + let gameMode = 1; + + targetClient.on('parsed_chat', (data) => { + if (data.toString() === 'Successfully enabled CommandSpy' || data.toString() === ' Enabled your command spy.' || data.toString() === ' Your command spy is already enabled.') cspy = true; + if (data.toString() === 'Successfully disabled CommandSpy' || data.toString() === ' Disabled your command spy.') cspy = false; + }); + + targetClient.on('entity_status', (data) => { + if (data.entityId !== targetClient.entityId) return; + + switch (data.entityStatus) { + case 24: + op = false; + break; + case 28: + op = true; + break; + } + }); + + targetClient.on('game_state_change', (data) => { + if (data.reason !== 3) return; + + gameMode = data.gameMode; + }); + + targetClient.once('login', (data) => { + gameMode = data.gameMode; + }); + + const interval = setInterval(() => { + if (bot.options.host !== '0.tcp.ap.ngrok.io') { + if (!op && config.self_care.op) targetClient.chat('/minecraft:op @s[type=player]'); + if (!cspy && config.self_care.cspy) targetClient.chat('/commandspy:commandspy on'); + } + if (gameMode !== 1 && config.self_care.gamemode) targetClient.chat('/minecraft:gamemode creative @s[type=player]'); + }, config.self_care_check_interval); + + bot.once('end', () => { + clearInterval(interval); + }); +}; + +module.exports = {inject}; diff --git a/plugins/self_care.js b/plugins/self_care.js index f5368b6..386bb4f 100644 --- a/plugins/self_care.js +++ b/plugins/self_care.js @@ -65,55 +65,9 @@ function inject(bot, dcclient, config) { if (muted && config.self_care.mute) bot.chat('/essentials:mute ' + bot.uuid); }, config.self_care_check_interval); - bot.once('end', () => { - clearInterval(interval); - }); -} - -function proxy(bot, client, targetClient, config) { - let cspy = false; - let op = true; - let gameMode = 1; - - targetClient.on('parsed_chat', (data) => { - if (data.toString() === 'Successfully enabled CommandSpy' || data.toString() === ' Enabled your command spy.' || data.toString() === ' Your command spy is already enabled.') cspy = true; - if (data.toString() === 'Successfully disabled CommandSpy' || data.toString() === ' Disabled your command spy.') cspy = false; - }); - - targetClient.on('entity_status', (data) => { - if (data.entityId !== targetClient.entityId) return; - - switch (data.entityStatus) { - case 24: - op = false; - break; - case 28: - op = true; - break; - } - }); - - targetClient.on('game_state_change', (data) => { - if (data.reason !== 3) return; - - gameMode = data.gameMode; - }); - - targetClient.once('login', (data) => { - gameMode = data.gameMode; - }); - - const interval = setInterval(() => { - if (bot.options.host !== '0.tcp.ap.ngrok.io') { - if (!op && config.self_care.op) targetClient.chat('/minecraft:op @s[type=player]'); - if (!cspy && config.self_care.cspy) targetClient.chat('/commandspy:commandspy on'); - } - if (gameMode !== 1 && config.self_care.gamemode) targetClient.chat('/minecraft:gamemode creative @s[type=player]'); - }, config.self_care_check_interval); - bot.once('end', () => { clearInterval(interval); }); }; -module.exports = {inject, proxy}; +module.exports = {inject}; diff --git a/util/chat.js b/util/chat.js new file mode 100644 index 0000000..0b542bd --- /dev/null +++ b/util/chat.js @@ -0,0 +1,107 @@ +/* eslint-disable max-len */ +/** + * for the chat packet listener (in util cuz proxy + bot) + * @param {object} packet chat packet + * @param {object} ChatMessage basically just require prismarine-chat + * @param {string} previousMessage previous message. + * @param {object} bot bot + */ +function chatPacketListener(packet, ChatMessage, previousMessage, bot) { + // try catch prevents hi % exploit (it uses prismarine-chat) + // and try catch also prevents json parse error + try { + const parsedMessage = JSON.parse(packet.message); + if (parsedMessage.translate === 'translation.test.invalid' || + parsedMessage.translate === 'translation.test.invalid2') return; + + // down here it prevents command set message + + // for ayunboom cuz its 1.17.1 + // VVVVVVVVVVVVVVVVVVVVVVVVVVVV + if (parsedMessage.extra) { + if (parsedMessage.extra[0].text === 'Command set: ') return; + } + // for 1.18 or newer(?) + // VVVVVVVVVVVVVVVVVVVVV + if (parsedMessage.translate === 'advMode.setCommand.success') return; + + const message = ChatMessage.fromNotch(packet.message); + + // before emitting, prevent spam first!!! + if (previousMessage === message.toString()) return; + previousMessage = message.toString(); + + bot.emit('parsed_chat', message, packet); + } catch (e) { + return; + } +}; + +/** + * parse player messages (for prismarine-chat) + * @param {object} message prismarine-chat ChatMessage + * @param {object} packet chat packet + * @param {object} bot bot + */ +function parsePlayerMessages(message, packet, bot) { + try { + // prevent braille cuz it CRASHES THE ENTIRE LAPTOP + // but sometimes this may not work + if (message.toString().includes('⣿')) return; + + // then here is all the player message parsing thing + const raw = message.toMotd().substring(0, 32767); + if (raw.match(/.* .*: .*/g)) { + // if (packet.sender === '00000000-0000-0000-0000-000000000000') return; + const username = raw.replace(/.*?\[.*?\] /, '').replace(/:.*/g, '').replace(/§#....../gm, ''); + const message = raw.split(': ')[1]; + bot.emit('message', username, message, packet.sender); + } else if (raw.match(/.* .*\u203a .*/g)) { + // if (packet.sender === '00000000-0000-0000-0000-000000000000') return; + const username = raw.replace(/.*?\[.*?\] /g, '').replace(/\u203a.*/g, '').replace(/§#....../gm, '').split(' ')[0]; + const message = raw.split('\u203a ')[1].substring(2); + bot.emit('message', username, message, packet.sender); + } else if (raw.match(/.* .*\u00BB .*/g)) { + // if (packet.sender === '00000000-0000-0000-0000-000000000000') return; + const username = raw.replace(/.*?\[.*?\] /g, '').replace(/\u00BB.*/g, '').replace(/§#....../gm, '').split(' ')[0]; + const message = raw.split('\u00BB ')[1].substring(2); + bot.emit('message', username, message, packet.sender); + } else if (raw.match(/<.*§r> .*/g)) { + // if (packet.sender === '00000000-0000-0000-0000-000000000000') return; + + const username = raw.substring(3).split('§r>')[0]; + const message = raw.split('§r> ')[1]; + + bot.emit('message', username, message, packet.sender); + } else if (raw.match(/<.*> .*/g)) { + // if (packet.sender === '00000000-0000-0000-0000-000000000000') return; + + const username = raw.substring(3).split('>')[0]; + const message = raw.split('> ')[1]; + + bot.emit('message', username, message, packet.sender); + } else if (raw.match(/§.*§b: §b\/.*/g)) { + const username = raw.split('§b: §b')[0]; + const command = raw.split('§b: §b')[1]; + + bot.emit('cspy', username, command); + } else if (raw.match(/§.*§e: §e\/.*/g)) { + const username = raw.split('§e: §e')[0]; + const command = raw.split('§e: §e')[1]; + bot.emit('cspy', username, command); + } else if (raw.match(/§.*§b: \/.*/g)) { + const username = raw.split('§b: ')[0]; + const command = raw.split('§b: ')[1]; + + bot.emit('cspy', username, command); + } else if (raw.match(/§.*§e: \/.*/g)) { + const username = raw.split('§e: ')[0]; + const command = raw.split('§e: ')[1]; + bot.emit('cspy', username, command); + } + } catch (e) { + return; + } +}; + +module.exports = {chatPacketListener, parsePlayerMessages}; diff --git a/util/loadPlugins.js b/util/loadPlugins.js index d063126..66d81db 100644 --- a/util/loadPlugins.js +++ b/util/loadPlugins.js @@ -14,10 +14,10 @@ const path = require('path'); * @param {boolean} proxy is proxy */ async function loadPlugins(bot, dcclient, config, rl, targetClient, client, proxy) { - const plugins = await fs.readdir(path.join(__dirname, '..', 'plugins')); - if (!proxy) { + const plugins = await fs.readdir(path.join(__dirname, '..', 'plugins')); plugins.forEach((plugin) => { + if (!plugin.endsWith('.js')) return; try { const plug = require(path.join(__dirname, '..', 'plugins', plugin)); plug.inject(bot, dcclient, config, rl); @@ -27,11 +27,12 @@ async function loadPlugins(bot, dcclient, config, rl, targetClient, client, prox } }); } else { + const plugins = await fs.readdir(path.join(__dirname, '..', 'plugins', 'proxy')); plugins.forEach((plugin) => { + if (!plugin.endsWith('.js')) return; try { - const plug = require(path.join(__dirname, '..', 'plugins', plugin)); - if (!plug.proxy) return; - plug.proxy(bot, client, targetClient, config); + const plug = require(path.join(__dirname, '..', 'plugins', 'proxy', plugin)); + plug.inject(bot, client, targetClient, config); } catch (e) { console.log(`Proxy Plugin ${plugin} is having exception loading the plugin:`); console.log(util.inspect(e));