# hyer0705/study-assistant architecture

> Architecture diagram of the GitHub repository hyer0705/study-assistant, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hyer0705/study-assistant
- Repository: https://github.com/hyer0705/study-assistant

## Overview

##### Project Type and Purpose
- This is a specialized bot application built on top of Discord.js.
- Its sole purpose is to automatically post next-week’s algorithm study problems (and presenters) into a Discord server.
- It uses Google authentication to fetch data (presumably from a Google Sheet or API) and exposes a minimal health‐check endpoint for uptime monitoring.

##### File-Structure Analysis
- Top level
- package.json, bun.lock, tsconfig.json → TypeScript project managed by Bun
- Dockerfile, .dockerignore → containerized deployment
- README.md → high-level description, usage
- src/ folder (all TypeScript source)
- auth/ → GoogleAuth integration (OAuth credentials, token handling)
- commands/ → slash-command definitions and implementations
– index.ts → loader for commands
– notify/problem.ts → “/문제공지” command logic
- events/ → event handlers for Discord.js
– ready.ts → on bot login
– interactionCreate.ts → on slash-command invocation
- deploy-command.ts → script to register slash commands with Discord API
- healthCheck.ts → simple HTTP server for liveness probes
- index.ts → entry point: initialize client, wire modules, start health-check
- types/ → shared TypeScript interfaces (Command, ExtendedClient, GoogleAuth)

##### Key Components
a. Bot Application (Node.js process running on Bun)
- Entry Module (index.ts)
- Command Registrar (deploy-command.ts)
- Health-Check Server (healthCheck.ts)
b. Discord Gateway (Discord API)
- Events channel (ready, interactionCreate)
- Slash-Command registration endpoint
c. Commands Subsystem
- Command Loader (commands/index.ts)
- Notify/Problem Command (commands/notify/problem.ts)
d. Events Subsystem
- Ready Handler
- Interaction Handler
e. Google Authentication Service
- OAuth flow modules in auth/google-auth.ts
- External Google APIs (Sheets, Drive, etc.)
f. Types Module (TypeScript interfaces, shared contracts)
g. Deployment Target
- Docker container
- Bun runtime

##### Relationships and Data Flows
- index.ts → initializes Discord client and HTTP server
- deploy-command.ts → on startup, registers slash commands with Discord
- Discord Gateway → emits “ready” and “interactionCreate” events → caught by events subsystem
- interactionCreate → routes to command handler via commands/index.ts → notify/problem → calls GoogleAuth → fetches next-week problems → returns data
- notify/problem → sends message payload back to Discord through the client
- healthCheck.ts → listens on a port; external load-balancer or k8s probes hit this endpoint

##### Architectural Patterns & Principles
- Modular separation (commands vs. events vs. auth vs. types)
- Single Responsibility Principle: each file/folder has one concern
- Asynchronous event-driven flow (Discord.js events driving business logic)
- Minimal external dependencies (only Docker, Bun, Discord.js, Google API)

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Deployment"
        Docker["Docker Container<br/>Bun Runtime"]:::infra
    end

    subgraph "Bot Application (src)"
        Index["index.ts"]:::entry
        Deploy["deploy-command.ts"]:::infra
        Health["healthCheck.ts"]:::infra
    end

    subgraph "Commands Subsystem"
        CmdLoader["commands/index.ts"]:::biz
        Notify["commands/notify/problem.ts"]:::biz
    end

    subgraph "Events Subsystem"
        EventsLoader["events/index.ts"]:::biz
        Ready["events/ready.ts"]:::biz
        Interaction["events/interactionCreate.ts"]:::biz
    end

    subgraph "Auth Subsystem"
        AuthIndex["auth/index.ts"]:::biz
        GoogleAuth["auth/google-auth.ts"]:::biz
    end

    subgraph "Types Module"
        TypesIndex["types/index.ts"]:::biz
        CmdType["types/Command.ts"]:::biz
        ExtClient["types/ExtendedClient.ts"]:::biz
        AuthType["types/GoogleAuth.ts"]:::biz
    end

    Discord[("Discord API")]:::external
    Google[("Google API")]:::external
    Monitoring[("LoadBalancer / K8s Probe")]:::external

    Docker -->|runs| Index
    Docker -->|runs| Deploy
    Docker -->|runs| Health

    Index -->|startup| Deploy
    Index -->|start server| Health
    Index -->|load handlers| EventsLoader
    Index -->|init client\nsubscribe events| Discord

    Deploy -->|"POST /commands"| Discord

    Discord -->|EVENT: ready| Ready
    Discord -->|EVENT: interactionCreate| Interaction

    Ready -->|handle ready| EventsLoader
    Interaction -->|route event| EventsLoader

    EventsLoader -->|on ready| Ready
    EventsLoader -->|on interactionCreate| Interaction

    Interaction -->|invoke| CmdLoader
    CmdLoader -->|execute| Notify

    Notify -->|OAuth token fetch| GoogleAuth
    GoogleAuth -->|"GET sheet data"| Google
    Google -->|data response| GoogleAuth
    GoogleAuth -->|return problems| Notify

    Notify -->|send message| Discord

    Health -->|"GET /health"| Monitoring

    %% Imports to types
    Index --> TypesIndex
    Deploy --> TypesIndex
    Health --> TypesIndex
    EventsLoader --> TypesIndex
    CmdLoader --> TypesIndex
    AuthIndex --> TypesIndex
    GoogleAuth --> TypesIndex

    TypesIndex --> CmdType
    TypesIndex --> ExtClient
    TypesIndex --> AuthType

    classDef entry fill:#ccf,stroke:#339;
    classDef biz fill:#cfc,stroke:#393;
    classDef infra fill:#eee,stroke:#999;
    classDef external fill:#fcf,stroke:#933,stroke-dasharray: 5 5;

    click Index "https://github.com/hyer0705/study-assistant/blob/master/src/index.ts"
    click Deploy "https://github.com/hyer0705/study-assistant/blob/master/src/deploy-command.ts"
    click Health "https://github.com/hyer0705/study-assistant/blob/master/src/healthCheck.ts"
    click EventsLoader "https://github.com/hyer0705/study-assistant/blob/master/src/events/index.ts"
    click Ready "https://github.com/hyer0705/study-assistant/blob/master/src/events/ready.ts"
    click Interaction "https://github.com/hyer0705/study-assistant/blob/master/src/events/interactionCreate.ts"
    click CmdLoader "https://github.com/hyer0705/study-assistant/blob/master/src/commands/index.ts"
    click Notify "https://github.com/hyer0705/study-assistant/blob/master/src/commands/notify/problem.ts"
    click GoogleAuth "https://github.com/hyer0705/study-assistant/blob/master/src/auth/google-auth.ts"
    click AuthIndex "https://github.com/hyer0705/study-assistant/blob/master/src/auth/index.ts"
    click TypesIndex "https://github.com/hyer0705/study-assistant/blob/master/src/types/index.ts"
    click CmdType "https://github.com/hyer0705/study-assistant/blob/master/src/types/Command.ts"
    click ExtClient "https://github.com/hyer0705/study-assistant/blob/master/src/types/ExtendedClient.ts"
    click AuthType "https://github.com/hyer0705/study-assistant/blob/master/src/types/GoogleAuth.ts"
    click Docker "https://github.com/hyer0705/study-assistant/tree/master/Dockerfile"
    click Docker "https://github.com/hyer0705/study-assistant/blob/master/.dockerignore"
```
