Skip to main content

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

The NapCatCore class is the heart of NapCat, managing all core functionality and API services.
Key responsibilities:
  • Initialize and manage API wrappers
  • Handle event system lifecycle
  • Manage configuration and logging
  • Coordinate packet handling
  • Maintain self information and online status
Source: packages/napcat-core/index.ts:113

InstanceContext

The InstanceContext interface provides runtime dependencies to all components:
This design ensures:
  • Clean dependency injection
  • Consistent access to shared resources
  • Environment-aware behavior
  • Easy testing and mocking
Source: 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
All API wrappers are initialized in the NapCatCore constructor:
Source: packages/napcat-core/index.ts:144

Example: NTQQMsgApi

The Message API provides comprehensive message handling:
Source: 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:
This allows waiting for specific events with conditions:

TypedEventEmitter

Provides type-safe event emission throughout NapCat:
Used for high-level events like:
  • KickedOffLine - Bot was kicked offline
  • PacketReceived - Protocol packet received
  • Custom packet events from services
Source: packages/napcat-core/packet/handler/typeEvent.ts

Kernel Listeners

NapCat registers listeners with NTQQ’s kernel services:
Source: packages/napcat-core/index.ts:225

Packet Handler

The NativePacketHandler 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

Source: packages/napcat-core/packet/handler/client.ts:28

Example Usage

Source: packages/napcat-framework/napcat.ts:68

Service Registry

NapCat uses dependency injection for packet service handlers:
This pattern enables:
  • Automatic service registration
  • Clean separation of concerns
  • Easy testing and mocking
  • Protocol version compatibility
Source: packages/napcat-core/index.ts:158

Initialization Flow

The initialization process:
  1. Constructor phase: Create API wrappers, bind services
  2. initCore phase: Setup filesystem, sync time, init APIs, register listeners
  3. Ready: System is ready to handle messages and events
Source: packages/napcat-core/index.ts:170

Best Practices

Always access logger, session, and paths through context rather than global variables.
NTQQ APIs are async and may timeout. Always use try-catch and timeouts.
Some packets (like database passphrase) are only sent during login. Register handlers before initCore().
Always store and clean up listener removal functions to prevent memory leaks.

Working Environments

Learn about Shell vs Framework modes

OneBot Protocol

Understand the OneBot 11 implementation

Adapters

Configure network adapters