Files
yt-dlp-bot/app/misc.go
William P a44bb1bf34
All checks were successful
Build only (for PRs) / build-only (pull_request) Successful in 3m33s
optimize codebase
main.go:
- Eliminated ~160 lines of duplicate code: Extracted 3 new helper functions at the bottom of the file:
buildVideoMenuOptions([]VideoOption) — builds the Discord select menu options for video formats
buildAudioMenuOptions([]AudioOption) — same for audio
fetchAndShowFormats(s, i, url) — fetches formats, sorts them, builds menus, stores state, and edits the interaction; previously duplicated identically in both the download and download video command handlers
- Fixed time.Sleep ordering bug: The startAsyncDownload goroutine was launched before InteractionRespond with a 100ms sleep to compensate. Now the download is launched after InteractionRespond returns — no sleep needed. Removed "time" import.
- Used helpers in video_select handler: The two inline menu-building loops in that handler now call buildAudioMenuOptions / buildVideoMenuOptions

misc.go:
- Moved regexp.MustCompile(...) to a package-level var urlPattern — previously it recompiled the regex on every call to extractURLFromString
- Simplified the function body to a single return line
2026-03-09 10:46:10 -04:00

34 lines
659 B
Go

package main
import (
"fmt"
"os"
"regexp"
)
var loading_emoji = os.Getenv("LOADING_EMOJI")
var urlPattern = regexp.MustCompile(`https?://\S+`)
func extractURLFromString(in_url string) string {
return string(urlPattern.Find([]byte(in_url)))
}
// Helper function to create string pointer
func ptr(s string) *string {
return &s
}
func formatBytes(b int) string {
switch {
case b >= 1<<30:
return fmt.Sprintf("%.1f GB", float64(b)/float64(1<<30))
case b >= 1<<20:
return fmt.Sprintf("%.1f MB", float64(b)/float64(1<<20))
case b >= 1<<10:
return fmt.Sprintf("%.1f KB", float64(b)/float64(1<<10))
default:
return fmt.Sprintf("%d B", b)
}
}