-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
121 lines (100 loc) · 3.79 KB
/
bot.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import time
import configparser
from datetime import datetime, timedelta
import praw
import praw.exceptions
boo_SendReply = True
# subreddits_to_monitor = 'Test'
subreddits_to_monitor = 'all'
bot_username = ''
def create_satine_reply_message():
output = ''
output += 'Hey, just want to let you know, '
output += '[Its Satine Actually...](https://i.makeagif.com/media/10-27-2016/XqQlyU.gif)'
output += '\n\n^You\'re ^welcome'
return output
def satan_exists(content):
lst_body = content.lower().split()
if "satan" in content.lower():
return True
else:
return False
def show_comment(comment):
print ('From ' + str(comment.author))
print ('ID: ' + comment.id)
print ('Parent: ' + comment.parent_id)
print (comment.body)
def bot_exists_in_parent_comments(comment):
# print 'Current ID: ' + comment.id
# print 'Checking Parent: ' + comment.parent_id
# print 'Perma link ' + comment.permalink
if str(comment.author) == bot_username:
return True
if comment.parent_id[0:3] == 't3_':
# print 'Is top comment, bailing...'
return False
# print 'Checking parent'
parent = comment.parent()
return bot_exists_in_parent_comments(parent)
def bot_exists_in_sibling_comments(comment):
comment.refresh()
replies = comment.replies
# print replies
# print 'Number of replies: ' + str(len(replies))
for reply in replies:
if str(reply.author) == bot_username:
return True
return False
def comment_reply_message(comment, message):
print ('Posting reply to ' + str(comment.id))
try:
new_comment = comment.reply(message)
print ('New Comment Posted: ' + str(new_comment))
return new_comment
except praw.exceptions.APIException as e:
print (e)
return None
def start_bot():
config = configparser.ConfigParser()
config.read('bot.cfg')
client_id = config['DEFAULT']['CLIENT_ID']
client_secret = config['DEFAULT']['CLIENT_SECRET']
username = config['DEFAULT']['USERNAME']
password = config['DEFAULT']['PASSWORD']
user_agent = config['DEFAULT']['USER_AGENT']
reddit = praw.Reddit(client_id=client_id,
client_secret=client_secret,
password=password,
user_agent=user_agent,
username=username)
bot_username = str(reddit.user.me())
print ('Running bot....' + bot_username)
subreddit = reddit.subreddit(subreddits_to_monitor) # subscribe to /r/all
for comment in subreddit.stream.comments(): # Stream all comments
if satan_exists(comment.body): # Check if 'satan' exists in comment body
if not bot_exists_in_parent_comments(comment): # Check if this bot exists in any parent comments
if not bot_exists_in_sibling_comments(comment): # Check to make sure comment hasn't already been made
# show_comment(comment) # [DEBUG] Show comment
if boo_SendReply:
msg = create_satine_reply_message()
if comment_reply_message(comment, msg) == None: # Attempt to send message
seconds = reddit.auth.limits['remaining'] # If it fails, wait for x amount of seconds
print ('Need to sleep: ' + str(reddit.auth.limits['remaining']) + ' seconds')
now = datetime.now()
expire = now + timedelta(seconds=seconds)
print (str(now.time()) + ' - Reddit Sending timeout. Sleeping for ' + str(seconds) + ' seconds. Sending at ' + str(expire.time()))
time.sleep(seconds)
if comment_reply_message(comment, msg) == None:
print ('Failed to send reply')
# # else:
# # print 'Sending replies is turned off'
# else:
# print 'Bot reply exists in sibling comment'
# else:
# print 'Bot reply exists in a parent comment'
# else:
# print 'A non-satan comment is here'
# show_comment(comment)
# Dont print anything here, otherwise all comments will be printed
if __name__ == '__main__':
start_bot();