2016-08-04 61 views
0

我创建了一个带有PRAW的Reddit机器人,它在发现关键字时自动响应消息。问题在于人们现在正在发送关键词,而其中一个mod告诉我限制每个线程回复一条评论。我不是主程序员,但我相信机器人只会扫描所有线程合并的25条最新评论。它并不关心当前的单个线程。有关如何限制机器人只回复每个线程一条评论的任何想法?PRAW限制每个线程的Reddit机器人

谢谢

回答

0

您的问题有多种解决方案。使用已经响应的线程保留数据库将是最干净的,或者在这种情况下,因为它的信息非常少,您可以将线程ID保存到文件中。

但是,如果这个数据库/文件丢失,或者手动删除bot帖子或整个其他场景,这将是有问题的。因此,现在最好的方法是动态地执行此操作,如果稍后会减慢bot的速度,则可以考虑将上述方法添加为查找以获得更快的响应。

我现在所谈论的是每次查看评论时,您应该得到submission_id并扫描所有评论以确保没有机器人回复已添加(或者ceratian阈值未被通过)。

def scan(): 
    for c in reddit_client.get_comments('chosen_subreddit'): 
     if keyword not in c.body.lower(): #check main requirement 
      continue 
     if c.author == None: #comment deleted 
      continue 
     if c.author.name == bot_name: #don't bother with comments made by bot 
      continue 
     answer(c,c.link_id[3:]) 

def answer(comment, sub_id) 
    sub = reddit_client.get_submission(submission_id=sub_id) 
    sub.replace_more_comments(limit=None,threshold=0) 
    flat_comments = praw.helpers.flatten_tree(sub.comments) 
    if len([com for com in flat_comments if com.author != None and com.author.name.lower() == bot_name.lower()]) > 0: 
     return False 
    #here you can prepare response and do other stuff 
    #.... 
    #.... 
    comment.reply(prepared_response) 
    return True