Whether you like it or not, a significant number of Reddit users are bots. Usually, they perform just two tasks: they look for comments that match a few well-defined criteria and then automatically reply to them. Creating such bots takes less than 5 minutes if you know how to use Python.
In this tutorial, I’ll show you how to use the new and very powerful PRAW 4 Python package to create a Reddit bot that can not only look for comments containing a specific word in a given subreddit, but also automatically reply to such comments with a predefined message.
For the sake of a clear example, we’ll be looking for the word Mars in the comments of the r/space subreddit. The canned reply will be Why no love for Titan?
Let’s start by installing the PRAW 4 package. The easiest way to do so is to use Python pip. If you don’t have it, here’s how you can get it on Ubuntu 14.04:
pip3, you can install PRAW using the following command:
At this point, you can start using PRAW 4.
All Reddit bots must be registered with Reddit. Therefore, log in to Reddit using a new account, or an account you already have, and click on the preferences link.
Next, click on the apps tab.
Here, click on the create an app… button. You’ll now see a form asking for various details about the application.
Give a unique name and meaningful description to the application, and select script. The about url and redirect uri fields are not necessary for a script-based bot. Therefore, you can specify any URL. I’ll use example.com.
Once you press the create app button, your bot will be registered and you will be able to see its client ID and client secret. Make a note of them because you’ll be needing them next.
Create a new file called mybot.py and open it using your favorite code editor. The first thing you need to do inside it is import the PRAW package using the following statement:
You can now create an instance of your bot using the
praw.Reddit class. To its constructor, you must pass a meaningful user-agent, the client id, the client secret, your Reddit account’s username, and its password. Note that the Reddit account you use here is the same as the one you used while registering the bot.
The bot is now ready to be used.
To point it to a specific subreddit, you must use the
subreddit() method. Here’s how you point it to the r/space subreddit.
PRAW4 makes it very easy for you to continuously monitor a subreddit for new posts and comments. All you need to do is make use of the
stream object. By using
stream, you don’t have to create any loop, or bother about Reddit’s rate limits. PRAW 4 handles it all for you automatically.
Once you have the
comments generator, you can create a
for loop to analyze each item it returns. For now, let us just extract the comment’s
Once you are sure that the
body contains the keyword Mars, you can call the
reply() method of the
comment object to send a canned reply. Here’s how you would do that:
That’s all you need to do. Save the file, and exit your code editor. From the command line, you can run the bot using the following command:
Note that the script will never exit, unless you press Ctrl+C. That’s because it is continuously fetching new comments, analyzing them, and replying to them.
You can, of course, take a look at its replies directly on Reddit.
In this tutorial, you learned how to create a Reddit bot using PRAW 4. For best results, I suggest you use a Reddit account that is at least a week old for your bot. Otherwise, Reddit might be restricting it considerably.