feat: add ;man command#29
Conversation
Summary by BeetleThis PR introduces two key improvements to the Discord bot: code refactoring for better maintainability and a new documentation feature. The first commit centralizes the changelog parsing logic by extracting duplicate code into a shared utility module, while also fixing a formatting issue with double newlines. The second commit adds a new 📁 File Changes Summary (Consolidated across all commits):
Total Changes: 5 files changed, +185 additions, -69 deletions 🗺️ Walkthrough:sequenceDiagram
participant User
participant Discord
participant ManCommand as "man.ts Command"
participant ManUtil as "man() Utility"
participant ArchLinux as "man.archlinux.org"
User->>Discord: ;man ls
Discord->>ManCommand: Execute command
ManCommand->>ManUtil: man("ls")
ManUtil->>ManUtil: Validate term (alphanumeric check)
ManUtil->>ArchLinux: GET /man/ls.txt
ArchLinux-->>ManUtil: Raw man page text
ManUtil->>ManUtil: Parse sections (NAME, SYNOPSIS, etc.)
ManUtil->>ManUtil: Extract title & description
ManUtil-->>ManCommand: Return ManPage object
ManCommand->>ManCommand: Build Discord Embed
ManCommand->>ManCommand: Create "Read Full" button
ManCommand-->>Discord: Reply with embed + button
Discord-->>User: Display formatted man page
Note over User,ArchLinux: User can click button to view full documentation
🎯 Key Changes:
📊 Impact Assessment:
⚙️ SettingsSeverity Threshold: 📖 User Guide
|
|
Note Linting checks passed successfully 🎉 All formatting and code quality checks are clean. You're good to merge 🚀 |
|
|
||
| const url = `https://man.archlinux.org/man/${encodeURIComponent(term)}.txt`; | ||
|
|
||
| const res = await fetch(url); |
There was a problem hiding this comment.
The fetch call has no timeout or error handling, which can cause the Discord bot to hang indefinitely if the archlinux.org server is slow or unresponsive. This could lead to resource exhaustion and bot unresponsiveness.
Confidence: 5/5
Suggested Fix
| const res = await fetch(url); | |
| const controller = new AbortController(); | |
| const timeoutId = setTimeout(() => controller.abort(), 5000); | |
| try { | |
| const res = await fetch(url, { signal: controller.signal }); | |
| clearTimeout(timeoutId); | |
| if (!res.ok) return null; | |
| } catch (error) { | |
| clearTimeout(timeoutId); | |
| if (error.name === 'AbortError') { | |
| throw new Error("Man page request timed out."); | |
| } | |
| throw new Error("Failed to fetch man page."); | |
| } | |
Add a 5-second timeout using AbortController to prevent indefinite hangs. Wrap the fetch in try-catch to handle network errors gracefully. This prevents the bot from becoming unresponsive when the external API is slow or down.
Prompt for AI
Copy this prompt to your AI IDE to fix this issue locally:
In src/utils/man.ts around line 17, the fetch call has no timeout or error handling which can cause the Discord bot to hang indefinitely if archlinux.org is slow or unresponsive; add an AbortController with a 5-second timeout and wrap the fetch in a try-catch block to handle network errors and timeouts gracefully, throwing appropriate error messages for timeout and fetch failures.
No description provided.