diff --git a/mymc-premium.js b/mymc-premium.js index 32e86be..270427c 100644 --- a/mymc-premium.js +++ b/mymc-premium.js @@ -2,6 +2,7 @@ const Discord = require('discord.js'); const Docker = require('dockerode'); const { CronJob } = require('cron'); const { execSync } = require('child_process'); +const fs = require('fs').promises; // Added for file operations require('dotenv').config(); const client = new Discord.Client({ @@ -35,6 +36,7 @@ const SUPER_UPGRADED_SWAP = parseInt(process.env.SUPER_UPGRADED_SWAP) * 1024 * 1 const RESET_UNKNOWN_TO_DEFAULT = process.env.RESET_UNKNOWN_TO_DEFAULT === 'true'; const EXEC_TIMEOUT = parseInt(process.env.EXEC_TIMEOUT); +const CACHE_FILE = '/var/www/html/current_upgraded.json'; // Cache file path async function execWithTimeout(container, cmd, timeout = EXEC_TIMEOUT) { try { @@ -126,8 +128,19 @@ async function updateContainerConfig(container, name, userId, memoryLimit) { } } +async function updateCache(upgradedContainers) { + try { + const data = JSON.stringify(upgradedContainers, null, 2); + await fs.writeFile(CACHE_FILE, data); + console.log(` ✅ Cache updated at ${CACHE_FILE} with ${upgradedContainers.length} upgraded containers`); + } catch (err) { + console.error(` ❌ Error writing to cache file ${CACHE_FILE}: ${err.message}`); + } +} + async function checkContainers() { console.log('\n=== Starting Container Check ===\n'); + const upgradedContainers = []; // Track upgraded containers for cache try { // List running containers @@ -168,6 +181,15 @@ async function checkContainers() { currentMem === SUPER_UPGRADED_MEMORY && currentSwap === SUPER_UPGRADED_SWAP; + // Track upgraded containers + if (isUpgraded || isSuperUpgraded) { + upgradedContainers.push({ + containerName: name, + userId: userId, + upgradeType: isSuperUpgraded ? 'super' : 'standard' + }); + } + // Handle unknown limits if (!isDefault && !isUpgraded && !isSuperUpgraded) { console.log(` ⚠️ Warning: Unknown limits detected!`); @@ -218,6 +240,7 @@ async function checkContainers() { }); await updateContainerConfig(container, name, userId, SUPER_UPGRADED_MEMORY); console.log(` ✅ Super Upgraded to: CPUs=${SUPER_UPGRADED_CPUS}, Memory=${SUPER_UPGRADED_MEMORY / 1024 / 1024} MiB, Swap=${SUPER_UPGRADED_SWAP / 1024 / 1024} MiB`); + upgradedContainers.push({ containerName: name, userId: userId, upgradeType: 'super' }); } else if (!hasSuperUpgradeRole && hasStandardOrManualRole && !isUpgraded) { // Apply standard upgrade console.log(` 🔼 Upgrading container...`); @@ -228,6 +251,7 @@ async function checkContainers() { }); await updateContainerConfig(container, name, userId, UPGRADED_MEMORY); console.log(` ✅ Upgraded to: CPUs=${UPGRADED_CPUS}, Memory=${UPGRADED_MEMORY / 1024 / 1024} MiB, Swap=${UPGRADED_SWAP / 1024 / 1024} MiB`); + upgradedContainers.push({ containerName: name, userId: userId, upgradeType: 'standard' }); } else if (!hasSuperUpgradeRole && !hasStandardOrManualRole && (isUpgraded || isSuperUpgraded)) { // Downgrade console.log(` 🔽 Downgrading container...`); @@ -238,12 +262,18 @@ async function checkContainers() { }); await updateContainerConfig(container, name, userId, DEFAULT_MEMORY); console.log(` ✅ Downgraded to: CPUs=${DEFAULT_CPUS}, Memory=${DEFAULT_MEMORY / 1024 / 1024} MiB, Swap=${DEFAULT_SWAP / 1024 / 1024} MiB`); + // Remove from upgradedContainers if present + const index = upgradedContainers.findIndex(c => c.containerName === name); + if (index !== -1) upgradedContainers.splice(index, 1); } else { console.log(` ✅ No action needed. Container settings match role status.`); } console.log('----------------------------------------'); } } + + // Update cache file with upgraded containers + await updateCache(upgradedContainers); console.log('\n=== Container Check Completed ===\n'); } catch (err) { console.error(`\n❌ Error in container check: ${err.message}\n`);