chomens-bot-js/plugins/chat.js
2022-09-04 18:51:35 +07:00

71 lines
3 KiB
JavaScript

/* eslint-disable max-len */
/* eslint-disable require-jsdoc */
// eslint-disable-next-line no-undef
// const parse = require('../util/text_parser');
const ChatMessage = require('prismarine-chat')('1.18.2');
function inject(bot) {
bot._client.on('chat', (packet) => {
try {
// const message = parse(packet.message);
const message = ChatMessage.fromNotch(packet.message);
bot.emit('parsed_chat', message, packet);
} catch (e) {
return;
}
});
bot.on('parsed_chat', (message, packet) => {
try {
const raw = message.toMotd().substring(0, 32767);
if (raw.match(/.* .*§r: §.*/g)) {
// if (packet.sender === '00000000-0000-0000-0000-000000000000') return;
const username = raw.replace(/.*?\[.*?\] /, '').replace(/:.*/g, '').replace(/§#....../gm, '');
const message = raw.split('§r: ')[1].substr(2);
bot.emit('message', username, message, packet.sender);
} else 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].substr(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].substr(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.substr(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.substr(3).split('>')[0];
const message = raw.split('> ')[1];
bot.emit('message', username, message, packet.sender);
} 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};