# dysphie/discord-nbot-ts architecture

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

- Interactive diagram: https://gitdiagram.com/dysphie/discord-nbot-ts
- Repository: https://github.com/dysphie/discord-nbot-ts

## Overview

##### Identify the Project Type and Purpose
The NBot project is a **Discord bot** designed to operate within a small guild, providing various interactive features such as emote management, word games, weather updates, and more. Its primary purpose is to enhance user interaction and automate tasks within a Discord server.

**Key Indicators:**
- **Project Description:** The README clearly states that it's a Discord bot with multiple modules.
- **Features:** Multiple slash commands and functionalities like Emoter, Wordle, Weather, etc.
- **Technologies:** Utilizes `discord.js` for Discord interactions, `MongoDB` for data storage, and integrates with several external APIs (e.g., OpenAI, Uberduck).

##### Analyze the File Structure
The file structure reveals a modular architecture with a clear separation of concerns.

- **Top-Level Directories and Files:**
- `.vscode`, `.gitignore`, `Procfile`, configuration files (`.eslintrc.json`, `tsconfig.json`).
- `src/`: Contains all source code.
- `bot.ts`, `config.ts`, `interaction.ts`, `module_mgr.ts`: Core functionalities.
- `modules/`: Each subdirectory represents a distinct module (e.g., `animals.ts`, `emoter.ts`, `weather/`).
- `mongodb.ts`, `register_commands.ts`, `settings.ts`: Support and configuration scripts.
- `package.json` and `package-lock.json`: Manage dependencies.

**Architectural Patterns:**
- **Modular Design:** Each feature is encapsulated within its own module, promoting scalability and maintainability.
- **MVC-Like Separation:** Although not strictly MVC, there is a separation between core bot functionalities and individual modules.

**Configuration and Deployment:**
- **Procfile:** Indicates deployment on platforms like Heroku.
- **Environment Variables:** Managed via `config.ts`, essential for API keys and database connections.

##### Examine the README for Additional Insights
The README provides comprehensive information about the bot's functionalities, requirements, environment variables, and modules. It highlights the use of various external services and APIs, indicating points of integration within the system.

**Key Insights:**
- **Dependencies:** Lists all technologies and APIs powering the bot.
- **Modules:** Detailed explanations of each module, their purposes, and associated commands.
- **Environment Setup:** Instructions for necessary environment variables and optional configurations.

##### a. Identify the Main Components
- **Core Bot Application:**
- **Bot Core (`bot.ts`, `config.ts`, `interaction.ts`):** Manages the bot's lifecycle, event handling, and configuration.
- **Modules (`src/modules/`):** Each module (e.g., Emoter, Wordle, Weather) represents a distinct feature.
- **Database:**
- **MongoDB (`mongodb.ts`):** Stores persistent data required by various modules.
- **External Services:**
- **Discord API (`discord.js`):** Facilitates interaction with Discord servers.
- **External APIs:** OpenCage, TomorrowIO, OpenAI, Uberduck, Craiyon.
- **Configuration and Deployment:**
- **Environment Variables (`config.ts`):** Manages API keys and sensitive information.
- **Deployment Scripts (`Procfile`, `package.json` scripts):** Handle deployment processes.

##### b. Determine Relationships and Interactions
- **Bot Core ↔ Discord API:** The core communicates with Discord using `discord.js` to send and receive events.
- **Bot Core ↔ Modules:** The core manages and delegates tasks to various modules based on user commands.
- **Modules ↔ MongoDB:** Modules interact with MongoDB to store and retrieve data as needed.
- **Modules ↔ External APIs:** Certain modules (e.g., Weather, Auto Completion) interact with external APIs to fetch or process data.
- **Configuration ↔ All Components:** Environment variables and configurations are accessed by the bot core and modules to manage operations.

##### c. Highlight Architectural Patterns and Design Principles
- **Modular Architecture:** Enhances scalability by allowing independent development and testing of modules.
- **Separation of Concerns:** Distinguishes between core functionalities, module-specific logic, and external integrations.
- **Dependency Injection:** Modules receive necessary dependencies (e.g., database connections, API clients) from the core.

##### d. Include Relevant Technologies, Frameworks, and Libraries
- **Programming Language:** TypeScript
- **Framework:** `discord.js` for Discord interactions
- **Database:** MongoDB
- **External APIs:** OpenCage, TomorrowIO, OpenAI, Uberduck, Craiyon
- **Deployment:** Heroku (as indicated by `Procfile`)
- **Development Tools:** ESLint (`.eslintrc.json`), VSCode settings (`.vscode`), TypeScript configuration (`tsconfig.json`)

##### Elements

- **Clear Labels:**
- Data flow and dependencies (e.g., Bot Core → Discord API, Emoter Module ↔ MongoDB).
- Differentiate between types of components:
- **Core Components:** Use rectangles.

