# ligen131/sunbot architecture

> Architecture diagram of the GitHub repository ligen131/sunbot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ligen131/sunbot
- Repository: https://github.com/ligen131/sunbot

## Overview

##### Main Components
- Bot Core: The central entry points are found in files like src/index.ts, src/bot.ts, and the configuration through config/config.json.
- Message Handling & Plugin Manager: The src/Message folder contains the core message handling logic (src/Message/message.ts, send.ts, storage.ts) as well as the plugin infrastructure (src/Message/Plugins with subdirectories for each plugin such as DingDong, repeater, and fromapi). This shows an extensible architecture where commands trigger different responses.
- Plugins: Each plugin (e.g., DingDong, repeater, fromapi) is implemented as a separate module within the Plugin directory, emphasizing a loose coupling between core bot functionality and individual commands/features.
- Database Layer: The src/db folder (with file db.ts) indicates a dedicated module for database interaction. This layer is used by plugins that require persistence, like the repeater.
- Status Page: The src/status directory (including server.ts and index.html) provides a monitoring interface, likely built on Express, running on a configurable port (default http://localhost:4721).
- Utility Modules: The src/utils folder contains common helper functions (logs.ts, common.ts) that support debugging, logging, or other cross-cutting concerns.
- Configuration & Dependencies: The config folder holds JSON configuration files (with an example provided), and the package.json lists dependencies such as Wechaty, Express, and MySQL libraries.

##### Relationships and Data Flow
- A connection from certain plugins (e.g., the repeater) to the Database module, illustrating that these plugins perform database operations.
- Status Page component (Express server) as an independently running service that can be launched alongside the core bot, and indicate its communication channel with the Bot Core for status updates.
- Configuration flow by showing how the Bot Core reads settings from the config file.

##### Architectural Patterns and Design Principles
- Separation of Concerns: Clearly separate business logic (message command handling), plugin functionalities, external communication (status page), and persistence (database).
- Dependency Injection / Loose Coupling: The distinct modules (e.g., plugins, db, and status server) should communicate via well-defined interfaces to allow easy extension or replacement.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Core Components
    subgraph "Core System"
        Config("Configuration"):::core
        BotCore("Bot Core"):::core
    end
    Config -->|"reads"| BotCore

    %% Message Handling & Plugin Manager
    MessageHandler("Message Handler & Plugin Manager"):::core
    BotCore -->|"dispatches commands"| MessageHandler

    %% Plugin Subsystem
    subgraph "Plugins"
        DingDong("DingDong Plugin"):::plugin
        FromApi("fromapi Plugin"):::plugin
        Repeater("repeater Plugin"):::plugin
    end
    MessageHandler -->|"routes to"| DingDong
    MessageHandler -->|"routes to"| FromApi
    MessageHandler -->|"routes to"| Repeater

    %% Database Layer
    Database("Database Layer"):::infrastructure
    Repeater -->|"database query"| Database

    %% Status Page
    StatusPage("Status Page"):::infrastructure
    BotCore -->|"status update"| StatusPage

    %% Utility Modules
    Utils("Utility Modules"):::utility
    BotCore -->|"helper usage"| Utils
    MessageHandler -->|"helper usage"| Utils
    Database -->|"helper usage"| Utils
    StatusPage -->|"helper usage"| Utils

    %% Click Events for Bot Core
    click BotCore "https://github.com/ligen131/sunbot/blob/main/src/index.ts"
    click BotCore "https://github.com/ligen131/sunbot/blob/main/src/bot.ts"
    click Config "https://github.com/ligen131/sunbot/blob/main/config/config-example.json"

    %% Click Events for Message Handling & Plugin Manager
    click MessageHandler "https://github.com/ligen131/sunbot/blob/main/src/Message/message.ts"
    click MessageHandler "https://github.com/ligen131/sunbot/blob/main/src/Message/send.ts"
    click MessageHandler "https://github.com/ligen131/sunbot/blob/main/src/Message/storage.ts"
    click MessageHandler "https://github.com/ligen131/sunbot/blob/main/src/Message/Plugins/Plugins.ts"

    %% Click Events for Plugins
    click DingDong "https://github.com/ligen131/sunbot/blob/main/src/Message/Plugins/DingDong/DingDong.ts"
    click FromApi "https://github.com/ligen131/sunbot/blob/main/src/Message/Plugins/fromapi/fromapi.ts"
    click Repeater "https://github.com/ligen131/sunbot/blob/main/src/Message/Plugins/repeater/repeater.ts"

    %% Click Event for Database Layer
    click Database "https://github.com/ligen131/sunbot/blob/main/src/db/db.ts"

    %% Click Events for Status Page
    click StatusPage "https://github.com/ligen131/sunbot/blob/main/src/status/server.ts"
    click StatusPage "https://github.com/ligen131/sunbot/blob/main/src/status/index.html"

    %% Click Events for Utility Modules
    click Utils "https://github.com/ligen131/sunbot/blob/main/src/utils/common.ts"
    click Utils "https://github.com/ligen131/sunbot/blob/main/src/utils/logs.ts"

    %% Styles
    classDef core fill:#FFD700,stroke:#DAA520,stroke-width:2px;
    classDef plugin fill:#87CEFA,stroke:#4682B4,stroke-width:2px;
    classDef infrastructure fill:#90EE90,stroke:#32CD32,stroke-width:2px;
    classDef utility fill:#FFB6C1,stroke:#FF69B4,stroke-width:2px;
```
