From a835f6afc82b8dc408eaa060fedeb8f8bd2197e5 Mon Sep 17 00:00:00 2001 From: ChomeNS Date: Sat, 26 Nov 2022 18:39:48 +0700 Subject: [PATCH] remove onetime inject and revert console --- bot.js | 1 - index.js | 2 - plugins/console.js | 160 ++++++++++++++++++++------------------------ util/loadPlugins.js | 6 +- 4 files changed, 75 insertions(+), 94 deletions(-) diff --git a/bot.js b/bot.js index 2a86370..9ef04c6 100644 --- a/bot.js +++ b/bot.js @@ -51,7 +51,6 @@ async function createBot(server, config, getBots, dcclient, rl) { bot.visibility = false; bot.getBots = getBots; bot.getplayerusername = {}; - if (typeof bot.messageLogging === 'undefined') bot.messageLogging = true; await sleep(200); diff --git a/index.js b/index.js index 07f2363..6b1be40 100644 --- a/index.js +++ b/index.js @@ -21,8 +21,6 @@ dcclient.on('ready', () => { // await is important cuz the function is async // VVVVV const bot = await createBot(server, config, getBots, dcclient, rl); - await loadPlugins(bot, dcclient, config, rl, true); - bot.console.setRl(rl); bots.push(bot); }); }); diff --git a/plugins/console.js b/plugins/console.js index 64b5438..0e31047 100644 --- a/plugins/console.js +++ b/plugins/console.js @@ -4,6 +4,8 @@ const moment = require('moment-timezone'); const util = require('util'); function inject(bot, _dcclient, config, rl) { + if (!config.console) return; + // readline > fix on log function log(...args) { rl.output.write('\x1b[2K\r'); @@ -32,94 +34,78 @@ function inject(bot, _dcclient, config, rl) { }; bot.on('parsed_chat', (message) => { - if (!bot.messageLogging) return; bot.console.log(message.toAnsi()); }); + + rl.on('line', function(line) { + try { + if (line.toLowerCase() === '' || + line.toLowerCase().startsWith(' ')) return; + + if (line.startsWith('.csvr ')) { + const host = line.substring(6); + bot.getBots().forEach((eachBot) => eachBot.console.host = host); + bot.console.info(`Host set to: ${host}`); + return; + } + + if (bot.options.host !== bot.console.host && bot.console.host !== 'all') return; + if (line.toLowerCase() === '.exit' || line.toLowerCase() === '.end') { + bot.end('end command'); + return; + } + if (line.toLowerCase().startsWith('.servereval ')) { + try { + bot.tellraw('@a', { + text: `${util.inspect(eval(`${line.substring(12)}`))}`, + color: 'green', + }); + return; + } catch (err) { + bot.tellraw('@a', {text: `${util.inspect(err)}`, color: 'red'}); + return; + } + } + if (line === '.kill') process.exit(); + + if (line.startsWith('.')) { + return bot.command_handler.run( + bot.username, + bot.username, + '*' + line.substring(1), + bot.uuid, + null, + 'h', + 'o', + ); + } + bot.tellraw('@a', [ + { + text: '[', + color: 'dark_gray', + }, + { + text: `${bot.username} Console`, + color: 'gray', + }, + { + text: '] ', + color: 'dark_gray', + }, + { + text: 'chayapak ', + color: 'green', + }, + { + text: '\u203a ', + color: 'dark_gray', + }, + chatMessage.MessageBuilder.fromString('&7' + line), + ]); + } catch (e) { + bot.console.error(e); + } + }); } -function oneTimeInject(bot, _dcclient, config, rl) { - if (!config.console) return; - - bot.console.setRl = setRl; - function setRl(rl) { - rl.on('line', function(line) { - try { - if (line.toLowerCase() === '' || - line.toLowerCase().startsWith(' ')) return; - - if (line.startsWith('.csvr ')) { - const host = line.substring(6); - bot.getBots().forEach((eachBot) => eachBot.console.host = host); - bot.console.info(`Host set to: ${host}`); - return; - } - - if (bot.options.host !== bot.console.host && bot.console.host !== 'all') return; - if (line.toLowerCase() === '.exit' || line.toLowerCase() === '.end') { - bot.end('end command'); - return; - } - if (line.toLowerCase().startsWith('.servereval ')) { - try { - bot.tellraw('@a', { - text: `${util.inspect(eval(`${line.substring(12)}`))}`, - color: 'green', - }); - return; - } catch (err) { - bot.tellraw('@a', {text: `${util.inspect(err)}`, color: 'red'}); - return; - } - } - if (line === '.messagelogging on') { - bot.messageLogging = true; - return; - } - if (line === '.messagelogging off') { - bot.messageLogging = false; - return; - } - if (line === '.kill') process.exit(); - - if (line.startsWith('.')) { - return bot.command_handler.run( - bot.username, - bot.username, - '*' + line.substring(1), - bot.uuid, - null, - 'h', - 'o', - ); - } - bot.tellraw('@a', [ - { - text: '[', - color: 'dark_gray', - }, - { - text: `${bot.username} Console`, - color: 'gray', - }, - { - text: '] ', - color: 'dark_gray', - }, - { - text: 'chayapak ', - color: 'green', - }, - { - text: '\u203a ', - color: 'dark_gray', - }, - chatMessage.MessageBuilder.fromString('&7' + line), - ]); - } catch (e) { - bot.console.error(e); - } - }); - } -}; - -module.exports = {inject, oneTimeInject}; +module.exports = {inject}; diff --git a/util/loadPlugins.js b/util/loadPlugins.js index c985022..bf75203 100644 --- a/util/loadPlugins.js +++ b/util/loadPlugins.js @@ -9,17 +9,15 @@ const path = require('path'); * @param {object} dcclient discord client * @param {object} config the config * @param {object} rl readline. - * @param {boolean} oneTime load plugins one time */ -async function loadPlugins(bot, dcclient, config, rl, oneTime) { +async function loadPlugins(bot, dcclient, config, rl) { const dir = path.join(__dirname, '..', 'plugins'); const plugins = await fs.readdir(dir); plugins.forEach((plugin) => { if (!plugin.endsWith('.js')) return; try { const plug = require(path.join(dir, plugin)); - if (!oneTime) plug.inject(bot, dcclient, config, rl); - if (oneTime && plug.oneTimeInject) plug.oneTimeInject(bot, dcclient, config, rl); + plug.inject(bot, dcclient, config, rl); } catch (e) { console.log(`Plugin ${plugin} is having exception loading the plugin:`); console.log(util.inspect(e));