Counting while the bot was away

PokeSpot has a counting channel. The rules fit in one line: count up together, one number per message, and never twice in a row. It was the first thing my bot, Poke Helper, ever did, back in July 2025, and the first version fit in one file:
It listened for new messages and compared each one with the last number in the database. It had bugs I only saw later. parseInt happily reads 5abc and 5.9 as 5, so both counted. Two people sending the same number at the same moment were both counted, as both checks read the database before either saved. And every error was passed to logger.error without its logger, so none of them were ever logged.
But the one that ended runs was simpler: the bot only counted while it was running. In November I rewrote the game, and the easiest way to show what it does now is to restart it. The times below are made up. The steps are what the bot does.
11:58, the count is at 100
Someone sends 100. The bot checks it, it's right, and the game saves two things. One is the last number, 100. The other is new: lastCheckedMessageId, the last message the bot has looked at, right or wrong.
Those two aren't the same. A wrong number gets deleted, but it was still checked, and nothing should look at it again. The bookmark is what this whole post hangs on.
12:00, the bot goes away
A deploy. It could just as well be an update, a crash or the server rebooting. Nobody in the channel is told, so people keep counting.
12:00 to 12:03, nobody is checking
In those three minutes the channel gets:
- 101
- 102
- 102 again, from someone who didn't scroll down
- "Nikki pinned a message"
- 103
The old bot would have come back to a database saying 100, seen the next message after its restart, and called it wrong. The run would have ended on the first number anyone sent.
12:03, the bot comes back
When the bot logs in to Discord, it starts a new session, and Discord fires ShardReady. That's when catching up runs. A short dropout is different: the bot resumes its session and Discord replays the events it missed, so there's nothing to catch up on. Only a new session starts from nothing, which is why it's ShardReady and not every reconnect.

12:03, reading from the bookmark
The bot asks Discord for every message after the bookmark. Discord's message IDs are snowflakes, which start with the time they were sent, so they sort by time and "after this ID" means "sent after this message". Discord hands them out 100 at a time, newest first, so each batch gets sorted:
Games from before the bookmark existed start from their last counted message, and if even that's missing, from a snowflake made out of the time the game last changed.
12:03, the same rules as always
Now the five messages get checked, oldest first. The easy mistake would be a second, slightly different check for catching up. Instead, there's one pure function for both. It gets the game's state and a message, and gives back the new state, what should happen to the message, and what to record for the stats:
Live counting calls it once per message. Catching up calls it in a loop, carrying the state from one message to the next. So for my five:
| Message | What happens |
|---|---|
| 101 | counts |
| 102 | counts |
| 102 again | wrong, costs a life (with lives on), gets deleted |
| "Nikki pinned a message" | skipped, as it isn't a number someone typed |
| 103 | counts |
The pin notice is in there because it once wasn't skipped. System messages like that have a member as their author, so for a while they were checked like counts: deleted, recorded as a mistake, and with lives on, they cost one. Now only normal messages and replies count, live and when catching up.
What counts is stricter too. A number comes first, followed by a space or nothing, so 5 nice counts and 5abc, 5.9 and 05 don't. A server can also turn on math, and then 2^3 or 3! count as 8 and 6.
12:03, saving
The new state goes into the database with one condition: the bookmark there must still be the one the check started from. If anything else had changed the game in the meantime, the save is refused and logged, instead of quietly overwriting it. Nothing should get that far, though, because of what happens next.
12:03, someone sends 104
Catching up takes a moment, and people don't stop counting for it. Someone sends 104 while the bot is still busy with 101 to 103. If it were checked right away, against a database still saying 100, it would be wrong. Two checks running at once is also exactly where the old double-counting bug came from.
So everything that touches a server's game runs through one queue per server:
104 waits until the catch-up is done, then gets checked against 103, and counts. Different servers still run side by side.
There's one more case. If 104 was sent just before the catch-up fetched the channel, the catch-up has already counted it, and then the live check comes along with the same message. The bookmark answers that too: anything at or before it is skipped.
12:03, one message instead of a warning each
One thing the catch-up did before 104's turn came: it cleaned up. Live, a wrong number gets a warning that deletes itself with the number, after three seconds, or six when it cost a life. Catching up on a long outage that way could mean fifty warnings in a row. So the catch-up deletes all its wrong messages in one go, in bulk for messages under 14 days old, which is all Discord allows, and posts a single summary: three numbers counted, one message removed, one life lost, the lives left, and that the next number is 104.
If a run was reset along the way, the summary says at which number and by whom. Only the first five are listed, with "and 3 more resets" after them, because a burst of wrong numbers could otherwise make the summary longer than a Discord message can be.
What if it had been gone all weekend?
Catching up stops after 1,000 messages. Past that, the bookmark jumps to now, so counting goes on from the present rather than from somewhere in a backlog nobody remembers.
Exactly 1,000 needed one more check. A full last page can't tell whether anything comes after it, so the bot asks for one more message. Without that, a channel with exactly 1,000 missed messages was reported as having more.
12:04
The fix wasn't really about being offline. It was about the bot knowing exactly how far it had read, and treating a message the same way whenever it reads it. With that, a restart is just a pause in the middle of a run, and PokeSpot's count is past 5,800.


No comments yet. Be the first!