chomens-bot-js/plugins/chat.js
2022-11-18 07:46:52 +07:00

52 lines
1.4 KiB
JavaScript

/* eslint-disable max-len */
/* eslint-disable require-jsdoc */
// eslint-disable-next-line no-undef
// const parse = require('../util/text_parser');
const {containsIllegalCharacters} = require('../util/containsIllegalCharacters');
const {chatPacketListener, parsePlayerMessages} = require('../util/chat');
function inject(bot) {
bot.chatQueue = [];
let chatQueueSplitted;
const chatQueue = setInterval(function() {
if (bot.chatQueue[0] || bot.chatQueue[0] === '') {
if (containsIllegalCharacters(bot.chatQueue[0])) {
bot.chatQueue.shift();
return;
};
chatQueueSplitted = bot.chatQueue[0].match(/.{0,224}/g);
chatQueueSplitted.pop();
}
}, 20);
const sendChatQueue = setInterval(function() {
if (chatQueueSplitted) {
try {
bot.write('chat', {message: chatQueueSplitted[0]});
chatQueueSplitted.shift();
bot.chatQueue.shift();
} catch (e) {
bot.console.error(e);
}
}
}, 450);
bot.chat = (message) => {
bot.chatQueue.push(String(message));
};
bot.once('end', () => {
clearInterval(chatQueue);
clearInterval(sendChatQueue);
});
let previousMessage;
const ChatMessage = require('prismarine-chat')(bot.version);
bot._client.on('chat', (packet) => chatPacketListener(packet, ChatMessage, previousMessage, bot));
bot.on('parsed_chat', (message, packet) => parsePlayerMessages(message, packet, bot));
}
module.exports = {inject};