Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.22 KB | None | 0 0
  1. import praw
  2. import config
  3. import time
  4. import re
  5. import datetime
  6. import os
  7.  
  8. def bot_login():
  9. print ("\nLoggin in...")
  10. r = praw.Reddit(username = config.username,
  11. password = config.password,
  12. client_id = config.client_id, # Messy stuff at the beginning
  13. client_secret = config.client_secret,
  14. user_agent = "r/rotmg autoremoval bot")
  15. print ("I've logged on!\n")
  16.  
  17. return r
  18.  
  19.  
  20. limit = 5
  21. version = "0.7"
  22. Question_thread_goal = 50
  23. Fame_train_goal = 10
  24. Guild_recruitment_goal = 10
  25. Lost_halls_goal = 10
  26. Returning_player_goal = 10
  27. boolean = 0
  28.  
  29. weekly_question_thread_link = "https://www.reddit.com/r/RotMG/comments/6soxcy/" # Posted August 10th
  30.  
  31.  
  32.  
  33. # Main Function
  34.  
  35. def main(r, posts_replied_to): # No idea why it needs r
  36. print("Start of a fresh run:")
  37. count = 0
  38. now = datetime.datetime.now()
  39. for submission in r.subreddit('rotmg').new(limit=limit): # For each of the submissions on r/rotmg/new, up to submission # limit...
  40.  
  41.  
  42. count = count + 1 # Numbering each of the submissions for easy reading in the terminal
  43.  
  44. # Nasty set of if statements for each regex needing to be checked, very inneficient but it works
  45. # A demo with comments is below:
  46.  
  47. # infractions = 0 # number of penalty points accumulated
  48.  
  49. # matcher = re.match("Does",submission.title) # Regex for the if statement, chenges every if statement
  50.  
  51. # if(matcher != None): # If the matcher found something in the body of the post
  52. # print("Success!") # Debugger
  53. # infractions = infractions + 10 # Add penalty points
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. # Start of Question Thread Checker
  63.  
  64. print("\n " + str(count) + ": " + submission.title)
  65. author = submission.author # Getting a reddit user object
  66. print(" Author: " + author.name) # Gets the username of the reddit user
  67. infractions = 0
  68.  
  69. matcher = re.match(".+\?",submission.title) # Question mark at the end of the title
  70. if(matcher != None):
  71. infractions = infractions + 40
  72. print(" Infractions: " + str(infractions))
  73. print(" Number: 1")
  74.  
  75. matcher = re.match("(?i)(which|what) (character|pet|weapon|character) (?:\w+ ?){0,4} (max|feed|fuse|hatch|equip|buy|)",submission.selftext)
  76. if(matcher != None):
  77. infractions = infractions + 10
  78. print(" Infractions: " + str(infractions))
  79. print(" Number: 2 \n")
  80.  
  81. matcher = re.match("(?i)(how|what) (good|bad|terrible|awesome|amazing|okay) (?:\w+ ?){0,5} (skull|robe|spell|helm|wand|bow|armor|UT|shield|trap|ring)",submission.title)
  82. if(matcher != None):
  83. infractions = infractions + 10
  84. print(" Infractions: " + str(infractions))
  85. print(" Number: 3 \n")
  86.  
  87. matcher = re.match("(?i)(how|what) (good|bad|terrible|awesome|amazing|okay) (?:\w+ ?){0,5} (skull|robe|spell|helm|wand|bow|armor|UT|shield|trap|ring)",submission.selftext)
  88. if(matcher != None):
  89. infractions = infractions + 10
  90. print(" Infractions: " + str(infractions))
  91. print(" Number: 4 \n")
  92.  
  93. if(infractions >= Question_thread_goal and author.name not in whitelisted_users): # Checks if enough infractions were accumulated to remove the post at the end
  94. if(submission.id not in posts_replied_to):
  95. #submission.remove(spam=False) (Not on a moderator account atm)
  96. submission.reply("**Autodetect: Question.** Please comment your question on the [stickied weekly question thread] (" +
  97. weekly_question_thread_link + ") instead of making a post on the subreddit, thank you! \n \n" +
  98. "--- \n \n ^(I am a bot, and this action was performed automatically) ^| ^(Version: " + version + " (Beta)^) ^| ^(Reply to leave feedback)")
  99.  
  100. print(" I've left a weekly question thread comment! \n")
  101.  
  102. posts_replied_to.append(submission.id)
  103.  
  104. with open ("posts_replied_to.txt", "a") as f:
  105. f.write(submission.id + "\n")
  106.  
  107.  
  108. else:
  109. print(" This post has already been replied to!")
  110.  
  111.  
  112. # End of Question Thread Checker
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126. # Start of Fame Train Checker
  127.  
  128. infractions = 0
  129.  
  130. matcher = re.match("(?i)Looking for (?:\w+ ?){0,4} \w*train discord",submission.title)
  131. if(matcher != None):
  132. infractions = infractions + 10
  133. print(" Infractions: " + str(infractions))
  134. print(" Number: 5 \n")
  135.  
  136.  
  137. matcher = re.match("(?i)Looking for \w*train discord",submission.title)
  138. if(matcher != None):
  139. infractions = infractions + 10
  140. print(" Infractions: " + str(infractions))
  141. print(" Number: 6 \n")
  142.  
  143.  
  144. matcher = re.match("(?i)(where|what) is (?:\w+ ?){0,4} (\w*train|\w*train discord)",submission.title)
  145. if(matcher != None):
  146. infractions = infractions + 10
  147. print(" Infractions: " + str(infractions))
  148. print(" Number: 7 \n")
  149.  
  150. matcher = re.match("(?i)what server (?:\w+ ?){0,4} \w*train",submission.title)
  151. if(matcher != None):
  152. infractions = infractions + 10
  153. print(" Infractions: " + str(infractions))
  154. print(" Number: 7 \n")
  155.  
  156. if(infractions >= Fame_train_goal and author.name not in whitelisted_users):
  157. if(submission.id not in posts_replied_to):
  158. #submission.remove(spam=False) (Not on a moderator account atm)
  159. submission.reply("**Autodetect: Fame Train.** The Fame Train, previously located on the server EUN2, now moves around to whichever " +
  160. "realm provides the best fpm (fame per minute). Their current location can be found in the #click-here-for-train channel of their discord: " +
  161. "https://discord.gg/pVGQe7g. \n \n" +
  162. "--- \n \n ^(I am a bot, and this action was performed automatically) ^| ^(Version: " + version + " (Beta)^) ^| ^(Reply to leave feedback)")
  163. print(" I've left a fame train comment! \n")
  164.  
  165. posts_replied_to.append(submission.id)
  166.  
  167. with open ("posts_replied_to.txt", "a") as f:
  168. f.write(submission.id + "\n")
  169.  
  170. else:
  171. print(" This post has already been replied to!")
  172.  
  173. # End of Fame Train Checker
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186. # Start of Guild Recruitment Checker
  187.  
  188. infractions = 0
  189.  
  190. matcher = re.match("(?i)Recruiting for",submission.title)
  191. if(matcher != None):
  192. infractions = infractions + 10
  193. print(" Infractions: " + str(infractions))
  194. print(" Number: 8 \n")
  195.  
  196. matcher = re.match("(?i)Looking for (?:\w+ ?){0,3} guild",submission.title)
  197. if(matcher != None):
  198. infractions = infractions + 10
  199. print(" Infractions: " + str(infractions))
  200. print(" Number: 9 \n")
  201.  
  202. matcher = re.match("(?i)Looking for (?:\w+ ?){0,3} guild",submission.selftext)
  203. if(matcher != None):
  204. infractions = infractions + 10
  205. print(" Infractions: " + str(infractions))
  206. print(" Number: 10 \n")
  207.  
  208. matcher = re.match("(?i)Requirement (?:\w+ ?){0,4} (./8|fame)",submission.selftext)
  209. if(matcher != None):
  210. infractions = infractions + 10
  211. print(" Infractions: " + str(infractions))
  212. print(" Number: 11 \n")
  213.  
  214. matcher = re.match("(?i)Requirement (?:\w+ ?){0,4} (./8|fame)",submission.title)
  215. if(matcher != None):
  216. infractions = infractions + 10
  217. print(" Infractions: " + str(infractions))
  218. print(" Number: 12 \n")
  219.  
  220.  
  221. if(infractions >= Guild_recruitment_goal and author.name not in whitelisted_users):
  222. if(submission.id not in posts_replied_to):
  223. #submission.remove(spam=False) (Not on a moderator account atm)
  224.  
  225. submission.reply("**Autodetect: Guild Recruitment/Looking to join a guild.** Please post over at the " +
  226. "[Realmeye Forums'] (https://www.realmeye.com/forum/c/guilds) guild recruitment section instead of reddit, thanks! \n \n" +
  227. "--- \n \n ^(I am a bot, and this action was performed automatically) ^| ^(Version: " + version + " (Beta)^) ^| ^(Reply to leave feedback)")
  228.  
  229. print(" I've left a guild recruitment comment! \n")
  230.  
  231. posts_replied_to.append(submission.id)
  232.  
  233. with open ("posts_replied_to.txt", "a") as f:
  234. f.write(submission.id + "\n")
  235.  
  236. else:
  237. print(" This post has already been replied to!")
  238.  
  239. # End of Guild Recruitment Checker
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253. # Beginning of Lost Halls Checker
  254.  
  255. infractions = 0
  256.  
  257. matcher = re.match("(?i)(where|what) (?:\w+ ?){0,4} (halls|lost|LH|lost halls) (discord|invite)",submission.title)
  258. if(matcher != None):
  259. infractions = infractions + 10
  260. print(" Infractions: " + str(infractions))
  261. print(" Number: 13 \n")
  262.  
  263. matcher = re.match("(?i)(where|what) (?:\w+ ?){0,4} (halls|lost|LH|lost halls) (discord|invite)",submission.selftext)
  264. if(matcher != None):
  265. infractions = infractions + 10
  266. print(" Infractions: " + str(infractions))
  267. print(" Number: 14 \n")
  268.  
  269.  
  270. matcher = re.match("(?i)Looking for (?:\w+ ?){0,4} (Lost|halls|LH|lost halls) (discord|invite)",submission.title)
  271. if(matcher != None):
  272. infractions = infractions + 10
  273. print(" Infractions: " + str(infractions))
  274. print(" Number: 15 \n")
  275.  
  276. matcher = re.match("(?i)Looking for (?:\w+ ?){0,4} (Lost|halls|LH|lost halls) (discord|invite)",submission.selftext)
  277. if(matcher != None):
  278. infractions = infractions + 10
  279. print(" Infractions: " + str(infractions))
  280. print(" Number: 16 \n")
  281.  
  282. matcher = re.match("(?i)(Link|invite) (?:\w+ ?){0,4} (halls|lost|LH|lost halls) discord",submission.title)
  283. if(matcher != None):
  284. infractions = infractions + 10
  285. print(" Infractions: " + str(infractions))
  286. print(" Number: 17 \n")
  287.  
  288.  
  289.  
  290. if(infractions >= Lost_halls_goal and author.name not in whitelisted_users):
  291. if(submission.id not in posts_replied_to):
  292. #submission.remove(spam=False) (Not on a moderator account atm)
  293. submission.reply("**Autodetect: Lost Halls discord. The Public Lost Halls discord can be found here: https://discord.gg/EKFPG73.** \n \n"
  294. "--- \n \n ^(I am a bot, and this action was performed automatically) ^| ^(Version: " + version + " (Beta)^) ^| ^(Reply to leave feedback)")
  295. print(" I've left a Lost Halls comment! \n")
  296.  
  297. posts_replied_to.append(submission.id)
  298.  
  299. with open ("posts_replied_to.txt", "a") as f:
  300. f.write(submission.id + "\n")
  301.  
  302. else:
  303. print(" This post has already been replied to!")
  304.  
  305. # End of Lost Halls Checker
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320. # Beginning of Returning Player Checker
  321.  
  322. matcher = re.match("(What|what's|anything) changed",submission.title)
  323. if(matcher != None):
  324. infractions = infractions + 10
  325. print(" Infractions: " + str(infractions))
  326. print(" Number: 18 \n")
  327.  
  328.  
  329.  
  330. if(infractions >= Returning_player_goal and author.name not in whitelisted_users):
  331. if(submission.id not in posts_replied_to):
  332. #submission.remove(spam=False) (Not on a moderator account atm)
  333. submission.reply("**Autodetect: Returning Player. Please check the [Developement History]" +
  334. "(https://www.realmeye.com/wiki/development-and-release-history) on Realmeye and all the [Official Posts](https://www.reddit.com" +
  335. "/r/RotMG/search?sort=new&restrict_sr=on&q=flair%3AOfficial%2BDeca) made " +
  336. "by Deca on reddit for what's changed since you've been gone! \n \n"
  337. "--- \n \n ^(I am a bot, and this action was performed automatically) ^| ^(Version: " + version + " (Beta)^) ^| ^(Reply to leave feedback)")
  338.  
  339. print(" I've left a Returning Player comment! \n")
  340.  
  341. posts_replied_to.append(submission.id)
  342.  
  343. with open ("posts_replied_to.txt", "a") as f:
  344. f.write(submission.id + "\n")
  345.  
  346. else:
  347. print(" This post has already been replied to!")
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361. print ("\n The time is " + str(now.hour) + ":" + str(now.minute) + "\n Finished! Sleeping for 5 minutes... \n") # Gives the time that it finished at
  362. time.sleep(300) # Sleeps for 5 minutes
  363.  
  364.  
  365. def get_saved_posts():
  366. if not os.path.isfile("posts_replied_to.txt"):
  367. posts_replied_to = []
  368. else:
  369. with open("posts_replied_to.txt", "r") as f:
  370. posts_replied_to = f.read()
  371. posts_replied_to = posts_replied_to.split("\n") # Entries in the list are divided by new lines
  372. posts_replied_to = list(filter(None, posts_replied_to)) # Dunno what this does, it works though
  373.  
  374. return posts_replied_to
  375.  
  376.  
  377. def get_whitelisted_users():
  378. if not os.path.isfile("whitelisted_users.txt"):
  379. whitelisted_users = []
  380. else:
  381. with open("whitelisted_users.txt", "r") as f:
  382. whitelisted_users = f.read()
  383. whitelisted_users = whitelisted_users.split("\n")
  384. whitelisted_users = list(filter(None, whitelisted_users))
  385.  
  386. return whitelisted_users
  387.  
  388.  
  389. posts_replied_to = get_saved_posts()
  390.  
  391. r = bot_login() # Not sure what this does either honestly, but it works
  392. posts_replied_to = get_saved_posts() # Creating lists from .txt files for use above
  393. whitelisted_users = get_whitelisted_users()
  394. while True:
  395. main(r, posts_replied_to) # Runs the main function cotninuously when redditbot.py is run
  396.  
  397.  
  398.  
  399. # Notes for myself:
  400. #
  401. # submission.selftext = body of a text post, empty line for link posts
  402. # submission.author = author of a post
  403. # submission.remove(spam=False) = removes a post
  404. # re.match(pattern, string)
  405. # submission.title = title of a post
  406. # comment.reply("I am a cat") = how to reply to a comment
  407. # print now.year, now.month, now.day, now.hour, now.minute, now.second
  408. # 2015 5 6 8 53 40
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement