Creating a Discord bot using discord.js to restart a Docker container on the host system involves several steps. Below is a complete guide to set this up, including the necessary code.
Setup Node.js and discord.js:
Ensure you have Node.js installed. Then, create a new project and install the necessary dependencies.
mkdir discord-docker-bot; cd discord-docker-bot
npm init -y
npm install discord.js@14 @discordjs/rest discord-api-types
npm install dotenv js-yaml
Create a .env file:
echo "DISCORD_TOKEN='YOUR_BOT_TOKEN'" > .env
This file will store your bot's token securely.
change these values according to your needs:
- const dockerComposeFile = '/home/$user/docker-minecraft/docker-compose.yml';
- const dockerContainerName = 'mcserver';
- const YourGuildID = 'YOUR_GUILD_ID';
Execute your bot script using Node.js:
node bot.js
You need to grab your actual bot key on https://discord.com/developers/applications/ then add in the .env file as shown bellow replace:
with your Discord server ID/bot token
to create commands, read messages, and add reactions.
on the host system where this script runs. Adjust the variables located in bot.js (or requires additional flags.)
TODO
- Wrap parts of code and try to handle potential errors.
- even Better error handling
- Implement cooldown for the main restart function to prevent spamming and avoid corruption.
- Implement cooldown for other commands to maintain the bot's performance and prevent user spamming.
- Set a timeout for your bot's polls, ending them if no reaction is received within that time.
- Set a countdown before execution/poll closes
- Limit reactions per user to 1 for more accurate poll results.
- Easily configure the number of reactions required
- Implement logging for all command usages to aid debugging.
- Create a /help command to provide brief descriptions of all available commands and improve user understanding.
- Before executing critical commands, check if the user has the necessary permissions. If not, return an error message.
- Implement rate limiting on command usage to prevent abuse and overloading your bot.
- Utilize Discord Embeds to make bot's responses more visually appealing.