Discord bot for our Minecraft server, Part 2
2024, March 9
Creating a Discord bot for our Minecraft server.
Part 2: Posting minecraft logs into Discord channel automatically
This is a continuation of the previous blog post:
Last post ended on this note:
Right now your friends can start the server and join, but we don't know what happens in the game if we are not online in Minecraft. Let's send logs from our Minecraft server to a Discord channel so that everyone can see when people join the game, leave it, or get killed by a Creeper.
So in this article we will do that, and we will also add an additional message that will let us know if the server shut down.
For this article I expect that you already completed the # NixOS Minecraft Server article as we will be using what we learned there. We will be using the same github repo for this article, but a different branch. So make sure you cloned the repo and switch to the discord-channel-logging
branch:
Create a Discord Webhook URL
You can follow the steps here.
- Choose a channel on your Discord server
- Click settings > Integrations > Create Webhook
- Get the URL from "Copy Webhook URL
Deploy the new app
- Just add the new env var to
.envrc
, it will be the hook you just copied inDISCORD_WEBHOOK_URL
. - After you add the new env var, run
direnv allow
in your project directory. - Start by turning on your instance, as it is probably turned off if you followed the first blog post. Use
make turn-on
for convenience. - Push the image, the steps are the same as we did on the first blog post. Just use
make mc-push
for convenience. - That's it! Now you should see logs like these in your Discord channel:
Starting minecraft server version 1.20.4
Done (20.132s)!
User123 logged in at (-370.57222477896454, 125.0, 58.30000001192093)
User123 joined the game
<User123> hi
User123 lost connection: Disconnected
User123 left the game
Shutting down server.
What's new in the codebase
You can see the changes here:
- The diff between the
main
anddiscord-channel-logging
branches.
We added a new Rust app here mc-server/journal-bot/cargo/Cargo.toml
.
- It parses the logs from
journactl
command. - It sends the logs to the Discord channel using serenity crate.
- It removes the player's IP Address from the logs that get sent as a message for a little privacy.
- It removes some very noisy logs that Minecraft creates on startup, we don't need all this info.
- When we configure it as a systemd service, we give it access to the
journalctl
binary, and pass it theDISCORD_WEBHOOK_URL
env var.
We added a new build script to record the env var so that nix build
can find it.
- We added
DISCORD_WEBHOOK_URL
to thedocker run
command as an env var. - We added
make-env.sh
as the first step ofpush.sh
make-env.sh
creates a file in/env-vars/DISCORD_WEBHOOK_URL
with the URL.- With a Nix function to make sure we did not make a mistake and the contents are not empty, we load the contents of the file inside
journal-bot/configuration.nix
- Finally, it is passed as an env var to the systemd service.
We gave the mc-watcher
service the ability to post messages to the channel too, so that it can notify us when the server is shutting down.
- As usual, passed the env var to the service.
- It sends the message before running the
shutdown
command.
Congrats! You made it all the way here. Now go have fun with the game :)
Go back to the top