# ifbars/discord-unity-data-communication architecture

> Architecture diagram of the GitHub repository ifbars/discord-unity-data-communication, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ifbars/discord-unity-data-communication
- Repository: https://github.com/ifbars/discord-unity-data-communication

## Overview

##### Project type and purpose
- This is a two-part integration system combining:
a) A Unity-based game client that gathers and sends runtime stats via a Discord webhook
b) A Node.js/discord.js-based Discord Bot that listens for those webhook messages, parses and manages the stats, and provides user-command interactions (linking game IDs, querying/resetting stats).

##### File structure
- Top-level files only:
- UnityScript.cs (the C# component you drop into Unity to send stats)
- DiscordBot.js (the Node.js/discord.js bot implementation)
- README.md (detailed integration guide)
- No separate “src” or “lib” directories: both client and server code live at top level.
- No explicit database/config directories—persistence is implied to be in-memory or file-based for the bot side.

##### Key insights from the README
- Unity side:
– StatsHandler class holds the data model
– DiscordStatsManager component formats stats and POSTs via coroutine to a Discord webhook URL
- Discord Bot side:
– Utilizes discord.js, Node.js runtime
– Listens for incoming webhook messages in a specific channel
– Parses stats payloads, matches gameIDs to Discord users (via a !link command), and supports commands like !stats, !reset
– Bot configuration via botToken, webhookUserID, statsChannelID, yourUserID, and gameIDLength

##### Main components
a. Unity Client (Game)
- StatsHandler (data model)
- DiscordStatsManager (HTTP POST client)
b. Discord Platform (External Service)
- Webhook Endpoint (receives HTTP POST from Unity)
- Stats Channel (message sink for webhook + bot responses)
c. Discord Bot (Node.js/discord.js service)
- Discord API Gateway (WebSocket connection for events)
- Command Processor (handles !link, !stats, !reset)
- Stats Manager (in-memory or file-based storage of stats per gameID + userID)
d. (Optional) Persistence Layer
- Local JSON file or lightweight database (e.g., SQLite) for long-term stat storage and retrieval

##### Relationships and data flow
1) UnityStatsManager → HTTP POST → Discord Webhook Endpoint
2) Discord Webhook Endpoint → posts a message in Stats Channel
3) Discord Bot (via discord.js Gateway) receives new-message event in Stats Channel
4) Bot parses content → hands off to Stats Manager → persists/updates stats
5) User issues commands (e.g., !link gameID) → Bot’s Command Processor → updates link mapping
6) Bot replies in Stats Channel with confirmations or requested stats

##### Architectural patterns and principles
- Event-driven integration: Unity triggers webhook events; Bot reacts to Discord events
- Separation of concerns:
- Unity handles data collection + dispatch
- Discord Bot handles data processing + persistence + user interaction
- Extensibility: Command Processor in Bot can be expanded with new commands (e.g., threshold alerts)
- Configuration-driven endpoints and IDs (webhook URL, tokens, IDs are injected via script variables)

##### Essential elements
- Clear labels on each component (e.g., “DiscordStatsManager”, “Webhook Endpoint”, “Command Processor”)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Actors
    User["User"]:::actor

    %% Unity Layer
    subgraph "Unity Game" 
        direction TB
        SH["StatsHandler"]:::unity
        DStats["DiscordStatsManager"]:::unity
    end

    %% External Discord Platform
    subgraph "Discord Platform" 
        direction TB
        Webhook["Webhook Endpoint"]:::external
        Channel["Stats Channel"]:::external
    end

    %% Bot Service Layer
    subgraph "Discord Bot Service"
        direction TB
        APIGateway["Discord API Gateway"]:::bot
        CmdProc["Command Processor"]:::bot
        StatsMgr["Stats Manager"]:::bot
    end

    %% Persistence Layer
    Persistence["Local JSON / SQLite"]:::persistence

    %% Flows
    SH -->|"collects stats"| DStats
    DStats -->|"POST /webhook"| Webhook
    Webhook -->|"routes message"| Channel
    Channel -->|"new-message event"| APIGateway
    APIGateway -->|"parse payload"| StatsMgr
    StatsMgr -->|"persist stats"| Persistence

    User -->|"!link,!stats,!reset"| CmdProc
    CmdProc -->|"update/link"| StatsMgr
    CmdProc -->|"responds"| Channel

    %% Click Events
    click SH "https://github.com/ifbars/discord-unity-data-communication/blob/main/UnityScript.cs"
    click DStats "https://github.com/ifbars/discord-unity-data-communication/blob/main/UnityScript.cs"
    click APIGateway "https://github.com/ifbars/discord-unity-data-communication/blob/main/DiscordBot.js"
    click CmdProc "https://github.com/ifbars/discord-unity-data-communication/blob/main/DiscordBot.js"
    click StatsMgr "https://github.com/ifbars/discord-unity-data-communication/blob/main/DiscordBot.js"
    click README "https://github.com/ifbars/discord-unity-data-communication/blob/main/README.md"

    %% Styles
    classDef unity fill:#cce5ff,stroke:#004085
    classDef external fill:#ffe5cc,stroke:#b35b00
    classDef bot fill:#d4edda,stroke:#155724
    classDef persistence fill:#e2e3e5,stroke:#6c757d
    classDef actor fill:#f8d7da,stroke:#721c24
```
