Designing a Discord AutoMod: rules, strikes and escalation
How Krypton's AutoMod handles a message: who is skipped, strongest rule wins, one case per member per rule, strikes counted in PostgreSQL, escalation that never lowers, and failing safe when the database is down.
Krypton's AutoMod used to check three things: flood, links and invites, with one server-wide punishment and a fixed 15-minute mute. This is the design that replaced it.
Where it started
- Enforcement ran at the end of the AFK cog's message handler, after unrelated replies.
- Messages without text, such as attachments or stickers, were never checked.
- GIF picker links counted as links, so every GIF was a violation.
- Missing permissions failed silently, and nothing recorded why someone was punished.
How a message is handled
- Skip bots, webhooks, system messages, the owner and backup owner, administrators and exempt roles.
- Check each enabled rule that applies, minus per-rule exempt roles and channels. Threads and categories count as their parent channel.
- Exempt staff listed in the bot's own admin system. This database read happens only after a message has been flagged, so clean messages cost nothing.
- Strongest hit wins. Other matches are listed in the log.
- One case per member and rule per 30 seconds. The rest of a burst is just deleted.
- Act: delete (plus earlier messages of the burst), warn with a self-deleting notice that names the rule but never the matched word, time out, kick or ban.
- Log: rule, reason, action, strike count, any problems, the content escaped with no pings, and a case number.
Strikes and escalation
Strikes are counted in the same database query that records the case, so they survive restarts and are shared across every cluster. Escalation steps raise the action ("at 3 strikes: timeout 1 hour") and never lower it. Alert-only rules never escalate, and a case marked as a mistake lifts its timeout and stops counting.
Two bots, one server
Krypton and its premium edition can run in the same server and read the same settings, which once meant double deletions and double punishments. Each case is now claimed by message id in the events table: only one bot acts, and when they disagree, the stricter decision wins.
Failing safe
- Settings are cached per server for 60 seconds, and commands or the dashboard invalidate the cache immediately through PostgreSQL notifications.
- If the database is down, the last known settings keep protecting the server for up to an hour. Without them, AutoMod pauses rather than guessing.
- If a case cannot be saved, the action still happens and the log says the case was not saved.
- Admin-written regex is checked for nested repeats and backreferences and runs on at most 4,000 characters, to avoid catastrophic backtracking.
Honest limits
There are no machine-learning classifiers; image and hate-speech models need a paid model or API. Phishing detection is heuristic: link signals, commonly phished brands, and cross-server reports, where a domain counts everywhere only after three servers report it. Writing limits down is part of shipping a moderation tool people can trust. The same pattern of claims in PostgreSQL shows up in PostgreSQL patterns for multi-process bots.
