remove onetime inject and revert console

This commit is contained in:
ChomeNS 2022-11-26 18:39:48 +07:00
parent 57ccc0b3e1
commit a835f6afc8
4 changed files with 75 additions and 94 deletions

1
bot.js
View file

@ -51,7 +51,6 @@ async function createBot(server, config, getBots, dcclient, rl) {
bot.visibility = false; bot.visibility = false;
bot.getBots = getBots; bot.getBots = getBots;
bot.getplayerusername = {}; bot.getplayerusername = {};
if (typeof bot.messageLogging === 'undefined') bot.messageLogging = true;
await sleep(200); await sleep(200);

View file

@ -21,8 +21,6 @@ dcclient.on('ready', () => {
// await is important cuz the function is async // await is important cuz the function is async
// VVVVV // VVVVV
const bot = await createBot(server, config, getBots, dcclient, rl); const bot = await createBot(server, config, getBots, dcclient, rl);
await loadPlugins(bot, dcclient, config, rl, true);
bot.console.setRl(rl);
bots.push(bot); bots.push(bot);
}); });
}); });

View file

@ -4,6 +4,8 @@ const moment = require('moment-timezone');
const util = require('util'); const util = require('util');
function inject(bot, _dcclient, config, rl) { function inject(bot, _dcclient, config, rl) {
if (!config.console) return;
// readline > fix on log // readline > fix on log
function log(...args) { function log(...args) {
rl.output.write('\x1b[2K\r'); rl.output.write('\x1b[2K\r');
@ -32,94 +34,78 @@ function inject(bot, _dcclient, config, rl) {
}; };
bot.on('parsed_chat', (message) => { bot.on('parsed_chat', (message) => {
if (!bot.messageLogging) return;
bot.console.log(message.toAnsi()); 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) { module.exports = {inject};
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};

View file

@ -9,17 +9,15 @@ const path = require('path');
* @param {object} dcclient discord client * @param {object} dcclient discord client
* @param {object} config the config * @param {object} config the config
* @param {object} rl readline. * @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 dir = path.join(__dirname, '..', 'plugins');
const plugins = await fs.readdir(dir); const plugins = await fs.readdir(dir);
plugins.forEach((plugin) => { plugins.forEach((plugin) => {
if (!plugin.endsWith('.js')) return; if (!plugin.endsWith('.js')) return;
try { try {
const plug = require(path.join(dir, plugin)); const plug = require(path.join(dir, plugin));
if (!oneTime) plug.inject(bot, dcclient, config, rl); plug.inject(bot, dcclient, config, rl);
if (oneTime && plug.oneTimeInject) plug.oneTimeInject(bot, dcclient, config, rl);
} catch (e) { } catch (e) {
console.log(`Plugin ${plugin} is having exception loading the plugin:`); console.log(`Plugin ${plugin} is having exception loading the plugin:`);
console.log(util.inspect(e)); console.log(util.inspect(e));