예제 #1
0
        public async Task FmFriendsRecentAsync()
        {
            User userSettings = await _userService.GetUserSettingsAsync(Context.User).ConfigureAwait(false);

            if (userSettings?.UserNameLastFM == null)
            {
                await UsernameNotSetErrorResponseAsync();

                return;
            }

            try
            {
                List <Friend> friends = await _friendsService.GetFMFriendsAsync(Context.User).ConfigureAwait(false);

                if (friends?.Any() != true)
                {
                    await ReplyAsync("We couldn't find any friends. To add friends:\n" +
                                     "`.fmaddfriends 'lastfmname/discord name'`").ConfigureAwait(false);

                    return;
                }

                EmbedAuthorBuilder eab = new EmbedAuthorBuilder
                {
                    IconUrl = Context.User.GetAvatarUrl()
                };

                EmbedBuilder builder = new EmbedBuilder();
                builder.WithAuthor(eab);
                string URI = "https://www.last.fm/user/" + userSettings.UserNameLastFM;
                builder.WithUrl(URI);
                builder.Title = await _userService.GetUserTitleAsync(Context).ConfigureAwait(false);

                string amountOfScrobbles = "Amount of scrobbles of all your friends together: ";

                if (friends.Count > 1)
                {
                    builder.WithDescription("Songs from " + friends.Count + " friends");
                }
                else
                {
                    builder.WithDescription("Songs from your friend");
                    amountOfScrobbles = "Amount of scrobbles from your friend: ";
                }

                const string  nulltext      = "[undefined]";
                int           indexval      = (friends.Count - 1);
                int           playcount     = 0;
                List <string> failedFriends = new List <string>();

                foreach (Friend friend in friends)
                {
                    try
                    {
                        string friendusername = friend.FriendUser != null ? friend.FriendUser.UserNameLastFM : friend.LastFMUserName;

                        PageResponse <LastTrack> tracks = await _lastFmService.GetRecentScrobblesAsync(friendusername, 1).ConfigureAwait(false);

                        string TrackName  = string.IsNullOrWhiteSpace(tracks.FirstOrDefault().Name) ? nulltext : tracks.FirstOrDefault().Name;
                        string ArtistName = string.IsNullOrWhiteSpace(tracks.FirstOrDefault().ArtistName) ? nulltext : tracks.FirstOrDefault().ArtistName;
                        string AlbumName  = string.IsNullOrWhiteSpace(tracks.FirstOrDefault().AlbumName) ? nulltext : tracks.FirstOrDefault().AlbumName;

                        builder.AddField(friendusername + ":", TrackName + " - " + ArtistName + " | " + AlbumName);


                        if (friends.Count <= 8)
                        {
                            LastResponse <LastUser> userinfo = await _lastFmService.GetUserInfoAsync(friendusername).ConfigureAwait(false);

                            playcount += userinfo.Content.Playcount;
                        }
                    }
                    catch
                    {
                        //failedFriends.Add(friendusername);
                    }
                }

                if (friends.Count <= 8)
                {
                    EmbedFooterBuilder efb = new EmbedFooterBuilder
                    {
                        Text = amountOfScrobbles + playcount.ToString("0")
                    };
                    builder.WithFooter(efb);
                }

                //if (failedFriends.Count > 0)
                //{
                //    await ReplyAsync("Couldn't retrieve data for one or more of the following friends:\n" +
                //        string.Join(", ", friends.Select(s => s.LastFMUserName).ToArray()))
                //        .ConfigureAwait(false);
                //}

                await Context.Channel.SendMessageAsync("", false, builder.Build()).ConfigureAwait(false);

                this._logger.LogCommandUsed(Context.Guild?.Id, Context.Channel.Id, Context.User.Id, Context.Message.Content);
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message, Context.Message.Content, Context.User.Username, Context.Guild?.Name, Context.Guild?.Id);

                await ReplyAsync("Unable to show friends due to an internal error. Try removing all your current friends using `.fmremoveallfriends` and try again.").ConfigureAwait(false);
            }
        }