- Rust 87%
- Nix 13%
factorio-discord-bot #
A two-way bridge between a Factorio dedicated server and a Discord channel —
chat, joins/leaves, deaths, rockets and completed research each way, plus
relaying Discord messages into the game. No companion Lua mod required: the
server side is read straight from journalctl and driven over RCON.
How it works #
The bot is a single binary that runs eight tasks at once, wired together by bounded channels so a slow side (Discord's API, a laggy game) can never stall the other:
- journal tails the server's console output (
journalctl -u <unit> -f) and hands every line to... - broker (
src/broker.rs— read this file first, everything else is plumbing), which recognises chat/join/leave lines, decides what each one means, and routes it: to Discord, into the game, to a debug channel, or nowhere (e.g.[WARNING]lines the bot's own RCON commands trigger). - discord holds a Discord gateway connection open: it posts what the broker routes to it, and hands incoming channel messages to...
- rcon, which escapes and prints them into the game via
/silent-command. - metrics polls a mod's Prometheus textfile output (see Metrics dependency below) for deaths and player count, since neither reaches the console log at all — the player count also becomes the bot's Discord "Playing ..." status.
- research polls RCON for the researched-technology count, and once it moves, pages through the full set to name what just finished — a Prometheus counter can say how many researches finished, never which.
- death_location answers where, once metrics has detected a death,
by asking RCON to scan for the matching
character-corpseentity.
No task returns while healthy. If one does — even cleanly — the whole process
exits and lets your process supervisor (systemd's Restart=always, e.g.)
restart it, rather than continuing to run with one pump silently dead.
Requirements #
- A Factorio dedicated server whose console output you can read (
journalctlaccess, or adaptsrc/journal.rsto another log source) and whose RCON port you can reach. - A Discord application with a bot user:
- Create an application at the Discord Developer Portal, add a bot.
- Under Bot, enable the Message Content intent — it's privileged, and without it the gateway connects fine but every message arrives with an empty body. This is the single most common setup mistake.
- Invite it with scopes
bot+applications.commandsand permissions68608(View Channels, Send Messages, Read Message History — nothing more is used). - With Developer Mode on, right-click the channel you want and Copy Channel ID.
Configuration #
Everything is read from the environment (src/config.rs), validated up front
— a missing required variable fails immediately and plainly, rather than one
task dying a few seconds after the others started.
| Variable | Required | Default | Meaning |
|---|---|---|---|
DISCORD_BOT_TOKEN |
yes | — | The bot's token. |
DISCORD_CHANNEL_ID |
yes | — | Channel for game events and Discord→game relaying. |
DEBUG_CHANNEL_ID |
no | — | Unrecognised console lines and relay failures go here instead of just stderr — worth setting, since it's how a new Factorio log format announces itself. |
FACTORIO_UNIT |
no | factorio.service |
systemd unit whose journal carries the console output. |
FACTORIO_RCON_ADDR |
no | 127.0.0.1:27015 |
RCON host:port. |
FACTORIO_RCON_PASSWORD |
yes | — | RCON password. |
FACTORIO_METRICS |
no | /run/factorio-script-output/statfactory/game.prom |
Path to the mod's Prometheus textfile output (deaths, player count). |
A channel ID of 0 (the common "unset" placeholder) is rejected explicitly
rather than silently accepted or left to panic.
Metrics dependency (deaths/player count) #
Deaths and the live player count come from a Prometheus textfile a game mod writes, not from the console log — a real death produces zero death-related text anywhere in the server's journal. The bot reads two series from it:
factorio_players_connected 3
factorio_player_deaths{force="player",name="danzibob"} 1
This was built against
statfactory, which emits exactly
this shape. Without a mod producing it, FACTORIO_METRICS simply never
exists and these two features see no events — everything else (chat,
joins/leaves, research, Discord→game relaying) works regardless.
Running it #
With Nix (builds via naersk,
pinned in npins/):
just build # nix-build, runs the test suite as part of the build
just shell # dev shell: cargo, rustc, rust-analyzer, rustfmt, clippy
just test # cargo test against the working tree
just run # cargo run, reading the env vars above from your shell
Or plain Cargo (cargo build --release) — the only dependencies are
serenity (Discord) and tokio; RCON's wire format is hand-rolled (src/rcon.rs,
unit-tested) rather than pulled in as a crate.
On NixOS, the intended deployment is via the homelab.factorio.discordBot
module in ftvkyo/homelab,
which wires this binary up as a systemd service alongside a
homelab.factorio server — reading its journal via SupplementaryGroups = [ "systemd-journal" ], no BindReadOnlyPaths needed for that part — and
derives FACTORIO_UNIT/FACTORIO_RCON_ADDR/FACTORIO_RCON_PASSWORD from the
same options the server itself uses, so the two can't drift apart.
Development notes #
- RCON is hand-rolled, Discord is not. The Source RCON protocol is four
fields and ~40 lines;
serenityearns its keep (and bringstokio+ ~185 transitive crates) because the gateway side needs heartbeats, identify and resume. rcon::escape_luais a trust boundary, not cosmetics — RCON executes as the server, so an unescaped Discord message containing")could run arbitrary Lua.- Two known limitations, commented in the source rather than hidden:
rcon::recvreads exactly one packet, so replies over the protocol's 4096-byte cap are truncated (research.rspages its queries specifically to stay under this); andjournal::tailuses async line reading rather than a blocking loop behindspawn_blocking, so it can actually be cancelled. just test/cargo testcover the packet framing, the broker's line classification (timestamp-vs-uptime discriminator, rich-text rendering,[WARNING]suppression), the metrics diffing, and the death-location RCON reply parsing — no live Factorio server or Discord connection needed for any of it.