コード例 #1
0
        public async Task <ActionResult> Command_SearchAlbumAsync(
            [Name("Album Name")]
            [Description("The album name to search for.")]
            [Remainder]
            [DefaultValueDescription("The album of the track you're currently listening to.")]
            string albumQuery = null)
        {
            SpotifyAlbum album;

            if (albumQuery == null)
            {
                if (!(Context.Invoker.Activity is SpotifyGame spot))
                {
                    return(BadRequest(
                               "You didn't supply an album name, and you're not currently listening to anything!"));
                }

                var track = await _spotify.GetTrackAsync(spot.TrackId).ConfigureAwait(false);

                album = await track.Album.GetFullEntityAsync().ConfigureAwait(false);
            }
            else
            {
                try
                {
                    var result = await _spotify.SearchAsync(albumQuery, SearchType.Album).ConfigureAwait(false);

                    var sa0 = result.Albums.Items.FirstOrDefault();

                    if (sa0 == null)
                    {
                        return(BadRequest("Cannot find album by that name."));
                    }

                    album = await _spotify.GetAlbumAsync(sa0.Id.Id).ConfigureAwait(false);
                }
                catch (Exception)
                {
                    return(BadRequest("An error occurred while searching for the album."));
                }
            }

            var embed = new EmbedBuilder
            {
                Color  = BotService.DefaultEmbedColour,
                Author = new EmbedAuthorBuilder
                {
                    Name    = album.Artists.Select(a => a.Name).Humanize(),
                    IconUrl = album.Images.FirstOrDefault()?.Url,
                    Url     = album.Artists.FirstOrDefault()?.Id.Url
                },
                Title        = album.Name,
                ThumbnailUrl = album.Images.FirstOrDefault()?.Url,
                Footer       = new EmbedFooterBuilder
                {
                    Text = string.Join("\n",
                                       album.Copyrights.Distinct().Select(a =>
                                                                          $"[{a.CopyrightType.Humanize()}] {a.CopyrightText}"))
                }
            };

            var tracks = album.Tracks.Items;
            var size   = 0;

            var tracksOutput = tracks.TakeWhile((track, len) =>
            {
                if (size > 2000)
                {
                    return(false);
                }
                var st =
                    $"{len + 1} - {UrlHelper.CreateMarkdownUrl(track.Name, track.Id.Url)} by {track.Artists.Select(ab => ab.Name).Humanize()}";
                size += st.Length;
                return(size <= 2000);
            }).Select((track, b) =>
                      $"{b + 1} - {UrlHelper.CreateMarkdownUrl(track.Name, track.Id.Url)} by {track.Artists.Select(ab => ab.Name).Humanize()}")
                               .ToList();

            if (tracksOutput.Count != tracks.Length)
            {
                tracksOutput.Add($"And {tracks.Length - tracksOutput.Count} more...");
            }

            embed.Description = string.Join("\n", tracksOutput);

            embed.AddField("Release Date", album.ReleaseDate.ToString("D"), true);

            var length = TimeSpan.FromMilliseconds(album.Tracks.Items.Sum(a => a.Duration.TotalMilliseconds));

            embed.AddField("Length", $"{length.Hours} hours, {length.Minutes} minutes", true);
            return(Ok(embed));
        }