# seriaati/hoyo-buddy architecture

> Architecture diagram of the GitHub repository seriaati/hoyo-buddy, 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/seriaati/hoyo-buddy
- Repository: https://github.com/seriaati/hoyo-buddy

## Overview

##### Project Type and Purpose
- This is a feature-rich Discord bot designed for Hoyoverse gamers. Its purpose is to provide multi-game, multi-account, and multi-language support while generating interactive content (e.g., beautiful character cards) and integrating with multiple external game-data APIs.
- There is also a complementary web component (the “web_app” and “web_server” directories) that suggests a user-facing interface for certain interactions (like login or viewing cards), so the design must account for both bot and web application functionalities.

##### File Structure for Architectural Clues
- Core Application Modules:
- The main codebase resides under the “hoyo_buddy” directory.
- The “bot” folder houses the Discord bot functionality (e.g., command handlers, error handling, cache).
- “cogs” and “commands” directories indicate modular command and feature implementations, likely organized by functionality (admin, characters, leaderboard, etc.).
- Business Logic & Services:
- The “db” folder includes database configurations, models, and utilities indicating a database layer (likely PostgreSQL given the “pgsql.py” file).
- “hoyo/auto_tasks” contains background tasks, such as daily check-ins or code redemption, indicating scheduled or asynchronous components.
- “hoyo/clients” hosts API wrappers for external services (e.g., enka, ambr, hakushin) which means the system readily interacts with various third-party APIs.
- Presentation Layers:
- “ui” directories and subfolders (account, hoyo, paginator) suggest there is an interactive or visual component (possibly Discord user interface elements).
- The existence of a “web_app” implies a separate web server providing pages (login, mobile, error, etc.) to users.
- Cross-Cutting Concerns:
- Internationalization is supported via the “l10n” directory.
- Migrations (under “migrations”) show the relational data structure management.
- Configuration and dependency management are handled via files like “config.py”, “pyproject.toml”, and environment configs.
- CI/CD and code quality checks are automated using GitHub workflows, ruff, and other configuration files.

##### Key Components
a. Discord Bot Layer:
- Bot Application Module (“hoyo_buddy/bot”): Entry point for Discord events, command parsing, error handling, and cache management.
- Commands and Cogs (“hoyo_buddy/commands” and “hoyo_buddy/cogs”): Separate modules that implement specific commands (e.g., login, gacha, leaderboard) and event-driven functionalities.
b. Business Logic & Service Layer:
- Database Access (“hoyo_buddy/db”): Contains models, configuration, and utilities for interaction with the PostgreSQL (or similar) database.
- API Clients (“hoyo_buddy/hoyo/clients”): Abstractions for external API integrations (enka.py, hakushin.py, ambr.py, etc.) for fetching game data.
- Background Tasks (“hoyo_buddy/hoyo/auto_tasks”): Scheduled tasks for automatic operations, such as daily check-ins or notifications.
c. Presentation and Web Layer:
- Web App (“hoyo_buddy/web_app”): Serves webpages for interactions like login, device info, and gacha logs.
- Web Server (“hoyo_buddy/web_server”): Contains HTML templates and the actual server implementation for user-side web interactions.
- UI Modules (“hoyo_buddy/ui”): Components that render Discord interface elements (buttons, modals, views) tailored for the bot’s interactivity.
d. External Integrations:
- External API Wrappers: Represent wrappers like ambr.py, hakushin.py, enka.py, yatta.py, etc., that provide interfaces to various Hoyoverse game data sources.
- Assets (“hoyo-buddy-assets”): A separate repository (or submodule) that holds static images/assets used by the bot and web app.
- Localization (“l10n”): YAML files that support multiple languages, used by various components to cater to a diverse user base.
- Migrations: Represents the schema evolution and versioning of the database.

##### Relationships and Interaction Flow
- Commands/Cogs in the Discord bot layer receive events from Discord, then process them by calling functions in the business logic layer.
- The Bot communicates with the database module for caching user data, tracking state, and retrieving game-related information.
- When commands require visual output, the drawing engine is invoked to generate images (cards, team layouts) which are then sent back through the bot.
- The bot and web application both interact with external API clients to fetch live game data.
- Background auto-tasks run periodically to update statuses or send periodic notifications.
- The web app and web server manage user interactions (e.g., login, viewing results) and may share some business logic with the bot.

