예제 #1
0
        public async Task fmfriendssetAsync([Summary("Friend names")] params string[] friends)
        {
            User userSettings = await _userService.GetUserSettingsAsync(Context.User).ConfigureAwait(false);

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

                return;
            }

            try
            {
                string SelfID = Context.Message.Author.Id.ToString();

                List <string> friendList         = new List <string>();
                List <string> friendNotFoundList = new List <string>();

                int friendcount = 0;

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

                if (existingFriends.Count + friends.Length > 10)
                {
                    await ReplyAsync("Sorry, but you can't have more then 10 friends.").ConfigureAwait(false);

                    return;
                }

                foreach (string friend in friends)
                {
                    if (existingFriends?.Where(w => w.LastFMUserName != null).Select(s => s.LastFMUserName.ToLower()).Contains(friend.ToLower()) != true)
                    {
                        if (!_guildService.CheckIfDM(Context))
                        {
                            IGuildUser friendUser = await _guildService.FindUserFromGuildAsync(Context, friend).ConfigureAwait(false);

                            if (friendUser != null)
                            {
                                User friendUserSettings = await _userService.GetUserSettingsAsync(friendUser).ConfigureAwait(false);

                                if (friendUserSettings == null || friendUserSettings.UserNameLastFM == null)
                                {
                                    if (await _lastFmService.LastFMUserExistsAsync(friend).ConfigureAwait(false))
                                    {
                                        await _friendsService.AddLastFMFriendAsync(Context.User.Id.ToString(), friend).ConfigureAwait(false);

                                        friendcount++;
                                    }
                                    else
                                    {
                                        friendNotFoundList.Add(friend);
                                    }
                                }
                                else
                                {
                                    await _friendsService.AddDiscordFriendAsync(Context.User.Id.ToString(), friendUser.Id.ToString()).ConfigureAwait(false);

                                    friendcount++;
                                }
                            }
                            else
                            {
                                if (await _lastFmService.LastFMUserExistsAsync(friend).ConfigureAwait(false))
                                {
                                    await _friendsService.AddLastFMFriendAsync(Context.User.Id.ToString(), friend).ConfigureAwait(false);

                                    friendcount++;
                                }
                                else
                                {
                                    friendNotFoundList.Add(friend);
                                }
                            }
                        }
                        else
                        {
                            if (await _lastFmService.LastFMUserExistsAsync(friend).ConfigureAwait(false))
                            {
                                await _friendsService.AddLastFMFriendAsync(Context.User.Id.ToString(), friend).ConfigureAwait(false);

                                friendcount++;
                            }
                            else
                            {
                                friendNotFoundList.Add(friend);
                            }
                        }
                    }
                }

                if (friendcount > 1)
                {
                    await ReplyAsync("Succesfully added " + friendcount + " friends.").ConfigureAwait(false);
                }
                else if (friendcount == 1)
                {
                    await ReplyAsync("Succesfully added a friend.").ConfigureAwait(false);
                }

                if (friendNotFoundList.Count > 0)
                {
                    if (friendNotFoundList.Count > 1)
                    {
                        await ReplyAsync("Could not find " + friendNotFoundList.Count + " friends. Please ensure that you spelled their names correctly.").ConfigureAwait(false);
                    }
                    else
                    {
                        await ReplyAsync("Could not find 1 friend. Please ensure that you spelled the name correctly.").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);

                int friendcount = friends.Length;

                if (friendcount > 1)
                {
                    await ReplyAsync("Unable to add " + friendcount + " friends due to an internal error.").ConfigureAwait(false);
                }
                else
                {
                    await ReplyAsync("Unable to add a friend due to an internal error.").ConfigureAwait(false);
                }
            }
        }