# koishi-plugin/onebot-manager architecture

> Architecture diagram of the GitHub repository koishi-plugin/onebot-manager, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-11-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/koishi-plugin/onebot-manager
- Repository: https://github.com/koishi-plugin/onebot-manager

## Overview

##### Project Type and Purpose
- This is an open-source Koishi plugin (“koishi-plugin-onebot-manager”) written in TypeScript.
- It plugs into a Koishi bot instance (via the OneBot v11 adapter), listens for group/friend events, applies auto-approval rules or routes events to human operators, and implements a suite of group-management commands.

##### File Structure Analysis
- Top-level files:
- package.json (declares dependencies on koishi, koishi-plugin-adapter-onebot, TypeScript, etc.)
- tsconfig.json (TypeScript compiler options)
- readme.md (detailed feature & configuration docs)
- src/ directory (the entire plugin code):
- index.ts – plugin entrypoint, registers event listeners & commands
- listener.ts – subscribes to OneBot events (join, leave, admin changes, kicks)
- request.ts – processes incoming friend/invite/group-join requests, applies auto-rules, escalates to manual review
- command.ts – defines text commands for manual review (y<n>, na, qgroup …) and group management actions (mute, kick, tag, essence, etc.)
- utils.ts – shared helper functions (regex matching, placeholder formatting, request tracking)

##### Key Architectural Patterns
- Event-driven: the plugin reacts to incoming OneBot events via Koishi’s event bus.
- Command pattern: administrative commands are parsed and dispatched to handlers in command.ts.
- Rule evaluation pipeline in request.ts: automatic vs. manual branching.

##### Main Components
- External Chat Platform (OneBot adapter)
- Koishi Core (event bus & command dispatcher)
- OneBot-Manager Plugin
- Event Listeners (listener.ts)
- Request Processor (request.ts)
- Command Handlers (command.ts)
- Utilities & Config Module (utils.ts + Koishi plugin config)
- Notification Sink (notifyTarget: group or private chat)
- Target Group Environment (actual QQ group where management actions occur)

##### Component Interactions
1. OneBot Adapter emits an event (friend request, group join, invite, admin change, group exit).
2. Koishi Core forwards the event to all registered plugins.
3. OneBot-Manager.listener.ts receives the event.
4. For request events:
– request.ts evaluates auto-approval rules (regex, level, whitelist, group size).
– If auto-approved → plugin calls OneBot API to accept.
– Else → plugin formats a notification and sends it to notifyTarget.
– The pending request is stored in memory (with an ID).
5. For command events (commands prefixed qgroup, y#, n#, ya, na):
– Koishi’s command dispatcher invokes command.ts.
– command.ts identifies the request by ID or executes bulk operations.
– Plugin calls OneBot API for accept/reject or group-management commands.
6. Event notifications (mute, kick, admin set/unset) are similarly listened for in listener.ts and forwarded to notifyTarget.

##### Technologies & Frameworks to Label
- TypeScript & Node.js
- Koishi Core & Command Framework
- koishi-plugin-adapter-onebot (OneBot v11 API)
- In-memory store (for tracking manual-review requests)
- Regular expressions (for auto-approval rules)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Event Source Layer
    subgraph "Event Source Layer"
        Adapter["OneBot Adapter (koishi-plugin-adapter-onebot)"]:::external
    end

    %% Core Layer
    Adapter -->|"FriendRequestEvent\nGroupJoinEvent\nAdminChangeEvent"| Koishi["Koishi Core"]:::processing

    %% Plugin Layer
    subgraph "OneBot-Manager Plugin"
        Index["Plugin Entry Point\nindex.ts"]:::processing
        Listener["Event Listeners\nlistener.ts"]:::processing
        Request["Request Processor\nrequest.ts"]:::processing
        Command["Command Handlers\ncommand.ts"]:::processing
        Utils["Utilities & Config\nutils.ts"]:::config
        InMem["In-Memory Request Store"]:::storage
        Notify["Notification Sink"]:::target
    end

    %% External Targets
    subgraph "External Targets"
        QQGroup["QQ Group Environment"]:::target
        OneBotAPI["OneBot API"]:::external
    end

    %% Deployment Assets
    subgraph "Deployment Assets"
        Package["package.json"]:::config
        Tsconfig["tsconfig.json"]:::config
        Readme["readme.md"]:::config
    end

    %% Component Interactions
    Koishi -->|"registerPlugin"| Index
    Index -->|"subscribeEvents"| Listener
    Listener -->|"emit RequestEvent"| Request
    Request -->|"apply Rules"| Utils
    Utils -->|providesRules| Request
    Request -->|"store PendingRequest"| InMem
    Request -->|"auto-approved"| OneBotAPI
    Request -->|"manualReviewNeeded"| Notify
    Koishi -->|"CommandMessage"| Command
    Command -->|"executeAction"| OneBotAPI
    Listener -->|"adminEvent"| Notify
    Notify -->|"sendNotification"| QQGroup

    %% Styles
    classDef external fill:#ADD8E6,stroke:#333;
    classDef processing fill:#90EE90,stroke:#333;
    classDef target fill:#FFD580,stroke:#333;
    classDef storage fill:#FFB6C1,stroke:#333;
    classDef config fill:#FFFF99,stroke:#333;

    %% Click Events
    click Index "https://github.com/koishi-plugin/onebot-manager/blob/main/src/index.ts"
    click Listener "https://github.com/koishi-plugin/onebot-manager/blob/main/src/listener.ts"
    click Request "https://github.com/koishi-plugin/onebot-manager/blob/main/src/request.ts"
    click Command "https://github.com/koishi-plugin/onebot-manager/blob/main/src/command.ts"
    click Utils "https://github.com/koishi-plugin/onebot-manager/blob/main/src/utils.ts"
    click Package "https://github.com/koishi-plugin/onebot-manager/blob/main/package.json"
    click Tsconfig "https://github.com/koishi-plugin/onebot-manager/blob/main/tsconfig.json"
    click Readme "https://github.com/koishi-plugin/onebot-manager/blob/main/readme.md"
```
