Please note that is my first repo that I am pushing out and I know it is not perfect (as nothing is in life). I had the help of AI tools to help me understand and learn more as I go and also read documents such as RCON docs, Minecraft Docs, and more. I used Python to create the commands so the code definitely needs more work done to it to make it better as I feel I could have this more cleaner. Please be sure to report any issues or anything that you know or think can help me create this better as I am still learning and growing my developer skills. REPO STILL IN TESTING STAGE USE AT YOUR OWN DISCRETION
Currently when you use the command '/start' this will start the server but if you stop the bot, the Minecraft world itself will stop as well. This is something I need to work on with the bot.py code.
- Work on bot.py code to make it cleaner
- Create a custom dashboard on Grafana for the MC Node Exporter
This repository contains a Minecraft server setup that is managed using a Discord bot written in Python. The bot allows server management through a variety of commands, and the server's metrics and logs are visualized using Grafana.
The Discord bot currently supports the following commands for managing the Minecraft server:
-
/start
Description: Starts the Minecraft server. -
/info
Description: Retrieves server information, including the server's address. -
/stop
Description: Stops the Minecraft server, but only if no players are currently online. -
/say
Description: Sends a message in the Minecraft server chat. -
/ipcheck
Description: Checks and updates the server address if the one provided in the /info command doesn't work. -
/cmd
Description: Executes a command directly on the Minecraft server.
- Minecraft server: A running Minecraft server to connect the bot to or you can use the 'server.jar' in the 'newmcworld' directory to create a new world.
- Discord Bot: A Discord bot token to manage the server, a Discord server to add the bot to and a channel to type the commands to.
- Java OpenJDK 21: Currently we are using Java 21 to run the Minecraft server for now.
-
Clone this repository:
git clone https://github.com/neftyIT/mcbot.git
-
Install necessary dependencies:
cd mcbot pip install -r requirements.txt -
Configure the bot:
- Add your Discord bot token and Minecraft server details in the
bot.envfile.
- Add your Discord bot token and Minecraft server details in the
-
Start the bot:
python3 bot.py
-
Set up Grafana to visualize metrics of your Minecraft Server (Prometheus, Minecraft Exporter, and any other Grafana tool).
-
Set up Prometheus and add it as a data source to Grafana
-
Set up the Minecraft Exporter and add it to your Prometheus.yml config (For assistance on how to set this up, go here https://github.com/dirien/minecraft-prometheus-exporter?tab=readme-ov-file#getting-started)
To monitor the Minecraft server, we utilize the following tools:
- Prometheus: Collects metrics for visualization on Grafana.
- Dirien's Minecraft Exporter: Exports Minecraft server metrics to Prometheus for visualization.
Follow these steps to set up Grafana for monitoring your Minecraft server:
We’ve created a script for easy installation and setup of Grafana. The script will install Grafana, configure it, and set up the necessary services. You can find the script in the /scripts directory.
To use the script:
-
Navigate to the
scriptsdirectory -
Run the script ./grafana_install.sh
-
This script should install Grafana and it's configuration needed. You should be able to find the 'grafana.ini' file in the '/etc/grafana' directory.
-
Grafana runs on port 3000 so ensure your firewall allows the port 3000.
We’ve created a script for easy installation and setup of Prometheus and Prometheus Node Exporter. The script will install Prometheus **and adds the custom configuration needed for Dirien's MC Exporter
-
Navigate to the
scriptsdirectory -
Run the script with sudo ./prometheus_install.sh. This allows for the MC Exporter configuration to be added to the prometheus.yml
-
This script should install Prometheus and it's configuration needed. You should be able to find the 'prometheus.yml' file in the '/etc/prometheus' directory.
-
Prometheus runs on port 9090 so ensure your firewall allows the port 9090.
-
The Prometheus Node Exporter is included in the script. Be sure to run sudo systemctl status prometheus-node-exporter to verify that it was installed and is running.
-
You can go to http://localhost:9090/ which should direct you to the Prometheus web interface.
The Minecraft Node Exporter being used in this repo was created by Dirien on Github. Please see below for the link to his repo for the MC Node Exporter to take a further look at his repo. [https://github.com/dirien/minecraft-prometheus-exporter]
-
I've included the MC Exporter and the Systemd Unit File in the config/minecraft_exporter directory so you will need to do the following: cp minecraft-exporter /usr/local/bin cp minecraft-exporter.service /etc/systemd/system
-
After setting up the MC Exporter, be sure to add the port 9150 to your firewall rules.
-
Access https://localhost:9150/metrics to ensure you can see metrics from the MC Exporter.
-
Create a Discord Developer account and create an application.
-
Copy your Token key from the Bot tab and then head to OAuth2
-
In the OAuth2 page, scroll down OAuth2 URL Generator and click the following:
Scope: Bot \
Bot Permissions: Manage Messages and Use Slash Commands -
Once this is completed, copy the URL and invite your bot to your Discord Server
-
Create a channel on Discord and copy the channel ID.
-
Go to the bot.env and fill out the variables:
e.g
bot-token=thisis4examplew1lln0tw0rk
server-address=192.168.230.67:25565
start-script=home/user/mcbot/rconstart.sh
server-ip=192.168.230.67
server-op=Himothy
chat-channel-id=1328407534503067688
#enable-rcon & rcon.port should stay the same unless you like customizing
enable-rcon=true
rcon.password=wh@t1s@p@ssw0rd
rcon.port=25575 -
After entering you environmental variables, start the bot using 'python3 (or just python) bot.py'.
-
You should see the bot online in discord and you should be able to use commmands.
-
Ensure that you have the setting 'enable-rcon=true' in the server.properties of your Minecraft world as the default is set to false
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.