From a80298567f7b21996ecb51de882e03e016364a8b Mon Sep 17 00:00:00 2001 From: ChomeNS Date: Wed, 9 Nov 2022 19:04:14 +0700 Subject: [PATCH] real console update!1!1 --- config.js | 1 + index.js | 89 +++++---------------------------------------- plugins/console.js | 84 ++++++++++++++++++++++++++++++++++++++++++ util/loadPlugins.js | 4 +- 4 files changed, 96 insertions(+), 82 deletions(-) create mode 100644 plugins/console.js diff --git a/config.js b/config.js index 3ebe5d2..1205ba1 100644 --- a/config.js +++ b/config.js @@ -11,6 +11,7 @@ module.exports = { 'normalKey': '�iB_D���k��j8H�{?[/ڭ�f�}Ѣ�^-=�Ț��v]��g>��=c', 'ownerHashKey': 'b)R��nF�CW���#�\\[�S*8"t^eia�Z��k����K1�8zȢ�', }, + 'console': true, 'useChat': false, 'core': { 'layers': 3, diff --git a/index.js b/index.js index c1ec08b..246fd2b 100644 --- a/index.js +++ b/index.js @@ -87,12 +87,11 @@ function botThings() { }; bot.visibility = false; bot.getplayerusername = {}; + if (!bot.messageLogging) bot.messageLogging = true; - loadPlugins(bot, dcclient, config); + loadPlugins(bot, dcclient, config, rl); } -let messageloggingEnabled = true; - /** * Main bot function */ @@ -122,7 +121,7 @@ function main() { }; dcmsg.queue = ''; - consoleQueue = []; + bot.consoleQueue = []; bot.playersAddedPlayers = {}; bot.getplayerusername = {}; @@ -146,17 +145,17 @@ function main() { }, 1000); consoleQueueInterval = setInterval(function() { - if (!messageloggingEnabled) return; - if (consoleQueue.length > 50) consoleQueue = []; - if (consoleQueue[0] || consoleQueue[0] === '') { + if (!bot.messageLogging) return; + if (bot.consoleQueue.length > 50) bot.consoleQueue = []; + if (bot.consoleQueue[0] || bot.consoleQueue[0] === '') { console.log( '\u001b[48:5:208m' + bot.options.host + '\u001b[0m' + ': ' + - consoleQueue[0].substring(0, 10000), + bot.consoleQueue[0].substring(0, 10000), ); - consoleQueue.shift(); + bot.consoleQueue.shift(); } }, 100); @@ -289,7 +288,7 @@ function main() { .replaceAll('http', 'http\u200b') .replaceAll('\u001b[9', '\u001b[3'); - consoleQueue.push(message.toAnsi()); + bot.consoleQueue.push(message.toAnsi()); if (message.toMotd().startsWith('§8[§eChomeNS §9Discord§8] §c')) return; dcmsg.queue += '\n' + discordMsg; @@ -367,76 +366,6 @@ dcclient.on('ready', async () => { bot.ownerhashchannel = dcclient.channels.cache.get(ownerhashchannel); bot.channelId = channel.id; attachmentlink = ''; - - // Console COMMANDS - rl.on('line', function(line) { - try { - if (line.toLowerCase() === '' || - line.toLowerCase().startsWith(' ')) return; - if (line.toLowerCase() === '.exit' || line.toLowerCase() === '.end') { - bot.emit('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 === '.clearconsolequeue') { - consoleQueue = []; - return; - } - if (line === '.messagelogging on') { - messageloggingEnabled = true; - return; - } - if (line === '.messagelogging off') { - messageloggingEnabled = false; - return; - } - if (line === '.kill') process.exit(); - if (line.startsWith('.')) { - return bot.command_handler.run( - 'Console', - '§a§lConsole§r', - '*' + line.substring(1), - 'c0ns0le-uuid', - ); - } - 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) { - console.log(e); - } - }); }); // Redirect Discord messages to in-game chat diff --git a/plugins/console.js b/plugins/console.js new file mode 100644 index 0000000..2514d9a --- /dev/null +++ b/plugins/console.js @@ -0,0 +1,84 @@ +/* eslint-disable prefer-rest-params */ + +function inject(bot, dcclient, config, rl) { + if (!config.console) return; + + // readline > fix on console.log + const log = console.log; + console.log = function() { + rl.output.write('\x1b[2K\r'); + log.apply(console, Array.prototype.slice.call(arguments)); + rl._refreshLine(); + }; + const {MessageBuilder} = require('prismarine-chat')('1.18.2'); + rl.on('line', function(line) { + try { + if (line.toLowerCase() === '' || + line.toLowerCase().startsWith(' ')) return; + if (line.toLowerCase() === '.exit' || line.toLowerCase() === '.end') { + bot.emit('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 === '.clearconsolequeue') { + consoleQueue = []; + return; + } + if (line === '.messagelogging on') { + bot.consoleQueue = []; + bot.messageLogging = true; + return; + } + if (line === '.messagelogging off') { + bot.consoleQueue = []; + bot.messageLogging = false; + return; + } + if (line === '.kill') process.exit(); + if (line.startsWith('.')) { + return bot.command_handler.run( + 'Console', + '§a§lConsole§r', + '*' + line.substring(1), + 'c0ns0le-uuid', + ); + } + 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', + }, + MessageBuilder.fromString('&7' + line), + ]); + } catch (e) {} + }); +}; + +module.exports = {inject}; diff --git a/util/loadPlugins.js b/util/loadPlugins.js index 606d717..2f426de 100644 --- a/util/loadPlugins.js +++ b/util/loadPlugins.js @@ -2,13 +2,13 @@ const fs = require('fs/promises'); const util = require('util'); const path = require('path'); -async function loadPlugins(bot, dcclient, config) { +async function loadPlugins(bot, dcclient, config, rl) { const plugins = await fs.readdir(path.join(__dirname, '..', 'plugins')); plugins.forEach((plugin) => { try { const plug = require(path.join(__dirname, '..', 'plugins', plugin)); - plug.inject(bot, dcclient, config); + plug.inject(bot, dcclient, config, rl); } catch (e) { console.log(`Plugin ${plugin} is having exception loading the plugin:`); console.log(util.inspect(e));