chomens-bot-js/run.js
2022-11-11 19:03:44 +07:00

83 lines
2.1 KiB
JavaScript

/* 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');
const config = require('./config');
require('./uptime');
rl.setMaxListeners(Infinity);
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';
}
if (host=='play.kaboom.pw') {
theServer = 'kaboom';
}
if (host=='192.168.1.103') {
theServer = 'local';
}
if (host=='kitsune.icu') {
theServer = 'kitsune';
}
if (host=='mc.chomens41793.ga') {
theServer = 'chomens';
}
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();
});
bots[server].stderr.on('data', function(chunk) {
rl.output.write('\x1b[2K\r');
require('process').stderr.write(`${chunk}`);
rl._refreshLine();
});
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');
require('process').once('SIGINT', () => {
bots[server].kill();
require('process').exit();
});
});
}
for (const server of config.servers) {
start(server.host, server.port);
}