chomens-bot-js/plugins/console.js

147 lines
4.1 KiB
JavaScript
Raw Normal View History

2022-11-15 21:33:16 -05:00
/* eslint-disable require-jsdoc */
/* eslint-disable max-len */
const moment = require('moment-timezone');
2022-11-13 01:57:18 -05:00
const util = require('util');
2022-11-09 07:04:14 -05:00
2022-11-20 02:29:11 -05:00
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();
};
2022-11-09 07:04:14 -05:00
const chatMessage = require('prismarine-chat')(bot.version);
function prefix(prefix, _message) {
const message = `[${moment().format('HH:mm:ss')} ${prefix}§r] [${bot.options.host}] `;
const component = chatMessage.MessageBuilder.fromString(message).toJSON();
return chatMessage.fromNotch(component).toAnsi() + _message;
}
bot.console = {};
2022-11-14 05:45:49 -05:00
bot.console.host = 'all';
bot.console.log = function(message) {
2022-11-20 00:41:35 -05:00
log(prefix('&6LOG', message));
};
bot.console.info = function(message) {
2022-11-20 00:41:35 -05:00
log(prefix('&aINFO', message));
};
2022-11-14 05:45:49 -05:00
bot.console.error = function(error) {
2022-11-20 00:41:35 -05:00
log(prefix('&cERROR', typeof error === 'string' ? error : error.stack));
};
// bot.consoleQueue = [];
// const consoleQueueInterval = setInterval(function() {
// if (!bot.messageLogging) return;
// if (bot.consoleQueue.length > 50) bot.consoleQueue = [];
// if (bot.consoleQueue[0] || bot.consoleQueue[0] === '') {
// bot.console.log(bot.consoleQueue[0].substring(0, 10000));
// bot.consoleQueue.shift();
// }
// }, 100);
bot.on('parsed_chat', (message) => {
if (!bot.messageLogging) return;
bot.console.log(message.toAnsi());
});
// bot.once('end', () => {
// clearInterval(consoleQueueInterval);
// rl.removeAllListeners();
// });
2022-11-20 02:29:11 -05:00
}
2022-11-20 02:29:11 -05:00
function oneTimeInject(bot, _dcclient, config, rl) {
if (!config.console) return;
2022-11-20 00:41:35 -05:00
2022-11-20 02:29:11 -05:00
bot.console.setRl = setRl;
function setRl(rl) {
rl.on('line', function(line) {
try {
if (line.toLowerCase() === '' ||
line.toLowerCase().startsWith(' ')) return;
2022-11-14 06:53:52 -05:00
if (line.startsWith('.csvr ')) {
const host = line.substring(6);
bot.getBots().forEach((eachBot) => eachBot.console.host = host);
bot.console.info(`Host set to: ${host}`);
2022-11-09 07:04:14 -05:00
return;
}
if (bot.options.host !== bot.console.host && bot.console.host !== 'all') return;
if (line.toLowerCase() === '.exit' || line.toLowerCase() === '.end') {
bot.emit('end', 'end command');
2022-11-09 07:04:14 -05:00
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;
}
}
2022-11-20 02:29:11 -05:00
// 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();
2022-11-14 05:45:49 -05:00
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);
2022-11-09 07:04:14 -05:00
}
});
}
2022-11-09 07:04:14 -05:00
};
2022-11-20 02:29:11 -05:00
module.exports = {inject, oneTimeInject};