Search code examples
pythonredditpraw

Reddit bot stops after checking all comments


I coded a reddit bot and i tested it alot and I can say it works well except for the last part automation. The first time it checks every comment left there and replies to them from the list of the arrays called truths and dares when it finishes it just prints the messages that it checks but doesn't do anything to the new comments. Im using PRAW(Pythons Reddit API Wrapper) and python 3.5. Thank you

    #1. Import libraries
import praw
import random
import time

#2. Write the truths and dares
dares = ["Draw snoo", "Draw the android logo", "Spin in a circle for a minute", "Wish a random contact on a social media platform Happy birthday but it isn't their birthday", "Eat a potato" ]
truths = ["Do you like someone on this subreddit", "Do you do drugs","Do you watch porn", "Do you pirate stuff", "What is your favorite Disney princess", "How many times have you been drunk"]
cache = []


#3. Connect to reddit
r = praw.Reddit(user_agent = "TruthAndDare by @UnknownDevelope /u/unknowndeveloper")
r.login("Username","pass")

def run_bot():
    print("Getting Subreddit ...")
    subreddit = r.get_subreddit("subreddit")
    print("Getting comments ...")
    comments = subreddit.get_comments(limit=200)
#4. Check subreddit
#5. Check for a truth or a dares
#6. Reply

submission = r.get_submission(submission_id='submissionid')
flat_comments = praw.helpers.flatten_tree(submission.comments)
already_done = set()
for comment in flat_comments:
    if comment.body == "/u/TruthAndDareBot Truth" and comment.id not in already_done:
        randomTruth = random.choice(truths)
        comment.reply(randomTruth)
        print("SIR I found a truth and im gonna reply to it. The post ID is: "+ comment.id)
        cache.append(comment.id)
    if comment.body == "/u/TruthAndDareBot Dare" and comment.id not in already_done:
        randomDare = random.choice(dares)
        comment.reply(randomDare)
        print("SIR I found a Dare and im gonna reply to it. The post ID is: "+ comment.id)
        cache.append(comment.id)


while True:
    run_bot()
    time.sleep(10)

Solution

  • This part of the code was not properly indented so it didn't work. This is the code on how it should work:

    def run_bot():
            print("Getting Subreddit ...")
            subreddit = r.get_subreddit(SUBREDDIT)
            print("Getting comments ...")
            comments = subreddit.get_comments(limit=SUBMISSION_ID)
            submission = r.get_submission(submission_id='49q8l1')
            flat_comments = praw.helpers.flatten_tree(submission.comments)
            already_done = set()
            for comment in flat_comments:
                print comment.body
                if comment.body == "/u/TruthAndDareBot Truth" and comment.id not in already_done:
                    randomTruth = random.choice(truths)
                    comment.reply(randomTruth)
                    print("Found a truth and I'm going to reply to it. Comment ID is: "+ comment.id)
                    cache.append(comment.id)
                if comment.body == "/u/TruthAndDareBot Dare" and comment.id not in already_done:
                    randomDare = random.choice(dares)
                    comment.reply(randomDare)
                    print("Found a Dare and I'm going to reply to it. Comment ID is: "+ comment.id)
                    cache.append(comment.id)