How ifbars/discord-unity-data-communication works
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”)