← All writing

1,152 servers in one loop

My bot builds Discord servers from a list of switches. Who can see and write in each channel depends on all of them at once. I checked every combination with a loop, found nothing, changed the question, and found two bugs.

·2 min read

My bot builds a Discord server from choices: a starting point (gaming, study, open source…), a bot check or not, a waiting room, a staff area, tickets, a trap channel for spam bots, and more.

Every choice changes permissions. And permissions in Discord stack: what @everyone gets, then what your roles add or take away, per category and per channel. A mistake here isn’t a broken button. It’s a new member reading the staff chat.

I had tests for the cases I thought of. I wanted the cases I didn’t think of.

Ask the plan, not Discord

The builder first makes a plan: every role, every channel, every permission line. That plan is plain data. So I can work out “who sees what” from it with the same maths Discord uses, without touching Discord at all.

Then it’s a loop. Six starting points, eight switches, every combination that makes sense: 1,152 plans. For each one, five kinds of people: a newcomer, a member, a bot with the Bots role, a moderator, an admin. For each channel, a few rules that must always hold:

  • nobody outside the staff sees a staff channel
  • before the bot check, a newcomer can’t write anywhere
  • with the waiting room on, a newcomer sees one channel
  • a member can’t post where the channel is read-only, and neither can a bot
  • the staff see everything

It ran in a few seconds and found nothing. All 1,152 were right.

Then I changed the question

“Is a fresh server right?” was the wrong question, or at least the easy one. Servers aren’t fresh for long. People change their mind.

So the next loop built a server, flipped one switch, applied the change, and compared the result with a server built that way from the start. 288 flips. They should always match.

Two didn’t.

Turning the bot check on later. The staff channels and the rules channel kept their old permission lines. Nobody could see more than before, so it wasn’t a leak. But the repair page then listed six things as “broken” on a server I had just changed. The cause is its own short story.

One starting point plus one switch. The open source starting point has its own links channel. The “Links” switch adds a links channel. Both used the same internal name. The build made two channels, kept track of one, and the repair page called the other “changed in Discord”.

What I took from it

The first loop was satisfying and useless. It checked the thing I had already been careful about.

The second loop checked a path instead of a place: not “is this state right” but “do I arrive at the same state whichever way I come”. That’s where the bugs were, because that’s where I had only ever tested by hand, once.

If two roads should lead to the same place, build both and compare.