# shubhamrawat5/whatsapp-bot-ts architecture

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

- Interactive diagram: https://gitdiagram.com/shubhamrawat5/whatsapp-bot-ts
- Repository: https://github.com/shubhamrawat5/whatsapp-bot-ts

## Overview

##### Main Components
- Bot Core & WhatsApp Connection:
– Files like src/bot.ts, src/connectionUpdate.ts, src/groupParticipantsUpdate.ts, and src/messagesUpsert.ts are the entry and event processing points that establish and maintain the connection to WhatsApp.
- Command Handling & Routing:
– The src/commands folder (with subfolders for admins, coc, members, owner, public) shows a modular separation of different types of commands. Each command (e.g., add, kick, mute, vote commands) is implemented as its own TypeScript module.
- Business Logic & Functions:
– src/functions includes various helper functions (e.g., addCommands, todayStats, cricket, forwarding stickers) that encapsulate the bot’s core logic and background tasks.
- Data Persistence & Database Layer:
– The src/db folder encompasses many database-related files. There is a subfolder for Prisma (src/db/prisma) which suggests use of the Prisma ORM to interact with the PostgreSQL database. Separate DB files for things like auth, badges, birthdays, and warnings also exist.
- Utilities & Configuration:
– The src/utils directory contains helper functions (logging, caching, configuration, constants) that support various parts of the project.
- Interfaces & Type Definitions:
– The src/interfaces directory contains TypeScript interfaces for things like Bot, Chats, Coc data, and other message or command structures, ensuring type safety across the application.
- Web/Static Assets:
– Files like index.html and asset/style.css serve as the landing webpage (maybe for documentation or status display) linked from the README.

##### Architectural Relationships & Interactions
- The Bot Core listens for incoming WhatsApp events (connection updates, group participant updates, incoming messages) using the Baileys package.
- Upon receiving a message or event, the bot’s event handlers (e.g., in connectionUpdate.ts and messagesUpsert.ts) trigger command parsing.
- Command modules invoke business logic functions from src/functions to process the command, and often interact with the database via the modules in src/db (or through Prisma in src/db/prisma) to update or retrieve persistent data.
- Responses, notifications (like posting tech news or birthday messages), and sticker operations are then executed by the Bot Core.
- Utilities from src/utils (for logging, caching, or configuration) support error handling, configuration, and runtime optimizations.

##### Key Architectural Patterns & Design Principles
- Modular Design: Separation of concerns is evident by splitting commands, functions, and database access into dedicated directories/modules.
- Layered Architecture: There is a clear division between the core bot logic (communication layer), command processing (application logic), database layer (data persistence), and utility modules.
- Loose Coupling: Interfaces (in src/interfaces) and utility modules help decouple implementations and enable ease of extension.
- Event-Driven Processing: The bot listens for real‐time updates and events from WhatsApp, triggering appropriate command handlers.

## Diagram (Mermaid)

