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.

Failure types

Every library-generated failure has a FailureType value. Compare failure.type in onPreconditionFailure to narrow built-in contexts. failure.identifier remains the original string identifier.

FailureTypeIdentifierContext
ArgumentCountARGUMENT_COUNTactual, minArgs, maxArgs (numbers)
GuildOnlyGUILD_ONLYNone
GuildRequiredGUILD_REQUIREDNone
MissingPermissionsMISSING_PERMISSIONSmissingPermissions (bigint array)
OwnerOnlyOWNER_ONLYNone
TestOnlyTEST_ONLYNone
CooldownInvalidDurationCOOLDOWN_INVALID_DURATIONduration (number)
CooldownScopeUnavailableCOOLDOWN_SCOPE_UNAVAILABLEscope (CooldownScope)
CooldownActiveCOOLDOWN_ACTIVEcooldownId (string), scope (CooldownScope), expiresAt and remaining (milliseconds)
CooldownCommitInvalidCOOLDOWN_COMMIT_INVALIDNone
PreconditionEmptyOrGroupPRECONDITION_EMPTY_OR_GROUPNone
PreconditionMissingMessageHandlerPRECONDITION_MISSING_MESSAGE_HANDLERNone
PreconditionMissingChatInputHandlerPRECONDITION_MISSING_CHAT_INPUT_HANDLERNone
PreconditionMissingContextMenuHandlerPRECONDITION_MISSING_CONTEXT_MENU_HANDLERNone
PreconditionUnavailablePRECONDITION_UNAVAILABLENone
InlinePreconditionFailedINLINE_PRECONDITION_FAILEDNone
CustomAny bot-defined identifierArbitrary context; values remain unknown

Custom also covers a malformed context attached to a reserved identifier. For example, a COOLDOWN_ACTIVE failure without a numeric remaining value cannot be treated as a typed cooldown failure.

On this page