Creating A Reddit Bot

Happy New Year!  It has been quite a long while since I last wrote a blog post.  After making it 100 straight blog posts without missing a Tuesday or Friday deadline, I decided to take a little break from blogging.  However, I’ve done a few exciting programming and database related things in that time, and I want to share that information!

Stat-Bot Overview

A couple of months ago (or so..), I decided I wanted to create a Reddit bot.  Reddit is a social media website composed of different communities (called subreddits).  A Reddit bot, in general, reads all of the posts and/or comments on specific subreddits (or all subreddits) and when it is summoned, it responds in some manner.  There are a lot of really cool Reddit bots out there already (some examples include one that converts measurements in the metric system to numbers that us Americans can understand, and there is one that you can call to remind you about a post in a set amount of time).

My bot is called stat-bot.  Currently, the bot monitors comments in its own community (/r/statbot) that call it.  A sample call is:

The general formula for this is /u/stat-bot [BGG Username] plays.  I have the required “plays” word at the end because I hope to add other cool stats in the future (such as looking at people’s collections).  In addition, users can give a date range (such as /u/stat-bot mad4hatter plays 2016-01-01 2016-12-31).

The bot responds within a few minutes with the total number of plays, the total play time, and their top 10 most played games in that time frame.  A sample output of this would be (the formatting looks a LOT better on Reddit!):

Python Overview

You can view all of the code here.

Every 15 minutes, the bot checks all of the comments on the statbot subreddit.  It only looks at comments that it hasn’t yet commented on (based on comment id) and for comments that have its name in it (/u/stat-bot).  If it finds a comment like that, it grabs the commend id, the Reddit username that posted the comment, and the comment itself and puts it into a SQLite database called comments.  This portion of the code is below:

Next, it removes everything before the bot call (/u/stat-bot).  The BGG username should be the next word after the bot call, so it grabs that word and puts it into a variable called username.  If the username is about, it comments a general comment about what it does and where you can find its code.  If the username is help, it gives a sample call.

Otherwise, the kind of call should be next (for now, it only knows “play”).  The next word is optional, but if it is there, it would be the minimum date (so it is stored in a variable called mindate).  Likewise, the word following that is optional and stored into a variable called maxdate.

After I have parsed the input, I start checking for errors.  The first check is to make sure the dates are valid.  All of that code is here:

If the call is correct for playsit goes to a function called playsmain.  If the user called collection (which will be available in the future), it goes to a function called collectionmain.  If the user called anything else, it comments back to the user that it didn’t understand the input.

If there are errors, it comments back a list of all of the errors to the user in addition to giving the user a sample call.

Once the reply is ready to be sent, it sends out the reply via the comment.reply(out) command and inserts into a SQLite database what it responded.  It waits 30 seconds between each comment that it reads.  When it has read all of the new comments, it waits 15 minutes before checking for more new comments.  This code is here:

Future Work

I definitely want to flesh this bot out more.  This bot is unlike anything else I’ve ever programmed before.  I had to really anticipate ways that other people would misuse the commands and flesh out error messages, output messages, input commands, etc so that hopefully the average user could understand.  It also feels insanely good to interact with a bot that you wrote yourself.  If you haven’t ever done something like this, I definitely recommend it!

As far as future work on /u/stat-bot, I want to add fun things about users collections (such as their most owned mechanic, publisher, artist, etc).  I also want to add comparison stats (such as “You have played X games since you asked me last” or “You have played Y more games than the requester”).  The opportunities are endless with this bot, and I definitely want to pursue them!

Have questions or suggestions?  Please feel free to comment below or contact me.

One thought on “Creating A Reddit Bot

Leave a Reply

Your email address will not be published. Required fields are marked *