```mermaid
graph TD
  %% External Services
  WhatsAppAPI["WhatsApp API (Baileys)"]:::external
  PostgreSQL["PostgreSQL (Supabase)"]:::external
  
  %% WhatsApp Connector/Bot Core
  subgraph "WhatsApp Connector/Bot Core"
    BotCore["Bot Core (src/bot.ts)"]:::core
    ConnectionUpdate["Connection Update (src/connectionUpdate.ts)"]:::core
    GroupParticipants["GroupParticipants Update (src/groupParticipantsUpdate.ts)"]:::core
    MessagesUpsert["Messages Upsert (src/messagesUpsert.ts)"]:::core
    GroupsUpdate["Groups Update (src/groupsUpdate.ts)"]:::core
  end

  %% Command Handling & Routing
  subgraph "Command Handling & Routing"
    Commands["Commands (src/commands)"]:::command
    AdminCommands["Admin Commands (src/commands/admins)"]:::command
    CocCommands["Coc Commands (src/commands/coc)"]:::command
    MemberCommands["Member Commands (src/commands/members)"]:::command
    OwnerCommands["Owner Commands (src/commands/owner)"]:::command
    PublicCommands["Public Commands (src/commands/public)"]:::command
  end

  %% Business Logic & Functions
  subgraph "Business Logic & Functions"
    AddCommands["AddCommands Function (src/functions/addCommands.ts)"]:::biz
    TodayStats["TodayStats Function (src/functions/todayStats.ts)"]:::biz
    Cricket["Cricket Function (src/functions/cricket.ts)"]:::biz
    ForwardSticker["ForwardSticker Function (src/functions/forwardSticker.ts)"]:::biz
  end

  %% Data Persistence & Database Layer
  subgraph "Data Persistence & Database Layer"
    DBModules["DB Modules (src/db)"]:::db
    PrismaORM["Prisma ORM (src/db/prisma)"]:::db
  end

  %% Utilities & Configuration
  Utilities["Utilities (src/utils)"]:::utils

  %% Interfaces & Type Definitions
  Interfaces["Interfaces (src/interfaces)"]:::interfaces

  %% Web/Static Assets
  subgraph "Web/Static Assets"
    IndexHTML["index.html"]:::web
    StyleCSS["Style.css (asset/style.css)"]:::web
  end

  %% Data Flow Connections
  WhatsAppAPI -->|"IncomingMessage"| BotCore
  BotCore -->|"EventHandling"| ConnectionUpdate
  BotCore -->|"EventHandling"| GroupParticipants
  BotCore -->|"EventHandling"| MessagesUpsert
  BotCore -->|"EventHandling"| GroupsUpdate

  BotCore -->|"CommandExecution"| Commands

  %% Command Routing within Command Handling
  Commands -->|"routesTo"| AdminCommands
  Commands -->|"routesTo"| CocCommands
  Commands -->|"routesTo"| MemberCommands
  Commands -->|"routesTo"| OwnerCommands
  Commands -->|"routesTo"| PublicCommands

  %% Command to Business Logic Interactions
  AdminCommands -->|"invoke"| AddCommands
  OwnerCommands -->|"invoke"| AddCommands
  MemberCommands -->|"invoke"| TodayStats
  PublicCommands -->|"invoke"| ForwardSticker
  CocCommands -->|"invoke"| Cricket

  %% Business Logic to Database interactions
  AddCommands -->|"DBQuery/Update"| DBModules
  TodayStats -->|"DBQuery/Update"| DBModules
  Cricket -->|"DBFetch"| DBModules
  ForwardSticker -->|"DBQuery/Update"| DBModules
  DBModules -->|"DataResponse"| AddCommands

  %% DB Modules to Prisma connection & External DB
  DBModules -->|"UsesORM"| PrismaORM
  DBModules -->|"StoresData"| PostgreSQL
  PostgreSQL -->|"ReturnsData"| DBModules

  %% Business Logic supported by Utilities and Interfaces
  AddCommands -->|"Logging_Config"| Utilities
  TodayStats -->|"Logging_Config"| Utilities
  Cricket -->|"Logging_Config"| Utilities
  ForwardSticker -->|"Logging_Config"| Utilities
  AddCommands -->|"TypeSafe"| Interfaces
  TodayStats -->|"TypeSafe"| Interfaces
  Cricket -->|"TypeSafe"| Interfaces
  ForwardSticker -->|"TypeSafe"| Interfaces

  %% Bot Core delivers notifications via Web Assets
  BotCore -->|"StatusDisplay"| IndexHTML

  %% Click Events
  click BotCore "https://github.com/shubhamrawat5/whatsapp-bot-ts/blob/main/src/bot.ts"
  click ConnectionUpdate "https://github.com/shubhamrawat5/whatsapp-bot-ts/blob/main/src/connectionUpdate.ts"
  click GroupParticipants "https://github.com/shubhamrawat5/whatsapp-bot-ts/blob/main/src/groupParticipantsUpdate.ts"
  click MessagesUpsert "https://github.com/shubhamrawat5/whatsapp-bot-ts/blob/main/src/messagesUpsert.ts"
  click GroupsUpdate "https://github.com/shubhamrawat5/whatsapp-bot-ts/blob/main/src/groupsUpdate.ts"
  click Commands "https://github.com/shubhamrawat5/whatsapp-bot-ts/tree/main/src/commands"
  click AdminCommands "https://github.com/shubhamrawat5/whatsapp-bot-ts/tree/main/src/commands/admins"
  click CocCommands "https://github.com/shubhamrawat5/whatsapp-bot-ts/tree/main/src/commands/coc"
  click MemberCommands "https://github.com/shubhamrawat5/whatsapp-bot-ts/tree/main/src/commands/members"
  click OwnerCommands "https://github.com/shubhamrawat5/whatsapp-bot-ts/tree/main/src/commands/owner"
  click PublicCommands "https://github.com/shubhamrawat5/whatsapp-bot-ts/tree/main/src/commands/public"
  click AddCommands "https://github.com/shubhamrawat5/whatsapp-bot-ts/blob/main/src/functions/addCommands.ts"
  click TodayStats "https://github.com/shubhamrawat5/whatsapp-bot-ts/blob/main/src/functions/todayStats.ts"
  click Cricket "https://github.com/shubhamrawat5/whatsapp-bot-ts/blob/main/src/functions/cricket.ts"
  click ForwardSticker "https://github.com/shubhamrawat5/whatsapp-bot-ts/blob/main/src/functions/forwardSticker.ts"
  click DBModules "https://github.com/shubhamrawat5/whatsapp-bot-ts/tree/main/src/db"
  click PrismaORM "https://github.com/shubhamrawat5/whatsapp-bot-ts/tree/main/src/db/prisma"
  click Utilities "https://github.com/shubhamrawat5/whatsapp-bot-ts/tree/main/src/utils"
  click Interfaces "https://github.com/shubhamrawat5/whatsapp-bot-ts/tree/main/src/interfaces"
  click IndexHTML "https://github.com/shubhamrawat5/whatsapp-bot-ts/blob/main/index.html"
  click StyleCSS "https://github.com/shubhamrawat5/whatsapp-bot-ts/blob/main/asset/style.css"

  %% Styling Classes
  classDef external fill:#ffcccc,stroke:#b30000,stroke-width:2px;
  classDef core fill:#cce5ff,stroke:#004085,stroke-width:2px;
  classDef command fill:#d4edda,stroke:#155724,stroke-width:2px;
  classDef biz fill:#fff3cd,stroke:#856404,stroke-width:2px;
  classDef db fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
  classDef utils fill:#f8d7da,stroke:#721c24,stroke-width:2px;
  classDef interfaces fill:#e2e3e5,stroke:#6c757d,stroke-width:2px;
  classDef web fill:#fefefe,stroke:#6c757d,stroke-width:2px;
```
