Skip to content

Latest commit

 

History

History
71 lines (50 loc) · 1.72 KB

Monitor Subreddit Comments.md

File metadata and controls

71 lines (50 loc) · 1.72 KB

Monitor Subreddit Comments

Author

Kris Craig

Required libraries

Overview

Monitors a subreddit for new comments (all posts).

Library Installation

In the NuGet Package Manager console:

Install-Package Reddit

The Code

using Reddit;
using Reddit.Controllers;
using Reddit.Controllers.EventArgs;
using System.Collections.Generic;

namespace MonitorSubredditComments
{
	class Program
	{
		public List<Comment> NewComments;
		
		static void Main(string[] args)
		{
			var reddit = new RedditClient("YourRedditAppID", "YourBotUserRefreshToken");
			
			NewComments = new List<Comment>();

			// Start monitoring the subreddit for new comments and register the callback function.  --Kris
			var subreddit = reddit.Subreddit("AskReddit");
			
			subreddit.Comments.GetNew();  // This call prevents any existing "new"-sorted comments from triggering the update event.  --Kris
			subreddit.Comments.MonitorNew();
			subreddit.Comments.NewUpdated += C_NewCommentsUpdated;

			while(true) { } // Replace this with whatever you've got for a program loop.  The monitoring will run asynchronously in a separate thread.  --Kris

			// Stop monitoring and unregister the callback function.  --Kris
			subreddit.Comments.MonitorNew();
			subreddit.Comments.NewUpdated -= C_NewCommentsUpdated;
		}
		
		private void C_NewCommentsUpdated(object sender, CommentsUpdateEventArgs e)
		{
			foreach (Comment comment in e.Added)
			{
				if (!NewComments.ContainsKey(comment.Fullname))
				{
					NewComments.Add(comment.Fullname, comment);
				}
			}
		}
	}
}

Source File

Monitor Subreddit Comments.cs