Skip to content

Instantly share code, notes, and snippets.

@derekantrican
Last active December 23, 2020 01:22
Show Gist options
  • Save derekantrican/a1e962eb2e4321ab41ea92146091852f to your computer and use it in GitHub Desktop.
Save derekantrican/a1e962eb2e4321ab41ea92146091852f to your computer and use it in GitHub Desktop.
A template for a Reddit Bot written in C# using RedditSharp
using RedditSharp;
using RedditSharp.Things;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
namespace RedditBotTemplate
{
/* GETTING STARTED:
* 1. Install the RedditSharp NuGet package (note that this is based on prerelease 2.0.0-CI00075)
* 2. Fill out the settings below
* 3. Modify the DoBotLoop method for your bot's logic
*/
class Program
{
//-----------SETTINGS-------------
private static readonly string username = "";
private static readonly string password = "";
private static readonly string clientId = "";
private static readonly string clientSecret = "";
private static readonly string redirectUri = "";
private static readonly string subreddit = "";
//---------------------------------
private static readonly string repliedToPath = "RepliedTo.txt";
private static Reddit redditService;
private static Subreddit subredditToCheck;
static void Main()
{
if (!File.Exists(repliedToPath))
File.Create(repliedToPath).Close();
Auth().Wait();
DoBotLoop().Wait();
}
private static async Task Auth()
{
BotWebAgent agent = new BotWebAgent(username, password, clientId, clientSecret, redirectUri);
redditService = new Reddit(agent, true);
subredditToCheck = await redditService.GetSubredditAsync(subreddit);
}
private static async Task DoBotLoop()
{
while (true)
{
try
{
List<Comment> comments = await subredditToCheck.GetComments(500).ToList(); //Adjust the recent comments to get. Alternatively, use "GetPosts" to monitor posts
string repliedTo = File.ReadAllText(repliedToPath);
comments.RemoveAll(c => c.IsArchived || repliedTo.Contains(c.Id));
foreach (Comment comment in comments)
{
//Fill out your logic here for if you should reply to the comment and use `async comment.ReplyAsync(message)`
File.AppendAllLines(repliedToPath, new string[] { comment.Id }); //Log comment been replied to
}
}
catch (Exception e)
{
if (e is AggregateException aggregateException)
{
foreach (Exception innerException in aggregateException.InnerExceptions)
{
Console.WriteLine($"Inner exception ({innerException.GetType()}: {innerException.Message}) thrown at \n\n{innerException.StackTrace}");
}
}
//Handle all sorts of "timeout" or internet connection errors
if (e is RedditHttpException ||
e is HttpRequestException ||
e is WebException ||
e is TaskCanceledException ||
e is OperationCanceledException)
{
Console.WriteLine($"\tIssue connecting to reddit: {e.Message}");
}
else //If it isn't one of the errors above, it might be more serious. So throw it to be caught as an unhandled exception
{
Console.WriteLine($"Exception ({e.GetType()}: {e.Message}) thrown at \n\n{e.StackTrace}");
throw;
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment