我不久前开始使用python,我为Reddit制作了一个简单的机器人。不幸的是,机器人的功能仅适用于在终端中运行机器人之前制作的帖子。如果在运行机器人之后对subreddit发布了帖子,则机器人不会评论或获取这些帖子。
另一件可能与之相关的事情是控制台输出“目前没有适用的帖子”。两次而不是一次。我不确定这是否相关。
以下是我认为导入此问题的代码,如果您需要更多上下文,请告诉我!
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.")
你可以使用stream
对象。
for post in reddit.subreddit(subreddit).stream.submissions(skip_existing=True):
# Do stuff
print(post.selftext)
这将在制作时实时打印帖子。