DEV Community

Cover image for Discord rate limits forced us to pre-create voice channels. The permission bug came free.
Tal for Sigh-Club

Posted on Edited on

Discord rate limits forced us to pre-create voice channels. The permission bug came free.

Every "join to create a voice channel" bot has the same bug hiding in it, and it isn't the join part.

The join part is easy. A voice state update arrives, you create a channel, you move the member in. What's hard is that you are doing a gateway write in the middle of a user's click. Discord's channel-create limit is per guild, and a busy evening of people hopping in and out will find it. When it does, the member sits in the anchor channel for three seconds wondering if the bot is broken.

So we stopped creating channels on demand. We pre-create them.

The pool

Two idle slots per anchor, held in memory, keyed by guild and category:

const POOL_TARGET_SIZE = 2;

async function refillChannelPool(client, guildId, categoryId) {
  const guildConfig = guildConfigCache.get(guildId);
  if (!guildConfig || !guildConfig.enablePooling) return;

  const poolKey = `${guildId}:${categoryId}`;
  const pool = channelPools.get(poolKey) || [];
  if (pool.length >= POOL_TARGET_SIZE) return;
  // ...create one slot, push it
}
Enter fullscreen mode Exit fullscreen mode

A claim shifts a slot off the pool and renames it in place, then refills in the background. The user gets a channel in one edit instead of one create, which is the difference between "instant" and "is it broken".

Idle slots are called 🕒 Preparing..., and @everyone is denied View and Connect, so nobody wanders into one.

The permission trap

This is the part worth writing down, because it cost us a weekend.

Our first pool slots were created inside the category and inherited its permission overwrites. That seems right: the room should look like the category. What actually happened is that every idle slot became visible to the whole server.

The reason is Discord's permission precedence. Channel-level overwrites beat role-level ones, but a role allow still beats an @everyone deny. If the category grants View to, say, a "Member" role, and your slot denies View to @everyone, the Member role wins for every member. Your @everyone deny did nothing for the people who matter.

So pool slots are created with no category inheritance at all, and two explicit overwrites:

permissionOverwrites: [
  { id: guild.id, deny: [ViewChannel, Connect] },
  { id: client.user.id, allow: [ViewChannel, Connect, ManageChannels, MoveMembers] },
],
Enter fullscreen mode Exit fullscreen mode

At claim time the overwrites are replaced wholesale with a layered set: category rules, then the anchor channel's rules, then the owner's explicit allow. Layering in that order matters, because the anchor is the thing the admin configured last and it has to win over the category for the same role id. And a deny always beats an allow at the same level, so you cannot "allow" your way past a role deny. You have to remove the deny.

Cleanup, and the bug that taught us to key by owner

Rooms have to die. Empty rooms, rooms whose owner left the server, rooms whose owner moved to another room, and the leftovers from a bot restart. We sweep for all four, plus a global pass on boot.

The interesting bug: our lock button read the clicker's temp-room record, not the room's. A moderator locking someone else's room would toggle their own flag and the room's stored state would stay wrong. Everything is keyed by the room owner now, resolved from the channel, because the person clicking and the person who owns the room are frequently not the same person.

The other one: a rename can take longer than the three seconds Discord gives an interaction to respond. Any control that renames or rewrites overwrites defers first, then acts. That's not defensive coding, it's the only way the button ever works.

What I'd do differently

  • Treat channel creation as a queue, not a promise. A create that fails should retry, not surface to the user.
  • Cap the pool hard and back off the refill when you're being rate limited, otherwise the refill is what hammers the limit.
  • Assume every voice state event can arrive twice, out of order, or not at all. The member who leaves during creation is the normal case, not the edge case.

The whole system is about 1,300 lines, most of it cleanup. The pool is maybe forty of them.

If you want to see it running, the bot is at sighnet.org. Free tier, and temp voice is in it.

Top comments (1)

Collapse
 
loocraftlabs profile image
LooCraft Labs •

That category-inheritance + @everyone deny trap bites almost everyone building temp-voice systems in Discord — role-level Allow overwrites on the parent category silently override an @everyone Deny on the child channel.

One other rate-limit gotcha to watch out for with pre-created pools: PATCH /channels/{id} (channel name/topic edits) has its own strict per-channel bucket (2 renames per 10 minutes on Discord). Because claiming a slot from 🕒 Preparing... to the room's initial name already consumes 1 of those 2 renames, if the room owner clicks a "Rename" button right after joining, the second edit works — and a third one will stall on a 10-minute cooldown unless you surface the retry-after to the user or debounce renames.

Passing explicit permissionOverwrites directly in the POST create call (instead of creating first and patching overwrites after) is also a great call — it avoids the brief race window where the channel is briefly visible in the category.