# jerichosy/abet-discord-bot architecture

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

- Interactive diagram: https://gitdiagram.com/jerichosy/abet-discord-bot
- Repository: https://github.com/jerichosy/abet-discord-bot

## Overview

##### Project Type and Purpose
- This is a containerized Python Discord-bot application built on discord.py, organized into modular “cogs” for commands and features.
- Primary purpose: respond to Discord events, execute commands in cogs (AI, Fun, Genshin, Math, etc.), persist or retrieve data via a relational database, and integrate with external APIs (AI, exchange rates, etc.).

##### File Structure
Top-level directories and files:
- bot.py – bot entrypoint / Discord client setup.
- cogs/ – modules handling specific command sets (e.g., AI.py, Admin.py, Fun.py).
- cogs/utils/ – shared helper functions (checks, formatters, API wrappers).
- models/ – database engine, ORM setup, schema definitions.
- database/ – migration scripts (V1, V2).
- Dockerfile, docker-compose.yml, .devcontainer/ – containerization & dev-environment config.
- .github/workflows/python-app.yml – CI pipeline.
- scripts/ – helper scripts to attach logs, restart containers.

##### README for Additional Insights

a. Main Components
- External System: Discord Gateway (WebSocket/API)
- Bot Core: bot.py + discord.py Client
- Cog Modules: each file in cogs/ encapsulates a feature set
- Utilities Layer: cogs/utils (rate limiting, formatting, API wrappers)
- Database Layer: models/ (SQLAlchemy engine, schema), database migration scripts
- External Services: AI API provider, Genshin API, Exchange-rate API, random-quote service
- Deployment: Docker container, Docker-Compose orchestration, GitHub Actions CI

b. Relationships & Interactions
1. Discord Gateway → Bot Core: event payloads (message, join/leave, voice)
2. Bot Core → Cog Modules: dispatch events to appropriate command handlers
3. Cog Modules → Utilities: shared logic (checks, sanitization, API wrappers)
4. Cog Modules → Database Layer: read/write operations via ORM
5. Cog Modules → External Services: REST/HTTP calls for AI, exchange rates, etc.
6. Bot Core & Models → Database: connection pooling via SQLAlchemy engine
7. CI (GitHub Actions) → Docker Build/Test → Deployment

c. Architectural Patterns & Principles
- Modular “Cog” pattern (discord.py extension) for separation of concerns
- Layered architecture: Presentation (Discord events), Application (cogs), Domain/Infrastructure (utils, models)
- Infrastructure as Code: Dockerfile, docker-compose, devcontainer.json
- CI/CD pipeline in GitHub Actions

d. Key Technologies & Frameworks
- Python 3.x, discord.py
- SQLAlchemy (models/db.py, engine.py, schema.py)
- Docker, Docker-Compose, DevContainer
- GitHub Actions for CI
- Third-party APIs (OpenAI or other AI, ExchangeRateAPI, Genshin API)

