import type { CommandDef } from '@moxxy/sdk'; /** * Registry of `/` slash commands shared across every channel. * Plugins contribute via `PluginSpec.commands`; channels read with * `list()` / `get()` / `match()` to render their picker and dispatch * matching user input. * * Naming conflicts throw at registration time so two plugins can't * silently shadow the same name — use `replace()` for explicit * overrides (e.g. user-config tweaks of a built-in). */ export class CommandRegistry { private readonly commands = new Map(); /** Reverse-lookup table: alias → primary name. */ private readonly aliases = new Map(); register(cmd: CommandDef): void { if (this.commands.has(cmd.name)) { throw new Error(`Command already registered: /${cmd.name}`); } if (this.aliases.has(cmd.name)) { throw new Error(`Command name already in use as an alias: /${cmd.name}`); } // Validate EVERY alias up front, before mutating any state, so a collision // on the second alias can't leave the command half-registered (def set + // some aliases) — the registry stays atomic on a throw. for (const alias of cmd.aliases ?? []) { if (this.aliases.has(alias) || this.commands.has(alias)) { throw new Error(`Command alias already in use: /${alias}`); } } this.commands.set(cmd.name, cmd); for (const alias of cmd.aliases ?? []) this.aliases.set(alias, cmd.name); } replace(cmd: CommandDef): void { const prior = this.commands.get(cmd.name); const priorAliases = new Set(prior?.aliases ?? []); // Validate the new aliases BEFORE mutating anything, mirroring register()'s // guards so replace() can't silently hijack an alias owned by a different // command or shadow another command's primary name. Validating first (rather // than deleting the prior aliases then re-adding) keeps replace() atomic: a // collision on a later alias must not have already destroyed the prior // definition's aliases. An alias the prior def itself owned is exempt (it's // about to be re-added under the same name). for (const alias of cmd.aliases ?? []) { const owner = this.aliases.get(alias); if (owner && owner !== cmd.name && !priorAliases.has(alias)) { throw new Error(`Command alias already in use: /${alias}`); } if (this.commands.has(alias) && alias !== cmd.name) { throw new Error(`Command alias already in use as a command name: /${alias}`); } } // All validated — now mutate. Clean up the prior definition's aliases, then // install the new def + its aliases. for (const alias of priorAliases) this.aliases.delete(alias); this.commands.set(cmd.name, cmd); for (const alias of cmd.aliases ?? []) this.aliases.set(alias, cmd.name); } unregister(name: string): void { const cmd = this.commands.get(name); if (!cmd) return; for (const alias of cmd.aliases ?? []) this.aliases.delete(alias); this.commands.delete(name); } get(name: string): CommandDef | undefined { const direct = this.commands.get(name); if (direct) return direct; const aliased = this.aliases.get(name); return aliased ? this.commands.get(aliased) : undefined; } has(name: string): boolean { return this.get(name) !== undefined; } list(): ReadonlyArray { return [...this.commands.values()]; } /** * Filter to commands visible in `channel`. Commands with no * `channels` field are visible everywhere; commands with a list are * visible only when `channel` appears in it. */ listForChannel(channel: string): ReadonlyArray { return this.list().filter( (c) => !c.channels || c.channels.includes(channel), ); } }