# Discord-Linux Premium Store ## Overview Discord-Linux Premium Store is a Node.js application that integrates with Discord and Docker to manage premium user subscriptions. It automatically adjusts Docker container resources (CPU, memory, and swap) based on users' Discord roles, ensuring that premium subscribers receive upgraded resources while non-premium users are assigned default settings. This code was published for educational purposes and requires our infrastructure to run, feel free to fork and use whatever code you may find useful. ## Features - **Discord Role-Based Resource Management**: Automatically upgrades or downgrades Docker container resources based on specific Discord roles. - **MySQL Integration**: Maps user IDs to Discord IDs for role verification. - **Cron Scheduling**: Periodically checks and updates container settings every 30 minutes. - **Container Resource Management**: Supports default and upgraded resource configurations for CPU, memory, and swap. - **Cache System**: Maintains a JSON cache (`current_upgraded.json`) of upgraded containers for tracking. - **Error Handling**: Robust logging and error handling for database queries, Discord API calls, and Docker operations. - **Configurable Settings**: Uses environment variables for flexible configuration. ## Prerequisites - **Node.js**: Version 14 or higher. - **Docker**: Installed and running with access to containers (e.g., SSH containers). - **MySQL**: A MySQL database with a `users` table containing `uid` and `discord_id` columns. - **Discord Bot Token**: A Discord bot with the necessary permissions and intents (`Guilds` and `GuildMembers`). ## Installation 1. **Clone the Repository**: ```bash git clone git@git.ssh.surf:dlinux-community/dlinux-premium.git cd dlinux-premium ``` 2. **Install Dependencies**: ```bash npm install ``` 3. **Set Up Environment Variables**: Create a `.env` file in the project root and configure the following: ```env DISCORD_TOKEN=your_discord_bot_token GUILD_ID=your_discord_guild_id ROLE_ID_STANDARD=standard_role_id ROLE_ID_MANUAL_UPGRADE=manual_upgrade_role_id NO_EXPIRE_CHANNEL_IDS=comma_separated_no_expire_role_ids SQLHOST=your_mysql_host SQLUSER=your_mysql_user SQLDATABASE=your_mysql_database SQLPASSWORD=your_mysql_password DEFAULT_CPUS=1 DEFAULT_MEMORY=512 DEFAULT_SWAP=1024 UPGRADED_CPUS=2 UPGRADED_MEMORY=1024 UPGRADED_SWAP=2048 RESET_UNKNOWN_TO_DEFAULT=true ``` 4. **Set Up MySQL Database**: Ensure the MySQL database is running and has a `users` table with at least the following schema: ```sql CREATE TABLE users ( uid VARCHAR(255) PRIMARY KEY, discord_id VARCHAR(255) NOT NULL ); ``` 5. **Run the Application**: ```bash node index.js ``` ## How It Works 1. **Discord Bot Initialization**: - The bot logs in using the provided `DISCORD_TOKEN` and connects to the specified Discord guild. - Required intents: `Guilds` and `GuildMembers`. 2. **Container Checking**: - A cron job runs every 30 minutes to check all running Docker containers. - Only containers with names starting with `SSH` are processed. - For each container: - The application retrieves the associated Discord ID from the MySQL database using the container name as the `uid`. - It inspects the container's current resource settings (CPU, memory, swap). - It checks the user's Discord roles in the specified guild. 3. **Resource Management**: - **Upgrading**: If the user has the `standard` or `manualUpgrade` role and the container is not already upgraded, the container is updated to use `UPGRADED_CPUS`, `UPGRADED_MEMORY`, and `UPGRADED_SWAP`. - **Downgrading**: If the user lacks the required roles but the container is upgraded, it is reverted to `DEFAULT_CPUS`, `DEFAULT_MEMORY`, and `DEFAULT_SWAP`. - **No-Expire Roles**: Containers for users with `noExpire` roles are tracked but not downgraded. - **Unknown Limits**: If a container has settings that match neither default nor upgraded configurations and `RESET_UNKNOWN_TO_DEFAULT` is `true`, it is reset to default settings. 4. **Caching**: - Upgraded containers are stored in `/var/www/html/current_upgraded.json` for tracking. - The cache is updated after each container check cycle. 5. **Delay Between Checks**: - A 3-second delay is added between container checks to prevent overwhelming the Docker API. ## Configuration The application uses environment variables for configuration. Key variables include: - **Discord Settings**: - `DISCORD_TOKEN`: The Discord bot token. - `GUILD_ID`: The ID of the Discord guild to monitor. - `ROLE_ID_STANDARD`: Role ID for standard premium users. - `ROLE_ID_MANUAL_UPGRADE`: Role ID for manually upgraded users. - `NO_EXPIRE_CHANNEL_IDS`: Comma-separated list of role IDs for users with non-expiring upgrades. - **MySQL Settings**: - `SQLHOST`, `SQLUSER`, `SQLDATABASE`, `SQLPASSWORD`: MySQL connection details. - **Docker Resource Settings**: - `DEFAULT_CPUS`, `DEFAULT_MEMORY`, `DEFAULT_SWAP`: Default container resource limits. - `UPGRADED_CPUS`, `UPGRADED_MEMORY`, `UPGRADED_SWAP`: Upgraded container resource limits. - **Other**: - `RESET_UNKNOWN_TO_DEFAULT`: Whether to reset containers with unknown limits to default settings (`true` or `false`). - `CACHE_FILE`: Path to the JSON cache file (default: `/var/www/html/current_upgraded.json`). ## Dependencies - `discord.js`: For interacting with the Discord API. - `dockerode`: For managing Docker containers. - `cron`: For scheduling container checks. - `mysql2`: For MySQL database queries. - `dotenv`: For loading environment variables. - `fs`: For cache file operations. ## Usage - Ensure the Discord bot is invited to the target guild with the necessary permissions. - Populate the MySQL `users` table with user IDs and their corresponding Discord IDs. - Start the application. It will: - Log in to Discord. - Perform an initial container check. - Schedule recurring checks every 30 minutes. ## Logging The application logs: - Container inspection details (CPU, memory, swap). - Discord role checks and user status. - Actions taken (upgrading, downgrading, or resetting containers). - Errors from Discord API, Docker API, or MySQL queries. - Cache updates.