chomens-bot-js/run.js

84 lines
2.1 KiB
JavaScript
Raw Normal View History

2022-08-14 05:51:45 -04:00
/* eslint-disable max-len */
/* eslint-disable no-var */
const readline = require('node:readline');
const {stdin: input, stdout: output} = require('node:process');
const rl = readline.createInterface({input, output});
const process = require('child_process');
2022-08-18 22:36:34 -04:00
const config = require('./config');
2022-08-14 05:51:45 -04:00
require('./uptime');
2022-08-18 07:02:14 -04:00
rl.setMaxListeners(Infinity);
2022-08-14 05:51:45 -04:00
const bots = {};
/**
* the start function
* @param {String} host
* @param {Number} port
*/
function start(host, port) {
const server = host;
let theServer;
bots[server] = process.exec('node index.js ' + host);
if (host=='sus.shhnowisnottheti.me') {
theServer = 'ayunboom';
}
if (host=='129.159.58.114') {
theServer = 'dinboom';
}
2022-10-18 04:14:29 -04:00
if (host=='kaboom.pw') {
2022-08-14 05:51:45 -04:00
theServer = 'kaboom';
}
if (host=='192.168.1.103') {
theServer = 'local';
}
if (host=='kitsune.icu') {
theServer = 'kitsune';
}
2022-08-20 22:21:59 -04:00
if (host=='mc.chomens41793.ga') {
theServer = 'chomens';
}
2022-08-14 05:51:45 -04:00
eval(`${theServer} = []`);
eval(`${theServer}loop = setInterval(() => {
if (${theServer}[0]) {
bots[server].stdin.write(${theServer}.at(0) + '\\n');
${theServer} = [];
}
}, 0)`);
bots[server].on('close', function() {
bots[server].kill();
eval(`clearInterval(${theServer}loop)`);
start(server);
});
bots[server].stdout.on('data', function(chunk) {
rl.output.write('\x1b[2K\r');
require('process').stdout.write(`${chunk}`);
rl._refreshLine();
});
2022-10-25 04:36:28 -04:00
bots[server].stderr.on('data', function(chunk) {
rl.output.write('\x1b[2K\r');
require('process').stderr.write(`${chunk}`);
rl._refreshLine();
});
2022-08-14 05:51:45 -04:00
rl.on('line', (line) => {
if (line.startsWith('/send ')) {
try {
const args = line.substring(1).trim().split(' ');
eval(`${args.at(1)}.push(args.slice(2).join(' ').toString())`);
} catch (e) {
console.log('Invalid.');
}
return;
}
bots[server].stdin.write(line + '\n');
2022-10-24 21:48:54 -04:00
require('process').once('SIGINT', () => {
2022-08-14 05:51:45 -04:00
bots[server].kill();
require('process').exit();
});
});
}
for (const server of config.servers) {
start(server.host, server.port);
}