##### Deployment Platform: Heroku connected to Bot Core.

**Visual Aids:**

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Core Components
    subgraph "Core Components"
        Bot_Core["Bot Core"]:::core
        Module_Mgr["module_mgr.ts"]:::core
        Interaction["interaction.ts"]:::core
        Config["config.ts"]:::core
        MongoDB_Integration["MongoDB Integration"]:::core
    end

    %% Modules
    subgraph "Modules"
        Emoter["Emoter Module"]:::module
        Wordle["Wordle Module"]:::module
        Weather["Weather Module"]:::module
        OpenAI["OpenAI Module"]:::module
        Uberduck["Uberduck Module"]:::module
    end

    %% Database
    MongoDB["MongoDB"]:::database

    %% External Services
    subgraph "External Services"
        DiscordAPI["Discord API"]:::external
        OpenAIAPI["OpenAI API"]:::external
        UberduckAPI["Uberduck API"]:::external
        CraiyonAPI["Craiyon API"]:::external
        TomorrowIOAPI["TomorrowIO API"]:::external
        OpenCageAPI["OpenCage API"]:::external
    end

    %% Configurations
    subgraph "Configuration"
        RegisterCommands["Command Registration"]:::config
        Settings["Settings Configuration"]:::config
    end

    %% Deployment
    subgraph "Deployment"
        Procfile["Procfile"]:::deployment
        PackageJSON["package.json"]:::deployment
        Heroku["Heroku"]:::deployment
    end

    %% Connections
    Bot_Core -->|"uses"| Module_Mgr
    Bot_Core -->|"uses"| Interaction
    Bot_Core -->|"uses"| Config
    Bot_Core -->|"uses"| DiscordAPI
    Bot_Core -->|"uses"| MongoDB_Integration
    Module_Mgr --> Emoter
    Module_Mgr --> Wordle
    Module_Mgr --> Weather
    Module_Mgr --> OpenAI
    Module_Mgr --> Uberduck
    MongoDB_Integration --> MongoDB
    Emoter -->|"uses"| MongoDB
    Wordle -->|"uses"| MongoDB
    Weather -->|"uses"| MongoDB
    OpenAI -->|"uses"| MongoDB
    Uberduck -->|"uses"| MongoDB
    Weather -->|"calls"| OpenCageAPI
    Weather -->|"calls"| TomorrowIOAPI
    Weather -->|"calls"| CraiyonAPI
    OpenAI -->|"calls"| OpenAIAPI
    Uberduck -->|"calls"| UberduckAPI
    RegisterCommands --> Bot_Core
    Settings --> Bot_Core
    Heroku -->|"deploys"| Bot_Core
    Heroku -->|"uses"| Procfile
    Heroku -->|"uses"| PackageJSON

    %% Click Events
    click Bot_Core "https://github.com/dysphie/discord-nbot-ts/blob/main/src/bot.ts"
    click Module_Mgr "https://github.com/dysphie/discord-nbot-ts/blob/main/src/module_mgr.ts"
    click Interaction "https://github.com/dysphie/discord-nbot-ts/blob/main/src/interaction.ts"
    click Config "https://github.com/dysphie/discord-nbot-ts/blob/main/src/config.ts"
    click MongoDB_Integration "https://github.com/dysphie/discord-nbot-ts/blob/main/src/mongodb.ts"
    click Emoter "https://github.com/dysphie/discord-nbot-ts/blob/main/src/modules/emoter.ts"
    click Wordle "https://github.com/dysphie/discord-nbot-ts/blob/main/src/modules/wordle.ts"
    click Weather "https://github.com/dysphie/discord-nbot-ts/tree/main/src/modules/weather/"
    click OpenAI "https://github.com/dysphie/discord-nbot-ts/blob/main/src/modules/openai.ts"
    click Uberduck "https://github.com/dysphie/discord-nbot-ts/blob/main/src/modules/uberduck.ts"
    click RegisterCommands "https://github.com/dysphie/discord-nbot-ts/blob/main/src/register_commands.ts"
    click Settings "https://github.com/dysphie/discord-nbot-ts/blob/main/src/settings.ts"
    click Procfile "https://github.com/dysphie/discord-nbot-ts/tree/main/Procfile"
    click PackageJSON "https://github.com/dysphie/discord-nbot-ts/blob/main/package.json"

    %% Styles
    classDef core fill:#1f77b4,stroke:#333,stroke-width:1px
    classDef module fill:#2ca02c,stroke:#333,stroke-width:1px
    classDef database fill:#ff7f0e,stroke:#333,stroke-width:1px
    classDef external fill:#9467bd,stroke:#333,stroke-width:1px
    classDef config fill:#ffbb78,stroke:#333,stroke-width:1px
    classDef deployment fill:#d62728,stroke:#333,stroke-width:1px
```
