← All writing

The bot that hid a channel from itself

My bot can hide a Discord channel at night and bring it back in the morning. One version hid the channel from itself too, and then couldn't bring it back. Some bugs can't be undone by the code that caused them.

·2 min read

My Discord bot has timed rules. “Hide this channel from 23:00 to 07:00.” It saves what the permissions were, changes them, and puts them back exactly when the time is up.

Hiding a channel means telling Discord: @everyone can’t view it.

The bot is part of @everyone.

Locked out

In Discord, if you can’t see a channel, you can’t do anything in it. You can’t read it, and you can’t change its permissions either. Discord answers “Missing Access” to everything.

So at 07:00 the bot went to un-hide the channel, and Discord said no. Every five minutes it tried again. Every five minutes, no. The channel stayed hidden for good, and the dashboard showed a message that blamed the wrong thing: “the bot’s role needs Manage Roles and Manage Channels”. It had both. They don’t help with a channel you can’t see.

Unless the bot had the Administrator permission. Administrators see everything, so on those servers the bug never showed. That makes it very easy to miss: test with a bot that has every permission and the rule looks perfect.

The fix for new rules

Before hiding a channel, the bot now gives itself a line on that channel: “this bot can view”. Only that, nothing more. Then it hides the channel from everyone. At the end it removes everyone’s line and its own, so the channel is exactly as it was.

The channels that were already stuck

This is the part I find interesting. The fix above is for the future. What about a channel that an older version already hid?

The bot can’t fix it. That’s not laziness, it’s the bug itself: the bot has no access, so no code I ship can get it back in. The only one who can fix it is a person with rights on that server.

So the honest fix is words. When un-hiding fails with “no access” on a hidden channel, the dashboard now says:

This channel is still hidden because the bot can’t see it either. In Discord: Edit Channel → Permissions → add the bot → allow View Channel. It then un-hides by itself within 5 minutes.

And the last sentence is true. The bot is still trying every five minutes. The moment a person lets it see the channel, the next try works and everything goes back.

What I keep from this

Two things.

Test without the master key. A bot with Administrator, or a user who is the owner, skips most of the rules. If I only test as them, I only test the easy path.

When code can’t recover, say exactly what a person must do. Not “something went wrong”. Not a guess at a permission. The clicks, in order, and what happens after.