/// <summary> /// Method used to actually monitor all comments from a subreddit. /// </summary> /// <param name="reddit"></param> /// <param name="subtomonitor"></param> /// <param name="StatisticLines"></param> static void MonitorSubreddit(Reddit reddit, String subtomonitor, int StatisticLines) { var subreddit = reddit.GetSubreddit(subtomonitor); Console.ForegroundColor = ConsoleColor.DarkYellow; Log("[" + subtomonitor + "] Gathering moderator names..."); Console.ForegroundColor = ConsoleColor.Yellow; //I don't want the bot to reply to the moderators of subreddits because.. well i assume they know what they are doing. List <string> ModListString = new List <string>(); foreach (ModeratorUser Mod in reddit.GetSubreddit(subtomonitor).Moderators) //So we get a list of moderator usernames { ModListString.Add(Mod.Name); } Console.ForegroundColor = ConsoleColor.Yellow; Log("[" + subtomonitor + "] Now monitoring."); //Start a comment stream to get all comments from the currently monitored subreddit as they are posted. foreach (var comment in subreddit.CommentStream) { //On every comment, call the ProcessComment() function to look through the comment body for product keys. CommentHandler.ProcessComment(comment, BotStats.BotLaunchUTC, ModListString, BotStats, subtomonitor); } }
/// <summary> /// Method to call that will find product keys, then post a reply if product keys were found. /// </summary> /// <param name="comment"></param> /// <param name="BotLaunchUTC"></param> /// <param name="ModListString"></param> /// <param name="BotStats"></param> /// <param name="currentsub"></param> public static void ProcessComment(Comment comment, DateTime BotLaunchUTC, List <string> ModListString, Stats BotStats, String currentsub) { //We don't want to comment on our own comments and also not on the comments of subreddit moderators because we assume they know what they are doing. if (comment.AuthorName != "AntiSerialCodeBot" && !ModListString.Contains(comment.AuthorName)) { //Make sure it doesn't reply to comments before the bot was launched. This shouldn't happen but i like having this check in here anyway. if (comment.CreatedUTC.DateTime > BotLaunchUTC) { //Keep some stats. BotStats.TotalCommentsRead++; //Get all product keys from the comment. Dictionary <String, List <Enums.KeyType> > ProductKeys = CommentHandler.getProductKeysFromComment(comment); //STATISTICS ARE GREAT BotStats.ProductKeysFound += (ulong)ProductKeys.Count; if (ProductKeys.Count != 0) { if (Configuration.AllowBotToPostComments) { try { BotStats.TotalRepliesPosted++; //Post a reply on the comment that contains the product key(s) PostReply(comment, BotStats, ProductKeys, currentsub); } catch (Exception e) { //Set the cursor at the bottom of the console past all other lines we already printed this session Console.SetCursorPosition(0, (int)(BotStats.TotalRepliesPosted - BotStats.RepliesPostedAtBotLaunch) + (int)BotStats.RepliesFailed + BotStats.StatisticLines + (int)BotStats.OwnCommentsDeleted); //We do +4 because there is 4 lines of statistics at the top of the console. //Log the error message Log(e.Message + " - " + comment.Shortlink); //S T A T I S T I C S BotStats.RepliesFailed++; } } } } } }