# aquitcd/live-noti-fire architecture

> Architecture diagram of the GitHub repository aquitcd/live-noti-fire, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aquitcd/live-noti-fire
- Repository: https://github.com/aquitcd/live-noti-fire

## Overview

##### Project Type and Purpose
- This is a single-service backend application built on Deno.
- It serves as a bot that registers slash commands on Discord, links Twitch accounts to Discord servers, and sends notifications when a Twitch stream starts or ends.
- The purpose is clearly detailed in the README: to auto-register bot commands and to handle stream events through defined API endpoints.

##### Analyzing the File Structure
- Top-level files such as .env.example, deno.jsonc, and deno.lock indicate environment configuration and dependency management.
- The “src” directory contains further subdirectories – “controllers”, “repositories”, “services”, “types”, and “utils” – showcasing a layered architecture:
- Controllers: Handle HTTP endpoint requests (for Discord interactions, Twitch webhooks, and debug endpoints).
- Services: Encapsulate business logic specific to Discord functionalities (e.g., sending notifications) and Twitch operations (e.g., subscribing to webhook events).
- Repositories: Likely act as interfaces for data persistence (for storing registration or notification-related data, such as mapping users and stream statuses).
- Types and Utils: Provide type definitions and helper functions such as environment variable management.
- The “scripts” directory (with register-commands.ts) is used for manually registering Discord commands if needed.

##### Components and Their Interactions
a. External Systems:
- Discord API: Handles slash command registration, user interactions, and sending notifications.
- Twitch API: Provides streamer information and sends webhook events (stream online/offline).
b. Application Layers:
- HTTP Controllers:
- Discord Controller: Receives interactions like the /add-streamer and /notify-settings commands, as well as command registration requests.
- Twitch Controller: Handles incoming webhook events (stream start and end) from Twitch.
- Debug Controller: Offers endpoints for examining and managing the KV-store (debug endpoints).
- Service Layer:
- Discord Service: Encapsulates the logic for sending messages and reactions, as well as interfacing with Discord for validations.
- Twitch Service: Handles interactions with Twitch to fetch broadcaster IDs and subscribe to or validate webhook events.
- Repository Layer:
- Guild, User, and Notification Repositories: Manage persistence for storing registrations, messages, and settings tied to Discord guilds and users.
- Utility & Configuration Layers:
- Environment and Type utilities: Load and manage environment variables and ensure type safety.
c. Data Stores / KV Store:
- A KV-store used to persist certain state information (e.g., user registration information, message IDs for notifications).

##### Final Checklist
- Every component is clearly labeled and easily identifiable.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Systems
    subgraph "External Systems"
        DiscordAPI["Discord API"]:::externalStyle
        TwitchAPI["Twitch API"]:::externalStyle
    end

    %% API Controllers
    subgraph "API Controllers"
        DiscordController["Discord Controller"]:::controllerStyle
        TwitchController["Twitch Controller"]:::controllerStyle
        DebugController["Debug Controller"]:::controllerStyle
    end

    %% Service Layer
    subgraph "Service Layer"
        DiscordService["Discord Service"]:::serviceStyle
        TwitchService["Twitch Service"]:::serviceStyle
    end

    %% Repository Layer
    subgraph "Repository Layer"
        GuildRepo["Guild Repository"]:::repositoryStyle
        NotificationRepo["Notification Repository"]:::repositoryStyle
        UserRepo["User Repository"]:::repositoryStyle
        KVStore["KV-Store"]:::repositoryStyle
    end

    %% Utilities & Scripts
    subgraph "Utilities & Scripts"
        EnvExample["Env Example (.env)"]:::utilityStyle
        EnvType["Env Type Definitions"]:::utilityStyle
        EnvUtil["Env Utility Functions"]:::utilityStyle
        CommandRegistration["Command Registration"]:::utilityStyle
    end

    %% Data flow - Registration Flow
    DiscordAPI -->|"slash_command"| DiscordController
    DiscordController -->|"process_command"| DiscordService
    DiscordService -->|"subscribe_event"| TwitchService
    TwitchService -->|"update_data"| GuildRepo
    TwitchService -->|"update_data"| NotificationRepo
    TwitchService -->|"update_data"| UserRepo
    DiscordService -->|"send_notification"| DiscordAPI

    %% Data flow - Webhook Flow
    TwitchAPI -->|"webhook_event"| TwitchController
    TwitchController -->|"handle_webhook"| TwitchService
    TwitchService -->|"lookup_data"| GuildRepo
    TwitchService -->|"lookup_data"| NotificationRepo
    TwitchService -->|"lookup_data"| UserRepo
    TwitchService -->|"send_notification"| DiscordService

    %% Debug Flow
    DebugController -->|"access_debug"| KVStore

    %% Click Events for Controllers
    click DiscordController "https://github.com/aquitcd/live-noti-fire/blob/main/src/controllers/discord.controller.ts"
    click TwitchController "https://github.com/aquitcd/live-noti-fire/blob/main/src/controllers/twitch.controller.ts"
    click DebugController "https://github.com/aquitcd/live-noti-fire/blob/main/src/controllers/debug.controller.ts"

    %% Click Events for Services
    click DiscordService "https://github.com/aquitcd/live-noti-fire/blob/main/src/services/discord.service.ts"
    click TwitchService "https://github.com/aquitcd/live-noti-fire/blob/main/src/services/twitch.service.ts"

    %% Click Events for Repositories
    click GuildRepo "https://github.com/aquitcd/live-noti-fire/blob/main/src/repositories/guild.repository.ts"
    click NotificationRepo "https://github.com/aquitcd/live-noti-fire/blob/main/src/repositories/notification.repository.ts"
    click UserRepo "https://github.com/aquitcd/live-noti-fire/blob/main/src/repositories/user.repository.ts"

    %% Click Events for Utilities & Scripts
    click EnvExample "https://github.com/aquitcd/live-noti-fire/blob/main/.env.example"
    click EnvType "https://github.com/aquitcd/live-noti-fire/blob/main/src/types/env.ts"
    click EnvUtil "https://github.com/aquitcd/live-noti-fire/blob/main/src/utils/env.ts"
    click CommandRegistration "https://github.com/aquitcd/live-noti-fire/blob/main/scripts/register-commands.ts"

    %% Styling Classes
    classDef controllerStyle fill:#f9e79f,stroke:#e67e22,stroke-width:2px;
    classDef serviceStyle fill:#aed6f1,stroke:#2980b9,stroke-width:2px;
    classDef repositoryStyle fill:#abebc6,stroke:#27ae60,stroke-width:2px;
    classDef externalStyle fill:#f5cba7,stroke:#d35400,stroke-width:2px;
    classDef utilityStyle fill:#fadbd8,stroke:#c0392b,stroke-width:2px;
```
