chomens-bot-js/util/chat.js

87 lines
3.4 KiB
JavaScript

/**
* for the chat packet listener (in util cuz proxy + bot)
* @param {object} packet chat packet
* @param {object} bot bot
* @param {boolean} mc119 minecraft 1.19 or newer
*/
function chatPacketListener (packet, bot, mc119) {
// try catch prevents json parse error (which prob never happens but still..)
try {
const ChatMessage = require('prismarine-chat')(bot.version)
const parsedMessage = JSON.parse(mc119 ? packet.content : packet.message)
// 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(mc119 ? packet.content : packet.message)
bot.emit('message', message, packet)
} catch (e) {
bot.console.error(e)
}
};
/**
* 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 {
// here is all the player message parsing thing
const raw = message.toMotd().substring(0, 32767)
if (raw.match(/.* .*: .*/g)) {
const username = raw.replace(/.*?\[.*?\] /g, '').replace(/:.*/g, '').replace(/§#....../gm, '')
const message = raw.split(': ').slice(1).join(' ').replace(/§#....../gm, '')
bot.emit('chat', username, message)
} else if (raw.match(/.* .*\u203a .*/g)) {
const username = raw.replace(/.*?\[.*?\] /g, '').replace(/\u203a.*/g, '').replace(/§#....../gm, '').split(' ')[0]
const message = raw.split('\u203a ').slice(1).join(' ').substring(2)
bot.emit('chat', username, message)
} else if (raw.match(/.* .*\u00BB .*/g)) {
const username = raw.replace(/.*?\[.*?\] /g, '').replace(/\u00BB.*/g, '').replace(/§#....../gm, '').split(' ')[0]
const message = raw.split('\u00BB ').slice(1).join(' ').substring(2)
bot.emit('chat', username, message)
} 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)
bot.emit('chat', username, message)
} else if (raw.match(/<.*> .*/g)) {
const username = raw.substring(1).split('>')[0]
const message = raw.split('> ').slice(1).join(' ')
bot.emit('chat', username, message)
} 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) {
}
};
module.exports = { chatPacketListener, parsePlayerMessages }