Skip to content

Commit

Permalink
FM_Flair_Bot.py: More robust flair setting options
Browse files Browse the repository at this point in the history
  • Loading branch information
Swaraj committed Jul 26, 2017
1 parent 6f1924b commit b6aa79c
Showing 1 changed file with 41 additions and 20 deletions.
61 changes: 41 additions & 20 deletions src/FM_Flair_Bot.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
import praw
import time
import sqlite3
import Config
import Config_1
from prawcore.exceptions import PrawcoreException

reddit = praw.Reddit(
client_id=Config.client_id,
client_secret=Config.client_secret,
user_agent=Config.user_agent,
username=Config.username,
password=Config.password)
client_id=Config_1.client_id,
client_secret=Config_1.client_secret,
user_agent=Config_1.user_agent,
username=Config_1.username,
password=Config_1.password)

LIMIT = 10 # Number of posts to iterate through in subreddit/new. The more frequent the submissions are in your
LIMIT = 10 # Number of posts to iterate through in subreddit/new. The more frequent the submissions are in your
# subreddit, the higher the number should be. 10 is more than enough for subreddits under ~50k

TIME_LIMIT = 120 # Number of seconds after which post should be removed if not flaired.

SUBREDDIT = reddit.subreddit('fmgtestsub') # Enter subreddit here.

COMMENT_FOOTNOTE = '''\n\n---^^I ^^am ^^a ^^bot. ^^I ^^was ^^created ^^by
[^^John_Yuki](https://www.reddit.com/user/John_Yuki/) ^^and [^^Swaraj](https://github.com/swrj)^^. ^^If ^^you ^^have
^^any ^^questions ^^or ^^need ^^to ^^report ^^a ^^bug/problem, ^^please ^^message ^^John_Yuki ^^on ^^Reddit. '''
# Footnote to add to comments stored in variable.

COMMENT_TEXT = '''Please flair your post. You may also reply to this comment with SETFLAIR followed by a space and one
of the following flair options and I will flair it for you: \n\n''' # First line of comment
for template in SUBREDDIT.flair.link_templates:
COMMENT_TEXT += '* ' + str(template['text']) + ' \n\n' # Adds all the flairs in the subreddit to comment in a list
COMMENT_TEXT += '***You have 15 minutes to reply to this comment with SETFLAIR followed by one of the above options ' \
'before your post is automatically removed. Example - SETFLAIR GUIDE***' # Text in comment after flairs
COMMENT_TEXT += COMMENT_FOOTNOTE # Adds footnote right at the end of the comment

REMOVAL_MESSAGE = '''This post has been removed as it was not flaired in time. Posts must be flaired within 15 minutes
of creation.''' # Removal message to be posted when time limit expires

HELP_REMOVAL_MESSAGE = '''This post has been removed as it was flaired as "HELP". All advice/help posts must go in the
daily thread that is stickied at the top of the subreddit.''' # Message to post before removal when flaired 'HELP'

flair_template = SUBREDDIT.flair.link_templates # returns link templates on specified subreddit
flair_list = [flair['text'] for flair in flair_template]

conn = sqlite3.connect('id_list.db') # Creates a connection object that represents id_list.db
c = conn.cursor() # Creates a cursor object to perform SQL commands
c.execute('CREATE TABLE IF NOT EXISTS Submissions_Commented_On(Submission_ID)') # DB to store unresolved submissions
Expand All @@ -39,20 +48,27 @@
conn.commit() # Saves changes to database

print('Opened database successfully.')
print('Logged in as:', Config.username)
print('Logged in as:', Config_1.username)


def check_comments():
print ("Checking comments...")
all_comments = submission.comments.list()
for comment in all_comments: # Checks to see if user comments to flair a post by replying to comment with a flair
if "SETFLAIR" in comment.body and comment.author == submission.author:
if "SETFLAIR" in comment.body.upper() and comment.author == submission.author:
index = comment.body.find("SETFLAIR") # finds index of SETFLAIR in OP's comment
flair = comment.body[9+index:] # sets flair text as all the text after SETFLAIR in the comment
submission.mod.flair(text=flair.upper(), css_class='') # Flairs the post with the text
comment.mod.remove() # Removes users flair comment
parent = comment.parent()
parent.mod.remove() # Removes bots comment instructing user to flair post
print('Set flair and removed comments.')
for flairs in flair_list:
if flairs.upper() in comment.body.upper():
flair = flairs.upper()
print (flair)
if flair.upper() in flair_list:
submission.mod.flair(text=flair.upper(), css_class='') # Flairs the post with the text
c.execute('INSERT INTO Submissions_To_Ignore VALUES (?)', (submission.id,))
conn.commit()
comment.mod.remove()
parent=comment.parent()
parent.mod.remove()
print('Set flair and removed comments.')


def send_flair_reminder():
Expand Down Expand Up @@ -84,15 +100,21 @@ def check_age():
removal_text = REMOVAL_MESSAGE
removal_text += COMMENT_FOOTNOTE # Adds footnote to removal message
if time.time() - submission.created_utc > TIME_LIMIT: # checks if time limit has passed
submission.reply(removal_text).mod.distinguish() # Posts removal message as a mod
submission.mod.remove() # removes submission
for comment in submission.comments:
try:
if comment.author.name == reddit.user.me():
c.execute('INSERT INTO Submissions_To_Ignore VALUES (?)', (submission.id,))
conn.commit()
comment.delete()
except PrawcoreException:
pass
submission.reply(removal_text).mod.distinguish()
submission.mod.remove()
print('Removed an old post.')


def check_flair():
print('Checking Flair...')
flair_template = SUBREDDIT.flair.link_templates # returns link templates on specified subreddit
flair_list = [flair['text'] for flair in flair_template]
if submission.link_flair_text == 'Help':
help_post() # redirects to help_post if post is flaired as "Help". Automatic removal.
elif submission.link_flair_text in flair_list:
Expand Down Expand Up @@ -121,4 +143,3 @@ def check_flair():
send_flair_reminder() # sends flair reminder if new submission
if submission.id in submissions_commented_on and submission.id not in submissions_to_ignore:
check_flair() # checks if flair is resolved if old unresolved submission

0 comments on commit b6aa79c

Please sign in to comment.