Skip to content

Latest commit

 

History

History
65 lines (45 loc) · 1.6 KB

Monitor Modmail.md

File metadata and controls

65 lines (45 loc) · 1.6 KB

Monitor Incoming Modmail

Author

Kris Craig

Required libraries

Overview

Monitors the authenticated user's modmail for new messages. The authenticated user's refresh token must have the modmail scope.

Library Installation

In the NuGet Package Manager console:

Install-Package Reddit

The Code

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

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

			// Start monitoring unread modmail messages and register the callback function.  --Kris
			reddit.Account.Modmail.MonitorUnread();
			reddit.Account.Modmail.UnreadUpdated += C_UnreadMessagesUpdated;

			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
			reddit.Account.Modmail.MonitorUnread();
			reddit.Account.Modmail.UnreadUpdated -= C_UnreadMessagesUpdated;
		}
		
		private void C_UnreadMessagesUpdated(object sender, ModmailConversationsEventArgs e)
		{
			foreach (KeyValuePair<string, ConversationMessage> pair in e.AddedMessages)
			{
				NewMessages.Add(pair.Value);
			}
		}
	}
}

Source File

Monitor Modmail.cs