chomens-bot-js/util/chat.js
2022-11-18 09:54:53 +07:00

107 lines
4.2 KiB
JavaScript

/* 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(/.*?\[.*?\] /, '').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(/.*?\[.*?\] /, '').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};