# meizubot/bot architecture

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

- Interactive diagram: https://gitdiagram.com/meizubot/bot
- Repository: https://github.com/meizubot/bot

## Overview

##### Main Components

a. Discord Bot Core
- This as a central component
- Connection to Discord API
- Core/ directory components (bot.py, context.py)

b. Extension System
- Extensions into logical categories:
- Core Features (general.py, useful.py)
- Gaming Features (games/osu.py, casino.py)
- Integration Features (twitter.py, api.py)
- Internal Operations (internal/*)
- Interactive Features (giveaways.py, interactions.py)

c. Database Layer
- PostgreSQL database
- Connection to utils/db.py
- Different SQL schemas (from scripts/sql/*)
- Users
- Events
- Stats
- Twitch
- General

d. Web Interface
- FastAPI/Uvicorn server
- IPC connection to bot
- Connection to database

e. Utility Layer
- Utils/ directory components
- Utilities:
- Button system (buttons/)
- Database utilities (db.py)
- Logging system (logging.py)
- Time utilities (time.py)

- Database connections from both bot and web interface
- IPC communication between bot and web server
- Extensions to core bot component
- Utility layer supporting all components

##### Important Details

- Configuration flow (config.yml)
- Logging structure (logs/ directory)
- Extension loading system
- Error handling flow (internal/errorhandler.py)
- Background task system (background.py)

##### Architectural Patterns to Highlight

- Plugin-based architecture (extensions/)
- Event-driven design (Discord bot events)
- Service layer pattern (utils/)
- MVC-like pattern in web interface

##### Data Access Layer (Database & Utils)

- Component types
- Any abbreviations used

Keep the abstraction level focused on the major components while showing the relationship between the bot, web interface, and database, as these are the key architectural elements of the system.

Remember to emphasize the asynchronous nature of the system, particularly in the bot's interaction with Discord's API and the database operations.

## Diagram (Mermaid)

```mermaid
graph TB
    %% External Services Layer
    subgraph External
        Discord[("Discord API")]
        DB[(PostgreSQL Database)]
    end

    %% Interface Layer
    subgraph BotCore["Discord Bot Core"]
        MainBot["Main Bot"]:::core
        BotContext["Bot Context"]:::core
        MainEntry["Main Entry"]:::core
    end

    %% Extension System
    subgraph Extensions["Extension System"]
        subgraph CoreFeatures["Core Features"]
            General["General Commands"]:::extension
            Useful["Utility Commands"]:::extension
        end

        subgraph GamingFeatures["Gaming Features"]
            Osu["OSU Integration"]:::extension
            Casino["Casino System"]:::extension
        end

        subgraph IntegrationFeatures["Integration Features"]
            Twitter["Twitter Module"]:::extension
            ApiExt["API Extension"]:::extension
        end

        subgraph InternalOps["Internal Operations"]
            ErrorHandler["Error Handler"]:::extension
            Help["Help System"]:::extension
            InternalApi["Internal API"]:::extension
        end

        subgraph InteractiveFeatures["Interactive Features"]
            Giveaways["Giveaways"]:::extension
            Interactions["Interactions"]:::extension
        end
    end

    %% Utility Layer
    subgraph Utils["Utility Layer"]
        ButtonSystem["Button System"]:::utility
        DBUtils["Database Utils"]:::utility
        LogSystem["Logging System"]:::utility
        TimeUtils["Time Utils"]:::utility
        FormatUtils["Format Utils"]:::utility
        CheckUtils["Check Utils"]:::utility
    end

    %% Database Layer
    subgraph DBSchemas["Database Schemas"]
        Users["Users Schema"]:::db
        Events["Events Schema"]:::db
        Stats["Stats Schema"]:::db
        Twitch["Twitch Schema"]:::db
        GeneralDB["General Schema"]:::db
    end

    %% Configuration and Background Tasks
    Config["Configuration"]:::config
    Background["Background Tasks"]:::background
    
    %% Relationships
    Discord <--> MainBot
    MainBot --> BotContext
    MainEntry --> MainBot
    MainBot --> Extensions
    Extensions --> Utils
    Utils --> DB
    DB --> DBSchemas
    Config --> MainBot
    Background --> MainBot

    %% Click Events
    click MainBot "https://github.com/meizuBot/bot/blob/main/core/bot.py"
    click BotContext "https://github.com/meizuBot/bot/blob/main/core/context.py"
    click MainEntry "https://github.com/meizuBot/bot/blob/main/main.py"
    click General "https://github.com/meizuBot/bot/blob/main/extensions/general.py"
    click Useful "https://github.com/meizuBot/bot/blob/main/extensions/useful.py"
    click Osu "https://github.com/meizuBot/bot/blob/main/extensions/games/osu.py"
    click Casino "https://github.com/meizuBot/bot/blob/main/extensions/casino.py"
    click Twitter "https://github.com/meizuBot/bot/blob/main/extensions/twitter.py"
    click ApiExt "https://github.com/meizuBot/bot/blob/main/extensions/api.py"
    click ErrorHandler "https://github.com/meizuBot/bot/blob/main/extensions/internal/errorhandler.py"
    click Help "https://github.com/meizuBot/bot/blob/main/extensions/internal/help.py"
    click InternalApi "https://github.com/meizuBot/bot/blob/main/extensions/internal/api.py"
    click Giveaways "https://github.com/meizuBot/bot/blob/main/extensions/giveaways.py"
    click Interactions "https://github.com/meizuBot/bot/blob/main/extensions/interactions.py"
    click DBUtils "https://github.com/meizuBot/bot/blob/main/utils/db.py"
    click Users "https://github.com/meizuBot/bot/blob/main/scripts/sql/users.sql"
    click Events "https://github.com/meizuBot/bot/blob/main/scripts/sql/events.sql"
    click Stats "https://github.com/meizuBot/bot/blob/main/scripts/sql/stats.sql"
    click Twitch "https://github.com/meizuBot/bot/blob/main/scripts/sql/twitch.sql"
    click GeneralDB "https://github.com/meizuBot/bot/blob/main/scripts/sql/general.sql"
    click ButtonSystem "https://github.com/meizuBot/bot/blob/main/utils/buttons/menus.py"
    click LogSystem "https://github.com/meizuBot/bot/blob/main/utils/logging.py"
    click TimeUtils "https://github.com/meizuBot/bot/blob/main/utils/time.py"
    click FormatUtils "https://github.com/meizuBot/bot/blob/main/utils/formats.py"
    click CheckUtils "https://github.com/meizuBot/bot/blob/main/utils/checks.py"
    click Config "https://github.com/meizuBot/bot/blob/main/config.py"
    click Background "https://github.com/meizuBot/bot/blob/main/extensions/background.py"

    %% Styling
    classDef core fill:#2b5797,color:#fff
    classDef extension fill:#f0a500,color:#000
    classDef utility fill:#647687,color:#fff
    classDef db fill:#006400,color:#fff
    classDef config fill:#a4c2f4,color:#000
    classDef background fill:#b45f06,color:#fff

    %% Legend
    subgraph Legend
        CoreL["Core Components"]:::core
        ExtL["Extensions"]:::extension
        UtilL["Utilities"]:::utility
        DBL["Database"]:::db
        ConfigL["Configuration"]:::config
        BGL["Background Tasks"]:::background
    end
```
