all repos — disgord @ 585845a7da99f6478c85019d190eb7f9446c55e4

A simple Discord bot in Go.

upgrade rabbitpipe
Marco Andronaco andronacomarco@gmail.com
Sun, 24 Nov 2024 02:27:17 +0100
commit

585845a7da99f6478c85019d190eb7f9446c55e4

parent

0769e5ef6b52a3e567e837fe232691bc6e57f912

5 files changed, 21 insertions(+), 42 deletions(-)

jump to
M go.modgo.mod

@@ -5,7 +5,7 @@

require ( github.com/FoxeiZ/dca v0.0.0-20240420115706-e4859a963796 github.com/birabittoh/myks v0.0.2 - github.com/birabittoh/rabbitpipe v0.0.2 + github.com/birabittoh/rabbitpipe v0.0.4 github.com/bwmarrin/discordgo v0.28.1 golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f )
M go.sumgo.sum

@@ -2,8 +2,8 @@ github.com/FoxeiZ/dca v0.0.0-20240420115706-e4859a963796 h1:H8+3uNS2PWDRABx7QixGgJY+sa+GeUpwSN9QNFKiK/Y=

github.com/FoxeiZ/dca v0.0.0-20240420115706-e4859a963796/go.mod h1:+hXF1jadw9rDuEoyhAIMWH3acfzvPcqVQG6kKGqNrFY= github.com/birabittoh/myks v0.0.2 h1:EBukMUsAflwiqdNo4LE7o2WQdEvawty5ewCZWY+IXSU= github.com/birabittoh/myks v0.0.2/go.mod h1:klNWaeUWm7TmhnBHBMt9vALwCHW11/Xw1BpCNkCx7hs= -github.com/birabittoh/rabbitpipe v0.0.2 h1:4ptBS4Ai9NJH9gv3uG5TZBp1H5gfgEabEw6XldSjUx0= -github.com/birabittoh/rabbitpipe v0.0.2/go.mod h1:6cEDb0WpwrRm2vt5IO3s2gPjzhZZLP7gYx+l9e3gx1k= +github.com/birabittoh/rabbitpipe v0.0.4 h1:RxleMxwGy2Ikeu/sesV/PQ5NZmmdcS5oKnh0SZ1hNUA= +github.com/birabittoh/rabbitpipe v0.0.4/go.mod h1:6cEDb0WpwrRm2vt5IO3s2gPjzhZZLP7gYx+l9e3gx1k= github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4= github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
M src/globals/utils.gosrc/globals/utils.go

@@ -1,11 +1,7 @@

package globals import ( - "errors" "fmt" - "io" - "net/http" - "net/url" "regexp" "strings" "time"

@@ -109,34 +105,3 @@ logger.Errorf("could not save config: %s", err)

} return defaultPrefix } - -func Search(query string) (videoID string, err error) { - escaped := url.QueryEscape(strings.ToLower(strings.TrimSpace(query))) - - cached, err := searchKS.Get(escaped) - if err == nil && cached != nil { - return *cached, nil - } - - resp, err := http.Get("https://www.youtube.com/results?search_query=" + escaped) - if err != nil { - return - } - defer resp.Body.Close() - - pageContent, err := io.ReadAll(resp.Body) - if err != nil { - return - } - - matches := searchPattern.FindAllStringSubmatch(string(pageContent), -1) - if len(matches) == 0 { - err = errors.New("no video found") - return - } - - videoID = matches[0][1] - searchKS.Set(escaped, videoID, 11*time.Hour) - - return matches[0][1], nil -}
M src/music/commands.gosrc/music/commands.go

@@ -21,7 +21,7 @@ MsgLeft = "Left."

MsgQueueLine = "%d. %s\n" ) -var yt = rabbitpipe.New() +var yt = rabbitpipe.New("") func HandlePlay(args []string, s *discordgo.Session, m *discordgo.MessageCreate) string { r, _, vc := gl.GetVoiceChannelID(s, m)
M src/music/video.gosrc/music/video.go

@@ -6,7 +6,6 @@ "regexp"

"strings" "time" - gl "github.com/birabittoh/disgord/src/globals" "github.com/birabittoh/rabbitpipe" )

@@ -71,11 +70,26 @@

return } +func search(query string) (videoID string, err error) { + results, err := yt.Search(query) + if err == nil && results != nil { + for _, result := range *results { + if result.Type == "video" && !result.LiveNow && !result.IsUpcoming && !result.Premium { + logger.Printf("Video found by API.") + + return result.VideoID, nil + } + } + err = errors.New("search did not return any valid videos") + } + + return "", err +} + func getVideo(args []string) (*rabbitpipe.Video, error) { videoID, err := extractVideoID(args[0]) if err != nil { - searchQuery := strings.Join(args, " ") - videoID, err = gl.Search(searchQuery) + videoID, err = search(strings.Join(args, " ")) if err != nil || videoID == "" { return nil, err }