I am not a coder at all...I'm just someone who wants to create a Reddit bot and is trying to make it happen.
I created a simple test bot to post "Hello world" in the /test sub, and it worked.
Then I created a new bot, using ChatGPT, that would post a comment in a given sub in response to comments with certain keywords. The code is at the end of this post (minus my credentials).
When I save and run the code, something flashes on the screen below (where I would normally see the output) and then disappears. I managed to snap a screen grab of the results, and it says:
^CTraceback (most recent call last):
File
"«stdin>"
Line 1. 1n <module>
File
"/bin/pythonanywhere_runner.py",
line 30, in _pa_run
exec (code,
new variables)
File "/home/BotsDemandAction/test1.py", line 39, in «module>
time.sleep(10
KevboardInterrupt
Any suggestions? And please be gentle...again, I am not a coder but am learning. Thanks!
THE CODE
import praw
import time
Create a Reddit instance with your API credentials
[Deleted]
Define the subreddits and keywords to monitor
subreddits = ['test']
keywords = ['I love you', 'you love me']
Monitor the subreddits indefinitely
while True:
for subreddit_name in subreddits:
subreddit = reddit.subreddit(subreddit_name)
for submission in subreddit.new(limit=10):
Check if the submission contains any of the keywords
if any(keyword.lower() in submission.title.lower() for keyword in keywords):
Get the highest-rated comment in the thread
submission.comments.replace_more(limit=0)
top_comment = max(submission.comments, key=lambda comment: comment.score)
Check if the bot has already commented on this thread
already_commented = False
for comment in top_comment.replies:
if comment.author == reddit.user.me():
already_commented = True
break
If the bot has not commented yet, post a reply
if not already_commented:
top_comment.reply('Hello my lovely!')
print('Replied to thread: ' + submission.title)
Wait for 10 seconds before checking again
time.sleep(10)