From 28f3abdb455bf7b7948e2cc9037c13323aac10b9 Mon Sep 17 00:00:00 2001 From: Innokentiy Sokolov Date: Wed, 31 Jul 2024 10:56:56 +0300 Subject: [PATCH] fix: no need to use add command when adding during playing - experimenting with http client --- mods/music/discord/play.go | 19 +++++++++++++++---- mods/music/sources/youtube.go | 17 +++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/mods/music/discord/play.go b/mods/music/discord/play.go index 8d9b1d2..ccddb2f 100644 --- a/mods/music/discord/play.go +++ b/mods/music/discord/play.go @@ -92,6 +92,9 @@ func (d *Discord) handlePlayCommand(param string, enqueueOnly bool) { } // Enqueue playlist to the player + if d.Player.GetCurrentSong() != nil { + enqueueOnly = true + } err = playOrEnqueue(d, songs, s, m, enqueueOnly, pleaseWaitMessage.ID) if err != nil { slog.Error(err) @@ -286,8 +289,16 @@ func playOrEnqueue(d *Discord, playlist []*player.Song, s *discordgo.Session, m } if enqueueOnly { - showStatusMessage(d, s, m.Message.ChannelID, prevMessageID, playlist, previousPlaylistExist, false) - slog.Warn(d.Player.GetCurrentStatus().String()) + playlist = d.Player.GetSongQueue() + go func() { + for { + if d.Player.GetCurrentStatus() == player.StatusPlaying || d.Player.GetCurrentStatus() == player.StatusPaused { + showStatusMessage(d, s, m.Message.ChannelID, prevMessageID, playlist, previousPlaylistExist, false) + break + } + time.Sleep(250 * time.Millisecond) + } + }() } else { go func() { for { @@ -299,14 +310,14 @@ func playOrEnqueue(d *Discord, playlist []*player.Song, s *discordgo.Session, m } }() - slog.Warn("Current status is", d.Player.GetCurrentStatus().String()) - err := d.Player.Unpause(vs.ChannelID) if err != nil { return err } } + slog.Warn("Current status is", d.Player.GetCurrentStatus().String()) + return nil } diff --git a/mods/music/sources/youtube.go b/mods/music/sources/youtube.go index 0f70135..4a43850 100644 --- a/mods/music/sources/youtube.go +++ b/mods/music/sources/youtube.go @@ -3,6 +3,8 @@ package sources import ( "fmt" "io" + "net" + "time" "net/http" "regexp" @@ -36,6 +38,21 @@ func NewYoutube() IYoutube { } func (y *Youtube) parseSongInfo(url string) (*player.Song, error) { + // Experimental + y.youtubeClient.HTTPClient = &http.Client{ + Transport: &http.Transport{ + Proxy: http.ProxyFromEnvironment, + DialContext: (&net.Dialer{ + Timeout: 30 * time.Second, + }).DialContext, + MaxIdleConns: 10, + IdleConnTimeout: 30 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + }, + Timeout: 30 * time.Second, + } + song, err := y.youtubeClient.GetVideo(url) if err != nil { return nil, err