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.
FailureType | Identifier | Context |
|---|---|---|
ArgumentCount | ARGUMENT_COUNT | actual, minArgs, maxArgs (numbers) |
GuildOnly | GUILD_ONLY | None |
GuildRequired | GUILD_REQUIRED | None |
MissingPermissions | MISSING_PERMISSIONS | missingPermissions (bigint array) |
OwnerOnly | OWNER_ONLY | None |
TestOnly | TEST_ONLY | None |
CooldownInvalidDuration | COOLDOWN_INVALID_DURATION | duration (number) |
CooldownScopeUnavailable | COOLDOWN_SCOPE_UNAVAILABLE | scope (CooldownScope) |
CooldownActive | COOLDOWN_ACTIVE | cooldownId (string), scope (CooldownScope), expiresAt and remaining (milliseconds) |
CooldownCommitInvalid | COOLDOWN_COMMIT_INVALID | None |
PreconditionEmptyOrGroup | PRECONDITION_EMPTY_OR_GROUP | None |
PreconditionMissingMessageHandler | PRECONDITION_MISSING_MESSAGE_HANDLER | None |
PreconditionMissingChatInputHandler | PRECONDITION_MISSING_CHAT_INPUT_HANDLER | None |
PreconditionMissingContextMenuHandler | PRECONDITION_MISSING_CONTEXT_MENU_HANDLER | None |
PreconditionUnavailable | PRECONDITION_UNAVAILABLE | None |
InlinePreconditionFailed | INLINE_PRECONDITION_FAILED | None |
Custom | Any bot-defined identifier | Arbitrary 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.