A Discord bot to start a Minecraft server hosted via Crafty Controller. It also uses Home Assistant to control a smart plug to start the server from shutdown. And it automatically stops the Minecraft server if there are 0 players for too long.
- Clone this repo
- Go into the "mc-server-bot" folder
- Make a file called
.envwith the following contents and fill out all fields:
DISCORD_BOT_TOKEN=
DISCORD_BOT_CHANNEL_ID=
CRAFTY_HOST=
CRAFTY_TOKEN=
CRAFTY_SERVER_ID=
HOMEASSISTANT_URL=
HOMEASSISTANT_TOKEN=
HOMEASSISTANT_SWITCH_ID=
SSH_HOST=
SSH_USER=
SSH_PASSWORD=
Example (no these are not real tokens and id's, only for formatting reference):
DISCORD_BOT_TOKEN=IUG&T&G(*&FWE6.(G(AF24525GW(*GF(*WFG616
DISCORD_BOT_CHANNEL_ID=1242629651295
CRAFTY_HOST=https://192.168.0.60:8443
CRAFTY_TOKEN=iug*(&fg87ugf9fg9t9&F79f98F(&*F(8f79.g8&f87f87Fd(56s965s784doyf64s8i.*^%c976c^f76c976c
CRAFTY_SERVER_ID=rg34e4ty4wg4hw3-4he4y4-4h4ewhe-4hehe4eh44eh
HOMEASSISTANT_URL=http://192.168.0.59:8123/api
HOMEASSISTANT_TOKEN=OiuygO87G087(G)8&G)87g)8&g08g07G*&gF7tdOIDF&(Ôf.cd8&6dt867%D(*5d&^d9^&dd679
HOMEASSISTANT_SWITCH_ID=switch.mcserver
SSH_HOST=192.168.0.60
SSH_USER=mc-server-bot
SSH_PASSWORD=VerySecurePassword123
- Make a file called
responses.jsonwith the following contents, and of course you can change, add or remove responses:
{
"shutdown_busy": [
"This is the bot's response when /start-server is called but the server is shutting down.",
"You need to add at least one response.",
"But you can add some more to get random responses."
],
"server_already_online": [
"This is the bot's response when /start-server is called but the server is already running.",
"You need to add at least one response.",
"But you can add some more to get random responses."
],
"server_starting": [
"This is the bot's response when /start-server is called but the server is already starting.",
"You need to add at least one response.",
"But you can add some more to get random responses."
]
}
- Then create a python virtual environment by running
python -m venv ./ - Finally install all the required modules by running
./bin/pip install MODULEreplacingMODULEwith the module names listed below
- Create a file called
mc-server-bot.servicein/etc/systemd/systemwith the following contents and replaceUSERNAMEwith your username:
[Unit]
Description=The discord bot for the crafty server
After=network.target
[Service]
WorkingDirectory=/home/USERNAME/mc-server-bot
ExecStart=/home/USERNAME/mc-server-bot/bin/python /home/admin/mc-server-bot/main.py
Restart=always
User=USERNAME
[Install]
WantedBy=multi-user.target
- Then run
sudo systmemctl enable mc-server-bot.serviceto enable the autostart - And run
sudo systmemctl start mc-server-bot.serviceto start the bot now
- py-cord
- python-dotenv
- homeassistant_api
- paramiko
- cysystemd
- crafty_client