How to Build Your First Reddit Bot With Python [GUIDE]

Hello Guys, These post walks you through how to create a reddit bot.

Before proceeding, I want to bring it to notice that the next series of post will be all about bots. This post is about creating a Reddit bot.

But.

We still got a lot to cover. From Reddit bot, twitter bot to Facebook bot. Twitch Bot, Telegram Bot, Instagram Bot, Discord Bot. You name it.

So, what are we building now ?

A Reddit bot. !! yay !! .. Yes, we’re going to create a reddit bot

Why would you build a Reddit bot

There are tons of reason why you might want to build a reddit bot.

Let me give some examples

  • A bot that replies every comment with a static comment
  • Or bot that follows any user that up votes your content
  • Bot that insult every user that down votes your content (Please don’t take this serious (:)
  • Or maybe a bot that fetches hot content on a particular subreddit every morning and deliver it to your mail

I can keep writing bot ideas on here until i knock myself out

But:

i won’t do that ..

You can get more at the below URL on reddit itself. The below are compilation of bots that are already existing

https://www.reddit.com/r/autowikibot/wiki/redditbots

What does our Reddit Bot do ?

So, after tinkering around i decided on a simple bot idea we can build for the purpose of this write-up

This Reddit bot will be used to monitor a particular subreddit and alert you whenever there’s a new thread in the subreddit.

I choosed the learnpython subreddit for this https://www.reddit.com/r/learnpython/

But wait,

Before you proceed with this blog post, you should firstly get your self familiar with the Reddit platform as a whole and concepts surrounding it.

Concepts such as Karma, upvote, sub-reddits .. You can start here or just signup and walk around on the platform

Obviously you’re going to need an account as well.

This is the best simple bot idea for a reddit bot i could come up with.

If you have a better idea than this, please let me know in the comment section.

Registering the Reddit bot

Before i go ahead and start creating the bot , i first need to register the bot .

By register , i mean registering an app on reddit and getting the Client ID, and Client Secret so we can use with the reddit API.

I assume you already have an account with Reddit.

Visit here https://ssl.reddit.com/prefs/apps/

Firstly, you should select “Script”. As you advance, you can learn about the web and installed app selection.

Enter a meaningful name for your app and enter a simple description of it’s usage.

Fill the redirect URI and about URI, you can use one of your website or just enter Google.com (;

Finally, Click on create app.

You should see my input below

register-reddit-application-for-bot
create application

Installing PRAW

Since we’re creating our bot using the python programming language, we will be using the Python Reddit API Wrapper (PRAW) to create the bot.

A step at a time. Let’s install PRAW

PRAW can be installed using the python pip package manager, see below:

pip install praw

After installing praw, you can then import like below

import praw

If you got no error, then you good to go.

Creating the Reddit bot

Basically our bot will keep fetching top 10 posts in the subreddit known as submissions, and let us know when there’s a new submission in the retrieval.

The bot will print out the title and URI of each new post in the subreddit.

Thinking about it we need a way to track changes, so i decided to store all already retrieved submission ID in a list. So if there’s a new submission that has no ID in the list , it means it’s new and we gonna print it’s title and URI

So, let’s start

What’s a Reddit instance ?

You need an instance of the Reddit class to do anything with PRAW. This instance is either Read-only or Authorized.

In read-only mode, you can only fetch and process publicly available information.

While the Authorized mode allows you execute actions specific to an account such as posting a comment.

But what determines if it’s read-only or authorized. ?

The argument passed to praw.Reddit determines what mode is initialized.

Passing in only:

  1. client_id
  2. client_secret
  3. user_agent

Initializes a read-only mode but if you include the username and password argument , the authorized mode will be initialized.

But

Our bot only need the read-only mode because we’re only fetching publicly available information.

import praw
# create a reddit instance 
reddit = praw.Reddit(
    user_agent=f'Seachy (by /u/{username})',
    client_id=client_id, 
    client_secret=client_secret)

According to PRAW’s doc,

A user agent is a unique identifier that helps Reddit determine the source of network requests. To use Reddit’s API, you need a unique and descriptive user agent

So we now have a Reddit instance, we need to fetch a submission from a subreddit see below.

for submission in  reddit.subreddit.('learnpython').new(limit=10):
    print(f'Title: {submission.title} | URL: {submission.url} ')

The above snippet fetches the newest 10 submission in the subreddit and prints out their name and URL

The bot is almost done , If we run this in a loop every 5 minutes it will have a lot of repeated submission.

We need it to only prints out new submissions it hasn’t fetched before.

So, i’m gonna create a list called ID_storage and store all ID of fetched submission in it.

So i’m gonna edit the code to the below

for submission in  reddit.subreddit.('learnpython').new(limit=10):
    # check if the submission is already fetched earlier
    if submission.id not in ID_storage:
        # if not , print it's property
        print(f'Title: {submission.title} | URL: {submission.url} ')
        # add the ID to the storage 
        ID_storage.append(submission.id)

We also need to put this in a loop and add some cooling time in place. 5 minutes is a good time to cool.

Here’s the full code below

import praw
import time
client_id       = '<put your client id here>'
client_secret   = '<put your client secret here>'

# create a reddit instance 
reddit = praw.Reddit(
    user_agent=f'Geekycheckbt (by /u/{username})',
    client_id=client_id, 
    client_secret=client_secret)
    
ID_storage = []
while True:
    found_new = False
    for submission in  reddit.subreddit.('learnpython').new(limit=10):
        # check if the submission is already fetched earlier
        if submission.id not in ID_storage:
            found_new = True
            # if not , print it's property
            print(f'Title: {submission.title} | URL: {submission.url} ')
            # add the ID to the storage 
            ID_storage.append(submission.id)
            
    if not found_new:
        print('No new submission found in subreddit :( !')
        
    print('Cooling for 5 minutes .. !!')
    time.sleep(60*5)

I also added an extra check to check if it found new submission in each check

Testing our Reddit Bot

Here’s an image below showing testing the bot .

What Next ?

This write up is supposed to get you prepared for creating reddit bot. It get’s you prepared with getting familiar with it’s concept.

The sample bot create in this post is Read-only, you sure will need the Authorized mode most often and that’s where the real game is at.

So get prepared what’s coming your way if you want to become a bot master.

Conclusion.

Creating bots is fun. Especially when they work to help you ease your activities.

Like i said at the beginning of the blog post, There’s gonna be a lot of post on creating bots across various platform.

So, keep coming to check.

If you got any question for me or contribution to the post, please drop a comment in the comment section.

See: Reading and Writing a Google Spreadsheet Using Python [GUIDE]

One Response

Leave a Reply

Ad Blocker Detected

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Refresh