Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- # Define Constants
- REMOVAL_MINS = 10
- QUESTION_MINS = 15
- FIX_MINS = 1440
- # Login
- import praw
- from termcolor import colored
- def login():
- reddit = praw.Reddit(client_id='PmexbBBaeW8rVQ',
- client_secret='uN6kuwueKzp3z9x7oh34gdy__jQ',
- password='postmodbot',
- user_agent='Helps moderators',
- username='FEH_bot')
- print colored('Logged in as: ' + str(reddit.user.me()), 'green')
- return reddit
- r = login()
- # Initialize Database
- # how to fix error like this: require(...) is not a function
- import sqlite3
- conn = sqlite3.connect('posts.db')
- c = conn.cursor()
- # rposts --> id, created, isremoved
- # get id, time created, and removal status from post if it exists
- # otherwise, return False
- def getInfo(ident):
- t = (ident, )
- for row in c.execute('''SELECT * FROM rposts WHERE id=?''', t):
- postId = row[0]
- time_created = row[1]
- isRemoved = row[2]
- return [postId, time_created, isRemoved]
- return False
- # list all posts in the database
- def listAll():
- for row in c.execute('''SELECT * FROM rposts'''):
- print row
- # add a post for the first time when removing it
- def addPost(
- ident,
- timec,
- parId,
- isRem,
- ):
- ident = "'" + ident + "'"
- timec = "'" + timec + "'"
- parId = "'" + parId + "'"
- isRem = "'" + isRem + "'"
- c.execute('''INSERT INTO rposts VALUES ({}, {}, {}, {})'''.format(ident,
- timec, parId, isRem))
- # delete all posts (only used for bugfixing)
- def deleteAll():
- c.execute('''DELETE FROM rposts''')
- # delete post with specific id from database
- def deleteSpec(ident):
- t = (ident, )
- c.execute('''DELETE FROM rposts WHERE id=?''', t)
- # return the age of a post in minutes
- def checkAge(post):
- ctime = time.time() + 28800
- difference = float(ctime) - post.created
- return difference / 60
- # first main function; naive check for old questions
- def checkQuestion(post):
- if checkAge(post) > QUESTION_MINS \
- and str(post.link_flair_text).lower() == 'quick question' \
- and getInfo(post.id) == False:
- myComment = \
- 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:
- 1.) Submit the question again in an hour
- 2.) Ask your question again in the daily thread located [here](https://www.reddit.com/r/FireEmblemHeroes/comments/60by0t/rfireemblemheroes_megathread_index_questions/).
- If you believe that your post should not have been removed, contact the moderators in order to request that it be restored.''')
- myComment.mod.distinguish(how='yes', sticky=True)
- post.mod.remove()
- # second main function; check for old, unflaired posts
- def checkForOld(post):
- if checkAge(post) > REMOVAL_MINS and post.link_flair_text == None \
- and getInfo(post.id) == False:
- myComment = \
- 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.
- Flair | Command
- ---|---
- Quick Question | -question
- Discussion | -discussion
- Analysis | -analysis
- News | -news
- Fan Art | -fanart
- Chat | -chat
- Humor | -humor
- If you believe that this removal was a mistake, please contact the moderators.''')
- myComment.mod.distinguish(how='yes', sticky=True)
- post.mod.remove()
- addPost(post.id, str(post.created), myComment.id, 'true')
- # third main function; database checker/complex question checking
- def checkDatabase():
- for row in c.execute('''SELECT * FROM rposts'''):
- postId = row[0]
- postCreated = row[1]
- parId = row[2]
- postRemoved = row[3]
- actualPost = r.submission(id=postId)
- if str(actualPost.author) == 'None':
- deleteSpec(actualPost.id)
- elif postRemoved == 'true':
- if checkAge(actualPost) > FIX_MINS:
- r.comment(id=parId).edit('This post is older than 24 hours and can no longer be reapproved. Please resubmit and flair in time.'
- )
- deleteSpec(actualPost.id)
- elif str(actualPost.link_flair_text).lower() \
- == 'quick question':
- r.comment(id=parId).delete()
- actualPost.mod.approve()
- deleteSpec(actualPost.id)
- addPost(postId, str(time.time() + 28800), 'None',
- 'false')
- elif actualPost.link_flair_text != None:
- r.comment(id=parId).delete()
- actualPost.mod.approve()
- deleteSpec(actualPost.id)
- elif (float(time.time()) + 28800 - float(postCreated)) / 60 \
- > QUESTION_MINS:
- myComment = \
- actualPost.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:
- 1.) Submit the question again in an hour
- 2.) Ask your question again in the daily thread located [here](https://www.reddit.com/r/FireEmblemHeroes/comments/60by0t/rfireemblemheroes_megathread_index_questions/).
- If you believe that your post should not have been removed, contact the moderators in order to request that it be restored.''')
- deleteSpec(actualPost.id)
- myComment.mod.distinguish(how='yes', sticky=True)
- actualPost.mod.remove()
- def runTimeBot():
- for post in r.subreddit('FireEmblemHeroes').new(limit=20):
- if str(post.author).lower() != 'automoderator':
- checkQuestion(post)
- checkForOld(post)
- checkDatabase()
- conn.commit()
- while True:
- try:
- runTimeBot()
- except:
- pass
- conn.close()
Add Comment
Please, Sign In to add comment