Skip to content

Instantly share code, notes, and snippets.

@ty-porter
Created June 2, 2020 14:39
Show Gist options
  • Save ty-porter/ea5529b421e158e8a5ccfee8b49047e7 to your computer and use it in GitHub Desktop.
Save ty-porter/ea5529b421e158e8a5ccfee8b49047e7 to your computer and use it in GitHub Desktop.
Blacklist Notification Bot
import praw
SUBREDDIT = 'test'
REPLY_USERNAME = 'AlternisBot'
KEYWORDS = ['these', 'words', 'trigger', 'a', 'reply']
BLACKLISTED_KEYWORDS = ['only', 'if', 'these', 'are', 'absent']
reddit = praw.Reddit(username='REDDIT_USERNAME',
password='REDDIT_PASSWORD',
client_id='REDDIT_CLIENT_ID',
client_secret='REDDIT_CLIENT_SECRET',
user_agent='REDDIT_USER_AGENT')
for submission in reddit.subreddit(SUBREDDIT).stream.submissions(skip_existing=True):
if has_keyword(submission, KEYWORDS) and not has_keyword(submission, BLACKLISTED_KEYWORDS):
msg = 'A post was found containing the keyword(s) {} in /r/{}.\n\nLink here: {}'.format(keywords_in(submission),
submission.subreddit.name,
submission.permalink)
reddit.redditor(REPLY_USERNAME).message('Post found!', msg)
def has_keyword(submission, keywords):
for keyword in keywords:
if keyword in submission.title:
return True
return False
def keywords_in(submission):
return ', '.join([keyword for keyword in KEYWORDS if keyword in submission.title])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment