I started with python a little while ago, and I made a simple bot for Reddit. Unfortunately, the functionality of the bot only works on posts that are made before the bot is run in terminal. If a post is made to the subreddit after the bot has been run, the bot does not comment on or fetch these posts.
One other thing that may be related is that the console outputs "No applicable posts right now." twice instead of once. I'm not sure if that is related.
Here is the code I think is import for this issue, if you need more context let me know!:
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.")
while True:
after the check for new posts:for submission in subreddit.new(limit=5):
You will never get new posts again after you enter the infinite loop. You need to rearrange your code. – abcelse:
statement, and the program was sitting idle at that statement. if i had fixed that and left thewhile True
loop as well, it would have still had the same issue! thanks! – Bryn