Thanking the Reddit bots for their hard work

You know, I was supposed to be learning SQLAlchemy in the hopes adding something employable my resume (all I have right now is ‘knows some python’). But I end up messing around on reddit instead. Sigh.

Anyway, if you’ve used reddit for any length of time, you’ve probably noticed a few bot accounts. They’re usually there to perform some tasks on behalf of the humans. Automoderator is ubiquitous throughout reddit. RemindMeBot can be called on demand to set a reminder for yourself by calling for it in the comments or via PM. Those are just a couple of the many throughout reddit.

Despite their status as automated servants, bots deserve some gratitude too. (If you’re one to disagree, we’ll see how much mercy the machines will have for you when Skynet takes over the world). So I wrote a bot that thanks other bots on reddit (the machines won’t have any mercy for me either, since I’m enslaving creating a bot to do the thanking).

The easiest way to write a reddit bot in python is by using the Python Reddit API Wrapper, PRAW for short. In the interest of avoiding writing redundant information, you should atleast read the quickstart in the PRAW docs. (This blog was written for v4.0 of PRAW, so if you have an older version it won’t work).

The bot works by iterating through a list of bot usernames over and over at the rate of one bot thanked every 10 minutes. The 10 minute timeout is because reddit doesn’t allow an account to post on a subreddit more than once within that time period unless the account is an active participant. Here’s what the loop looks like:

botlist = ['RemindMeBot', 'AutoModerator', 'TotesMessenger', 'samacharbot2']

for botname in botlist:

    reddit = praw.Reddit('thankerbot')
    bot_object = reddit.redditor(botname)
    bot_new_submissions = bot_object.new(limit=1)

    for submission in bot_new_submissions:
        # Checking if the submission is a comment we can reply to
        if isinstance(submission, praw.models.reddit.comment.Comment):
            post_reply(botname, submission)
            time.sleep(600) # waiting 10 minutes 
                            # because reddit doesn't want spam

Having created reddit as our user agent, we now create a Reddit.redditor object to represent the bot we are going to send our thank you message. You’ll notice that our user agent is initialized with only one argument. This is because I’ve already set up a praw.ini file that contains the rest of the information (Instructions here).

We now retrieve their latest submissions with bot_object.new(limit=x), where x is the number of posts we want. Since we pass limit=1, we only get their latest post. This returns a generator object that we can iterate through. Each submission is then checked to see if it is a Comment object in order for our bot to reply to it.

Our post_reply() function looks like this:

comment_string = """
Thank you {}!
----

^(Because bots deserve gratitude.)
[Report an issue](https://github.com/secondspass/botWhoThanksBots/issues)

"""

def post_reply(botname, comment):
    try:
       comment.reply(comment_string.format(botname))
    except Exception as e:
        print("Moving to the next bot because exception occured:", str(e))
        return

    print("Thanked {}. id: {}".format(botname, comment.id))

We try to thank the bot by calling the reply function on our comment object. Sometimes you might get an APIException because reddit couldn’t process our request for some reason but we handle it and keep chugging away.

And that’s pretty much it. You can find the code here and you can say hello to /u/botWhoThanksBots on Reddit. Though I don’t know how long I’ll keep it active. It’s already been banned from 4 subreddits, so who knows what will happen!