🏠 Home

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 in DISCORD_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 and discord-channel-logging branches.

We added a new Rust app here mc-server/journal-bot/cargo/Cargo.toml.

We added a new build script to record the env var so that nix build can find it.

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.


Congrats! You made it all the way here. Now go have fun with the game :)


Subscribe to my mailing list

Only to update you when I submit a new blog post.


Go back to the top
© Copyright 2023 by Jose.