Plugin Types
CommandKit supports two types of plugins:Runtime Plugins
Run during bot execution. Hook into command execution, events, and lifecycle methods.
Compiler Plugins
Transform code at compile time. Process files before they’re executed.
Runtime Plugins
Runtime plugins extend your bot’s behavior during execution.Creating a Runtime Plugin
Runtime Plugin Hooks
Runtime plugins provide numerous lifecycle hooks:Example: Command Logger Plugin
src/plugins/command-logger.ts
Example: Rate Limiter Plugin
src/plugins/rate-limiter.ts
src/index.ts
Compiler Plugins
Compiler plugins transform code before execution.Creating a Compiler Plugin
src/plugins/add-header.ts
Example: Environment Variable Injector
src/plugins/env-injector.ts
Plugin Options
Plugins can accept configuration options:src/plugins/configurable.ts
Preloading Files
Runtime plugins can preload JavaScript files:Advanced Examples
Database Integration Plugin
src/plugins/database.ts
Command Usage Analytics Plugin
src/plugins/analytics.ts
Best Practices
Clean Up Resources
Always clean up resources in the
deactivate() method.Error Handling
Handle errors gracefully to prevent plugin failures from crashing the bot.
Type Safety
Use TypeScript and type your plugin options for better DX.
Document Behavior
Document what your plugin does and how to configure it.
Testing Plugins
tests/plugins/logger.test.ts
API Reference
RuntimePlugin<T>
Base class for runtime plugins.
Properties:
name- Plugin name (required)type- Plugin type (automatic)options- Plugin configurationpreload- Set of files to preload
CompilerPlugin<T>
Base class for compiler plugins.
Properties:
name- Plugin name (required)type- Plugin type (automatic)
transform(params)- Transform code
CommandKitPluginRuntime
Context passed to runtime plugin hooks.
Properties:
commandkit- CommandKit instance
PluginTransformParameters
Parameters for compiler transform method.
Properties:
code- Source code stringid- File path