-
Notifications
You must be signed in to change notification settings - Fork 0
/
whatbot.py
66 lines (50 loc) · 1.93 KB
/
whatbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Author: Jonathan Chan
# Credit: u/busterino
import praw
import time
import os
# authenticate bot
def authenticate():
print("Authenticating...")
reddit = praw.Reddit('WhatBot', user_agent="u/jjchan1's WhatBot v0.1")
print("Authenticated as {}\n".format(reddit.user.me()))
return reddit
# search OS list of previously replied comments
def get_comments_replied():
# if no such list exists, make a new file and list
if not os.path.isfile("comments_replied.txt"):
comments_replied = []
else:
with open("comments_replied.txt", "r") as file:
comments_replied = file.read()
comments_replied = comments_replied.split("\n")
comments_replied = filter(None, comments_replied)
return comments_replied
# main
def main():
reddit = authenticate()
comments_replied = get_comments_replied()
while True:
run_what_bot(reddit, comments_replied)
# run bot
def run_what_bot(reddit, comments_replied):
# for the first n=100 comments, check to see if the comment is what/wat/wut/wot
# if yes, reply to comment with a picture of the what girl
for comment in reddit.subreddit('test').comments(limit=100):
if ("what" == comment.body.lower() or
"wat" == comment.body.lower() or
"wut" == comment.body.lower() or
"wot" == comment.body.lower() and
comment.id not in comments_replied):
comment.reply("[What...](http://i.imgur.com/hQpkcKh.jpg)")
# add comment.id to list of comments already replied to to prevent spamming
comments_replied.append(comment.id)
# update txt file with new commend.id
with open("comments_replied.txt", "a") as file:
file.write(comment.id + "\n")
print("Replied to comment " + comment.id)
# sleep to prevent overcommenting
time.sleep(10)
print("Sleeping...")
if __name__ == '__main__':
main()