-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmsg_monitor.py
59 lines (56 loc) · 2.43 KB
/
msg_monitor.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
import time
import praw
import prawcore
import Config
footer = "\n\n*****\n\n^^^[Source](https://github.com/a-ton/gpd-bot) ^^^| ^^^[Suggestions?](https://www.reddit.com/message/compose?to=Swimmer249)"
reddit = praw.Reddit(client_id=Config.cid,
client_secret=Config.secret,
password=Config.password,
user_agent=Config.agent,
username=Config.user)
print("Monitoring inbox...")
while True:
try:
for msg in reddit.inbox.stream():
# checks if bot has already replied (good if script has to restart)
if isinstance(msg, praw.models.Comment):
responded = False
for comment in msg.refresh().replies:
if comment.author.name == "GPDBot":
responded = True
break
if responded:
continue
# checks if the message body contains "expired" or "oops"
if isinstance(msg, praw.models.Comment):
msg_text = msg.body.lower()
oops = False
expired = False
try:
if msg_text.index("oops") > -1:
oops = True
except ValueError:
pass
try:
if msg_text.index("expired") > -1:
expired = True
except ValueError:
pass
reply_msg = ""
if oops:
msg.submission.mod.flair(text=None, css_class=None)
print("unflairing... responded to: " + msg.author.name)
reply_msg = "Flair removed." + footer
elif expired:
msg.submission.mod.flair(text='Deal Expired', css_class='expired')
print("flairing... responded to: " + msg.author.name)
reply_msg = "Deal marked as expired. Reply with \"oops\" if this is incorrect." + footer
if reply_msg != "":
msg.mark_read()
msg.reply(body=reply_msg)
except (prawcore.exceptions.RequestException, prawcore.exceptions.ResponseException):
print ("Error connecting to reddit servers. Retrying in 5 minutes...")
time.sleep(300)
except praw.exceptions.APIException:
print ("rate limited, wait 5 seconds")
time.sleep(5)