* Initial plan * Add plugin system core infrastructure with API, examples, and tests Co-authored-by: PatrickFanella <61631520+PatrickFanella@users.noreply.github.com> * Integrate plugin system with bot and API server Co-authored-by: PatrickFanella <61631520+PatrickFanella@users.noreply.github.com> * Add plugin system to README and configure linting Co-authored-by: PatrickFanella <61631520+PatrickFanella@users.noreply.github.com> * Fix type safety in PluginLoader (code review feedback) Co-authored-by: PatrickFanella <61631520+PatrickFanella@users.noreply.github.com> * Update backend/plugins/template/index.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: PatrickFanella <61631520+PatrickFanella@users.noreply.github.com> Co-authored-by: ⓪ηηωεε忧世 <onnweexd@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
6.0 KiB
6.0 KiB
SpyWatcher Plugins
This directory contains plugins for extending SpyWatcher functionality.
Directory Structure
plugins/
├── examples/ # Example plugins demonstrating features
│ ├── message-logger/
│ ├── analytics-extension/
│ └── webhook-notifier/
├── template/ # Template for creating new plugins
└── README.md # This file
Getting Started
Quick Start
-
Use the template to create a new plugin:
cp -r template my-plugin cd my-plugin -
Edit manifest.json:
- Change the
id,name,author, anddescription - Add required
permissions
- Change the
-
Implement your plugin in
index.js:- Implement the
init()method (required) - Add hooks with
registerHooks()(optional) - Register API routes with
registerRoutes()(optional)
- Implement the
-
Restart SpyWatcher to load your plugin
Example Plugins
We provide three example plugins to help you get started:
1. Message Logger
- Path:
examples/message-logger/ - Purpose: Logs all Discord messages to a file
- Features: Basic plugin structure, Discord event hooks, file I/O
2. Analytics Extension
- Path:
examples/analytics-extension/ - Purpose: Adds custom analytics endpoints
- Features: API routes, database access, Redis caching
3. Webhook Notifier
- Path:
examples/webhook-notifier/ - Purpose: Sends notifications to external webhooks
- Features: Discord event monitoring, HTTP requests, configuration
Plugin Development
Plugin Structure
Each plugin must have:
manifest.json- Plugin metadata and configurationindex.js- Plugin implementationREADME.md- Plugin documentation (recommended)
Manifest Format
{
"id": "my-plugin",
"name": "My Plugin",
"version": "1.0.0",
"author": "Your Name",
"description": "What your plugin does",
"permissions": ["discord:events", "api:routes"]
}
Plugin Interface
module.exports = {
manifest: require('./manifest.json'),
// Required
async init(context) {
// Initialize plugin
},
// Optional
async start() {},
async stop() {},
async destroy() {},
registerHooks(hooks) {},
registerRoutes(router) {},
async healthCheck() {}
};
Permissions
Plugins must declare required permissions:
discord:client- Access Discord bot clientdiscord:events- Listen to Discord eventsapi:routes- Register API routesapi:middleware- Register Express middlewaredatabase:access- Access databasecache:access- Access Redis cachewebsocket:access- Access WebSocket servicemonitoring:access- Access monitoring/metricsfs:access- File system access (restricted)network:access- Make HTTP requests
Plugin API
Context Object
The plugin context provides access to services:
context.discordClient // Discord bot client
context.app // Express app
context.config // Plugin configuration
context.dataDir // Plugin data directory
context.logger // Logger instance
context.events // Event emitter
context.services // Services (database, cache, websocket)
Event Hooks
Available hooks:
discord:readydiscord:presenceUpdatediscord:messageCreatediscord:guildMemberAdddiscord:guildMemberRemoveanalytics:beforeCalculateanalytics:afterCalculateapi:requestapi:responsewebsocket:connectwebsocket:disconnect
API Routes
Plugins can register custom routes:
registerRoutes(router) {
// Routes are prefixed with /api/plugins/{plugin-id}/
router.get('/hello', (req, res) => {
res.json({ message: 'Hello!' });
});
}
Testing Plugins
Manual Testing
- Place plugin in
plugins/directory - Restart SpyWatcher
- Check logs for initialization messages
- Test API endpoints (if any)
- Verify functionality
API Testing
# Get all plugins
curl http://localhost:3001/api/plugins
# Get plugin details
curl http://localhost:3001/api/plugins/my-plugin
# Get plugin health
curl http://localhost:3001/api/plugins/my-plugin/health
# Start plugin
curl -X POST http://localhost:3001/api/plugins/my-plugin/start
# Stop plugin
curl -X POST http://localhost:3001/api/plugins/my-plugin/stop
Best Practices
- Error Handling: Always catch and log errors
- Resource Cleanup: Clean up resources in
destroy() - Logging: Use the provided logger
- Configuration: Use environment variables for secrets
- Documentation: Document your plugin's purpose and usage
- Testing: Test your plugin thoroughly
- Permissions: Request only needed permissions
- Versioning: Follow semantic versioning
Security
- Plugins run with restricted permissions
- Only granted permissions are available
- File system access is limited to plugin data directory
- Network access can be restricted
- Validate all external input
Troubleshooting
Plugin Not Loading
- Check
manifest.jsonis valid JSON - Verify
index.jsexists and exports correctly - Check console logs for errors
- Ensure dependencies are loaded first
Permission Denied
- Add required permissions to
manifest.json - Restart SpyWatcher after changes
Plugin Crashes
- Add error handling in your code
- Check error logs
- Verify resource cleanup
Documentation
For complete documentation, see:
Support
For questions and support:
- GitHub Issues: discord-spywatcher/issues
- Documentation: PLUGIN_SYSTEM.md
Contributing
To contribute a plugin:
- Create your plugin following this guide
- Test thoroughly
- Document usage and configuration
- Submit a pull request
License
Plugins should specify their own license. SpyWatcher core is licensed under LICENSE.