Skip to main content

Overview

NapCat provides low-level packet monitoring through the NativePacketHandler, allowing you to intercept and analyze QQ protocol packets. This is useful for debugging, protocol analysis, and implementing advanced features.
Packet inspection operates at the protocol level. Incorrect usage may cause instability. This feature is intended for advanced users and developers.

NativePacketHandler

From client.ts:28-227, the NativePacketHandler provides a native hook into the QQ protocol layer.

Initialization

The packet handler is initialized during framework startup:

Hook Modes

NapCat supports two hook modes:
  • Standard Mode (o3HookMode = 0): Default hook implementation
  • O3 Hook Mode (o3HookMode = 1): Alternative hooking method for specific environments
From napcat.ts:63:

Packet Types

Packets are classified by direction:

Packet Data Structure

Listening to Packets

Listen to All Packets

Listen by Direction

From client.ts:89-103:

Listen to Specific Commands

From client.ts:105-112:

One-Time Listeners

All listener methods have one-time variants:

Parsing Packet Data

Packet data is provided in hexadecimal format. Use protobuf to decode:

Example: Database Passphrase

From napcat.ts:67-83:

Example: Message Monitoring

Common Protocol Commands

Here are some commonly monitored commands:

Advanced Packet Filtering

Filter by Multiple Commands

Conditional Packet Processing

Packet Statistics

Removing Listeners

From client.ts:149-161:

Packet Capture Example

Complete example for debugging:

Performance Considerations

Packet monitoring can generate high volumes of data. Consider these best practices:
  1. Filter Early: Use specific onCmd or onExact listeners instead of onAll
  2. Limit Logging: Only log essential information
  3. Use Sampling: For high-frequency packets, sample instead of capturing all
  4. Clean Up: Remove listeners when no longer needed
  5. Avoid Heavy Processing: Keep packet handlers lightweight

Sampling Example

Debugging Protocol Issues

Capture Specific Operation

Integration with Plugins

Access packet handler from plugin context:
The packet handler is available through ctx.core.context.packetHandler in plugin contexts.

Best Practices

  1. Always check packet type before processing (send vs receive)
  2. Handle parsing errors gracefully with try-catch blocks
  3. Remove listeners when they’re no longer needed
  4. Use specific listeners (onCmd, onExact) over broad ones (onAll)
  5. Log selectively to avoid performance impact
  6. Document packet formats when you discover new ones
  7. Test in development before deploying to production

Next Steps

Plugin Development

Build custom plugins

Deployment

Deploy to production