Reddit机器人只会在运行时提取新帖子,而不会主动提取在机器人已经运行时发布的帖子

时间:2018-08-27 23:09:15

标签: python python-3.x bots reddit praw

不久前,我开始使用python,并为Reddit创建了一个简单的bot。不幸的是,该机器人的功能仅适用于在终端中运行该机器人之前发布的帖子。如果在运行bot后在subreddit上发布了帖子,则bot不会对这些帖子发表评论或获取帖子。

可能与此相关的另一件事是控制台输出“当前没有适用的帖子”。两次而不是一次。我不确定是否相关。

这是我认为此问题重要的代码,如果您需要更多上下文,请告诉我!:

def mainloop():
    counter1 = 0  # Counts submissions in new that have been crawled
    for submission in subreddit.new(limit=5):  # Get the 5 newest submissions
        counter1 = counter1 + 1

        callings = ['canada', 'canadian', '']  # Triggers
        normalized_title = submission.title.lower()
        normalized_text = submission.selftext.lower()

        while True:
            if submission.id not in posts_replied_to:  # If the post is new to the bot
                time.sleep(30)  # Keep spam low
                for canadian_mentions in callings:
                    if canadian_mentions in normalized_title:  # If trigger is in title
                        # Make the reply, print to console, then add the post to the replied storage
                        submission.reply(reply_text)
                        print("Bot replying to : ", submission.title, "\n")
                        posts_replied_to.append(submission.id)
                    elif canadian_mentions in normalized_text:  # If trigger is in text body
                        # Make the reply, print to console, then add the post to the replied storage
                        submission.reply(reply_text)
                        print("Bot replying to : ", submission.selftext, "\n")
                        posts_replied_to.append(submission.id)
                    else:
                        print("No applicable posts right now.")

1 个答案:

答案 0 :(得分:0)

您可以使用stream对象。

for post in reddit.subreddit(subreddit).stream.submissions(skip_existing=True):
    # Do stuff
    print(post.selftext)

这将实时打印帖子。