Core Architecture
NapCat is built on a modular architecture that wraps NTQQ (the native QQ protocol client) with a clean API layer. The architecture consists of several key components working together to provide a stable bot framework.NapCatCore Class
TheNapCatCore class is the heart of NapCat, managing all core functionality and API services.
- Initialize and manage API wrappers
- Handle event system lifecycle
- Manage configuration and logging
- Coordinate packet handling
- Maintain self information and online status
packages/napcat-core/index.ts:113
InstanceContext
TheInstanceContext interface provides runtime dependencies to all components:
- Clean dependency injection
- Consistent access to shared resources
- Environment-aware behavior
- Easy testing and mocking
packages/napcat-core/index.ts:332
API Wrappers
NTQQMsgApi
Message sending, receiving, recall, multi-message, emoji reactions
NTQQGroupApi
Group management, member operations, file handling, notifications
NTQQFriendApi
Friend list, requests, profile management
NTQQUserApi
User info, UID/UIN conversion, profile details
NTQQFileApi
File uploads, downloads, image/video handling
NTQQSystemApi
System utilities, status, platform info
NTQQPacketApi
Low-level packet sending and protocol access
NTQQDatabaseApi
SQLite database access with encryption support
NapCatCore constructor:
packages/napcat-core/index.ts:144
Example: NTQQMsgApi
The Message API provides comprehensive message handling:packages/napcat-core/apis/msg.ts:5
Event System
NapCat uses a sophisticated event system built on multiple layers:NTEventWrapper
Wraps NTQQ’s native event system with a Promise-based listener API:TypedEventEmitter
Provides type-safe event emission throughout NapCat:KickedOffLine- Bot was kicked offlinePacketReceived- Protocol packet received- Custom packet events from services
packages/napcat-core/packet/handler/typeEvent.ts
Kernel Listeners
NapCat registers listeners with NTQQ’s kernel services:packages/napcat-core/index.ts:225
Packet Handler
TheNativePacketHandler provides low-level protocol packet interception:
Packet handling requires native modules and may not be available on all platforms.
Features
- Bidirectional monitoring: Intercept both sent and received packets
- Flexible filtering: Listen by packet type, command, or exact match
- One-time listeners: Support for single-use event handlers
- Type safety: Full TypeScript support
Listener API
packages/napcat-core/packet/handler/client.ts:28
Example Usage
packages/napcat-framework/napcat.ts:68
Service Registry
NapCat uses dependency injection for packet service handlers:- Automatic service registration
- Clean separation of concerns
- Easy testing and mocking
- Protocol version compatibility
packages/napcat-core/index.ts:158
Initialization Flow
The initialization process:- Constructor phase: Create API wrappers, bind services
- initCore phase: Setup filesystem, sync time, init APIs, register listeners
- Ready: System is ready to handle messages and events
packages/napcat-core/index.ts:170
Best Practices
Handle async operations properly
Handle async operations properly
NTQQ APIs are async and may timeout. Always use try-catch and timeouts.
Register packet listeners early
Register packet listeners early
Some packets (like database passphrase) are only sent during login. Register handlers before
initCore().Clean up event listeners
Clean up event listeners
Always store and clean up listener removal functions to prevent memory leaks.
Related
Working Environments
Learn about Shell vs Framework modes
OneBot Protocol
Understand the OneBot 11 implementation
Adapters
Configure network adapters
