among us console fix + cloop discord fix

This commit is contained in:
ChomeNS 2022-11-20 13:00:00 +07:00
parent 7cacc880e0
commit a30eb19759
6 changed files with 111 additions and 118 deletions

5
bot.js
View file

@ -8,9 +8,6 @@ const generateEaglerUsername = require('./util/generateEaglerUsername');
const {EventEmitter} = require('events');
const {loadPlugins} = require('./util/loadPlugins');
const uuid = require('uuid-by-string');
const readline = require('node:readline');
const {stdin: input, stdout: output} = require('node:process');
const rl = readline.createInterface({input, output});
const moment = require('moment-timezone');
const cowsay = require('cowsay2');
const cows = require('cowsay2/cows');
@ -57,7 +54,7 @@ async function createBot(server, config, getBots, dcclient) {
await sleep(200);
await loadPlugins(bot, dcclient, config, rl);
await loadPlugins(bot, dcclient, config);
const channel = dcclient.channels.cache.get(config.discord.servers[bot.options.host]);

View file

@ -104,14 +104,14 @@ module.exports = {
} else if (args[0] === 'list') {
list(bot, true, channeldc);
} else if (args[0] === 'remove') {
remove(args[1]);
remove(args[1], bot);
const Embed = new MessageEmbed()
.setColor('#FFFF00')
.setTitle('Cloop')
.setDescription(`Removed cloop \`${args[1]}\``);
channeldc.send({embeds: [Embed]});
} else if (args[0] === 'removeall') {
clear();
clear(bot);
const Embed = new MessageEmbed()
.setColor('#FFFF00')
.setTitle('Cloop')

View file

@ -1,3 +1,6 @@
const readline = require('node:readline');
const {stdin: input, stdout: output} = require('node:process');
const rl = readline.createInterface({input, output});
const config = require('./config');
const {createBot} = require('./bot');
const {
@ -8,13 +11,6 @@ const intents = new Intents(['GUILDS', 'GUILD_MESSAGES']);
const dcclient = new Client({
intents,
});
// 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 bots = [];
@ -24,12 +20,9 @@ dcclient.on('ready', () => {
// await is important cuz the function is async
// VVVVV
const bot = await createBot(server, config, getBots, dcclient);
bot.console.setRl(rl);
bots.push(bot);
});
});
dcclient.login(config.discord.token);
process.on('uncaughtException', (error) => {
console.log(error);
});

View file

@ -3,7 +3,7 @@
const moment = require('moment-timezone');
const util = require('util');
function inject(bot, _dcclient, config, rl) {
function inject(bot, _dcclient, config) {
if (!config.console) return;
const chatMessage = require('prismarine-chat')(bot.version);
@ -16,6 +16,8 @@ function inject(bot, _dcclient, config, rl) {
bot.console = {};
bot.console.host = 'all';
bot.console._rl = null;
bot.console.setRl = setRl;
bot.console.log = function(message) {
log(prefix('&6LOG', message));
};
@ -26,32 +28,36 @@ function inject(bot, _dcclient, config, rl) {
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.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) => {
bot.consoleQueue.push(message.toAnsi());
if (!bot.messageLogging) return;
bot.console.log(message.toAnsi());
});
bot.once('end', () => {
clearInterval(consoleQueueInterval);
rl.removeAllListeners();
});
// bot.once('end', () => {
// clearInterval(consoleQueueInterval);
// rl.removeAllListeners();
// });
// readline > fix on log
function log(...args) {
rl.output.write('\x1b[2K\r');
bot.console._rl.output.write('\x1b[2K\r');
console.log(args.toString());
rl._refreshLine();
bot.console._rl._refreshLine();
};
function setRl(rl) {
bot.console._rl = rl;
rl.on('line', function(line) {
try {
if (line.toLowerCase() === '' ||
@ -59,7 +65,7 @@ function inject(bot, _dcclient, config, rl) {
if (line.startsWith('.csvr ')) {
const host = line.substring(6);
bot.getBots().forEach((bot) => bot.console.host = host);
bot.getBots().forEach((eachBot) => eachBot.console.host = host);
bot.console.info(`Host set to: ${host}`);
return;
}
@ -135,6 +141,7 @@ function inject(bot, _dcclient, config, rl) {
bot.console.error(e);
}
});
}
};
module.exports = {inject};

View file

@ -1,7 +1,7 @@
/* eslint-disable require-jsdoc */
/* eslint-disable max-len */
const {escapeMarkdown} = require('../util/escapeMarkdown');
async function inject(bot, dcclient, config, rl) {
async function inject(bot, dcclient, config) {
const channel = dcclient.channels.cache.get(config.discord.servers[bot.options.host]);
let queue = '';

View file

@ -8,19 +8,15 @@ 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 {object} targetClient proxy target
* @param {object} client proxy client
* @param {boolean} proxy is proxy
*/
async function loadPlugins(bot, dcclient, config, rl) {
async function loadPlugins(bot, dcclient, config) {
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, rl);
plug.inject(bot, dcclient, config);
} catch (e) {
console.log(`Plugin ${plugin} is having exception loading the plugin:`);
console.log(util.inspect(e));