import praw import time import os def bot_start(): r = praw.Reddit(username = '' password = '' client_id = '', client_secret = '', user_agent = "") return r def bot_run(r, comments_replied_to): subreddit = r.subreddit('subredditname') for comment in subreddit.comments(limit=2): if 'keyword' in comment.body and comment.id not in comments_replied_to and comment.author != r.user.me(): print ("String found: " + comment.id) print(20*"-") print ("Author: ", comment.author) print(20*"-") comment.reply("reply") print ("Replied to: " + comment.id) print(20*"-") comments_replied_to.append(comment.id) with open ("comments_replied_to.txt", "a") as f: f.write(comment.id + "\n") print ("Search complete") print ("waiting...") time.sleep(10) def get_saved_comments(): if not os.path.isfile("comments_replied_to.txt"): comments_replied_to = [] else: with open("comments_replied_to.txt", "r") as f: comments_replied_to = f.read() comments_replied_to = list(comments_replied_to) comments_replied_to = list(filter(None, comments_replied_to)) return comments_replied_to r = bot_start() comments_replied_to = get_saved_comments() while True: bot_run(r, comments_replied_to)