chomens-bot-js/plugins/chat.js

43 lines
1.2 KiB
JavaScript
Raw Normal View History

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-11-16 20:46:04 -05:00
const {chatPacketListener, parsePlayerMessages} = require('../util/chat');
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() {
if (bot.chatQueue[0] || bot.chatQueue[0] === '') {
2022-11-17 19:46:52 -05:00
try {
if (containsIllegalCharacters(bot.chatQueue[0])) {
bot.chatQueue.shift();
return;
};
bot.write('chat', {message: bot.chatQueue[0]});
2022-11-17 19:46:52 -05:00
bot.chatQueue.shift();
} catch (e) {
bot.console.error(e);
2022-11-12 04:34:17 -05:00
}
}
}, 450);
bot.chat = (message) => {
bot.chatQueue.push(String(message));
};
bot.once('end', () => {
clearInterval(chatQueue);
});
let previousMessage;
2022-10-18 20:59:58 -04:00
const ChatMessage = require('prismarine-chat')(bot.version);
bot._client.on('chat', (packet) => chatPacketListener(packet, ChatMessage, previousMessage, bot));
2022-11-16 20:46:04 -05:00
bot.on('parsed_chat', (message, packet) => parsePlayerMessages(message, packet, bot));
}
2022-11-16 20:46:04 -05:00
module.exports = {inject};