##### Architectural Patterns and Design Principles to Highlight
- Modular decomposition: The project’s structure advocates separation of concerns (bot commands, API clients, drawing, database, UI).
- Layered Architecture: Clear separation between Presentation (Discord UI and web app), Business Logic (commands, drawing, API integrations), and Data (database and migrations).
- Extensibility: Adding new features (e.g., new commands or integrations) is streamlined by the established modularity.
- Re-usability: Common functionalities are abstracted into shared modules (e.g., clients, utils) that can be reused across different parts.
- Asynchrony: Likely use of asynchronous programming (especially due to Discord interactions) across command processing and background tasks.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Discord Bot Layer
    subgraph "Discord Bot"
        BotApp["Discord Bot – Bot Application Module"]:::discordStyle
        Commands["Discord Bot – Command Handlers"]:::discordStyle
        Cogs["Discord Bot – Cog Handlers"]:::discordStyle
    end

    %% Business Logic Layer
    subgraph "Business Logic"
        DB["Business Logic – Database Layer"]:::businessStyle
        Drawing["Business Logic – Drawing Engine"]:::businessStyle
        API["Business Logic – API Clients"]:::businessStyle
        AutoTasks["Business Logic – Background Tasks"]:::businessStyle
    end

    %% Presentation Layer
    subgraph "Presentation"
        WebApp["Presentation – Web App"]:::presentationStyle
        WebServer["Presentation – Web Server"]:::presentationStyle
        DiscordUI["Presentation – Discord UI Modules"]:::presentationStyle
    end

    %% External Integration and Data Management
    subgraph "External and Data"
        Localization["External Integration – Localization"]:::externalStyle
        Assets["External Integration – Assets"]:::externalStyle
        Migrations["Data Management – Migrations"]:::dataStyle
    end

    %% Connections within Discord Bot
    BotApp -->|"handles"| Commands
    BotApp -->|"handles"| Cogs

    %% Connections from Discord Bot to Business Logic
    Commands -->|"queries"| DB
    Cogs -->|"queries"| DB
    Commands -->|"generates"| Drawing
    Cogs -->|"generates"| Drawing
    Commands -->|"fetches"| API
    Cogs -->|"fetches"| API

    %% Auto Tasks interaction
    BotApp -->|"schedules"| AutoTasks
    AutoTasks -->|"updates"| DB
    AutoTasks -->|"triggers"| API

    %% Presentation interactions
    WebApp -->|"HTTP_request"| WebServer
    WebServer -->|"serves"| WebApp
    WebServer -->|"fetches"| API
    WebApp -->|"interacts_with"| DiscordUI

    %% External integrations
    API -->|"calls"| Localization
    API -->|"calls"| Assets

    %% Data management connection
    DB -->|"versioned_by"| Migrations
    DiscordUI -->|"displays_to"| BotApp

    %% Click Events for components
    click BotApp "https://github.com/seriaati/hoyo-buddy/blob/main/hoyo_buddy/bot/bot.py"
    click Commands "https://github.com/seriaati/hoyo-buddy/tree/main/hoyo_buddy/commands"
    click Cogs "https://github.com/seriaati/hoyo-buddy/tree/main/hoyo_buddy/cogs"
    click DB "https://github.com/seriaati/hoyo-buddy/tree/main/hoyo_buddy/db"
    click Drawing "https://github.com/seriaati/hoyo-buddy/tree/main/hoyo_buddy/draw"
    click API "https://github.com/seriaati/hoyo-buddy/tree/main/hoyo_buddy/hoyo/clients"
    click AutoTasks "https://github.com/seriaati/hoyo-buddy/tree/main/hoyo_buddy/hoyo/auto_tasks"
    click WebApp "https://github.com/seriaati/hoyo-buddy/tree/main/hoyo_buddy/web_app"
    click WebServer "https://github.com/seriaati/hoyo-buddy/tree/main/hoyo_buddy/web_server"
    click DiscordUI "https://github.com/seriaati/hoyo-buddy/tree/main/hoyo_buddy/ui"
    click Localization "https://github.com/seriaati/hoyo-buddy/tree/main/l10n"
    click Assets "https://github.com/seriaati/hoyo-buddy/tree/main/hoyo-buddy-assets"
    click Migrations "https://github.com/seriaati/hoyo-buddy/tree/main/migrations"

    %% Styles
    classDef discordStyle fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef businessStyle fill:#A9DFBF,stroke:#1D8348,stroke-width:2px;
    classDef presentationStyle fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef externalStyle fill:#F5B7B1,stroke:#C0392B,stroke-width:2px;
    classDef dataStyle fill:#F5CBA7,stroke:#AF601A,stroke-width:2px;
```
