How shubhamrawat5/whatsapp-bot-ts works
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.