chomens-bot-js/util/chat.js

95 lines
3.7 KiB
JavaScript
Raw Normal View History

2022-11-16 20:46:04 -05:00
/* 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 {object} bot bot
*/
2022-11-27 02:35:28 -05:00
function chatPacketListener (packet, ChatMessage, bot) {
2022-11-16 20:46:04 -05:00
// try catch prevents hi % exploit (it uses prismarine-chat)
// and try catch also prevents json parse error
try {
2022-11-27 02:35:28 -05:00
const parsedMessage = JSON.parse(packet.message)
2022-11-16 20:46:04 -05:00
if (parsedMessage.translate === 'translation.test.invalid' ||
2022-11-27 02:35:28 -05:00
parsedMessage.translate === 'translation.test.invalid2') return
2022-11-16 20:46:04 -05:00
// down here it prevents command set message
// for ayunboom cuz its 1.17.1
// VVVVVVVVVVVVVVVVVVVVVVVVVVVV
if (parsedMessage.extra) {
2022-11-27 02:35:28 -05:00
if (parsedMessage.extra[0].text === 'Command set: ') return
2022-11-16 20:46:04 -05:00
}
// for 1.18 or newer(?)
// VVVVVVVVVVVVVVVVVVVVV
2022-11-27 02:35:28 -05:00
if (parsedMessage.translate === 'advMode.setCommand.success') return
2022-11-16 20:46:04 -05:00
2022-11-27 02:35:28 -05:00
const message = ChatMessage.fromNotch(packet.message)
2022-11-16 20:46:04 -05:00
2022-11-27 02:35:28 -05:00
bot.emit('parsed_chat', message, packet)
2022-11-16 20:46:04 -05:00
} catch (e) {
2022-11-27 02:35:28 -05:00
2022-11-16 20:46:04 -05:00
}
};
/**
* parse player messages (for prismarine-chat)
* @param {object} message prismarine-chat ChatMessage
* @param {object} packet chat packet
* @param {object} bot bot
*/
2022-11-27 02:35:28 -05:00
function parsePlayerMessages (message, packet, bot) {
2022-11-16 20:46:04 -05:00
try {
// prevent braille cuz it CRASHES THE ENTIRE LAPTOP
// but sometimes this may not work
2022-11-27 02:35:28 -05:00
if (message.toString().includes('⣿')) return
2022-11-16 20:46:04 -05:00
// then here is all the player message parsing thing
2022-11-27 02:35:28 -05:00
const raw = message.toMotd().substring(0, 32767)
2022-11-16 20:46:04 -05:00
if (raw.match(/.* .*: .*/g)) {
2022-11-27 02:35:28 -05:00
const username = raw.replace(/.*?\[.*?\] /g, '').replace(/:.*/g, '').replace(/§#....../gm, '')
const message = raw.split(': ').slice(1).join(' ').replace(/§#....../gm, '')
2022-11-27 02:35:28 -05:00
bot.emit('message', username, message, packet.sender)
2022-11-16 20:46:04 -05:00
} else if (raw.match(/.* .*\u203a .*/g)) {
2022-11-27 02:35:28 -05:00
const username = raw.replace(/.*?\[.*?\] /g, '').replace(/\u203a.*/g, '').replace(/§#....../gm, '').split(' ')[0]
const message = raw.split('\u203a ').slice(1).join(' ').substring(2)
2022-11-27 02:35:28 -05:00
bot.emit('message', username, message, packet.sender)
2022-11-16 20:46:04 -05:00
} else if (raw.match(/.* .*\u00BB .*/g)) {
2022-11-27 02:35:28 -05:00
const username = raw.replace(/.*?\[.*?\] /g, '').replace(/\u00BB.*/g, '').replace(/§#....../gm, '').split(' ')[0]
const message = raw.split('\u00BB ').slice(1).join(' ').substring(2)
bot.emit('message', username, message, packet.sender)
} else if (raw.match(/.* .*> .*/g)) {
const username = raw.replace(/.*?\[.*?\] /g, '').replace(/>.*/g, '').replace(/§#....../gm, '').split(' ')[0]
const message = raw.split('> ').slice(1).join(' ').substring(2)
2022-11-27 02:35:28 -05:00
bot.emit('message', username, message, packet.sender)
2022-11-16 20:46:04 -05:00
} else if (raw.match(/<.*> .*/g)) {
2022-11-27 02:35:28 -05:00
const username = raw.substring(1).split('>')[0]
const message = raw.split('> ').slice(1).join(' ')
2022-11-16 20:46:04 -05:00
2022-11-27 02:35:28 -05:00
bot.emit('message', username, message, packet.sender)
2022-11-16 20:46:04 -05:00
} else if (raw.match(/§.*§b: §b\/.*/g)) {
2022-11-27 02:35:28 -05:00
const username = raw.split('§b: §b')[0]
const command = raw.split('§b: §b')[1]
2022-11-16 20:46:04 -05:00
2022-11-27 02:35:28 -05:00
bot.emit('cspy', username, command)
2022-11-16 20:46:04 -05:00
} else if (raw.match(/§.*§e: §e\/.*/g)) {
2022-11-27 02:35:28 -05:00
const username = raw.split('§e: §e')[0]
const command = raw.split('§e: §e')[1]
bot.emit('cspy', username, command)
2022-11-16 20:46:04 -05:00
} else if (raw.match(/§.*§b: \/.*/g)) {
2022-11-27 02:35:28 -05:00
const username = raw.split('§b: ')[0]
const command = raw.split('§b: ')[1]
2022-11-16 20:46:04 -05:00
2022-11-27 02:35:28 -05:00
bot.emit('cspy', username, command)
2022-11-16 20:46:04 -05:00
} else if (raw.match(/§.*§e: \/.*/g)) {
2022-11-27 02:35:28 -05:00
const username = raw.split('§e: ')[0]
const command = raw.split('§e: ')[1]
bot.emit('cspy', username, command)
2022-11-16 20:46:04 -05:00
}
} catch (e) {
2022-11-27 02:35:28 -05:00
2022-11-16 20:46:04 -05:00
}
};
2022-11-27 02:35:28 -05:00
module.exports = { chatPacketListener, parsePlayerMessages }