1
Deadman
kingston edited this page 2026-11-03 12:51:18 -06:00

Deadman

A switch that alerts nominated users if the bot owner does not disable it before a deadline. The intended use is a solo operator who wants someone to be told if they stop responding.

Requirements

None beyond Red. The cog uses Red's global configuration store and does not need any Python package outside the standard library and discord.py.

Commands

All commands are in the deadman group and the group requires the bot owner. The check is on the group, so every subcommand inherits it.

Command Arguments Behavior
[p]deadman arm <duration> <users...> [channel] Arms the switch, sets the deadline, and DMs each listed user
[p]deadman disable none Clears the armed state, deadline, users and alert channel
[p]deadman status none Shows armed state, deadline, remaining time and the user list

arm

[p]deadman arm 6h @Alice @Bob #alerts
  • duration is required and parsed by the flexible parser described below.
  • At least one user is required. Without one the bot replies "You must provide at least one user to alert."
  • users uses commands.Greedy, so it consumes mentions until it reaches something that is not a user. The optional channel must therefore be a channel mention or ID that Discord resolves as a channel, placed after the users.
  • Each listed user receives a direct message describing the deadline and telling them to contact the bot owner if this was a mistake.
  • The confirmation message reports the deadline in UTC, the relative duration, the mentioned users, the alert channel if one was given, how many direct messages succeeded, and which users could not be reached.

Direct message failures do not stop arming. A user with closed direct messages still appears in the stored list and still gets mentioned in the alert channel.

disable

Does nothing except reply "The deadman switch is not currently armed." when the switch is already off. When armed, it resets all four config keys and clears the in-memory trigger flag.

status

When disarmed, replies that the switch is disarmed. When armed, shows the expiry in UTC, the remaining time as a timedelta string, and the stored user mentions. A negative remaining time means the deadline passed and the alert has already fired.

Duration format

The parser lowercases the input and reads digits followed by a unit, repeating for each component.

Unit accepted Meaning
s, sec, secs, second, seconds seconds
m, min, mins, minute, minutes minutes
h, hr, hrs, hour, hours hours
d, day, days days
w, week, weeks weeks

Examples that parse: 30s, 6h, 2h30m, 90s, 1d, 1d12h, 1w2d3h4m5s.

Examples that fail: 5m30 (trailing digits with no unit), 6 hours from now (text after a unit that is not a unit), 0s (zero total), -5m (a minus is read as a unit character before any digits, so parsing stops).

A failed parse replies with the example list and does not arm.

How it works

  1. arm computes datetime.utcnow() + duration and stores it as a naive ISO string. The armed flag, user IDs and channel ID are stored in global config, so the switch survives restarts.
  2. cog_load starts _monitor, an asyncio task that sleeps 30 seconds between checks and swallows every exception to keep running.
  3. _check_expiry returns immediately when the switch is not armed or has no deadline. Otherwise it compares the current UTC time with the stored deadline.
  4. On expiry, _trigger runs once per process. It resolves each stored user ID with the cache first and fetch_user as a fallback, sends each user a direct message, then posts to the alert channel if one is configured.
  5. The alert channel message mentions only users the bot can resolve from its cache, because the mention string skips IDs where bot.get_user returns nothing.
  6. _triggered stays true until disable runs or the process restarts. The switch also stays armed, so status continues to show an expired deadline.

Delivery behavior

  • Direct messages are sent one at a time. discord.Forbidden and any other exception are swallowed, so a blocked user is silently skipped.
  • The channel post is skipped entirely when no channel was configured. When the channel was deleted or is no longer visible, the send raises and is swallowed.
  • The alert text is fixed: a heading, one sentence stating the switch triggered, and a request to contact the operator.

Limits and failure modes

  • The 30 second loop means an alert can arrive up to about 30 seconds after the deadline.
  • Trigger state is in memory. If the bot restarts after the deadline but before disable, the alert fires again, because the stored state is still armed and expired.
  • There is no catch-up logic for downtime. An expired switch triggers as soon as the cog loads and the monitor runs its first check.
  • User IDs are stored as integers. red_delete_data_for_user does nothing, so a deletion request does not remove a user from an armed switch.
  • The deadline uses naive UTC. Any change to the host clock or timezone does not affect it, but a clock jump forward or backward shifts the effective deadline.

Stored data

Global config under identifier 918273645:

Key Type Default
armed bool false
expires_at ISO string or null null
users list of int []
trigger_channel int or null null

There is no per-guild configuration. One switch exists for the whole bot.