Updated index.js

This commit is contained in:
ShadowVR 2024-05-31 16:40:10 -04:00
parent fcaefaeff9
commit 41a09040ed

View file

@ -1,5 +1,4 @@
const Discord = require('discord.js');
const chalk = require('chalk');
const fs = require('node:fs');
@ -88,24 +87,45 @@ async function updateBotFromGitHub() {
// Force update by resetting to the latest commit on the remote main branch
await git.reset(['--hard', 'origin/main']);
console.log('Bot updated from GitHub. Restarting...');
// Restart the bot to apply changes
process.exit();
console.log('Bot updated from GitHub.');
} catch (error) {
console.error('Error updating bot from GitHub:', error);
}
}
function startBot() {
console.log('Starting the bot...');
const botProcess = exec('node bot.js', {
cwd: path.resolve(__dirname),
});
botProcess.stdout.on('data', (data) => {
console.log(data.toString());
});
botProcess.stderr.on('data', (data) => {
console.error(data.toString());
});
botProcess.on('exit', (code) => {
console.log(`Bot process exited with code ${code}`);
});
}
async function updateAndStartBot() {
await updateBotFromGitHub();
startBot();
}
// Function to check for updates at regular intervals
function checkForUpdates() {
// Set an interval to check for updates (e.g., every 24 hours)
const interval = 24 * 60 * 60 * 1000; // 24 hours in milliseconds
setInterval(updateBotFromGitHub, interval);
setInterval(updateAndStartBot, interval);
// Perform initial check for updates on bot startup
updateBotFromGitHub();
updateAndStartBot();
}
// Call the function to check for updates