Skip to content

Recovering a Discord server when the owner account is lost

Designing Krypton's backup owner codes: a one-time recovery code that hands bot-level ownership to a trusted person, stored as a bound hash, with lockouts and automatic cleanup on transfer.

4 min read

Discord servers have exactly one owner. When that account is hacked, banned or simply lost, every setting in a security bot that is limited to the owner is frozen with it. For a bot like Krypton, where anti-nuke whitelists and punishments are owner decisions, that is a real recovery problem. Backup owner codes are the answer I designed for it.

The problem

The bot cannot change who owns a server; only Discord can. What it can change is who it treats as the owner for its own commands. The goal: let the real owner, in advance, nominate a way to hand that bot-level ownership to someone they trust, without that becoming a new way to take over a server.

How it works

  • The real owner runs generate. The code is shown once: privately for the slash command, by DM for the prefix command. If the DM fails, no code stays active.
  • Anyone in the server can run redeem with the code. It is slash-only, so the code never appears in chat. The bot checks lockouts, verifies the code, asks for confirmation, and claims it. The owner and the anti-nuke log channel are notified.
  • The redeemer becomes the Backup Owner, and Krypton treats them as owner for commands, permission checks and anti-nuke trust, but only while the owner who issued the code still owns the server.
  • The owner can revoke at any time, and status shows the fingerprint, dates, current Backup Owner, failed attempts and recent events, never the code.

Generating and storing codes

A code has a recognisable prefix, the guild, the issue time and a 256-bit random secret:

text
KBC1.<base64 guild id>.<base64 issue time>.<43-char secret>

The database never stores the code itself, only a hash bound to the bot and the guild:

python
digest = sha256(f"krypton-backup-code:{bot_id}:{guild_id}:{code}".encode()).hexdigest()

Binding means a code can never work in another server or for the other Krypton bot, and a unique index stops two servers from ever holding the same one. Comparison is constant-time with hmac.compare_digest, codes appear in logs only as a short fingerprint, and they expire after a year.

One definition of owner

The largest part of the change was not cryptography. Krypton had well over a hundred places that asked "is the person running this command the owner?", each written its own way. They all now call one function that is true for the real owner, or for the Backup Owner while their grant matches the current owner. A custom command context also gives the Backup Owner full permissions, so existing permission decorators treat them like the owner without being touched.

Checks about a target, such as "you cannot ban the owner", were deliberately left alone. They protect the real owner and should keep doing so.

Abuse protection

  • Lockouts: five wrong codes per user per server per hour lock that user out for an hour. Stored in the database, so they survive restarts and apply across clusters.
  • Alerts: repeated failures in a server DM the owner and post to the log channel.
  • No server-wide lockout: guessing a 256-bit secret is infeasible, and a server-wide lockout would let an attacker block the real owner's recovery.
  • One winner: redemption uses SELECT … FOR UPDATE and a conditional update, so exactly one person wins, even across processes.
  • Leaked codes: a code posted in any channel, or sent to the bot by DM, is deleted where possible and invalidated.

When ownership changes

A live ownership transfer clears the code and the Backup Owner, then DMs the new owner a fresh code. If the transfer happened while the bot was offline, any check that sees the grant's owner differ from the current owner refuses it and runs the same cleanup. Each server is handled by one process, which keeps the Backup Owner in memory and re-reads it periodically; a generation counter stops a reload from overwriting a newer change.

What it cannot do

Being honest about limits is part of the design. A leaked code gives owner-level control of Krypton until it is redeemed or revoked, which is why it works once, needs confirmation, notifies everyone and expires. An attacker who takes the owner account and transfers ownership also wipes the legitimate Backup Owner; the audit table keeps the history. And the Backup Owner has no power over Discord itself: they cannot take ownership, and Discord still limits what the bot can do.

Recovery pairs with prevention: see how the anti-nuke decides who to punish, or the Krypton case study.

Illustrated avatar of Kumar Manas

Written by Kumar Manas

Full-stack developer in India. I build Melodius and Krypton.

Related projectKrypton: discord security bot