- C# 100%
| McLogBot | ||
| McLogBot.Tests | ||
| .gitignore | ||
| LICENSE | ||
| McLogBot.sln | ||
| README.md | ||
McLogBot
Publish Minecraft server events to a Discord webhook
Installation
Requirements
This program has been designed to run on GNU/Linux, and has been tested on Ubuntu 24.04. To build and run you'll need to install the .NET SDK
I've been running it on the log file from a Minecraft 1.21.10 java edition server. The server is also running as my user, so I don't have any permissions issues reading the logs.
$ dotnet --version
10.0.100
To build
$ dotnet publish -c Release
The compiled program can be found at McLogBot/bin/Release/net10.0/publish/
This program expects the path to the log file, and the Discord webhook as environment variables.
My Minecraft server log location is /home/rob/minecraft/java/logs/latest.log, so to run McLogBot I can use
# navigate to the publish directory
$ cd McLogBot/bin/Release/net10.0/publish
# set the necessary environment variables
$ export LOG_FILEPATH=/home/rob/minecraft/java/logs/latest.log
$ export DISCORD_WEBHOOK=https://discordapp.com/api/webhooks/123...
# run the program in the current terminal session
$ ./McLogBot
The program will show error messages if the environment variables are missing, if it can't find the Minecraft logfile, or if it can't send messages to Discord.
Running the program as a service
You can either run McLogBot from the publish directory, or you can copy it somewhere else
# create new McLogBot directory in your home directory
$ mkdir ~/McLogBot
# copy across the published files
$ cp McLogBot/bin/Release/net10.0/publish/* ~/McLogBot
Create a new systemd service file
$ sudo nano /usr/lib/systemd/system/mclogbot.service
Paste in and edit the following
[Unit]
Description=Minecraft Discord Logbot
After=network.target
[Service]
Type=simple
WorkingDirectory=/home/rob/McLogBot
ExecStart=/home/rob/McLogBot/McLogBot
Restart=always
RestartSec=10
KillSignal=SIGINT
User=rob
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
Environment=DISCORD_WEBHOOK=https://discordapp.com/api/webhooks/123...
Environment=LOG_FILEPATH=/home/rob/minecraft/java/logs/latest.log
[Install]
WantedBy=multi-user.target
Change the WorkingDirectory to the location your published files are, ExecStart to the path of the published executable, User to your username (mine is rob).
Replace the Discord webhook on the line starting Environment=DISCORD_WEBHOOK= with your webhook, and the path to your Minecraft server log file on the line starting Environment=LOG_FILEPATH=.
Save the file using Ctrl+o, then enable and start the service
$ sudo systemctl enable mclogbot.service
$ sudo systemctl start mclogbot.service
You can check the logs of the service with
$ sudo journalctl -u mclogbot.service
Or follow the live logs with
$ sudo journalctl -u mclogbot.service -f
To stop the service
$ sudo systemctl stop mclogbot.service
And to disable it (so it doesn't start at boot)
$ sudo systemctl disable mclogbot.service
Development
# to build
$ dotnet build
# to run the (limited) unit tests
$ dotnet test
How it works
On startup McLogBot is initialised by reading the log file in the LogClient, paying attention to only INFO log lines. The state of the log file at initialisation is stored using a HashSet.
After initialisation every DELAY_SECONDS seconds (defaulting to 10 seconds), the log file is read again, and any new lines are passed to the LogProcessor. The LogProcessor scans each line using regular expressions to search for players joining or leaving the server, and getting advancements.
Any processed log lines are then passed to the DiscordClient, which sends the messages one by one to the webhook.
It should be simple to add new log line types to the LogLineType enum, and to add new regex patterns to the LogProcessor to extend the functionality.