npm
Source
Description
A set of decorators for configuring@wolfstar/http-framework pieces and
gating their methods, modelled after @sapphire/decorators. They
shipped with the framework itself until @wolfstar/http-framework 6.0.0. The framework now only keeps the Register*
decorators and RestrictGuildIds, which are part of command registration.
Use this package when you want to set piece options without writing a constructor, restrict a method to guild or DM
interactions, or check user and application permissions before a method runs.
Installation
@wolfstar/http-framework is a peer dependency, so install both packages. The decorators use the legacy
(experimentalDecorators) decorator semantics, like the Register* decorators of the framework.
@wolfstar/decorators is one of the default imports.presets. If your project uses the stars CLI’s auto imports, you
get every export of @wolfstar/decorators without an explicit import once the package is installed. See the
CLI guide.
Usage
ApplyOptions
Sets the options of any Piece (Command, Listener, or InteractionHandler) without writing a constructor. The
decorator merges its values on top of the options the piece is constructed with, so they win on conflicting keys.
Note:ApplyOptionsreturns aProxywrapping the class. Apply it above (outside of) any other class decorator that keys metadata by class identity, such asRegisterCommand, as in the example above. A decorator applied aboveApplyOptionswould run after it and register against the proxy. Instances constructed from the exported class still resolve.constructorto the original, unproxied class, so the framework could never find that metadata. Thewolfstar/apply-options-decorator-orderrule of@wolfstar/eslint-plugin-http-frameworkenforces this.
RequiresGuildContext / RequiresDMContext
Restrict a method to interactions received from a guild, or to interactions received outside of one (DMs and
user-installed app contexts). Both take an optional fallback that receives the same arguments as the decorated method.
Without a fallback, the decorator silently skips the method, which resolves to undefined.
RequiresUserPermissions / RequiresClientPermissions
Check the permissions of the invoking user (member.permissions) or of the application (app_permissions) in the
channel the interaction was sent from. Permissions are given as PermissionFlagsBits values, as flag names, or as any
nested array of both.
PreconditionError. Its identifier is
Identifiers.PreconditionUserPermissions or Identifiers.PreconditionClientPermissions, and its
context: { missing, missingNames } describes the missing permissions. A PreconditionError is a UserError, so the
framework emits it as chatInputCommandDenied or contextMenuCommandDenied (and as interactionHandlerDenied for
interaction handlers) instead of commandError. Listen to these events to turn the errors into a user-facing reply.
commandError only receives unexpected errors:
- Before
@wolfstar/http-framework6.3.0, this error reachedcommandError. If a listener on that event handlesPreconditionError, move it to the*Deniedevents. See Expected errors. - Members with
Administratorimplicitly satisfy every check. RequiresUserPermissionspasses for interactions received outside of a guild, since there are no guild permissions to check. Combine it withRequiresGuildContextwhen the method must be guild-only.RequiresClientPermissionspasses whenapp_permissionsis absent from the payload, since there is nothing to check against.
Enumerable / EnumerableMethod
Control whether a field or a method shows up in Object.keys, JSON.stringify, and console output.
Note:Enumerableinstalls a setter on the prototype.useDefineForClassFields(enabled by@sapphire/ts-config, and the default fromES2022onwards) emits anObject.definePropertycall for class fields, which bypasses that setter. Mark the field asdeclareso no field definition is emitted, and assign it in the constructor.
Building your own decorators
createClassDecorator, createMethodDecorator, createProxy, and createFunctionPrecondition are the primitives the
decorators above are built on, and are exported so you can build your own.
Note:createFunctionPreconditionreplaces the decorated method with anasyncone, so a decorated method always returns aPromise, even when both the precondition and the method are synchronous.
Migrating from @wolfstar/http-framework 5.x
Install @wolfstar/decorators and move the imports of the utility decorators over. Nothing else changes:
PieceConstructor and ContextFallback types moved too. See the migration guide
for the full checklist.