This commit is contained in:
ChomeNS 2022-11-20 14:29:11 +07:00
parent a30eb19759
commit b15d7c52a8
4 changed files with 28 additions and 23 deletions

7
bot.js
View file

@ -22,9 +22,10 @@ const mineflayer = require('mineflayer');
* @param {object} config the config file
* @param {Function} getBots get bots function in index.js
* @param {Class} dcclient discord client
* @param {object} rl readline.
* @return {object} the bot object
*/
async function createBot(server, config, getBots, dcclient) {
async function createBot(server, config, getBots, dcclient, rl) {
const bot = new EventEmitter();
bot.options = {
username: /* process.argv[2] */ server.host === 'mc.chomens41793.ga' ?
@ -54,7 +55,7 @@ async function createBot(server, config, getBots, dcclient) {
await sleep(200);
await loadPlugins(bot, dcclient, config);
await loadPlugins(bot, dcclient, config, rl);
const channel = dcclient.channels.cache.get(config.discord.servers[bot.options.host]);
@ -158,7 +159,7 @@ async function createBot(server, config, getBots, dcclient) {
setTimeout(() => {
bot.end();
createBot(server, config, getBots, dcclient);
createBot(server, config, getBots, dcclient, rl);
}, timeout);
});

View file

@ -2,6 +2,7 @@ const readline = require('node:readline');
const {stdin: input, stdout: output} = require('node:process');
const rl = readline.createInterface({input, output});
const config = require('./config');
const {loadPlugins} = require('./util/loadPlugins');
const {createBot} = require('./bot');
const {
Client,
@ -19,7 +20,8 @@ dcclient.on('ready', () => {
const getBots = () => bots;
// await is important cuz the function is async
// VVVVV
const bot = await createBot(server, config, getBots, dcclient);
const bot = await createBot(server, config, getBots, dcclient, rl);
await loadPlugins(bot, dcclient, config, rl, true);
bot.console.setRl(rl);
bots.push(bot);
});

View file

@ -3,8 +3,13 @@
const moment = require('moment-timezone');
const util = require('util');
function inject(bot, _dcclient, config) {
if (!config.console) return;
function inject(bot, _dcclient, config, rl) {
// readline > fix on log
function log(...args) {
rl.output.write('\x1b[2K\r');
console.log(args.toString());
rl._refreshLine();
};
const chatMessage = require('prismarine-chat')(bot.version);
@ -16,8 +21,6 @@ function inject(bot, _dcclient, config) {
bot.console = {};
bot.console.host = 'all';
bot.console._rl = null;
bot.console.setRl = setRl;
bot.console.log = function(message) {
log(prefix('&6LOG', message));
};
@ -47,17 +50,13 @@ function inject(bot, _dcclient, config) {
// clearInterval(consoleQueueInterval);
// rl.removeAllListeners();
// });
}
// readline > fix on log
function log(...args) {
bot.console._rl.output.write('\x1b[2K\r');
console.log(args.toString());
bot.console._rl._refreshLine();
};
function oneTimeInject(bot, _dcclient, config, rl) {
if (!config.console) return;
bot.console.setRl = setRl;
function setRl(rl) {
bot.console._rl = rl;
rl.on('line', function(line) {
try {
if (line.toLowerCase() === '' ||
@ -87,10 +86,10 @@ function inject(bot, _dcclient, config) {
return;
}
}
if (line === '.clearconsolequeue') {
consoleQueue = [];
return;
}
// if (line === '.clearconsolequeue') {
// consoleQueue = [];
// return;
// }
if (line === '.messagelogging on') {
bot.consoleQueue = [];
bot.messageLogging = true;
@ -144,4 +143,4 @@ function inject(bot, _dcclient, config) {
}
};
module.exports = {inject};
module.exports = {inject, oneTimeInject};

View file

@ -8,15 +8,18 @@ const path = require('path');
* @param {object} bot the bot object
* @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) {
async function loadPlugins(bot, dcclient, config, rl, oneTime) {
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));
plug.inject(bot, dcclient, config);
if (!oneTime) plug.inject(bot, dcclient, config, rl);
if (oneTime && plug.oneTimeInject) plug.oneTimeInject(bot, dcclient, config, rl);
} catch (e) {
console.log(`Plugin ${plugin} is having exception loading the plugin:`);
console.log(util.inspect(e));