How seriaati/hoyo-buddy works
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.