NyxBot sheetSheet Note N1 · Sheet 03 TR / EN

Note N1 · NyxBot

PER-GUILD COMMAND REGISTRATION

Per-guild command registration in discord.js: a disabled module's commands should not appear at all

Sheet
NYXBOT
Topic
discord.js · Slash commands
Source
utils/guildCommandSync.js
Date
2026-11-12
Reading
3 min

Short answer: don't register commands globally. For each server, collect the commands of the modules enabled there into one list and write it in bulk with PUT /applications/{id}/guilds/{guild}/commands. Batch changes that arrive in quick succession for 5 seconds, store a hash of the last list you wrote, and skip the request entirely when the hash matches. A disabled module's commands never show up in the user's / menu, and you stay clear of Discord's limit of 200 command creations per server per day.

01Problem

NyxBot's commands are split into 9 modules: moderation, security, economy, giveaways, music, private rooms, fun, utilities and management. A server admin can turn any module off from the web panel.

If every command is registered globally, every server sees all of them. On a server that disabled the economy module, the economy commands still sit in the menu, and running one returns "this module is disabled". The command should not be visible at all.

02Two sets

Commands are split in two. The global set is what every server needs; it is written once when the bot starts. The guild set is the module commands; each server only gets the commands of the modules enabled there.

// command bodies to send to a server, based on the enabled modules' command names
function guildBody(moduleCommands, enabledNames) {
  return moduleCommands
    .filter((c) => enabledNames.has(c.data.name))
    .map((c) => c.data.toJSON());
}

Commands switched off one by one in the panel are also removed from this list, so a single command can be taken out of the menu even while its module is on.

03Why a single PUT

Discord allows at most 200 command creations per server per day. Creating commands one by one with POST when a module is enabled and removing them with DELETE when it is disabled burns through that limit quickly. PUT overwrites the whole list instead.

await rest.put(Routes.applicationGuildCommands(applicationId, guildId), { body });

04Batching and hashing

If an admin enables three modules in a row, three separate requests should not go out. Every change resets that server's timer; a single sync runs 5 seconds after the last change.

clearTimeout(pending.get(guildId));
pending.set(guildId, setTimeout(() => syncGuild(ctx, guildId), 5000));

The list to be sent is sorted by name, hashed with sha1 and stored in the guild_command_sync table. When the bot restarts, every server is checked but only those whose hash changed get a request, with a 1-second gap between them.

05Errors and prefix commands

  • 30034: the daily limit is reached; the panel says "will retry automatically tomorrow".
  • 50001: the bot lacks the applications.commands permission on that server; the admin is told.
  • A command removed from the slash menu can still be typed with a prefix. The prefix path checks the same module state, cached for 10 minutes.
  • If the module state cannot be read, the command is treated as enabled: a database error should not lock up the bot.

06The help menu reads the same source

The command list users see (the help command, the panel's command page) has to match the slash menu. It is built from the same module state the sync uses:

const has = (name) => {
  if (loaded && !loaded.has(name)) return false;     // not loaded in the bot
  if (disabled.has(name)) return false;               // switched off individually in the panel
  const moduleId = moduleOfCommand(name);
  return !moduleId || !state || open.has(moduleId);   // is its module enabled
};
const closed = state.filter((r) => !r.enabled).map((r) => MODULE_BY_ID.get(r.id).label);

The names of disabled modules are returned separately: an admin can be told "this feature is off, enable it with this command", while regular members never see it. Whether a command "exists" depends on three conditions, all in one function, so the menu, the help and the prefix path cannot drift apart.

07Bot startup

When the bot restarts, the global set is written first, then every server the bot is in is checked one by one. Servers whose hash did not change get no request; changed ones are spaced 1 second apart. On hundreds of servers the bot does not fire hundreds of requests at Discord on every start; the startup log says how many servers were checked and how many were actually updated.

→More notes from this project

—End of notewritten by MUHAMMEDPYZ