Reddit is a social news aggregation, web content rating, and discussion website. It is a popular target for bots because of its large user base and open nature. Bots are used for a variety of purposes, including automated answering of simple questions. In this tutorial, I will show you how to automate Reddit posts using Python. This Reddit bot will pull all the comments from a subreddit and reply to it with something that we will define. This is a very simple Reddit bot and is just to get you started on creating a Reddit bot using python. I will also teach you how to use PRAW – Python Reddit API wrapper in your code to make your life easier.

Prerequisites

Praw:

Praw is an open-source code library for Python. It provides an API to allow users to interact with Reddit in their Python applications. It is designed to be used with Reddit’s OAuth authorization system and is an attempt to make OAuth easier to use. Praw is designed to work seamlessly with other applications so that they can share information and capabilities.

To install Praw use the following command in your terminal of command prompt.

pip install praw

Reddit API setup

To create a Reddit bot we are going to use Reddit API and it has a wrapper built for python in that API

Step 1:

Go to the following link and press on 

https://www.reddit.com/prefs/apps

Are you a developer? create an app…

Step 2:

You will have to fill out the following form and then press on create an app

Step 3:

Now you can see that your app is created and you will have to copy past two things from here to use in our code. It’s unique for everyone so you need to use your own:

  1. Client_id
  2. Client_secret

Create Reddit instance

import praw
reddit = praw.Reddit(
    client_id="Q3dfveBJYo_4FTwIdlKKmQ",
    client_secret="jUAAzmmqe9uVxOBqlJTUoZRxsAvAow",
    user_agent="<1st_bot>",
)
print(reddit.read_only)

Here we provided three pieces of information:

  1. Client id that we got earlier from the Reddit API
  2. Client secret that we also got from the Reddit API
  3. And in the last we have a user agent, here you have to use something unique that no one has used before.

And after that, you can check, whether your Reddit API is connected to work properly or not.

Output

Now let’s grab a subreddit from Reddit, that we call the variable subreddit 

# get subreddit
subreddit = reddit.subreddit("anime")
print(subreddit.display_name)

Output

Let’s get the recent post from the subreddit:

# you can get whatever amount of the post you want
for post in subreddit.hot(limit=10):
    print(post.title)

Output

Let’s get the comments on the posts:

import praw

reddit = praw.Reddit(
    client_id="Q3dfveBJYo_4FTwIdlKKmQ",
    client_secret="jUAAzmmqe9uVxOBqlJTUoZRxsAvAow",
    user_agent="<1st_bot>",
)

# get subreddit
subreddit = reddit.subreddit("anime")
print(subreddit.display_name)

# you can get whatever amount of the post you want
for submission in subreddit.hot(limit=10):
    print(submission.title)

    # getting the comment of the post
    for comment in submission.comments:
        print(comment)

Output

As you can see right now it’s grabbing a bunch of random things that we don’t understand because we are printing out comment object data.

# you can get whatever amount of the post you want
for submission in subreddit.hot(limit=10):
    print(submission.title)

    # getting the comment of the post
    for comment in submission.comments:
        # here we are defining that if the comment had a body then only print the comment
        if hasattr(comment, "body"):
            print(comment.body)

Output

Now it’s giving us comments on the post. And now let’s get the comment that has the word sad in there.

# you can get whatever amount of the post you want
for submission in subreddit.hot(limit=10):

    # getting the comment of the post
    for comment in submission.comments:
        # here we are defining that if the comment had a body then only print the comment
        if hasattr(comment, "body"):
            # converting the comment is lower case
            comment_lower = comment.body.lower()
            if " sad " in comment_lower:
                print("-----------")
                print(comment.body)

Here we are converting all the words in a comment to lowercase so it grabs every comment that has uppercase sad also. Then used the if statement to give us a comment that only had a sad word in the sentence. 

Output

You can see that every comment has a sad word.

Creating a Reddit Bot

import praw
import random
import time
reddit = praw.Reddit(
    client_id="Q3dfveBJYo_4FTwIdlKKmQ",
    client_secret="jUAAzmmqe9uVxOBqlJTUoZRxsAvAow",
    user_agent="<1st_bot>",
    username='pythonsample',
    password='Python@123'
)

# get subreddit
subreddit = reddit.subreddit("anime")

sad_quotes = [
"Every one of us must do what is in our powers! If we are going to die anyway, then it is better to die fighting than to do nothing!- Sakura Haruno",
"A real ninja is one who endures no matter what gets thrown at him ... All you do need is the guts to never give up. Jiraiya",
"Never underestimate your opponent, no matter how small they may seem.- Shino Aburame"
"Hard work is worthless for those that do not believe in themselves.- Naruto Uzumaki"
"Power is not will, it is the phenomenon of physically making things happen.- Madara Uchiha"
""
]


# you can get whatever amount of the post you want
for submission in subreddit.hot(limit=10):
    # print(submission.title)

    # getting the comment of the post
    for comment in submission.comments:
        # here we are defining that if the comment had a body then only print the comment
        if hasattr(comment, "body"):
            # converting the comment is lower case
            comment_lower = comment.body.lower()
            if " sad " in comment_lower:
                print("-----------")
                print(comment.body)
                # choosing random quote in the list
                random_index = random.randint(0, len(sad_quotes) - 1)
                comment.reply(sad_quotes[random_index])
                # this allows your bot to sleep for 700 seconds
                time.sleep(700)

Here we added two new libraries first is random to pick the random quote from our list and the second one is time because we need to reply to the comment is a time because Reddit doesn’t allow us to post a bunch of the comments at the same time. Then we added the Reddit account id and password. And that’s it.

Note: if you try to make anything that is not in Reddit policy it will ban your bot or even your Reddit account

Output:

Final Words

In this blog post, we learned about how to create a Reddit bot using the popular Python framework called PRAW. We learned about how to gain access to a Reddit user account, how to find their top posts and their comments from a Subreddit, and also how to reply to any comments with an automated process. This is not considered a top-level bot but you have to start somewhere right? If you have any questions about this blog post, please leave a comment below or contact us anytime.