##### Detailed Elements to Capture
- bot.py as the orchestrator/entrypoint
- cogs folder as extension modules—they each register commands/events
- cogs/utils as shared services (e.g., rate limiting, API clients)
- models directory for ORM and migrations
- .devcontainer & Docker assets for local/dev vs. deployed instances
- GitHub Actions workflow triggering tests & Docker builds

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        direction TB
        Discord["Discord Gateway"]:::external
        BotCore["Bot Core\n(bot.py)"]:::presentation
    end

    %% Application Layer
    subgraph "Application/Cogs" 
        direction TB
        AI_Cog["Cog: AI"]:::application
        Admin_Cog["Cog: Admin"]:::application
        Fun_Cog["Cog: Fun"]:::application
        Genshin_Cog["Cog: Genshin"]:::application
        Info_Cog["Cog: Info"]:::application
        Math_Cog["Cog: Math"]:::application
        Quotes_Cog["Cog: Quotes"]:::application
        Roleplay_Cog["Cog: Roleplay"]:::application
        Tools_Cog["Cog: Tools"]:::application
        VoiceListen_Cog["Cog: VoiceListen"]:::application
        Waifu_Cog["Cog: Waifu"]:::application
    end

    %% Utilities / Shared Helpers
    subgraph "Utilities Layer" 
        direction TB
        ExchangeRate["Exchange Rate API Wrapper"]:::infrastructure
        GenshinUtils["Genshin API Utils"]:::infrastructure
        AIResponses["AI Response Templates"]:::infrastructure
        RandomQuotes["Random Quotes Service"]:::infrastructure
        Checks["Rate Limiting / Checks"]:::infrastructure
        ContextExt["Context Extensions"]:::infrastructure
        Formatting["Formatting Helpers"]:::infrastructure
        CharLimits["Character Limit Utilities"]:::infrastructure
        TimeUtils["Time Utilities"]:::infrastructure
        WaifuPics["Waifu Image Service"]:::infrastructure
    end

    %% Database Layer
    subgraph "Database Layer"
        direction TB
        Engine["ORM Setup & Engine"]:::infrastructure
        DBSession["Database Connection & Session"]:::infrastructure
        Schema["Schema Definitions"]:::infrastructure
        MigrV1["Migration V1"]:::infrastructure
        MigrV2["Migration V2"]:::infrastructure
    end

    %% External Services
    subgraph "External Services"
        direction TB
        ExtExchange["Exchange Rate Service"]:::external
        ExtGenshin["Genshin API"]:::external
        ExtAI["AI API"]:::external
        ExtRandom["Random Quote Service"]:::external
    end

    %% Deployment & Environment
    subgraph "Deployment & Environment"
        direction TB
        DockerfileNode["Dockerfile"]:::infrastructure
        DockerCompose["Docker Compose"]:::infrastructure
        DevContainer["Dev Container Config"]:::infrastructure
        DevCompose["Dev Compose"]:::infrastructure
        EnvTemplate["Env Template"]:::infrastructure
    end

    %% CI/CD Pipeline
    subgraph "CI/CD Pipeline"
        direction TB
        CICD["GitHub Actions Workflow"]:::infrastructure
    end

    %% Main Connections
    Discord -->|"WebSocket"| BotCore
    BotCore -->|"dispatch events"| AI_Cog
    BotCore --> Admin_Cog
    BotCore --> Fun_Cog
    BotCore --> Genshin_Cog
    BotCore --> Info_Cog
    BotCore --> Math_Cog
    BotCore --> Quotes_Cog
    BotCore --> Roleplay_Cog
    BotCore --> Tools_Cog
    BotCore --> VoiceListen_Cog
    BotCore --> Waifu_Cog

    %% Cogs interactions
    subgraph "Interactions"
        direction TB
        AI_Cog -->|"uses"| ExchangeRate
        AI_Cog --> GenshinUtils
        AI_Cog --> AIResponses
        AI_Cog --> RandomQuotes
        AI_Cog --> Checks
        AI_Cog --> ContextExt
        AI_Cog --> Formatting
        AI_Cog --> CharLimits
        AI_Cog --> TimeUtils
        AI_Cog --> WaifuPics

        Admin_Cog --> ExchangeRate
        Fun_Cog --> RandomQuotes
        Genshin_Cog --> GenshinUtils
        Info_Cog --> Checks
        Math_Cog --> Formatting
        Quotes_Cog --> DBSession
        Roleplay_Cog --> ContextExt
        Tools_Cog --> TimeUtils
        VoiceListen_Cog --> Checks
        Waifu_Cog --> WaifuPics

        %% Each Cog to DB and External
        AI_Cog -->|"SQLAlchemy"| Engine
        Admin_Cog --> Engine
        Fun_Cog --> Engine
        Genshin_Cog --> Engine
        Info_Cog --> Engine
        Math_Cog --> Engine
        Quotes_Cog --> Engine
        Roleplay_Cog --> Engine
        Tools_Cog --> Engine
        VoiceListen_Cog --> Engine
        Waifu_Cog --> Engine

        AI_Cog --> ExtAI
        Fun_Cog --> ExtRandom
        Genshin_Cog --> ExtGenshin
        AI_Cog --> ExtExchange
    end

    %% Database migrations
    MigrV1 --> MigrV2

    %% CI/CD to Deployment
    CICD -->|"build/test"| DockerfileNode
    DockerfileNode --> DockerCompose
    DockerCompose --> BotCore

    %% Styles
    classDef presentation fill:#CFE2F3,stroke:#0366d6;
    classDef application fill:#D9EAD3,stroke:#38761D;
    classDef infrastructure fill:#FCE5CD,stroke:#F57C00;
    classDef external fill:#EAD1DC,stroke:#A64D79;

    %% Click Events
    click BotCore "https://github.com/jerichosy/abet-discord-bot/blob/main/bot.py"
    click AI_Cog "https://github.com/jerichosy/abet-discord-bot/blob/main/cogs/AI.py"
    click Admin_Cog "https://github.com/jerichosy/abet-discord-bot/blob/main/cogs/Admin.py"
    click Fun_Cog "https://github.com/jerichosy/abet-discord-bot/blob/main/cogs/Fun.py"
    click Genshin_Cog "https://github.com/jerichosy/abet-discord-bot/blob/main/cogs/Genshin.py"
    click Info_Cog "https://github.com/jerichosy/abet-discord-bot/blob/main/cogs/Info.py"
    click Math_Cog "https://github.com/jerichosy/abet-discord-bot/blob/main/cogs/Math.py"
    click Quotes_Cog "https://github.com/jerichosy/abet-discord-bot/blob/main/cogs/Quotes.py"
    click Roleplay_Cog "https://github.com/jerichosy/abet-discord-bot/blob/main/cogs/Roleplay.py"
    click Tools_Cog "https://github.com/jerichosy/abet-discord-bot/blob/main/cogs/Tools.py"
    click VoiceListen_Cog "https://github.com/jerichosy/abet-discord-bot/blob/main/cogs/VoiceListen.py"
    click Waifu_Cog "https://github.com/jerichosy/abet-discord-bot/blob/main/cogs/Waifu.py"
    click ExchangeRate "https://github.com/jerichosy/abet-discord-bot/blob/main/cogs/utils/ExchangeRateUSDPHP.py"
    click GenshinUtils "https://github.com/jerichosy/abet-discord-bot/blob/main/cogs/utils/genshinutils.py"
    click AIResponses "https://github.com/jerichosy/abet-discord-bot/blob/main/cogs/utils/responses_abet.py"
    click RandomQuotes "https://github.com/jerichosy/abet-discord-bot/blob/main/cogs/utils/responses_random.py"
    click Checks "https://github.com/jerichosy/abet-discord-bot/blob/main/cogs/utils/checks.py"
    click ContextExt "https://github.com/jerichosy/abet-discord-bot/blob/main/cogs/utils/context.py"
    click Formatting "https://github.com/jerichosy/abet-discord-bot/blob/main/cogs/utils/format.py"
    click CharLimits "https://github.com/jerichosy/abet-discord-bot/blob/main/cogs/utils/character_limits.py"
    click TimeUtils "https://github.com/jerichosy/abet-discord-bot/blob/main/cogs/utils/timeutils.py"
    click WaifuPics "https://github.com/jerichosy/abet-discord-bot/blob/main/cogs/utils/waifupics.py"
    click Engine "https://github.com/jerichosy/abet-discord-bot/blob/main/models/engine.py"
    click DBSession "https://github.com/jerichosy/abet-discord-bot/blob/main/models/db.py"
    click Schema "https://github.com/jerichosy/abet-discord-bot/blob/main/models/schema.py"
    click MigrV1 "https://github.com/jerichosy/abet-discord-bot/blob/main/database/migrate_V1_quotes.py"
    click MigrV2 "https://github.com/jerichosy/abet-discord-bot/blob/main/database/migrate_V2_quotes.py"
    click DockerfileNode "https://github.com/jerichosy/abet-discord-bot/tree/main/Dockerfile"
    click DockerCompose "https://github.com/jerichosy/abet-discord-bot/blob/main/docker-compose.yml"
    click DevContainer "https://github.com/jerichosy/abet-discord-bot/blob/main/.devcontainer/devcontainer.json"
    click DevCompose "https://github.com/jerichosy/abet-discord-bot/blob/main/.devcontainer/docker-compose.yml"
    click EnvTemplate "https://github.com/jerichosy/abet-discord-bot/blob/main/.env.template"
    click CICD "https://github.com/jerichosy/abet-discord-bot/blob/main/.github/workflows/python-app.yml"
```
