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 {EventEmitter} = require('events');
const {loadPlugins} = require('./util/loadPlugins'); const {loadPlugins} = require('./util/loadPlugins');
const uuid = require('uuid-by-string'); 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 moment = require('moment-timezone');
const cowsay = require('cowsay2'); const cowsay = require('cowsay2');
const cows = require('cowsay2/cows'); const cows = require('cowsay2/cows');
@ -57,7 +54,7 @@ async function createBot(server, config, getBots, dcclient) {
await sleep(200); 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]); 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') { } else if (args[0] === 'list') {
list(bot, true, channeldc); list(bot, true, channeldc);
} else if (args[0] === 'remove') { } else if (args[0] === 'remove') {
remove(args[1]); remove(args[1], bot);
const Embed = new MessageEmbed() const Embed = new MessageEmbed()
.setColor('#FFFF00') .setColor('#FFFF00')
.setTitle('Cloop') .setTitle('Cloop')
.setDescription(`Removed cloop \`${args[1]}\``); .setDescription(`Removed cloop \`${args[1]}\``);
channeldc.send({embeds: [Embed]}); channeldc.send({embeds: [Embed]});
} else if (args[0] === 'removeall') { } else if (args[0] === 'removeall') {
clear(); clear(bot);
const Embed = new MessageEmbed() const Embed = new MessageEmbed()
.setColor('#FFFF00') .setColor('#FFFF00')
.setTitle('Cloop') .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 config = require('./config');
const {createBot} = require('./bot'); const {createBot} = require('./bot');
const { const {
@ -8,13 +11,6 @@ const intents = new Intents(['GUILDS', 'GUILD_MESSAGES']);
const dcclient = new Client({ const dcclient = new Client({
intents, 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 = []; const bots = [];
@ -24,12 +20,9 @@ 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); const bot = await createBot(server, config, getBots, dcclient);
bot.console.setRl(rl);
bots.push(bot); bots.push(bot);
}); });
}); });
dcclient.login(config.discord.token); dcclient.login(config.discord.token);
process.on('uncaughtException', (error) => {
console.log(error);
});

View file

@ -3,7 +3,7 @@
const moment = require('moment-timezone'); const moment = require('moment-timezone');
const util = require('util'); const util = require('util');
function inject(bot, _dcclient, config, rl) { function inject(bot, _dcclient, config) {
if (!config.console) return; if (!config.console) return;
const chatMessage = require('prismarine-chat')(bot.version); const chatMessage = require('prismarine-chat')(bot.version);
@ -16,6 +16,8 @@ function inject(bot, _dcclient, config, rl) {
bot.console = {}; bot.console = {};
bot.console.host = 'all'; bot.console.host = 'all';
bot.console._rl = null;
bot.console.setRl = setRl;
bot.console.log = function(message) { bot.console.log = function(message) {
log(prefix('&6LOG', message)); log(prefix('&6LOG', message));
}; };
@ -26,115 +28,120 @@ function inject(bot, _dcclient, config, rl) {
log(prefix('&cERROR', typeof error === 'string' ? error : error.stack)); log(prefix('&cERROR', typeof error === 'string' ? error : error.stack));
}; };
bot.consoleQueue = []; // bot.consoleQueue = [];
const consoleQueueInterval = setInterval(function() { // const consoleQueueInterval = setInterval(function() {
if (!bot.messageLogging) return; // if (!bot.messageLogging) return;
if (bot.consoleQueue.length > 50) bot.consoleQueue = []; // if (bot.consoleQueue.length > 50) bot.consoleQueue = [];
if (bot.consoleQueue[0] || bot.consoleQueue[0] === '') { // if (bot.consoleQueue[0] || bot.consoleQueue[0] === '') {
bot.console.log(bot.consoleQueue[0].substring(0, 10000)); // bot.console.log(bot.consoleQueue[0].substring(0, 10000));
bot.consoleQueue.shift(); // bot.consoleQueue.shift();
} // }
}, 100); // }, 100);
bot.on('parsed_chat', (message) => { bot.on('parsed_chat', (message) => {
bot.consoleQueue.push(message.toAnsi()); if (!bot.messageLogging) return;
bot.console.log(message.toAnsi());
}); });
bot.once('end', () => { // bot.once('end', () => {
clearInterval(consoleQueueInterval); // clearInterval(consoleQueueInterval);
rl.removeAllListeners(); // rl.removeAllListeners();
}); // });
// readline > fix on log // readline > fix on log
function log(...args) { function log(...args) {
rl.output.write('\x1b[2K\r'); bot.console._rl.output.write('\x1b[2K\r');
console.log(args.toString()); console.log(args.toString());
rl._refreshLine(); bot.console._rl._refreshLine();
}; };
rl.on('line', function(line) { function setRl(rl) {
try { bot.console._rl = rl;
if (line.toLowerCase() === '' ||
line.toLowerCase().startsWith(' ')) return;
if (line.startsWith('.csvr ')) { rl.on('line', function(line) {
const host = line.substring(6); try {
bot.getBots().forEach((bot) => bot.console.host = host); if (line.toLowerCase() === '' ||
bot.console.info(`Host set to: ${host}`); line.toLowerCase().startsWith(' ')) return;
return;
}
if (bot.options.host !== bot.console.host && bot.console.host !== 'all') return; if (line.startsWith('.csvr ')) {
if (line.toLowerCase() === '.exit' || line.toLowerCase() === '.end') { const host = line.substring(6);
bot.emit('end', 'end command'); bot.getBots().forEach((eachBot) => eachBot.console.host = host);
return; bot.console.info(`Host set to: ${host}`);
}
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; 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('.')) { if (bot.options.host !== bot.console.host && bot.console.host !== 'all') return;
return bot.command_handler.run( if (line.toLowerCase() === '.exit' || line.toLowerCase() === '.end') {
bot.username, bot.emit('end', 'end command');
bot.username, return;
'*' + line.substring(1), }
bot.uuid, if (line.toLowerCase().startsWith('.servereval ')) {
null, try {
'h', bot.tellraw('@a', {
'o', 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(
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);
} }
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}; module.exports = {inject};

View file

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

View file

@ -8,19 +8,15 @@ const path = require('path');
* @param {object} bot the bot object * @param {object} bot the bot object
* @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} 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 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));
plug.inject(bot, dcclient, config, rl); plug.inject(bot, dcclient, config);
} 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));