A Discord bot that manages your Valheim server using either Docker or Kubernetes.
This approach minimizes resource consumption since the valheim server docker image I use doesn't support auto pausing the server when there are no players on the server.
The bot registers 3 slash commands:
- /start
- /stop
- /status
The bot supports two runtime modes:
- kubernetes: Manages Valheim servers running as Kubernetes deployments
- docker: Manages Valheim servers running as Docker containers
Set the runtime mode using the RUNTIME_MODE environment variable (defaults to kubernetes).
- A Kubernetes cluster
kubectlconfigured to access your cluster- Helm 3 installed
- (Optional) HashiCorp Vault for secret management
-
Invite your bot to a Discord server
- Use the OAuth2 URL Generator from https://discord.com/developers/applications
- Select the required bot permissions
-
Install using Helm
# Navigate to the helm chart directory cd deployment/helm # Install the chart helm install valheim-bot . -n game-servers --create-namespace
-
Configure the bot
The Helm chart supports two configuration methods:
Option A: Using ConfigMap (for non-sensitive data)
Edit the ConfigMap after installation:
kubectl edit configmap valheim-bot-discord-bot -n game-servers
Set the following environment variables:
RUNTIME_MODE: Set tokubernetes(default)DISCORD_TOKEN: Your Discord bot tokenDISCORD_CLIENT_ID: Your Discord application client IDGUILD_IDS: Comma-separated list of Discord server IDsDEPLOYMENT_NAME: The name of your Valheim server deployment in KubernetesNAMESPACE: The namespace where your Valheim server runs (defaults to the chart namespace)SERVER_NAME: Your Valheim server nameJOIN_CODE_LOOP_COUNT: (Optional) Number of retries for join code (default: 20)JOIN_CODE_LOOP_TIMEOUT_MILLIS: (Optional) Timeout in milliseconds (default: 5000)
Option B: Using HashiCorp Vault (recommended for production)
If you have Vault enabled (
vault.enabled: truein values.yaml):- Store secrets in Vault at path:
kv/game-servers/valheim-bot-discord-bot - The chart will automatically create a VaultStaticSecret resource
-
Customize the deployment
Create a custom
values.yaml:namespace: game-servers discordBotImage: ghcr.io/floryn08/valheim-server-discord-bot:1.2.0 vault: enabled: false # Set to true if using Vault
Install with custom values:
helm install valheim-bot . -f custom-values.yaml -n game-servers --create-namespace -
Verify the deployment
kubectl get pods -n game-servers kubectl logs -f deployment/valheim-bot-discord-bot -n game-servers
To upgrade to a new version:
helm upgrade valheim-bot . -n game-serversTo remove the deployment:
helm uninstall valheim-bot -n game-servers- Docker installed and running
- A running Valheim server container
- Discord bot credentials
-
Invite your bot to a Discord server
- Use the OAuth2 URL Generator from https://discord.com/developers/applications
- Select the required bot permissions
-
Create a
.envfile (for local development)RUNTIME_MODE=docker DISCORD_TOKEN=your_discord_bot_token DISCORD_CLIENT_ID=your_discord_client_id GUILD_IDS=your_guild_id1,your_guild_id2 CONTAINER_NAME=your_valheim_container_name SERVER_NAME=YourValheimServerName # Optional # DOCKER_SOCKET_PATH=/var/run/docker.sock # JOIN_CODE_LOOP_COUNT=20 # JOIN_CODE_LOOP_TIMEOUT_MILLIS=5000
Environment Variables for Docker Mode:
RUNTIME_MODE: Set todockerDISCORD_TOKEN: Your Discord bot tokenDISCORD_CLIENT_ID: Your Discord application client IDGUILD_IDS: Comma-separated list of Discord server IDsCONTAINER_NAME: The name or ID of your Valheim server Docker containerDOCKER_SOCKET_PATH: (Optional) Path to Docker socket (default:/var/run/docker.sock)SERVER_NAME: Your Valheim server name (used to extract join code from logs)JOIN_CODE_LOOP_COUNT: (Optional) Number of retries for join code (default: 20)JOIN_CODE_LOOP_TIMEOUT_MILLIS: (Optional) Timeout in milliseconds (default: 5000)
-
Run with Docker Compose
Update the provided
deployment/docker-compose.yamlor create your own:services: valheim-server-discord-bot: container_name: valheim-server-discord-bot image: ghcr.io/floryn08/valheim-server-discord-bot:latest environment: RUNTIME_MODE: docker DISCORD_TOKEN: ${DISCORD_TOKEN} DISCORD_CLIENT_ID: ${DISCORD_CLIENT_ID} GUILD_IDS: ${GUILD_IDS} CONTAINER_NAME: ${CONTAINER_NAME} SERVER_NAME: ${SERVER_NAME} JOIN_CODE_LOOP_COUNT: ${JOIN_CODE_LOOP_COUNT:-20} JOIN_CODE_LOOP_TIMEOUT_MILLIS: ${JOIN_CODE_LOOP_TIMEOUT_MILLIS:-5000} volumes: - /var/run/docker.sock:/var/run/docker.sock restart: unless-stopped
Run:
docker compose -f deployment/docker-compose.yaml up -d
Important Notes:
- The bot needs access to the Docker socket to manage containers
- Ensure
/var/run/docker.sockis mounted - The bot container must be on the same Docker host as the Valheim server container
- This works with Portainer-managed stacks as well
-
Using with Portainer
You can deploy this stack through Portainer:
- In Portainer, go to Stacks → Add stack
- Name your stack (e.g.,
valheim-discord-bot) - Paste the docker-compose content from
deployment/docker-compose.yaml - Set the environment variables in Portainer's environment variables section
- Deploy the stack
The bot will manage your Valheim server container directly through the Docker API, no Portainer API configuration needed.