How decryptu/decryptgpt works
<explanation>
1. Project Type & Purpose
- DecryptGPT is a modular, back-end Discord bot built on Node.js.
- It uses Discord.js to connect to the Discord Gateway and OpenAI’s API suite (GPT-4o/4.1/o3, Whisper, GPT-Image) for chat, transcription, image generation/editing, and document processing.
- It is an open-source tool designed for medical-focused, French-language academic assistance in Discord.
2. Key Architectural Components
A. Discord Gateway Client
- index.js: Bootstraps the bot, loads environment/config, logs in to Discord via Discord.js (WebSocket).
B. Event Handling Layer
- events/*.js: Listeners for ‘ready’, ‘interactionCreate’, ‘messageCreate’.
- Routes incoming Gateway events to command handlers or message responders.
C. Command Handling Layer
- commands/*.js: Implements slash commands (/gpt-mode, /gpt-model, /image-gpt, /image-edit).
- Registered at startup (scripts/delete-commands.js can reset registration).
D. Business Logic / Utility Layer
- utils/*.js: Helpers for building conversation logs, splitting messages, error handling, transcribing voice (Whisper), generating/editing images, processing documents (PDF, CSV, Excel), reacting with emojis, etc.
E. External API Clients
- OpenAI REST calls: ChatCompletions, ImageGeneration, ImageEditing, Whisper
F. Configuration & Scripts
- .env / config.example.js / channels.mjs: Bot credentials, channel IDs, default settings (model, token limits).
- scripts/delete-commands.js: CLI for maintenance.
3. Interactions & Data Flow
1. Bot startup (index.js)
• Load .env & config, register slash commands via Discord REST.
• Establish WebSocket to Discord Gateway.
2. User Interaction
• User triggers a slash command or sends a message/voice.
• Discord Gateway pushes an event → event handler → command or message responder.
3. Command Execution
• Command handler invokes relevant util:
- For chat: buildConversationLog → call OpenAI Chat API → respond.
- For voice: transcribeVoiceMessage (Whisper) → process as text.
- For image gen/edit: createGptImage / editImage util → call GPT-Image API → upload back to Discord.
- For document: extractFileContent → analyze via Chat API.
4. Bot Response
• Process API response → utils/splitMessage if needed → Discord.js reply/edit.
5. Error Handling
• Centralized via utils/handleApiErrors & handleInteractionError → logs and safe user feedback.
4. Architectural Patterns & Principles
- Event-Driven: Decoupled event listeners and handlers.
- Modular: commands and events live in their own folders—easy to add or remove features.
- Single Responsibility: Each util does one task (transcription, image creation, file extraction).
- Configuration-Driven: Behavior controlled by config.js and environment variables—facilitates different deployments (dev vs prod).
- Extensible CLI Scripts: scripts/ folder for administrative tasks.
5. Tailoring the Diagram (Open-Source Backend Tool)
- Focus on core modules (Gateway, Events, Commands, Utils, API Clients).
- Show how new “commands” plug into the event layer.
- Highlight integration points with external services (Discord API, OpenAI API).
- Emphasize extension points (adding new commands or utils modules).
6. Diagramming Guidelines
- Components as boxes:
• Internal (Bot Core): Discord Client, Event Processor, Command Processor, Utils modules
• External: Discord Gateway (WebSocket), Discord REST API, OpenAI REST API
- Arrows:
• WebSocket connection → Gateway → Event Processor
• Event Processor → Command Processor
• Command Processor → Utils → OpenAI API
• OpenAI API → Bot → Discord REST (to send messages)
- Color-coding:
• Blue = internal components
• Green = external services
• Grey = configuration/scripts
- Shape conventions:
• Rectangles = processes/modules
• Cylinders = data stores (if any—e.g., if you later add a DB for usage stats)
• Cloud icons = external APIs
- Labels:
• Protocols (WebSocket, HTTPS) on arrows
• Callouts for major flows (Chat, Voice, Image, Document)
7. Final Tips
- Be exhaustive: list each folder as a distinct module in your diagram.
- Capture error-handling flows (dotted arrows) back to the user.
- Show the startup sequence (registration of slash commands) as an initialization subflow.
- Use a legend for your color and shape conventions.
- Keep it readable: group related utils under one “Utility Layer” box but list key functions inside.