# xynnpg/discordai architecture

> Architecture diagram of the GitHub repository xynnpg/discordai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/xynnpg/discordai
- Repository: https://github.com/xynnpg/discordai

## Overview

This is a Discord AI chat bot with a companion Flask administration interface. Discord users select a model, send questions, and can inspect or clear per-user conversation memory; the bot uses stored preferences and history to provide context to the AI. Administrators configure the service and manage models through the web interface. The README documents these capabilities, while some command and route implementations are omitted from the excerpts, so uncertain wiring is not shown.

Main entry points: [`database.py`](https://github.com/xynnpg/discordai/blob/main/database.py) (Application database), [`discord_bot.py`](https://github.com/xynnpg/discordai/blob/main/discord_bot.py) (Bot commands), [`flask_web.py`](https://github.com/xynnpg/discordai/blob/main/flask_web.py) (Web routes), [`openrouter_client.py`](https://github.com/xynnpg/discordai/blob/main/openrouter_client.py) (OpenRouter client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_discord["Discord chat"]
  node_bot["Bot commands<br/>[discord_bot.py]"]
  node_preferences["Model preferences<br/>[discord_bot.py]"]
  node_chat_memory["Chat memory<br/>[discord_bot.py]"]
end

subgraph group_web["Web administration"]
  node_web_app["Web routes<br/>[flask_web.py]"]
  node_web_ui["Admin pages"]
  node_setup_config["Setup settings<br/>[setup_config.json]"]
end

subgraph group_ai["AI integration"]
  node_client["OpenRouter client"]
end

subgraph group_state["Persistent state"]
  node_database[("Application database<br/>[database.py]")]
  node_model_records["AI model records<br/>[database.py]"]
  node_user_records["User preferences<br/>[database.py]"]
  node_history_records["Conversation history<br/>[database.py]"]
end

node_discord_user(("Discord user"))
node_discord_platform(("Discord"))
node_admin(("Administrator"))
node_openrouter(("OpenRouter API"))

node_discord_user -->|"uses commands"| node_discord_platform
node_discord_platform -->|"dispatches commands"| node_bot
node_bot -->|"reads and updates"| node_preferences
node_bot -->|"reads and clears"| node_chat_memory
node_preferences -->|"reads and writes"| node_database
node_chat_memory -->|"reads and writes"| node_database
node_bot -->|"requests AI response"| node_client
node_client -->|"sends API requests"| node_openrouter
node_openrouter -->|"returns completions"| node_client
node_admin -->|"manages settings"| node_web_ui
node_web_ui -->|"submits requests"| node_web_app
node_web_app -->|"reads and writes"| node_setup_config
node_web_app -->|"manages models"| node_database
node_web_app -->|"tests models"| node_client
node_database -->|"stores"| node_model_records
node_database -->|"stores"| node_user_records
node_database -->|"stores"| node_history_records

click node_bot "https://github.com/xynnpg/discordai/blob/main/discord_bot.py"
click node_preferences "https://github.com/xynnpg/discordai/blob/main/discord_bot.py"
click node_chat_memory "https://github.com/xynnpg/discordai/blob/main/discord_bot.py"
click node_web_app "https://github.com/xynnpg/discordai/blob/main/flask_web.py"
click node_web_ui "https://github.com/xynnpg/discordai/tree/main/templates"
click node_setup_config "https://github.com/xynnpg/discordai/blob/main/setup_config.json"
click node_client "https://github.com/xynnpg/discordai/blob/main/openrouter_client.py"
click node_database "https://github.com/xynnpg/discordai/blob/main/database.py"
click node_model_records "https://github.com/xynnpg/discordai/blob/main/database.py"
click node_user_records "https://github.com/xynnpg/discordai/blob/main/database.py"
click node_history_records "https://github.com/xynnpg/discordai/blob/main/database.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_bot,node_preferences,node_chat_memory,node_discord_user toneBlue
class node_web_app,node_web_ui,node_setup_config toneAmber
class node_client,node_openrouter toneMint
class node_database,node_model_records,node_user_records,node_history_records toneRose
class node_discord_platform,node_admin toneIndigo
```

## Components

### Discord chat

- [Bot commands](https://github.com/xynnpg/discordai/blob/main/discord_bot.py): `discord_bot.py`
- [Model preferences](https://github.com/xynnpg/discordai/blob/main/discord_bot.py): `discord_bot.py`
- [Chat memory](https://github.com/xynnpg/discordai/blob/main/discord_bot.py): `discord_bot.py`

### Web administration

- [Web routes](https://github.com/xynnpg/discordai/blob/main/flask_web.py): `flask_web.py`
- [Admin pages](https://github.com/xynnpg/discordai/tree/main/templates): `templates`
- [Setup settings](https://github.com/xynnpg/discordai/blob/main/setup_config.json): `setup_config.json`

### AI integration

- [OpenRouter client](https://github.com/xynnpg/discordai/blob/main/openrouter_client.py): `openrouter_client.py`

### Persistent state

- [Application database](https://github.com/xynnpg/discordai/blob/main/database.py): `database.py`
- [AI model records](https://github.com/xynnpg/discordai/blob/main/database.py): `database.py`
- [User preferences](https://github.com/xynnpg/discordai/blob/main/database.py): `database.py`
- [Conversation history](https://github.com/xynnpg/discordai/blob/main/database.py): `database.py`

### Other components

- **Discord user**
- **Discord**
- **Administrator**
- **OpenRouter API**

## Connections

- Discord user → Discord: uses commands
- Discord → Bot commands: dispatches commands
- Bot commands → Model preferences: reads and updates
- Bot commands → Chat memory: reads and clears
- Model preferences → Application database: reads and writes
- Chat memory → Application database: reads and writes
- Bot commands → OpenRouter client: requests AI response
- OpenRouter client → OpenRouter API: sends API requests
- OpenRouter API → OpenRouter client: returns completions
- Administrator → Admin pages: manages settings
- Admin pages → Web routes: submits requests
- Web routes → Setup settings: reads and writes
- Web routes → Application database: manages models
- Web routes → OpenRouter client: tests models
- Application database → AI model records: stores
- Application database → User preferences: stores
- Application database → Conversation history: stores
