Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
fix: stream playback from history, nil error in auto reconnect fn
Browse files Browse the repository at this point in the history
  • Loading branch information
keshon committed Mar 14, 2024
1 parent 5a026a9 commit 1ccc91e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
25 changes: 23 additions & 2 deletions mod-music/discord/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion mod-music/player/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions mod-music/player/unpause.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 11 additions & 6 deletions mod-music/sources/youtube.go
Original file line number Diff line number Diff line change
Expand Up @@ -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=") {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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))
}

0 comments on commit 1ccc91e

Please sign in to comment.