AsyncLocalStorage. This allows you to access execution context, CommandKit instances, and shared state from anywhere within your command or event handlers without explicitly passing them around.
Understanding CommandKit Context
The context system operates on two levels:- Async Context - Tracks the current execution environment using Node.js async hooks
- Command/Event Context - Provides access to Discord.js objects, command metadata, and utilities
Async Context APIs
useEnvironment()
Returns the current CommandKit execution environment. Throws an error if called outside a CommandKit handler.packages/commandkit/src/context/async-context.ts:126
useStore()
Returns the shared data store (Discord.js Collection) for the current execution environment.packages/commandkit/src/context/async-context.ts:140
getCommandKit()
Retrieves the CommandKit instance. Can be called withstrict parameter to throw error if not found.
packages/commandkit/src/context/async-context.ts:101-112
CommandKitEnvironment
TheCommandKitEnvironment class represents the execution environment for commands and events.
Properties
Methods
setContext(context)
Sets the command context for this environment.packages/commandkit/src/context/environment.ts:81
getType()
Returns the environment type (e.g., ‘COMMAND_HANDLER’).packages/commandkit/src/context/environment.ts:117
registerDeferredFunction(fn)
Registers a function to run after command execution completes.packages/commandkit/src/context/environment.ts:156
getExecutionTime()
Returns the command execution time in milliseconds.packages/commandkit/src/context/environment.ts:229
Command Context
TheContext class provides access to Discord.js objects and command metadata.
Key Properties
Type Guards
packages/commandkit/src/app/commands/Context.ts:428-470
Forwarding Commands
Forward execution to another command while preserving context.packages/commandkit/src/app/commands/Context.ts:348
Deferred Functions
Schedule functions to run after command execution completes.after(fn)
Register a function to run after the current command finishes.packages/commandkit/src/context/environment.ts:253
cancelAfter(id)
Cancel a previously scheduled deferred function.packages/commandkit/src/context/environment.ts:277
Context-Aware Functions
CommandKit automatically wraps your handlers in context-aware functions usingmakeContextAwareFunction. This ensures:
- Async context is properly propagated
- Errors are captured and handled
- Deferred functions run after execution
- Cleanup occurs properly
packages/commandkit/src/context/async-context.ts:60
Best Practices
-
Use Context Parameter: Always prefer using the
ctxparameter passed to your handlers rather than callinguseEnvironment()directly. -
Store Scoping: Data in
ctx.storeis scoped to the current command execution, including all middlewares. -
Deferred Functions: Use
after()for cleanup operations that should happen regardless of success or failure. -
Type Safety: Use specific context types like
ChatInputCommandContextfor better type inference.