Esempio n. 1
0
        static void Main()
        {
            bool dbug = Debugger.IsAttached;

            Console.Title = "BookHorseBot " + Constants.Version;
            List <string> ignoredUsers = C.Ignored.User;

            //Does all the dirty work of handling oAuth and tokens. Gives botclient authentication.
            AuthorizeFimFictionBot();
            Reddit reddit     = AuthorizeRedditBot();
            string redditName = reddit.User.FullName;

            Console.WriteLine(dbug ? "Debug detected. Running on test subreddit!" : "Running on Main subreddit!");
            Subreddit             subreddit     = reddit.GetSubreddit(dbug ? "bronyvillers" : "mylittlepony");
            IEnumerable <Comment> commentStream =
                subreddit.CommentStream.Where(c => !ignoredUsers.Contains(c.AuthorName.ToLower()) &&
                                              c.CreatedUTC >= DateTime.UtcNow.AddMinutes(-120) &&
                                              c.AuthorName.ToLower() != redditName.ToLower()
                                              );


            foreach (Comment comment in commentStream)
            {
                //Look for { and }, if none are found, skip!
                MatchCollection matches = Regex.Matches(comment.Body, @"(?<=\{)[^}]*(?=\})", RegexOptions.None);
                if (matches.Count == 0)
                {
                    continue;
                }
                //Check to see if we already replied to this comment.
                Comment qualifiedComment = reddit.GetComment(new Uri(comment.Shortlink));
                if (qualifiedComment.Comments.All(x => x.AuthorName != redditName))
                {
                    string         username = qualifiedComment.AuthorName.ToLower();
                    List <Command> commands = ExtractCommands(matches, username);
                    if (commands.Count > 0)
                    {
                        GetPostText(commands);
                        string postReplyBody = GeneratePostBody(commands);
                        try
                        {
                            comment.Reply(postReplyBody);
                            Console.WriteLine($"Reply posted to {comment.AuthorName}!");
                        }
                        catch (Exception e)
                        {
                            System.Console.WriteLine(e);
                        }
                    }
                }
            }
        }