Skip to content

Instantly share code, notes, and snippets.

/Main.cs Secret

Created October 28, 2012 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/f2776ba78807a93ece5b to your computer and use it in GitHub Desktop.
Save anonymous/f2776ba78807a93ece5b to your computer and use it in GitHub Desktop.
r9kbot part 1
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using Newtonsoft.Json.Linq;
using System.Text.RegularExpressions;
namespace robot9k
{
class MainClass
{
static internal List<string> usedPhrases = new List<string>();
static internal List<string> checkedComments = new List<string>();
static internal Dictionary<string, string> userData = new Dictionary<string, string>();
static internal RedditLib redditBot = new RedditLib();
static internal Timer timer;
static internal string BadComment = "**Uh oh!** You posted a comment that is *unoriginal* in /r/obot9k. Do you know what that means?\r\n\r\n ban hammer swings down\r\n\r\n*USER WAS BANNED FOR {x}*\r\n\r\n**Want more info? [Click here](http://www.reddit.com/r/obot9k/comments/1274zm/meta_what_is_robot9k/)";
static internal int[] banTimes = new int[16]{2, 4, 8, 10, 12, 20, 33, 40, 64, 120, 240, 600, 1200, 4000, 12000, 20000};
static internal bool isDoingStuff = false;
//Inspired by http://blog.xkcd.com/2008/01/14/robot9000-and-xkcd-signal-attacking-noise-in-chat/ && 4chan's /r9k/
public static void Main (string[] args)
{
Console.ForegroundColor = ConsoleColor.Black;
Console.BackgroundColor = ConsoleColor.White;
Console.WriteLine ("Starting /r/obot9k");
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.Black;
Console.WriteLine ("Loading used phrases list");
try{
string path = "usedPhrases.txt";
if (!File.Exists(path))
{
File.Create(path);//Create file if it doesn't exist.
}
string line;
System.IO.StreamReader file = new System.IO.StreamReader(path);
while((line = file.ReadLine()) != null)
{
usedPhrases.Add (line);
}
file.Close();
}
catch (Exception e)
{
Console.WriteLine ("Error! I probably don't have permissions.");
Console.WriteLine (e.Message);
return;
}
Console.WriteLine ("Loading checked comments list");
try{
string path = "checkedComments.txt";
if (!File.Exists(path))
{
File.Create(path);//Create file if it doesn't exist.
}
string line;
System.IO.StreamReader file = new System.IO.StreamReader(path);
while((line = file.ReadLine()) != null)
{
checkedComments.Add (line);
}
file.Close();
}
catch (Exception e)
{
Console.WriteLine ("Error! I probably don't have permissions.");
Console.WriteLine (e.Message);
return;
}
Console.WriteLine ("Loading user list");
try{
string path = "user.txt";
if (!File.Exists(path))
{
File.Create(path);//Create file if it doesn't exist.
}
string line;
System.IO.StreamReader file = new System.IO.StreamReader(path);
while((line = file.ReadLine()) != null)
{
try{
userData.Add (line.Split(';')[0], int.Parse(line.Split(';')[1]) + ";" + line.Split(';')[2] + ";" + line.Split(';')[3]);
}
catch{}
}
file.Close();
}
catch (Exception e)
{
Console.WriteLine ("Error! I probably don't have permissions.");
Console.WriteLine (e.Message);
return;
}
Console.WriteLine ("Logging into reddit...");
redditBot.Login("username", "password");
Console.WriteLine ("Unbanning users who should be unbanned");
checkIfUnbanUsers();
Console.WriteLine ("Finished loading /r/obot9k! Beep boop!");
timer = new Timer(TimerTick, null, TimeSpan.Zero, new TimeSpan(0, 0, 2, 30));
while (true)
{
//Hold input forever
}
}
static void TimerTick(object state)
{
if (isDoingStuff)
{
return;
}
isDoingStuff = true;
List<string> tempUsedPhrases = new List<string>();
List<string> tempCheckedComments = new List<string>();
Dictionary<string, string> tempUserData = new Dictionary<string, string>(userData);
try{
JObject o = JObject.Parse(redditBot.GetJSONComments("obot9k"));
foreach (var v in Reverse(o["data"]["children"]))
{
if (!checkedComments.Contains((string)v["data"]["id"]))
{
checkedComments.Add((string)v["data"]["id"]);
tempCheckedComments.Add((string)v["data"]["id"]);
if ((string)v["data"]["author"] != "r9kbot")
{
Regex rgx = new Regex("[^a-zA-Z0-9 -]");
string m = (string)v["data"]["body"].ToString().Replace(" ", "");
string s = rgx.Replace(m, "");
if (usedPhrases.Contains(s))
{
redditBot.BanUser((string)v["data"]["author"], "Banned", "obot9k");
int CurrentBanLevel = 0;
if (userData.ContainsKey((string)v["data"]["author"]))
{
CurrentBanLevel = int.Parse(((string)tempUserData[(string)v["data"]["author"]]).Split(';')[0]) + 1;
}
else
{
tempUserData.Add((string)v["data"]["author"], 0 + ";" + DateTime.Now + ";true");
}
if (CurrentBanLevel >= 16)
{
CurrentBanLevel = 15;
}
tempUserData[(string)v["data"]["author"]] = CurrentBanLevel + ";" + DateTime.Now + ";true";
Thread.Sleep (2000);
TimeSpan ts = TimeSpan.FromMinutes(banTimes[CurrentBanLevel]);
string id = redditBot.Comment (BadComment.Replace("{x}", ToReadableString(ts)), (string)v["data"]["id"], RedditLib.TYPE_THING.COMMENT);
Thread.Sleep (2000);
redditBot.MODDistinguish(id, "yes", RedditLib.TYPE_THING.COMMENT);
Thread.Sleep (2000);
}
else
{
usedPhrases.Add(s);
tempUsedPhrases.Add(s);
}
}
}
}
}
catch {
return; //Reddit is down or acting up. Lets give it another 2 minutes 30 seconds.
}
using (StreamWriter w = File.AppendText("usedPhrases.txt"))
{
foreach (string s in tempUsedPhrases)
{
w.WriteLine (s);
}
}
using (StreamWriter w = File.AppendText("checkedComments.txt"))
{
foreach (string s in tempCheckedComments)
{
w.WriteLine (s);
}
}
using (StreamWriter w = new StreamWriter("user.txt", false))
{
foreach (var s in tempUserData)
{
w.WriteLine (s.Key + ";" + s.Value);
}
}
userData = new Dictionary<string, string>(tempUserData);
checkIfUnbanUsers();
isDoingStuff = false;
}
static void checkIfUnbanUsers()
{
Dictionary<string, string> tempUserData = new Dictionary<string, string>(userData);
foreach (var s in tempUserData)
{
string[] splitArray = s.Value.Split(';');
if(splitArray[2] != "false")
{
DateTime t = DateTime.Parse(splitArray[1]);
int amountOfTime = banTimes[int.Parse(splitArray[0])];
if (DateTime.Now.Subtract(t).TotalMinutes > amountOfTime)
{
userData[s.Key] = s.Value.Replace("true", "false");
redditBot.UnBanUser(s.Key, "Unbanned", "obot9k");
}
}
}
using (StreamWriter w = new StreamWriter("user.txt", false))
{
foreach (var s in userData)
{
w.WriteLine (s.Key + ";" + s.Value);
}
}
}
static string ToReadableString(TimeSpan span)
{
string formatted = string.Format("{0}{1}{2}{3}",
span.Duration().Days > 0 ? string.Format("{0:0} DAYS, ", span.Days) : string.Empty,
span.Duration().Hours > 0 ? string.Format("{0:0} HOURS, ", span.Hours) : string.Empty,
span.Duration().Minutes > 0 ? string.Format("{0:0} MINUTES, ", span.Minutes) : string.Empty,
span.Duration().Seconds > 0 ? string.Format("{0:0} SECONDS", span.Seconds) : string.Empty);
if (formatted.EndsWith(", ")) formatted = formatted.Substring(0, formatted.Length - 2);
if (string.IsNullOrEmpty(formatted)) formatted = "0 SECONDS";
return formatted;
}
static IEnumerable<T> Reverse<T>(IEnumerable<T> input)
{
return new Stack<T>(input);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment