public static async Task ReplyToApprovedPosts() { int removed = 0; foreach (string approvalRequestId in PostsPendingApproval.Keys) //Remove approval requests that have "timed out" { if (PostsPendingApproval[approvalRequestId].Force) { continue; } if ((DateTime.UtcNow - PostsPendingApproval[approvalRequestId].RedditPost.CreatedUTC).TotalMinutes > 30) { //Try to remove until we are able (another thread may be accessing it at this time) bool removeSuccess = PostsPendingApproval.TryRemove(approvalRequestId, out _); while (!removeSuccess) { removeSuccess = PostsPendingApproval.TryRemove(approvalRequestId, out _); } removed++; } } if (removed > 0) { ConsoleHelper.Write($"\tRemoved {removed} pending auto-replies that got too old", ConsoleColor.Red); } List <ApprovalRequest> approvedPosts = PostsPendingApproval.Where(p => p.Value.IsApproved).Select(p => p.Value).ToList(); foreach (ApprovalRequest approvalRequest in approvedPosts) { string reply = ""; foreach (MPObject mpObject in approvalRequest.ApprovedResults) { Area relatedLocation = approvalRequest.RelatedLocation; if (!mpObject.Parents.Contains(approvalRequest.RelatedLocation)) { relatedLocation = null; } reply += BotReply.GetFormattedString(new SearchResult(mpObject, relatedLocation)); reply += Markdown.HRule; } reply += BotReply.GetBotLinks(approvalRequest.RedditPost); if (!DryRun) { Comment botReplyComment = await RedditHelper.CommentOnPost(approvalRequest.RedditPost, reply); monitoredComments.Add(new CommentMonitor() { Parent = approvalRequest.RedditPost, BotResponseComment = botReplyComment }); ConsoleHelper.Write($"\tAuto-replied to post {approvalRequest.RedditPost.Id}", ConsoleColor.Green); BotUtilities.LogOrUpdateSpreadsheet(approvalRequest); } //Try to remove until we are able (another thread may be accessing it at this time) bool removeSuccess = PostsPendingApproval.TryRemove(approvalRequest.RedditPost.Id, out _); while (!removeSuccess) { removeSuccess = PostsPendingApproval.TryRemove(approvalRequest.RedditPost.Id, out _); } } }