3 Commits

Author SHA1 Message Date
ff02993a0a Merge branch 'master' into code-cleanup
All checks were successful
Build only (for PRs) / build-only (pull_request) Successful in 4m52s
2026-03-09 11:05:10 -04:00
52337254b6 bugfix: handle lack of separate audio/video streams
All checks were successful
Build only (for PRs) / build-only (pull_request) Successful in 3m47s
2026-03-09 10:58:56 -04:00
a44bb1bf34 optimize codebase
All checks were successful
Build only (for PRs) / build-only (pull_request) Successful in 3m33s
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
3 changed files with 2 additions and 40 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

View File

@@ -371,10 +371,6 @@ func main() {
}
})
log.Println("Initialize loading emoji")
initLoadingEmoji(s)
log.Println("Adding commands")
registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
for i, v := range commands {

View File

@@ -2,45 +2,11 @@ package main
import (
"fmt"
"log"
"os"
"regexp"
_ "embed"
"encoding/base64"
"github.com/bwmarrin/discordgo"
)
var loading_emoji string
//go:embed assets/loading.webp
var rawLoadingEmoji []byte
var loadingEmojiBase64 = func() string {
s := "data:image/webp;base64," + base64.StdEncoding.EncodeToString(rawLoadingEmoji)
rawLoadingEmoji = nil
return s
}()
func initLoadingEmoji(s *discordgo.Session) {
emojis, err := s.ApplicationEmojis(s.State.User.ID)
if err != nil {
log.Panic("Cannot get emojis")
}
for _, e := range emojis {
if e.Name == "loading" {
loading_emoji = fmt.Sprintf("<a:%s:%s>", e.Name, e.ID)
return
}
}
e, err := s.ApplicationEmojiCreate(s.State.User.ID, &discordgo.EmojiParams{
Name: "loading",
Image: loadingEmojiBase64,
})
if err != nil {
log.Panicf("Cannot create loading emoji: %s", err)
}
loading_emoji = fmt.Sprintf("<a:%s:%s>", e.Name, e.ID)
}
var loading_emoji = os.Getenv("LOADING_EMOJI")
var urlPattern = regexp.MustCompile(`https?://\S+`)