From 1ccc91e12cc11c65abe06b5824da2b4d15f4034c Mon Sep 17 00:00:00 2001 From: Innokentiy Sokolov Date: Fri, 15 Mar 2024 00:10:28 +0300 Subject: [PATCH] fix: stream playback from history, nil error in auto reconnect fn --- mod-music/discord/play.go | 25 +++++++++++++++++++++++-- mod-music/player/play.go | 4 +++- mod-music/player/unpause.go | 8 ++++++-- mod-music/sources/youtube.go | 17 +++++++++++------ 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/mod-music/discord/play.go b/mod-music/discord/play.go index fc2b35c..8065190 100644 --- a/mod-music/discord/play.go +++ b/mod-music/discord/play.go @@ -12,6 +12,7 @@ import ( "github.com/bwmarrin/discordgo" "github.com/gookit/slog" "github.com/keshon/melodix-player/internal/version" + "github.com/keshon/melodix-player/mod-music/history" "github.com/keshon/melodix-player/mod-music/player" "github.com/keshon/melodix-player/mod-music/sources" ) @@ -126,11 +127,31 @@ func fetchSongsToList(originType string, songsOrigins []string, d *Discord, m *d slog.Error("Cannot convert string id to int id") continue } - songs, err = youtube.FetchSongsByIDs(m.GuildID, []int{id}) + + h := history.NewHistory() + track, err := h.GetTrackFromHistory(m.GuildID, uint(id)) if err != nil { - slog.Warnf("Error fetching songs by history ID: %v", err) + slog.Error("Error getting track from history with ID %v", id) continue } + + var song []*player.Song + if youtube.IsYouTubeURL(track.URL) { + slog.Info("Track is from YouTube") + song, err = youtube.GetAllSongsFromURL(track.URL) + if err != nil { + slog.Error("error fetching new songs from URL: %v", err) + continue + } + } else { + slog.Info("Track is from Stream") + song, err = stream.FetchStreamsByURLs([]string{track.URL}) + if err != nil { + slog.Error("error fetching new songs from URL: %v", err) + continue + } + } + songs = append(songs, song...) case "youtube_title": songs, err = youtube.FetchSongsByTitle(songOrigin) if err != nil { diff --git a/mod-music/player/play.go b/mod-music/player/play.go index f1b07c2..06c731d 100644 --- a/mod-music/player/play.go +++ b/mod-music/player/play.go @@ -330,7 +330,9 @@ func (p *Player) setupVoiceConnection() (*discordgo.VoiceConnection, error) { if attempts > 0 { slog.Warn("Failed to join voice channel after multiple attempts, attempting to disconnect and reconnect next iteration") - voiceConnection.Disconnect() + if voiceConnection != nil { + voiceConnection.Disconnect() + } } slog.Warnf("Failed to join voice channel (attempt %d): %v", attempts+1, err) diff --git a/mod-music/player/unpause.go b/mod-music/player/unpause.go index e90ffd3..a3e530d 100644 --- a/mod-music/player/unpause.go +++ b/mod-music/player/unpause.go @@ -9,8 +9,12 @@ import ( func (p *Player) Unpause(channelID string) error { slog.Info("Resuming playback") - if p.GetCurrentStatus() == StatusPlaying || p.GetCurrentStatus() == StatusError { - return fmt.Errorf("the track is already playing (or error) %v", p.GetCurrentStatus().String()) + if p.GetCurrentStatus() == StatusError { + return fmt.Errorf("error playing the track %v", p.GetCurrentStatus().String()) + } + + if p.GetCurrentStatus() == StatusPlaying { + return fmt.Errorf("the track is already playing (use 'add' command instead) %v", p.GetCurrentStatus().String()) } // Set new channel if needed diff --git a/mod-music/sources/youtube.go b/mod-music/sources/youtube.go index 46db121..1ed0a1f 100644 --- a/mod-music/sources/youtube.go +++ b/mod-music/sources/youtube.go @@ -51,7 +51,7 @@ func (y *Youtube) GetSongFromVideoURL(url string) (*player.Song, error) { } // getAllSongsFromURL creates an array of Song instances from a YouTube playlist. -func (y *Youtube) getAllSongsFromURL(url string) ([]*player.Song, error) { +func (y *Youtube) GetAllSongsFromURL(url string) ([]*player.Song, error) { var songs []*player.Song if strings.Contains(url, "list=") { @@ -173,7 +173,7 @@ func (y *Youtube) FetchSongsByIDs(guildID string, ids []int) ([]*player.Song, er return nil, fmt.Errorf("error getting track from history with ID %v", id) } - song, err := y.getAllSongsFromURL(track.URL) + song, err := y.GetAllSongsFromURL(track.URL) if err != nil { return nil, fmt.Errorf("error fetching new songs from URL: %v", err) } @@ -194,7 +194,7 @@ func (y *Youtube) FetchSongsByTitles(titles []string) ([]*player.Song, error) { return nil, fmt.Errorf("error getting YouTube video URL by title: %v", err) } - songs, err = y.getAllSongsFromURL(url) + songs, err = y.GetAllSongsFromURL(url) if err != nil { return nil, fmt.Errorf("error fetching new songs from URL: %v", err) } @@ -212,7 +212,7 @@ func (y *Youtube) FetchSongsByTitle(title string) ([]*player.Song, error) { return nil, fmt.Errorf("error getting YouTube video URL by title: %v", err) } - songs, err = y.getAllSongsFromURL(url) + songs, err = y.GetAllSongsFromURL(url) if err != nil { return nil, fmt.Errorf("error fetching new songs from URL: %v", err) } @@ -225,7 +225,7 @@ func (y *Youtube) FetchSongsByURLs(urls []string) ([]*player.Song, error) { var songs []*player.Song for _, url := range urls { - song, err := y.getAllSongsFromURL(url) + song, err := y.GetAllSongsFromURL(url) if err != nil { return nil, fmt.Errorf("error fetching new songs from URL: %v", err) } @@ -240,7 +240,7 @@ func (y *Youtube) FetchSongsByURLs(urls []string) ([]*player.Song, error) { func (y *Youtube) FetchSongByURLs(url string) ([]*player.Song, error) { var songs []*player.Song - song, err := y.getAllSongsFromURL(url) + song, err := y.GetAllSongsFromURL(url) if err != nil { return nil, fmt.Errorf("error fetching new songs from URL: %v", err) } @@ -249,3 +249,8 @@ func (y *Youtube) FetchSongByURLs(url string) ([]*player.Song, error) { return songs, nil } + +func (y *Youtube) IsYouTubeURL(url string) bool { + pattern := regexp.MustCompile(`^(https?://)?(www\.)?(youtube\.com|youtu\.be)/.*$`) + return pattern.MatchString(strings.ToLower(url)) +}