forked from TGWaffles/cryopodbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
247 lines (247 loc) · 10.4 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
#!/usr/bin/python
import sys
import logging
import discord
import asyncio
import praw
import pdb
import re
import random
import time
#Imports all passwords from a hidden file ;)
from pw_bot import *
logging.basicConfig(level=logging.INFO)
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)
#Connects to the TCTH sub.
subreddit = r.get_subreddit("thecryopodtohell")
#Logs into Bot's Account from hidden file above.
r.login(REDDIT_USERNAME, REDDIT_PASS)
#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)
#if str(submission.title)[0:4].lower() == "part":
#Fetches all messages sent to the bot.
def removel(who):
f = open("list.txt","r+")
d = f.readlines()
f.seek(0)
for i in d:
if str(who) != i:
f.write(i)
f.truncate()
f.close()
messages = r.get_messages()
already_done = []
alreadyin = []
#For every message in the messages you just fetched:
for message in messages:
print("Opening message!")
#Open the username list
file = open('list.txt', 'r+')
#Add names from a username to a list and post ids to another.
for line in file:
linelen = len(line)
newlinelen = linelen - 1
if line[:newlinelen] not in alreadyin:
alreadyin.append(line[:newlinelen])
otherfile = open('done.txt', 'r+')
for i in range(2):
for line in otherfile:
linelen = len(line)
newlinelen = linelen - 1
if line[:newlinelen] not in already_done:
already_done.append(line[:newlinelen])
#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("BOT: You've been unsubscribed!")
f = open("list.txt","r+")
d = f.readlines()
f.seek(0)
for i in d:
if str(message.author) not in i:
f.write(i)
f.truncate()
f.close()
already_done.append(message.id)
otherfile.write(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:
file.write(str(message.author) + "\n")
message.reply("BOT: Thanks, you've been added to the list!")
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)
otherfile.write(str(message.id) + "\n")
otherfile.close()
file.close()
#Empty list to prevent double posts.
fixit = []
#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.
file = open('parts.txt','r+')
for line in file:
linelen = len(line)
newlinelen = linelen - 1
if line[:newlinelen] not in fixit:
fixit.append(line[:newlinelen])
#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:
file.write(id + "\n")
file.close()
file = open('lastpart.txt', 'r')
lastprt = file.readline()
file.close()
alreadyin = []
finished = []
todo = []
nxtparts = r.get_submission(lastprt)
nxtpart = nxtparts.comments[0]
add = nxtpart.body + "\n" + "\n" + "**[" + submission.title + "](" + submission.permalink + ")**"
nxtpart.edit(add)
prev = r.get_info(thing_id=nxtpart.parent_id)
prevurl = prev.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("Hi. I'm a bot, bleep bloop." + "\n" + "\n" + "\n" + "\n" + "If you want to chat with 200+ fellow Cryopod readers, join the Discord at https://discord.gg/6JtsQJR" + "\n" + "\n" + "\n" + "[Click Here to be PM'd new updates!](https://np.reddit.com/message/compose/?to=CryopodBot&subject=Subscribe&message=Subscribe) " + "[Click Here to unsubscribe!](https://np.reddit.com/message/compose/?to=CryopodBot&subject=unsubscribe&message=unsubscribe)" + "\n" + "\n" + "\n" + "If you want to donate to Klokinator, send paypal gifts to [email protected], but be sure to mark it as a gift or Paypal takes 10%. " + "\n" + "\n" + "Patreon can also be pledged to [here!](https://www.patreon.com/klokinator)" + "\n" + "\n" + "This part consisted of: " + str(len(submission.selftext)) + " characters, " + str(len(str(submission.selftext).split())) + " words, and " + str(wc) + " unique words!" + "\n" + "\n" + "[" + "Previous Part" + "](" + prevurl + ")")
file = open('lastpart.txt', 'w')
file.write(str(postedcomment.permalink))
file.close()
file = open('list.txt', 'r+')
submission.set_flair("STORY", "story")
#Sticky the comment that was just posted.
postedcomment.distinguish(sticky=True)
#Get the index list's ID.
toedit = r.get_submission(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:
for line in file:
linelen = len(line)
newlinelen = linelen -1
if line[:newlinelen] not in alreadyin:
alreadyin.append(line[:newlinelen])
#For every name in the list, send them this message with the link to the part.
for name in alreadyin:
try:
r.send_message(name, "New Post!", "New Post on /r/TheCryopodToHell! - [" + title + "](" + submission.permalink + ")")
finished.append(str(name))
except Exception as ex:
print(ex)
print(name)
f = open('offenders.txt','r+')
f.write(name + "\n")
f.close()
time.sleep(1)
time.sleep(10)
for line in file:
linelen = len(line)
newlinelen = linelen -1
if line[:newlinelen] not in finished:
todo.append(line[:newlinelen])
for name in todo:
try:
r.send_message(name, "New Post!", "New Post on /r/TheCryopodToHell! - [" + title + "](" + submission.permalink + ")")
except Exception as ex:
print(ex)
print(name)
f = open('offenders.txt','r+')
f.write(name + "\n")
f.close()
torem = name + "\n"
removel(torem)
time.sleep(1)
file.close()
else:
file.close()
#Gets all comments in the subreddit, then flattens them.
subreddit_comments = subreddit.get_comments()
subcomments = praw.helpers.flatten_tree(subreddit_comments)
#Loops through every comment in the sub.
for comment in subcomments:
#Opens file with comment ids.
otherfile = open('done.txt','r+')
#Do it twice to make sure.
for i in range(2):
for line in otherfile:
linelen = len(line)
newlinelen = linelen - 1
if line[:newlinelen] not in already_done:
already_done.append(line[:newlinelen])
#If someone's tagging us and we've not processed their comment:
if "/u/cryopodbot" in str(comment.body).lower() and str(comment.id) not in already_done:
#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("Hi. I'm a bot, bleep bloop." + "\n" + "\n" + "If you're about to post regarding a typo and this Part was just posted, please wait ten minutes, refresh, and then see if it's still there!" + "\n" + "\n" + "Also, if you want to report typos anywhere, please respond to this bot to keep the main post clutter free. Thank you!" + "\n" + "\n" + "\n" + "[Click Here to be PM'd new updates!](https://np.reddit.com/message/compose/?to=CryopodBot&subject=Subscribe&message=Subscribe) " + "[Click Here to unsubscribe!](https://np.reddit.com/message/compose/?to=CryopodBot&subject=unsubscribe&message=unsubscribe)" + "\n" + "\n" + "\n" + "If you want to donate to Klokinator, send paypal gifts to [email protected], but be sure to mark it as a gift or Paypal takes 10%. " + "\n" + "\n" + "Patreon can also be pledged to [here!](https://www.patreon.com/klokinator)")
#Post the ID to a file to prevent duplicates.
otherfile.write(str(comment.id) + "\n")
#If the post wants a flair and it's me or Klok:
elif "flair info" in str(comment.body).lower():
if str(comment.author).lower() == "thomas1672" or str(comment.author).lower() == "klokinator":
flairsubmtoset = r.get_submission(submission_id=str(comment.parent_id)[-6:])
#Flair and stop duplicate flairing (would only waste processor time)
flairsubmtoset.set_flair("INFO", "info")
otherfile.write(str(comment.id) + "\n")
elif "flair question" in str(comment.body).lower():
if str(comment.author).lower() == "thomas1672" or str(comment.author).lower() == "klokinator":
flairsubmtoset = r.get_submission(submission_id=str(comment.parent_id)[-6:])
flairsubmtoset.set_flair("QUESTION", "question")
otherfile.write(str(comment.id) + "\n")
elif str(comment.author).lower() != "cryopodbot":
select = int(random.randint(0,10))
if select == 1:
comment.reply("You called? ;)")
elif select == 2:
comment.reply("What's up?")
elif select == 3:
comment.reply("Hey!")
elif select == 4:
comment.reply("Go check out my discord at https://github.com/TGWaffles/cryopodbot")
elif select == 5:
comment.reply("Tagging me in a post can trigger specific things. One of the random replies means I didn't understand what you asked!")
elif select == 6:
comment.reply("Yo, 'sup?")
elif select == 7:
comment.reply("Go check out Klok's patreon [here!](https://www.patreon.com/klokinator)")
elif select == 8:
comment.reply("I was coded by /u/thomas1672 - direct all questions to him!")
elif select == 9:
comment.reply("Now taking suggestions for more of these random supplies in the discord!")
else:
comment.reply("Join the discord @ https://discord.gg/EkdeJER")
otherfile.write(str(comment.id) + "\n")
#Re-Save the file.
otherfile.close()