示例#1
0
        /// <summary>
        /// Searches Spotify for tracks
        /// </summary>
        /// <param name="channel">Channel in which to send to</param>
        /// <param name="url">Spotify URL</param>
        /// <returns><see cref="List{T}"/> of tracks (track name + artists' name) or null if not found</returns>
        public async Task <List <string> > SearchSpotify(ISocketMessageChannel channel, string url)
        {
            Regex r = new Regex(@"https?:\/\/(?:open\.spotify\.com)\/(?<type>\w+)\/(?<id>[\w-]{22})(?:\?si=(?:[\w-]{22}))?");

            if (!r.Match(url).Success)
            {
                var msg = await embedHelper.BuildMessageEmbed("Invalid Spotify link.");

                await channel.SendAndRemove(embed : msg, timeout : 5000);

                return(null);
            }

            string        type   = r.Match(url).Groups["type"].Value;
            string        id     = r.Match(url).Groups["id"].Value;
            List <string> tracks = new List <string>();

            switch (type)
            {
            case "album":
                await foreach (var item in Spotify.Paginate((await Spotify.Albums.Get(id)).Tracks))
                {
                    tracks.Add($"{item.Name} {string.Join(" ", item.Artists.Select(x => x.Name))}");
                }
                break;

            case "playlist":
                var playlist = await Spotify.Playlists.Get(id);

                await foreach (var item in Spotify.Paginate(playlist.Tracks))
                {
                    if (item.Track is FullTrack track)
                    {
                        tracks.Add($"{track.Name} {string.Join(" ", track.Artists.Select(x => x.Name))}");
                    }
                }
                break;

            case "track":
                var trackItem = await Spotify.Tracks.Get(id);

                tracks.Add($"{trackItem.Name} {string.Join(" ", trackItem.Artists.Select(x => x.Name))}");
                break;

            default:
                var msg = await embedHelper.BuildMessageEmbed("Must be a `track`, `playlist`, or `album`.");

                await channel.SendAndRemove(embed : msg, timeout : 6000);

                return(null);
            }

            return(tracks);
        }