Warm tip: This article is reproduced from stackoverflow.com, please click
praw python python-3.x reddit windows

My reddit bot replies to same comment and itself over and over

发布于 2020-05-25 13:26:13

So, I am very new to python and made a simple reddit bot which replies to a comment. It worked fine this morning, but now it replies to the same comment and even itself over and over again. I can't find how to fix this with my poor googling skills... so, here I am. The code is this

import praw
import time
import config

REPLY_MESSAGE = "Di Molto indeed"

def authenticate():
    print("Authenticating...")
    reddit = praw.Reddit(client_id = config.client_id,
                    client_secret = config.client_secret,
                    username = config.username,
                    password = config.password,
                    user_agent = 'FuriousVanezianLad by /u/FuriousVanezianLad')
    print("Authenticated as {}".format(reddit.user.me()))
    return reddit


def main():
    reddit = authenticate()
    while True:
            run_bot(reddit)


def run_bot(reddit):
    print("Obtaining 25 comments...")
    for comment in reddit.subreddit('test').comments(limit=25):
        if "Di Molto" in comment.body:
            print('String with "Di Molto" found in comment {}',format(comment.id))
            comment.reply(REPLY_MESSAGE)
            print("Replied to comment " + comment.id)

    print("Sleeping for 10 seconds...")
    time.sleep(10)


if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        print("Interrupted")

I took this code from Bboe's Updates to "How To Make A reddit Bot — Part One by Busterroni" I don't know what's wrong but it comments to itself. Sorry I know this a stupid question and it may be solved before but I couldn't find it...

Sorry again and Thanks in advance for helping!

Questioner
Himanshu Mishra
Viewed
26
jarhill0 2020-03-09 21:55

The problem is that you're fetching the newest 25 comments over and over again, and no new comments are being made (or they're being made at a slow rate), so you end up processing the same comments repeatedly.

I recommend instead using a stream, which is a PRAW feature. Streams fetch new items (in this case, comments) as they are posted. This way, you will not process the same comment more than once. Also, check if you made a particular comment before replying to it. Here's a modified version of your code that uses streams and checks if you made the comment:

def run_bot(reddit):
    me = reddit.user.me()
    try:
        for comment in reddit.subreddit('test').stream.comments(skip_existing=True):
            if "Di Molto" in comment.body and comment.author != me:
                print('String with "Di Molto" found in comment {}',format(comment.id))
                comment.reply(REPLY_MESSAGE)
                print("Replied to comment " + comment.id)

    except Exception as e:
        print("Got exception: {}".format(e))
        print("Sleeping for 10 seconds...")
        time.sleep(10)