-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.py
268 lines (194 loc) · 8.96 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/python
import logging
import random
import time
import praw
# Imports all passwords from a hidden file ;)
from pw_bot import *
from reddit.strings import *
logging.basicConfig(level = logging.INFO)
# if str(submission.title)[0:4] for submission in subreddit.get_new(limit = 1):
# author = submission.author
# print(author)
# time.sleep(5)
# if str(author).lower() == "klokinator":
# print("TEST!")
# time.sleep(5).lower() == "part":
def remove(filename, who):
with open(filename, "r+") as f:
d = f.readlines()
f.seek(0)
for i in d:
if str(who) != i:
f.write(i)
f.truncate()
def getlist(filename, otherls = None):
templs = []
if otherls is None:
otherls = templs
with open(filename, 'r') as file:
for line in file:
linelen = len(line)
newlinelen = linelen - 1
if line[:newlinelen] not in otherls:
templs.append(line[:newlinelen])
return templs
def write(file, stuff):
with open(file, 'a') as file:
file.write(str(stuff))
def domsgs():
messages = r.inbox.messages()
# For every message in the messages you just fetched:
for message in messages:
print("Opening message!")
alreadyin = getlist('list.txt')
already_done = getlist('done.txt')
# If the message talks about subscription, and if the author hasn't already been added and the id isn't done:
if "unsubscribe" in str(message.body).lower() and str(message.author) in alreadyin and str(
message.id) not in already_done:
message.reply(pm_unsub)
remove('list.txt', message.author)
already_done.append(message.id)
write('done.txt', str(message.id) + "\n")
elif "subscribe" in str(message.body).lower() and str(message.author) not in alreadyin \
and str(message.id) not in already_done:
print("Adding someone! - " + str(message.author))
# Double check to attempt to double-post proof.
if str(message.author) not in alreadyin:
# Write the sender's name in the username list.
# Tells the sender they've been added.
try:
write('list.txt', str(message.author) + "\n")
message.reply(pm_sub)
except Exception as e:
print(e)
time.sleep(2)
# Adds their name to the ID list and stuff.
alreadyin.append(message.author)
already_done.append(message.id)
write('done.txt', str(message.id) + "\n")
def dothrd():
# For thread in the subreddit, out of the newest thread.
for submission in subreddit.get_new(limit = 1):
time.sleep(3)
# Set variables to prevent annoying the reddit api.
author = submission.author
title = str(submission.title)
id_ = str(submission.id)
# Same as message checking but for threads.
fixit = getlist('parts.txt')
# If the author is Klok and it begins with part, do this:
if str(author).lower() == "klokinator" and title[0:4].lower() == "part" and id_ not in fixit or str(
author).lower() == "thomas1672" and title[0:4].lower() == "test" and id_ not in fixit:
write('parts.txt', id_ + "\n")
with open('lastpart.txt', 'r') as file:
lastprt = file.readline()
nxtparts = r.submission(lastprt)
nxtpart = nxtparts.comments[0]
add = nxtpart.body + "\n" + "\n" + "**[" + submission.title + "](" + submission.permalink + ")**"
nxtpart.edit(add)
prevurl = r.submission(id = nxtpart.parent_id).permalink
uwc = []
wc = 0
for i in str(submission.selftext).split():
if i not in uwc:
wc += 1
uwc.append(i)
# Post the comment on the thread.
postedcomment = submission.add_comment(replystr.format(chars = str(len(submission.selftext)),
words = str(len(str(submission.selftext).split())),
uwords = str(wc),
prevurl = prevurl))
with open('lastpart.txt', 'w') as file:
file.write(str(postedcomment.permalink))
submission.set_flair("STORY", "story")
# Sticky the comment that was just posted.
postedcomment.mod.distinguish(sticky = True)
# Get the index list's ID.
toedit = r.submission(id = '56tvbw')
time.sleep(2)
# Add post that was just posted to the index list.
tempedit = toedit.selftext
putin = tempedit + "\n" + "\n" + "[" + submission.title + "](" + submission.permalink + ")"
time.sleep(2)
toedit.edit(putin)
time.sleep(2)
if title[0:4].lower() != "test":
# Put all users in the username file into a list, then:
alreadyin = getlist('list.txt')
finished = []
# For every name in the list, send them this message with the link to the part.
for name in alreadyin:
try:
r.redditor(name).message("New Post!", pmbody.format(title = title,
permalink = submission.permalink))
finished.append(str(name))
except Exception as ex:
print(ex)
print(name)
write('offenders.txt', name + "\n")
time.sleep(1)
time.sleep(10)
todo = getlist('list.txt', finished)
for name in todo:
try:
r.redditor(name).message("New Post!", pmbody.format(title = title,
permalink = submission.permalink))
except Exception as ex:
print(ex)
print(name)
write('offenders.txt', name + "\n")
torem = name + "\n"
remove('list.txt', torem)
time.sleep(1)
def docomts():
# Loops through every comment in the sub.
for comment in subreddit.comments():
# Opens file with comment ids.
already_done = getlist('done.txt')
# If someone's tagging us and we've not processed their comment:
if not ("/u/cryopodbot" in str(comment.body).lower() and str(comment.id) not in already_done):
continue
# If it's talking about the post, comment the post.
if "post" in str(comment.body).lower():
# Make sure the bot doesn't respond to itself.
if str(comment.author).lower() != "cryopodbot":
# Reply!
comment.reply(replystr2)
# Post the ID to a file to prevent duplicates.
write('done.txt', str(comment.id) + "\n")
# If the post wants a flair and it's me or Klok:
elif "flair info" in str(comment.body).lower() and (str(comment.author).lower() == "thomas1672" or
str(comment.author).lower() == "klokinator"):
flairsubmtoset = r.submission(id = str(comment.parent_id)[-6:])
# Flair and stop duplicate flairing (would only waste processor time)
flairsubmtoset.mod.flair("INFO", "info")
write('done.txt', str(comment.id) + "\n")
elif "flair question" in str(comment.body).lower() and (str(comment.author).lower() == "thomas1672" or
str(comment.author).lower() == "klokinator"):
flairsubmtoset = r.submission(id = str(comment.parent_id)[-6:])
flairsubmtoset.mod.flair("QUESTION", "question")
write('done.txt', str(comment.id) + "\n")
elif str(comment.author).lower() != "cryopodbot":
select = int(random.randint(0, 10))
try:
reply = randreply[select]
except IndexError:
reply = randdef
comment.reply(reply)
write('done.txt', str(comment.id) + "\n")
if __name__ == '__main__':
user_agent = "CryoBot 1.0"
# Starts the main section of the reddit bot and assigns it to r.
r = praw.Reddit(
user_agent = user_agent,
username = REDDIT_USERNAME,
password = REDDIT_PASS,
client_id = CLIENT_ID,
client_secret = CLIENT_SECRET
)
# Connects to the TCTH sub.
subreddit = r.subreddit("thecryopodtohell")
domsgs()
dothrd()
docomts()