Skip to main content

Overview

NapCat’s plugin system allows you to extend the bot’s functionality through a modular architecture. Plugins can handle messages, events, register HTTP APIs, and interact with the core system.

Plugin Structure

A NapCat plugin consists of:

package.json

Define your plugin metadata:

Plugin Module Interface

From types.ts:304-340:

Plugin Context

The plugin context provides access to core functionality:

Basic Plugin Example

From index.ts:31-212:
1

Initialize Plugin

index.ts
2

Handle Messages

3

Handle All Events

4

Cleanup on Unload

Configuration System

Define Configuration Schema

Get and Set Configuration

Reactive Configuration

From index.ts:239-262:

WebUI Integration

Register HTTP Routes

From index.ts:92-157:

Serve Static Files

Register Plugin Pages

Plugin Communication

Call Other Plugins

From index.ts:160-195:

Advanced Features

Access Core APIs

Use Plugin Logger

Store Plugin Data

Complete Plugin Example

index.ts
Always implement proper error handling and cleanup in your plugins. Unhandled errors can crash the entire bot.

Best Practices

  1. Use the logger instead of console.log for better log management
  2. Handle errors gracefully - wrap async operations in try-catch
  3. Validate configuration before using config values
  4. Clean up resources in plugin_cleanup (timers, connections, files)
  5. Use cooldowns to prevent command spam
  6. Store persistent data in ctx.dataPath
  7. Document your plugin with clear descriptions and examples

Next Steps

Packet Inspection

Monitor protocol packets

Message Handling

Learn about message APIs