Guest User

Untitled

a guest
Oct 31st, 2017
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.36 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # Define Constants
  5.  
  6. REMOVAL_MINS = 10
  7. QUESTION_MINS = 30
  8. FIX_MINS = 1440
  9.  
  10. # Login
  11.  
  12. import praw
  13. import time
  14. from termcolor import colored
  15.  
  16.  
  17. def login():
  18.     reddit = praw.Reddit(client_id='PmexbBBaeW8rVQ',
  19.                          client_secret='uN6kuwueKzp3z9x7oh34gdy__jQ',
  20.                          password='postmodbot',
  21.                          user_agent='Helps moderators',
  22.                          username='FEH_bot')
  23.     print(colored('Logged in as: ' + str(reddit.user.me()), 'green'))
  24.     return reddit
  25.  
  26.  
  27. r = login()
  28.  
  29. # Initialize Database
  30. # how to fix error like this: require(...) is not a function
  31.  
  32. import sqlite3
  33. conn = sqlite3.connect('posts.db')
  34. c = conn.cursor()
  35. c.execute('''CREATE TABLE IF NOT EXISTS rposts(id INTEGER, created, parid, isremoved)''')
  36.  
  37. # rposts --> id, created, isremoved
  38.  
  39. # get id, time created, and removal status from post if it exists
  40. # otherwise, return False
  41.  
  42. def getInfo(ident):
  43.     t = (ident, )
  44.     for row in c.execute('''SELECT * FROM rposts WHERE id=?''', t):
  45.         postId = row[0]
  46.         time_created = row[1]
  47.         isRemoved = row[2]
  48.         return [postId, time_created, isRemoved]
  49.     return False
  50.  
  51.  
  52. # list all posts in the database
  53.  
  54. def listAll():
  55.     for row in c.execute('''SELECT * FROM rposts'''):
  56.         print(row)
  57.  
  58.  
  59. # add a post for the first time when removing it
  60.  
  61. def addPost(
  62.     ident,
  63.     timec,
  64.     parId,
  65.     isRem,
  66.     ):
  67.  
  68.     ident = "'" + ident + "'"
  69.     timec = "'" + timec + "'"
  70.     parId = "'" + parId + "'"
  71.     isRem = "'" + isRem + "'"
  72.     c.execute('''INSERT INTO rposts VALUES ({}, {}, {}, {})'''.format(ident,
  73.               timec, parId, isRem))
  74.  
  75.  
  76. # delete all posts (only used for bugfixing)
  77.  
  78. def deleteAll():
  79.     c.execute('''DELETE FROM rposts''')
  80.  
  81.  
  82. # delete post with specific id from database
  83.  
  84. def deleteSpec(ident):
  85.     t = (ident, )
  86.     c.execute('''DELETE FROM rposts WHERE id=?''', t)
  87.  
  88.  
  89. # return the age of a post in minutes
  90.  
  91. def checkAge(post):
  92.     ctime = time.time() + 28800
  93.     difference = float(ctime) - post.created
  94.     return difference / 60
  95.  
  96.  
  97. # first main function; naive check for old questions
  98.  
  99. def checkQuestion(post):
  100.     if checkAge(post) > QUESTION_MINS \
  101.         and str(post.link_flair_text).lower() == 'quick question' \
  102.         and getInfo(post.id) == False:
  103.         myComment = \
  104.             post.reply('''This post has been removed because it was flaired as "Quick Question" and has been up for 15 minutes. If you do not feel that your question was answered to your satisfaction, you may either:
  105.  
  106. 1.) Submit the question again in an hour
  107.  
  108. 2.) Ask your question again in the daily thread located [here](https://www.reddit.com/r/FireEmblemHeroes/comments/60by0t/rfireemblemheroes_megathread_index_questions/).
  109.  
  110. If you believe that your post should not have been removed, contact the moderators in order to request that it be restored.''')
  111.         myComment.mod.distinguish(how='yes', sticky=True)
  112.         post.mod.remove()
  113.  
  114.  
  115. # second main function; check for old, unflaired posts
  116.  
  117. def checkForOld(post):
  118.     if checkAge(post) > REMOVAL_MINS and post.link_flair_text == None \
  119.         and getInfo(post.id) == False:
  120.         myComment = \
  121.             post.reply('''This post has automatically been removed for not being flaired within 10 minutes. When the post receives a flair it will automatically be restored. Reply to your thread with one of the following commands and automod will flair it accordingly.
  122.  
  123.  
  124. Flair | Command
  125. ---|---
  126. Quick Question | -question
  127. Discussion | -discussion
  128. Analysis | -analysis
  129. News | -news
  130. Fan Art | -fanart
  131. Fan Art (OC) | -oc
  132. Chat | -chat
  133. Humor | -humor
  134. Unit Build | -unit
  135. Disgusting | -disgusting
  136. Doing their Best | -doingtheirbest
  137.  
  138.  
  139.  
  140.  
  141. If you believe that this removal was a mistake, please contact the moderators.''')
  142.         myComment.mod.distinguish(how='yes', sticky=True)
  143.         post.mod.remove()
  144.         addPost(post.id, str(post.created), myComment.id, 'true')
  145.  
  146.  
  147. # third main function; database checker/complex question checking
  148.  
  149. def checkDatabase():
  150.     for row in c.execute('''SELECT * FROM rposts'''):
  151.         postId = row[0]
  152.         postCreated = row[1]
  153.         parId = row[2]
  154.         postRemoved = row[3]
  155.         actualPost = r.submission(id=postId)
  156.         if str(actualPost.author) == 'None':
  157.             deleteSpec(actualPost.id)
  158.         elif postRemoved == 'true':
  159.             if checkAge(actualPost) > FIX_MINS:
  160.                 r.comment(id=parId).edit('This post is older than 24 hours and can no longer be reapproved. Please resubmit and flair in time.'
  161.                         )
  162.                 deleteSpec(actualPost.id)
  163.             elif str(actualPost.link_flair_text).lower() \
  164.                 == 'quick question':
  165.                 r.comment(id=parId).delete()
  166.                 actualPost.mod.approve()
  167.                 deleteSpec(actualPost.id)
  168.                 addPost(postId, str(time.time() + 28800), 'None',
  169.                         'false')
  170.             elif actualPost.link_flair_text != None:
  171.  
  172.                 r.comment(id=parId).delete()
  173.                 actualPost.mod.approve()
  174.                 deleteSpec(actualPost.id)
  175.         elif (float(time.time()) + 28800 - float(postCreated)) / 60 \
  176.             > QUESTION_MINS:
  177.             myComment = \
  178.                 actualPost.reply('''This post has been removed because it was flaired as "Quick Question" and has been up for 30 minutes. If you do not feel that your question was answered to your satisfaction, you may either:
  179.  
  180. 1.) Submit the question again in an hour
  181.  
  182. 2.) Ask your question again in the weekly thread located [here](https://www.reddit.com/r/FireEmblemHeroes/comments/6s4gj9/rfireemblemheroes_megathread_index_rules_weekly/).
  183.  
  184. If you believe that your post should not have been removed, contact the moderators in order to request that it be restored.''')
  185.             deleteSpec(actualPost.id)
  186.             myComment.mod.distinguish(how='yes', sticky=True)
  187.             actualPost.mod.remove()
  188.  
  189.  
  190. def runTimeBot():
  191.     for post in r.subreddit('FireEmblemHeroes').new(limit=100):
  192.         if str(post.author).lower() != 'automoderator':
  193.             checkQuestion(post)
  194.             checkForOld(post)
  195.     checkDatabase()
  196.     conn.commit()
  197.  
  198. rtb = runTimeBot()
  199.  
  200. while True:
  201.     try:
  202.         runTimeBot()
  203.     except:
  204.         pass
  205.  
  206. conn.close()
Add Comment
Please, Sign In to add comment