Added error handling to the bot.

This commit is contained in:
2023-06-26 09:04:15 +02:00
parent 912e501025
commit 9f882e1daf

View File

@@ -1,4 +1,4 @@
const { execSync } = require('child_process');
const { execSync, spawn } = require('child_process');
const fs = require('node:fs');
const os = require('os');
@@ -140,7 +140,24 @@ async function startBot(accountName) {
}
try {
execSync(`timeout 150m bash -c "${commandPrefix} ${commandSuffix}; exit"`, { stdio: 'inherit', shell: '/bin/bash' });
// execSync(`timeout 150m bash -c "${commandPrefix} ${commandSuffix}; exit"`, { stdio: 'inherit', shell: '/bin/bash' });
const childProcess = spawn('bash', ['-c', `${commandPrefix} ${commandSuffix}`], { stdio: 'inherit' });
childProcess.stdout.on('data', (data) => {
console.log(`[${accountName}] ${data.toString()}`);
if (data.includes('Press enter') || data.includes('Press any key')) {
setTimeout(() => {
childProcess.stdin.write('\n');
}, 1000);
}
});
childProcess.on('close', (code) => {
if (code !== 0) {
consoleLog(`[${host}] Bot process for ${accountName} exited with code ${code}. Restarting...`, 'error');
startBot(accountName);
}
});
}
catch (error) {
consoleLog(`[${host}] Failed to start bot for ${accountName}.`, 'error');