From 946fd88d36a8da2767cbeb206665b7642ddcba15 Mon Sep 17 00:00:00 2001 From: ChomeNS Date: Sat, 12 Nov 2022 16:34:17 +0700 Subject: [PATCH] move chat queue to chat plugin --- index.js | 31 ------------------------------- plugins/chat.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/index.js b/index.js index 6d23ba5..bb7927f 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,6 @@ const config = require('./config'); const crypto = require('crypto'); const colorConvert = require('color-convert'); const sleep = require('sleep-promise'); -const {containsIllegalCharacters} = require('./util/containsIllegalCharacters'); const generateEaglerUsername = require('./util/generateEaglerUsername'); const {EventEmitter} = require('events'); const {loadPlugins} = require('./util/loadPlugins'); @@ -58,7 +57,6 @@ async function botThings() { }; bot._client = mc.createClient(bot.options); bot.version = bot._client.version; - bot.queue = []; bot.write = (name, data) => bot._client.write(name, data); bot.end = (reason = 'end') => { bot.emit('end', reason); @@ -81,35 +79,11 @@ async function botThings() { function main() { const channel = dcclient.channels.cache.get(config.discord.servers[process.argv[2]]); - // allink's chat queue - chatQueue = setInterval(function() { - try { - if (bot.queue[0] || bot.queue[0] === '') { - try { - if (containsIllegalCharacters(bot.queue[0])) { - bot.queue.shift(); - return; - }; - bot.write('chat', {message: bot.queue[0].substring(0, 256)}); - bot.queue.shift(); - } catch (e) { - console.log(e.message); - } - } - } catch (e) { - return; - } - }, 450); - module.exports = bot; bot.playersAddedPlayers = {}; bot.getplayerusername = {}; - bot.chat = (message) => { - bot.queue.push(String(message)); - }; - bot.once('end', (reason, event) => { console.log( `Disconnected from ${bot.options.host} (${event} event): ${util.inspect(reason)}`, @@ -130,11 +104,6 @@ function main() { console.log(e); } - try { - clearInterval(notonline); - clearInterval(chatQueue); - } catch (e) {} - setTimeout(() => { bot.end(); botThings(); diff --git a/plugins/chat.js b/plugins/chat.js index a299148..68534db 100644 --- a/plugins/chat.js +++ b/plugins/chat.js @@ -2,7 +2,37 @@ /* eslint-disable require-jsdoc */ // eslint-disable-next-line no-undef // const parse = require('../util/text_parser'); +const {containsIllegalCharacters} = require('../util/containsIllegalCharacters'); function inject(bot) { + bot.chatQueue = []; + + const chatQueue = setInterval(function() { + try { + if (bot.chatQueue[0] || bot.chatQueue[0] === '') { + try { + if (containsIllegalCharacters(bot.chatQueue[0])) { + bot.chatQueue.shift(); + return; + }; + bot.write('chat', {message: bot.chatQueue[0].substring(0, 256)}); + bot.chatQueue.shift(); + } catch (e) { + console.log(e.message); + } + } + } catch (e) { + return; + } + }, 450); + + bot.chat = (message) => { + bot.chatQueue.push(String(message)); + }; + + bot.once('end', () => { + clearInterval(chatQueue); + }); + let previousMessage; const ChatMessage = require('prismarine-chat')(bot.version);