2016-03-06 25 views
1

我編寫了一個reddit機器人,我測試了它很多,我可以說它除了最後一部分自動化以外運作良好。它第一次檢查留下的每一條評論,並從名爲 真相dare的陣列中回覆它們,當它完成時它只是打印它檢查的消息,但對新評論沒有任何影響。我使用PRAW(Pythons Reddit API包裝)和python 3.5。謝謝檢查所有評論後,Reddit bot停下來

#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) 

回答

0

這部分代碼沒有正確縮進,所以它沒有工作。這是它應該如何工作的代碼:

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) 
+0

您是否認真地未能縮進問題?你需要刷上你的基本python –

相關問題