Skip to main content

Overview

NapCat’s adapter system allows you to create custom protocol implementations alongside or instead of the built-in OneBot 11 protocol. This enables support for proprietary protocols, custom APIs, or integration with other bot frameworks.

Architecture

The adapter system is managed by NapCatAdapterManager in packages/napcat-adapter/index.ts:70, which handles the lifecycle of multiple protocol adapters.

Built-in Adapters

  1. OneBot 11 Adapter - Default protocol adapter
  2. NapCat Protocol Adapter - Native NapCat protocol (optional)

Adapter Interface

All adapters must implement:

Creating a Custom Adapter

Step 1: Implement the Interface

Step 2: Listen to Core Events

Connect to NapCat’s event system:

Step 3: Implement Network Layer

Registering Your Adapter

Adapters are registered in the NapCatAdapterManager:

Network Adapter Pattern (Advanced)

For more complex protocols, use the network adapter pattern from OneBot 11:

Base Network Adapter

HTTP Server Adapter Example

Network Manager Pattern

Manage multiple network adapters:

Complete Example: Telegram-Style Protocol

Adapter Lifecycle

From NapCatAdapterManager.ts:89:

Best Practices

  1. Error handling: Always wrap adapter operations in try-catch
  2. Resource cleanup: Implement proper cleanup in close()
  3. Graceful degradation: Handle core API failures gracefully
  4. Logging: Use context.logger for consistent logging
  5. Configuration: Support enable/disable flags
  6. Event filtering: Only process events your protocol needs
  7. Performance: Avoid blocking operations in event handlers

Testing Your Adapter