2022-08-14 05:51:45 -04:00
|
|
|
/* eslint-disable max-len */
|
|
|
|
/* eslint-disable require-jsdoc */
|
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
// const parse = require('../util/text_parser');
|
2022-11-12 04:34:17 -05:00
|
|
|
const {containsIllegalCharacters} = require('../util/containsIllegalCharacters');
|
2022-08-14 05:51:45 -04:00
|
|
|
function inject(bot) {
|
2022-11-12 04:34:17 -05:00
|
|
|
bot.chatQueue = [];
|
|
|
|
|
|
|
|
const chatQueue = setInterval(function() {
|
|
|
|
try {
|
|
|
|
if (bot.chatQueue[0] || bot.chatQueue[0] === '') {
|
|
|
|
try {
|
|
|
|
if (containsIllegalCharacters(bot.chatQueue[0])) {
|
|
|
|
bot.chatQueue.shift();
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
bot.write('chat', {message: bot.chatQueue[0].substring(0, 256)});
|
|
|
|
bot.chatQueue.shift();
|
|
|
|
} catch (e) {
|
2022-11-13 01:52:37 -05:00
|
|
|
bot.console.error(e);
|
2022-11-12 04:34:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}, 450);
|
|
|
|
|
|
|
|
bot.chat = (message) => {
|
|
|
|
bot.chatQueue.push(String(message));
|
|
|
|
};
|
|
|
|
|
|
|
|
bot.once('end', () => {
|
|
|
|
clearInterval(chatQueue);
|
|
|
|
});
|
|
|
|
|
2022-11-10 07:38:14 -05:00
|
|
|
let previousMessage;
|
2022-10-18 20:59:58 -04:00
|
|
|
const ChatMessage = require('prismarine-chat')(bot.version);
|
|
|
|
|
2022-10-16 03:51:39 -04:00
|
|
|
bot._client.on('chat', async (packet) => {
|
2022-11-10 07:38:14 -05:00
|
|
|
// try catch prevents hi % exploit (it uses prismarine-chat)
|
|
|
|
// and try catch also prevents json parse error
|
2022-08-14 05:51:45 -04:00
|
|
|
try {
|
2022-11-06 03:59:56 -05:00
|
|
|
const parsedMessage = JSON.parse(packet.message);
|
2022-11-10 07:38:14 -05:00
|
|
|
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
|
2022-11-06 03:59:56 -05:00
|
|
|
if (parsedMessage.translate === 'advMode.setCommand.success') return;
|
2022-11-10 07:38:14 -05:00
|
|
|
|
2022-08-14 05:51:45 -04:00
|
|
|
const message = ChatMessage.fromNotch(packet.message);
|
2022-11-10 07:38:14 -05:00
|
|
|
|
|
|
|
// before emitting, prevent spam first!!!
|
|
|
|
if (previousMessage === message.toString()) return;
|
|
|
|
previousMessage = message.toString();
|
|
|
|
|
2022-08-14 05:51:45 -04:00
|
|
|
bot.emit('parsed_chat', message, packet);
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
bot.on('parsed_chat', (message, packet) => {
|
|
|
|
try {
|
2022-11-10 07:38:14 -05:00
|
|
|
// 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
|
2022-08-14 05:51:45 -04:00
|
|
|
const raw = message.toMotd().substring(0, 32767);
|
2022-10-25 07:15:21 -04:00
|
|
|
if (raw.match(/.* .*: .*/g)) {
|
2022-11-07 08:53:45 -05:00
|
|
|
// if (packet.sender === '00000000-0000-0000-0000-000000000000') return;
|
2022-08-16 06:33:06 -04:00
|
|
|
const username = raw.replace(/.*?\[.*?\] /, '').replace(/:.*/g, '').replace(/§#....../gm, '');
|
|
|
|
const message = raw.split(': ')[1];
|
|
|
|
bot.emit('message', username, message, packet.sender);
|
2022-08-24 06:26:43 -04:00
|
|
|
} else if (raw.match(/.* .*\u203a .*/g)) {
|
2022-11-07 08:53:45 -05:00
|
|
|
// if (packet.sender === '00000000-0000-0000-0000-000000000000') return;
|
2022-08-27 21:24:52 -04:00
|
|
|
const username = raw.replace(/.*?\[.*?\] /g, '').replace(/\u203a.*/g, '').replace(/§#....../gm, '').split(' ')[0];
|
2022-10-15 00:18:58 -04:00
|
|
|
const message = raw.split('\u203a ')[1].substring(2);
|
2022-08-24 06:12:40 -04:00
|
|
|
bot.emit('message', username, message, packet.sender);
|
2022-08-27 21:35:52 -04:00
|
|
|
} else if (raw.match(/.* .*\u00BB .*/g)) {
|
2022-11-07 08:53:45 -05:00
|
|
|
// if (packet.sender === '00000000-0000-0000-0000-000000000000') return;
|
2022-08-27 21:35:52 -04:00
|
|
|
const username = raw.replace(/.*?\[.*?\] /g, '').replace(/\u00BB.*/g, '').replace(/§#....../gm, '').split(' ')[0];
|
2022-10-15 00:18:58 -04:00
|
|
|
const message = raw.split('\u00BB ')[1].substring(2);
|
2022-08-27 21:35:52 -04:00
|
|
|
bot.emit('message', username, message, packet.sender);
|
2022-08-16 06:33:06 -04:00
|
|
|
} else if (raw.match(/<.*§r> .*/g)) {
|
2022-11-07 08:53:45 -05:00
|
|
|
// if (packet.sender === '00000000-0000-0000-0000-000000000000') return;
|
2022-08-14 05:51:45 -04:00
|
|
|
|
2022-10-15 00:18:58 -04:00
|
|
|
const username = raw.substring(3).split('§r>')[0];
|
2022-08-20 04:49:57 -04:00
|
|
|
const message = raw.split('§r> ')[1];
|
2022-08-14 05:51:45 -04:00
|
|
|
|
|
|
|
bot.emit('message', username, message, packet.sender);
|
|
|
|
} else if (raw.match(/<.*> .*/g)) {
|
2022-11-07 08:53:45 -05:00
|
|
|
// if (packet.sender === '00000000-0000-0000-0000-000000000000') return;
|
2022-08-14 05:51:45 -04:00
|
|
|
|
2022-10-15 00:18:58 -04:00
|
|
|
const username = raw.substring(3).split('>')[0];
|
2022-08-14 05:51:45 -04:00
|
|
|
const message = raw.split('> ')[1];
|
|
|
|
|
|
|
|
bot.emit('message', username, message, packet.sender);
|
2022-11-03 08:46:21 -04:00
|
|
|
} 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);
|
2022-08-14 05:51:45 -04:00
|
|
|
} 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 = {inject};
|