I created this bot to have a simple and easy way to get informations from a minecraft server and interact with it.
-
Get dynamic informations about the server
- Server Status
- CPU Usage
- Memory Usage
- Player Count
- Online Players Usernames
-
Interact with the service that runs the server
- Restart
- Shutdown
- Start
-
Auto-restart the server if it crashes
-
Logs messages in a separate Discord Channel
- Player Play time
- Send command via Discord
- Player Count as status
- If there is a features that you would like to see, tell me about it !
- Linux based Host
- Python 3.8+
- A Discord Bot
- Discord.py
- A Discord server !
This install is based on a FTB server with all the server files in the home directory of the user.
foo@bar:~$ sudo apt-get update
foo@bar:~$ sudo apt-get install screen
Login as the user that runs the minecraft server and execute those commands :
foo@bar:~$ cd ~/
foo@bar:~$ wget https://github.com/Padi1111/discord-minecraft-server-bot/archive/refs/heads/main.zip
foo@bar:~$ unzip main.zip
foo@bar:~$ rm main.zip
foo@bar:~$ mv discord-minecraft-server-bot-main discord-minecraft-server-bot
Move the start script to the root of the minecraft server folder. In my case, this is the root home of the user.
foo@bar:~$ mv ~/discord-minecraft-server-bot/start.sh ~/.
You can then edit it to make it work with your minecraft server
foo@bar:~$ nano ~/start.sh
You can now edit the bot to your desire and configure it for your discord server.
To do so, edit the file called user_settings.py
in the discord-minecraft-server-bot
folder.
foo@bar:~$ nano ~/discord-minecraft-server-bot/user_settings.py
To be able to run all the files in the Commands directory as sudo without giving any password we will be addind the scipts to our /etc/sudoers file. To do so, open it, as sudo, with your any editor :
foo@bar:~$ sudo nano /etc/sudoers
Go to this section :
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
And bellow it add the following command, making sure to change the [USERNAME] by the username of the user running the bot :
[USERNAME] ALL=(ALL) NOPASSWD: /home/[USERNAME]/discord-minecraft-server-bot/Commands/service_stop.sh
[USERNAME] ALL=(ALL) NOPASSWD: /home/[USERNAME]/discord-minecraft-server-bot/Commands/service_start.sh
[USERNAME] ALL=(ALL) NOPASSWD: /home/[USERNAME]/discord-minecraft-server-bot/Commands/service_restart.sh
You can now save and close this file.
We will now be creating the service for the Minecraft Server.
foo@bar:~$ sudo nano /etc/systemd/system/minecraft-server.service
Copy paste this, making sure to change the [USERNAME] by the username of the user running the bot :
[Unit]
Description=Minecraft Server
After=network.target
[Service]
Type=simple
RemainAfterExit=true
WorkingDirectory=/home/[USERNAME]
User=[USERNAME]
Restart=on-failure
ExecStart=/usr/bin/screen -dmS mc-server bash start.sh
ExecStop=bash discord-minecraft-server-bot/stop.sh
[Install]
WantedBy=multi-user.target
Save and close this file.
Enable the serivce and start it :
foo@bar:~$ sudo systemctl enable minecraft-server.service
foo@bar:~$ sudo systemctl start minecraft-server.service
If everything whent well, your minecraaft server should now start. To check, login as the user that runs the server and execute this command :
foo@bar:~$ screen -ls
You should see something like this :
There is a screen on:
2590603.mc-server (15. 04. 23 16:00:18) (Detached)
1 Socket in /run/screen/S-mcserver.
We will now be creating the service for the discord bot.
foo@bar:~$ sudo nano /etc/systemd/system/discord-minecraft-bot.service
Copy paste this, making sure to change the [USERNAME] by the username of the user running the bot :
[Unit]
Description=Discord Minecraft Bot
After=network.target
[Service]
Type=simple
RemainAfterExit=true
WorkingDirectory=/home/[USERNAME]/discord-minecraft-server-bot/
User=[USERNAME]
Restart=always
RestartSec=10
ExecStart=/usr/bin/python discord_bot.py
[Install]
WantedBy=multi-user.target
Save and close this file.
Enable the serivce and start it :
foo@bar:~$ sudo systemctl enable discord-minecraft-bot.service
foo@bar:~$ sudo systemctl start discord-minecraft-bot.service
If everything whent well, the bot should be online in your discord server and you should see the embedded message with all the informations about the minecraft server.