Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import praw
- from termcolor import colored
- import time
- #Log into PRAW with script credentials
- r = praw.Reddit(client_id='<your id>',
- client_secret='<your secret>',
- password='<your password>',
- user_agent='Advanced Flair Moderation Bot',
- username='FlairTimeBot')
- print(colored('Logged in as: ' + str(r.user.me()), 'green'))
- #Generate comment for a subreddit
- #Using their flairs and wiki settings
- def generateComment(subm, mins):
- template = '''This post has automatically been removed for not being flaired within {mins} minutes. When the post receives a flair it will automatically be restored. Reply to your thread with one of the following commands and the bot will flair it accordingly.\n\n
- Flair | Command
- ---|---
- {flairs}
- \n\n
- If you believe that this removal was a mistake, please [contact the moderators.](https://www.reddit.com/message/compose?to=%2Fr%2F{subr})'''.format(mins=mins, flairs=getChoices(subm, True), subr=subm.subreddit)
- return template
- def generateWiki(sub):
- print('GENNNERRATTTINGGG')
- r.subreddit(sub).wiki.create('FlairTimeBot', '''#This is the configuration page for /u/FlairTimeBot. Do not adjust any of the text, only the values!
- \n\n**Time until posts without flair are removed (minutes):** 10
- \n\n**Time users have to add flair after post is removed (hours; max is 72):** 24
- ''', reason='Initializing Flair Time Bot')
- def getSettings(subr):
- wiki = r.subreddit(subr).wiki['FlairTimeBot']
- wCon = str(wiki.content_md).split('values!')[1]
- mins = float(str(wCon.split(':')[1])[0:8].strip('**').strip(' ').replace(' ','').replace('\n','').replace('\r',''))
- hrs = float(str(wCon.split(':')[2])[:].strip('**').strip(' ').replace(' ','').replace('\n','').replace('\r',''))
- return mins, hrs
- def getChoices(subm, isReport):
- if isReport:
- return '\n'.join([str(flair['flair_text']) + ' | -' + str(flair['flair_text']) for flair in subm.flair.choices()])
- else:
- flist = [str(flair['flair_text']) for flair in subm.flair.choices()]
- fclist = [str(flair['flair_css_class']) for flair in subm.flair.choices()]
- mdict = dict(zip([x.lower() for x in flist], fclist))
- return flist, mdict
- #Initialize Database
- import sqlite3
- conn = sqlite3.connect('genposts.db');
- c = conn.cursor()
- c.execute('''CREATE TABLE IF NOT EXISTS rposts (id TEXT, created TEXT, parentId TEXT, isremoved TEXT);''')
- #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
- #second main function; check for old, unflaired posts
- def checkForOld(post, qmins):
- if checkAge(post)>qmins and post.link_flair_text == None and getInfo(post.id) == False:
- myComment = post.reply(generateComment(post, qmins))
- 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)
- qmins, fmins = getSettings(str(actualPost.subreddit))
- fmins *= 60
- if str(actualPost.author) == 'None':
- deleteSpec(actualPost.id)
- elif postRemoved == 'true':
- if checkAge(actualPost) > fmins:
- r.comment(id=parId).edit('This post is older than {} hours and can no longer be reapproved. Please resubmit and flair in time.'.format(fmins/60))
- deleteSpec(actualPost.id)
- elif actualPost.link_flair_text != None:
- r.comment(id=parId).delete()
- actualPost.mod.approve()
- deleteSpec(actualPost.id)
- def correctPerms(subr):
- for moderator in r.subreddit(subr).moderator():
- if str(moderator).lower() == 'flairtimebot':
- mperms = moderator.mod_permissions
- if 'all' in mperms:
- return True
- elif 'wiki' not in mperms or 'posts' not in mperms or 'flair' not in mperms:
- return False
- else:
- return True
- def checkInvites():
- for message in r.inbox.unread(limit=5):
- message.mark_read()
- if message.body.startswith('**gadzooks!') and message.subreddit != None:
- try:
- r.subreddit(str(message.subreddit)).mod.accept_invite()
- if not correctPerms(str(message.subreddit)):
- r.subreddit(str(message.subreddit)).moderator.leave()
- message.reply('Error: Incorrect permissions. This bot requires the wiki, post, and flair permissions to function properly. Please re-invite it accordingly.')
- else:
- generateWiki(str(message.subreddit))
- print('GenXd Page')
- message.reply('Setup successful. Thank you for using /u/FlairTimeBot! You can access the configuration here: https://www.reddit.com/r/{}/wiki/flairtimebot\n\nAny complaints or bug reports should be sent to /u/--Nylon.'.format(str(message.subreddit)))
- except:
- message.reply('There was an error joining the subreddit. Please try again or contact /u/--Nylon for support.')
- try:
- r.subreddit(str(message.subreddit)).moderator.leave()
- except:
- pass
- elif message.body.startswith('**gadzooks!') and message.subreddit == None:
- message.reply('Nice try, I\'m uncrashable ;)')
- def checkComments():
- for comment in r.inbox.comment_replies(limit=30):
- if comment.id not in open('dcomments.txt', 'r').read().split('\n') and str(comment.body)[0] == '-' and str(comment.author).lower() == str(comment.parent().parent().author).lower():
- open('dcomments.txt','a').write(comment.id + '\n')
- if 'older than' in str(comment.parent().body):
- r.redditor(str(comment.author)).message('Unable to assign flair to post.', '**Error:** Post is no longer eligible to be flaired (too old).')
- return False
- subm = comment.parent().parent()
- pflair = str(comment.body).strip('-').rstrip()
- flair_list, fdict = getChoices(subm, False)
- if pflair.lower() in [x.lower() for x in flair_list]:
- subm.mod.flair(text=flair_list[[x.lower() for x in flair_list].index(pflair.lower())], css_class=fdict[pflair.lower()])
- comment.mod.remove()
- else:
- r.redditor(str(comment.author)).message('Unable to assign flair to your post.', '**Error:** Invalid flair option ({}).'.format(pflair))
- def checkInbox():
- checkInvites()
- checkComments()
- def runTimeBot():
- for post in r.subreddit('mod').new(limit=50):
- mlist = [str(mod) for mod in r.subreddit(str(post.subreddit)).moderator]
- if str(post.author).lower() != "automoderator" and str(post.author) not in mlist:
- mins, hrs = getSettings(str(post.subreddit))
- checkForOld(post, mins)
- checkDatabase()
- conn.commit()
- checkInbox()
- while True:
- runTimeBot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement