Skip to content

duisterethomas/mc-server-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 

Repository files navigation

mc-server-bot

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.

Setup

Setting up the bot

  1. Clone this repo
  2. Go into the "mc-server-bot" folder
  3. Make a file called .env with 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
  1. Make a file called responses.json with 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."
    ]
}
  1. Then create a python virtual environment by running python -m venv ./
  2. Finally install all the required modules by running ./bin/pip install MODULE replacing MODULE with the module names listed below

systemd service for bot autostart

  1. Create a file called mc-server-bot.service in /etc/systemd/system with the following contents and replace USERNAME with 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
  1. Then run sudo systmemctl enable mc-server-bot.service to enable the autostart
  2. And run sudo systmemctl start mc-server-bot.service to start the bot now

Python PyPi modules

  • py-cord
  • python-dotenv
  • homeassistant_api
  • paramiko
  • cysystemd
  • crafty_client

About

A Discord bot to start a minecraft server.

Topics

Resources

Stars

Watchers

Forks

Languages