This application leverages Discord's Interaction API to manage Docker services via Discord commands. Built with Node.js, it offers a convenient way to dynamically manage Docker services using Discord's slash commands with autocomplete functionality.
- 🚀 Slash Commands with Autocomplete: Easily execute predefined commands within Discord, with autocomplete making interactions smooth and user-friendly.
- 🔄 Dynamic Service Management: Update your Docker services on-the-fly by changing the configuration file—no need to restart the container.
- 🔒 Role and Guild Restricted Access: Ensure only authorized roles within specific Discord guilds can use the commands, enhancing security and control.
To quickly start the application, follow these steps:
-
Create the Project Folder:
- Create a folder named
discord-interactions-webhook
to hold the.env
file, thedocker-compose.yaml
file, and thedata
folder.
- Create a folder named
-
Create and Configure
.env
File:-
Create an application in the Discord Developer Portal.
-
In the General Information tab of your application, copy the
Application ID
andPublic Key
. -
In the Bot tab of your application, generate a new token by clicking "Reset Token" and copy the
Bot Token
. -
Create a
.env
file in yourdiscord-interactions-webhook
folder with the following content:APPLICATION_ID=your_discord_application_id DISCORD_PUBLIC_KEY=your_discord_public_key DISCORD_TOKEN=your_discord_bot_token PORT=5000 ALLOWED_GUILD_IDS=allowed_guild_id ALLOWED_ROLE_IDS=allowed_role_id # Cron job configuration SHUTDOWN_ENABLED=false SHUTDOWN_TIME="0 3 * * *" # Timezone TZ=America/New_York
- Note: You can grab Guild IDs and Role IDs by enabling developer mode in Discord settings and right-clicking on the role and server to copy their IDs. These are required for the bot to function properly and are highly recommended for security since the container has access to the Docker socket.
-
-
Create Data Directory:
- Create a
data
directory in yourdiscord-interactions-webhook
folder to persist data.
- Create a
-
Manage Discord Commands:
-
Pull Commands: Populate
data/commands.json
with current commands from Discord by running:docker run --env-file .env -v ./data:/app/src/data --rm chrisae9/discord-webhook-service:latest /bin/sh -c "npm run pull"
-
Update Commands: Modify
data/commands.json
to include the following structure:[ { "id": "************2392", "application_id": "************2232", "version": "************3632", "default_member_permissions": null, "type": 1, "name": "service", "description": "Manage services", "dm_permission": true, "contexts": null, "integration_types": [0], "options": [ { "type": 3, "name": "name", "description": "Select a service", "autocomplete": true }, { "type": 3, "name": "action", "description": "Choose an action", "choices": [ { "name": "start", "value": "start" }, { "name": "stop", "value": "stop" } ] } ], "nsfw": false } ]
- Note: Ensure that the
id
,application_id
, andversion
fields match the data pulled from your specific application.
- Note: Ensure that the
-
Push Commands: Apply the updated commands by running:
docker run --env-file .env -v ./data:/app/src/data --rm chrisae9/discord-webhook-service:latest /bin/sh -c "npm run push"
-
Refer to the
service.json
in the repository's data folder to configure services:{ "name": "Minecraft", "value": "minecraft", "path": "/path/to/minecraft", "composeFile": "docker-compose.yaml", "launchOptions": "" }
- Note: The
path
should point to a directory containing another Docker Compose file. - Important: The
path
must be relative to the container, not the host. Ensure that the volume mounts in your Docker Compose or Docker Run commands align with these paths.
- Note: The
-
To build and run the application using Docker Compose, use the following steps:
-
Create a
docker-compose.yaml
File:-
Create a
docker-compose.yaml
file in yourdiscord-interactions-webhook
folder with the following content:services: discord-webhook-service: image: chrisae9/discord-webhook-service:latest container_name: discord-webhook-service restart: unless-stopped ports: - "5000:5000" env_file: - .env volumes: - ./data:/app/src/data - /var/run/docker.sock:/var/run/docker.sock - ./services:/services
-
-
Start the Application:
-
Use the following command to start the application:
docker-compose up -d
-
About the
./services
Directory:- The
./services
directory will be mounted to/services
in the container. This directory is used to specify paths or directories to services' Docker Compose files in theservice.json
configuration. You can add your own mounts to this directory. - It is recommended to copy the structure from the repository's
services/example/docker-compose.yaml
to first test your services. Here is an example of how yourservices.json
might look:{ "name": "Example Service", "value": "example-service", "path": "/services/example", "composeFile": "docker-compose.yaml", "launchOptions": "" }
- The
-
If you prefer not to use Docker Compose, you can run the application directly using docker run
with the following command:
- Run the Docker Container:
- Use the following command to start the application:
docker run --env-file .env -v ./services:/services -v ./data:/app/src/data -v /var/run/docker.sock:/var/run/docker.sock -p 5000:5000 --name discord-webhook-service chrisae9/discord-webhook-service:latest
- Use the following command to start the application:
The application includes a cron job feature to automatically shut down services at a specified time. This can be enabled and configured via the .env
file.
-
Enable Cron Job:
- Set
SHUTDOWN_ENABLED
totrue
in your.env
file.SHUTDOWN_ENABLED=true
- Set
-
Set Shutdown Time:
- Configure the
SHUTDOWN_TIME
variable in the.env
file to specify when the services should be shut down. The time format is a standard cron format.SHUTDOWN_TIME="0 3 * * *" # Default: every day at 3 AM
- Configure the
-
Set Timezone:
- Configure the
TZ
variable in the.env
file to set the timezone for the cron job.TZ=America/New_York # Default: UTC
- Configure the
The cron job script is located at /usr/local/bin/shutdown-services.sh
inside the container. It reads the services.json
file and runs docker-compose down
for each configured service.
- The cron job feature is only activated if
SHUTDOWN_ENABLED
is set totrue
in the.env
file. - Ensure the
TZ
environment variable is correctly set to match your desired timezone.
-
After running the container, configure an interaction endpoint URL in the Discord Developer Portal to point to your IP/domain, e.g.,
https://yourdomain.com/interactions
. -
This assumes that you have a reverse proxy set up to route Discord requests to the application.
This project is under the MIT License.
Contributions are welcome. Please adhere to the project's coding style and include tests for new features or bug fixes. For detailed setup instructions, see the CONTRIBUTING document.