2025-07-23 02:52:20 -04:00
2025-07-23 02:52:20 -04:00
2025-07-23 02:52:20 -04:00
2025-07-23 02:52:20 -04:00

MyMC Premium Store

Overview

MyMC Premium Store is a Discord bot that manages Docker containers for Minecraft servers, automatically adjusting container resources (CPU, memory, and swap) based on users' Discord roles. The bot checks for role changes every 5 minutes and updates container configurations accordingly, supporting default, upgraded, and super-upgraded tiers.

NOTE: This repo is for educational purposes, and the code is written specifically for our system, however, you may fork it and use it for any need you may have.

Features

  • Role-Based Resource Allocation: Adjusts Docker container resources (CPUs, memory, swap) based on Discord roles (standard, manualUpgrade, superUpgrade).
  • Periodic Checks: Runs every 5 minutes via a cron job to ensure container settings match user roles.
  • Container Configuration Updates: Updates PM2 process configurations within containers by copying appropriate startServer.json files (startServer_downgrade.json, startServer_upgrade.json, or startServer_superUpgrade.json).
  • Safety Mechanisms:
    • Handles unknown container limits by optionally resetting to default settings.
    • Implements command execution timeouts to prevent hanging operations.
  • Logging: Detailed console logs for container status, role checks, and configuration updates.

Prerequisites

  • Node.js: Version 16 or higher.
  • Docker: Installed and running, with the bot having access to the Docker socket.
  • Discord Bot Token: A valid Discord bot token with Guilds and GuildMembers intents.
  • Environment Variables: Configured via a .env file (see Configuration).
  • PM2: Used inside containers for process management.
  • Configuration Files: startServer_downgrade.json, startServer_upgrade.json, and startServer_superUpgrade.json must exist in the same directory as the bot script.

Installation

  1. Clone the repository:
    git clone git@git.ssh.surf:hypermc/mymc-premium.git
    cd mymc-premium
    
  2. Install dependencies:
    npm install discord.js dockerode cron dotenv
    
  3. Create a .env file in the project root (see Configuration).
  4. Ensure Docker is running and the bot has access to the Docker socket (/var/run/docker.sock).
  5. Place the required startServer_*.json files in the project directory.
  6. Start the bot:
    node index.js
    

Configuration

Create a .env file in the project root with the following variables:

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
ROLE_ID_SUPER_UPGRADE=super_upgrade_role_id
DEFAULT_CPUS=1
DEFAULT_MEMORY=512
DEFAULT_SWAP=1024
UPGRADED_CPUS=2
UPGRADED_MEMORY=1024
UPGRADED_SWAP=2048
SUPER_UPGRADED_CPUS=4
SUPER_UPGRADED_MEMORY=2048
SUPER_UPGRADED_SWAP=4096
RESET_UNKNOWN_TO_DEFAULT=true
EXEC_TIMEOUT=10000
  • DISCORD_TOKEN: Discord bot token.
  • GUILD_ID: ID of the Discord server (guild) to monitor.
  • ROLE_ID_*: Discord role IDs for each tier.
  • DEFAULT_*: Resource limits for default tier (CPUs, memory in MiB, swap in MiB).
  • UPGRADED_*: Resource limits for upgraded tier.
  • SUPER_UPGRADED_*: Resource limits for super-upgraded tier.
  • RESET_UNKNOWN_TO_DEFAULT: If true, resets containers with unknown limits to default settings.
  • EXEC_TIMEOUT: Timeout (in milliseconds) for Docker exec commands.

Usage

  • The bot logs in to Discord and performs an initial container check on startup.
  • Every 5 minutes, it checks all running containers with names starting with mc_ (e.g., mc_1234567890, where 1234567890 is the Discord user ID).
  • For each container:
    • Verifies current resource limits (CPUs, memory, swap).
    • Checks the user's Discord roles in the specified guild.
    • Updates container resources and configuration files if the role-based tier doesn't match the current settings:
      • Super Upgrade: Applies if the user has the superUpgrade role.
      • Standard/Manual Upgrade: Applies if the user has standard or manualUpgrade roles.
      • Default: Applies if the user has no relevant roles.
    • Copies the appropriate startServer_*.json file to the container and restarts the PM2 process.
  • Logs all actions, including errors, to the console.

Container Naming

Containers must be named with the prefix mc_ followed by the Discord user ID (e.g., mc_1234567890).

Configuration Files

The bot uses the following JSON files to configure PM2 processes inside containers:

  • startServer_downgrade.json: For default tier.
  • startServer_upgrade.json: For standard/manual upgrade tier.
  • startServer_superUpgrade.json: For super-upgraded tier.

These files must be present in the bot's working directory and contain valid PM2 configuration for the Minecraft server.

Error Handling

  • Docker Errors: Logs errors during container listing, inspection, or updates.
  • Discord Errors: Logs errors when fetching guild or member data.
  • Timeout Errors: Commands exceeding EXEC_TIMEOUT are terminated and logged.
  • File Copy Errors: Logs failures when copying configuration files.
  • PM2 Errors: Logs errors during PM2 process deletion or startup, but attempts to continue operations where possible.

Logging

The bot provides detailed console logs for:

  • Container details (name, user ID, current resources).
  • Role check results.
  • Actions taken (upgrade, downgrade, reset, or no action).
  • Errors during any operation.
Description
No description provided
Readme 41 KiB
Languages
JavaScript 100%