Skip to content

Commit

Permalink
feat: search spotify tracks on youtube
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTipo01 committed Jun 1, 2023
1 parent 46c7838 commit 2bc803f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Spotify/spotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,17 @@ func (s *Spotify) GetPlaylist(id spotify.ID) (*spotify.FullPlaylist, error) {

return p, nil
}

func (s *Spotify) GetTrack(id spotify.ID) (*spotify.FullTrack, error) {
t, err := s.client.GetTrack(s.ctx, id)
if err != nil {
err := s.refreshClient()
if err != nil {
return nil, err
}

return s.client.GetTrack(s.ctx, id)
}

return t, nil
}
15 changes: 15 additions & 0 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,21 @@ func spotifyPlaylist(s *discordgo.Session, guildID, user string, i *discordgo.In
}
}

// Gets info about a spotify track and plays it, searching it on YouTube
func spotifyTrack(s *discordgo.Session, guildID, user string, i *discordgo.Interaction, loop, priority bool, id spotify.ID) {
if spt != nil {
if track, err := spt.GetTrack(id); err == nil {
link, _ := searchDownloadAndPlay(track.Name + " - " + track.Artists[0].Name)
downloadAndPlay(s, guildID, link, user, i, false, loop, true, priority)
} else {
lit.Error("Can't get info on a spotify track: %s", err)
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField(errorTitle, spotifyError+err.Error()).SetColor(0x7289DA).MessageEmbed, i, time.Second*5)
}
} else {
sendAndDeleteEmbedInteraction(s, NewEmbed().SetTitle(s.State.User.Username).AddField(errorTitle, spotifyNotConfigure).SetColor(0x7289DA).MessageEmbed, i, time.Second*5)
}
}

// getInfo returns info about a song, with every line of the returned array as JSON of type YtDLP
func getInfo(link string) ([]string, error) {
// Gets info about songs
Expand Down
4 changes: 4 additions & 0 deletions wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ func play(s *discordgo.Session, song string, i *discordgo.Interaction, guild, us
spotifyPlaylist(s, guild, username, i, random, loop, priority, spotify.ID(strings.TrimPrefix(song, "spotify:playlist:")))
case strings.Contains(song, "spotify.com/playlist/"):
spotifyPlaylist(s, guild, username, i, random, loop, priority, spotify.ID(strings.Split(strings.TrimPrefix(song, "https://open.spotify.com/playlist/"), "?")[0]))
case strings.HasPrefix(song, "spotify:track:"):
spotifyTrack(s, guild, username, i, loop, priority, spotify.ID(strings.TrimPrefix(song, "spotify:track:")))
case strings.Contains(song, "spotify.com/track/"):
spotifyTrack(s, guild, username, i, loop, priority, spotify.ID(strings.Split(strings.TrimPrefix(song, "https://open.spotify.com/track/"), "?")[0]))
case isValidURL(song):
downloadAndPlay(s, guild, song, username, i, random, loop, true, priority)
default:
Expand Down

0 comments on commit 2bc803f

Please sign in to comment.