SWAGcommands
API reference

Precondition contract

The class methods, result helpers, and composition types.

A reusable precondition extends Precondition and implements one or more flow methods: messageRun, chatInputRun, and contextMenuRun. Use AllFlowsPrecondition when you want TypeScript to require all three.

Each run method receives (usage, command, context) and returns a PreconditionResult or a promise of one. Use this.ok() for success or this.error({ identifier, message?, context? }) for failure. Inline checks can return booleans or use preconditionOk() and preconditionError().

An optional matching commit method (messageCommit, chatInputCommit, or contextMenuCommit) runs only after the selected checks have succeeded. It receives the same arguments and returns a result. Cooldown uses this for an atomic claim.

preconditions: [
  "GuildOnly",
  { any: ["OwnerOnly", "TestOnly"] },
  Cooldown({ duration: 5_000 }),
]

PreconditionArrayResolvable is an array of named entries, factory entries, inline functions, or { all: [...] } / { any: [...] } groups. Built-in names include GuildOnly, OwnerOnly, TestOnly, HasPermissions, ArgumentCount, and Cooldown. Context-bearing entries may use a typed factory such as HasPermissions({ permissions: [...] }).

The preconditions guide covers ordering and failures. Custom preconditions walks through a class and typed factory.