-
-
Save JonnoFTW/4bd2c2300e43d619752ea08c738d7fad to your computer and use it in GitHub Desktop.
Reddit Dev Flair Bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Subreddit to check | |
subreddit = 'subreddit' | |
# Mod flair name | |
mod_flair = 'Developer' | |
# Credentials, subreddit management required | |
user_agent = 'UserAgent' | |
client_id = 'ClientID' | |
client_secret = 'ClientSecret' | |
username = 'Username' | |
password = 'Password' | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import praw | |
import config | |
import time | |
# Gets the correct link flair, None if untagged | |
def get_flair_id(flair_name, flair_data): | |
if flair_name is None: | |
return next(x for x in flair_data | |
if x['flair_text_editable'])['flair_template_id'] | |
for flair in flair_data: | |
if flair['flair_css_class'] == (flair_name + 'Dev'): | |
return flair['flair_template_id'] | |
reddit = praw.Reddit(user_agent=config.user_agent, | |
client_id=config.client_id, | |
client_secret=config.client_secret, | |
username=config.username, | |
password=config.password) | |
with open('tagged.txt', 'r') as f: | |
posts_tagged = set(x for x in f.readlines() if x) | |
# Bot running forever | |
while True: | |
# Goes to subbreddit | |
try: | |
battalion1944 = reddit.subreddit(config.subreddit) | |
# Checks new submissions | |
for submission in battalion1944.new(): | |
# Already tagged, ignore | |
if submission.id in posts_tagged: | |
continue | |
old_flair = submission.link_flair_text | |
choices = submission.flair.choices() | |
# Searches in comments | |
for comment in submission.comments: | |
author = comment.author | |
# Checks mod flair | |
if comment.author_flair_css_class == config.mod_flair: | |
print('Developer responded') | |
template_id = get_flair_id(old_flair, choices) | |
flair_text = 'Developer Response' if old_flair is not None else 'untaggedDev' | |
print(template_id) | |
print(flair_text) | |
# Changes the link flair | |
submission.flair.select(template_id, ) | |
# Saves the submission | |
posts_tagged.add(submission.id) | |
print('Link flair changed') | |
# If we flair a new post, append it to the file | |
with open("tagged.txt", "a") as f: | |
f.write(f'{submission.id}\n')) | |
except Exception as e: | |
print(e) | |
time.sleep(5) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment