Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import praw
- r = praw.Reddit(client_id='',
- client_secret='',
- password='',
- user_agent='',
- username='')
- #Settings
- SUBREDDIT = 'Robin_Redbreast'
- USER = 'Robin_Redbreast'
- POST_ID = '77v6xj'
- #Message Constants
- OPT_OUT = 'You have successfully opted out.'
- OPT_IN = 'You have successfully opted in.'
- FOOTER = '^(I am a bot, and this message was sent automatically. | Want to opt out? Reply to this message with !optout |) [^Link ^to ^post]({plink})'
- TITLE = 'New post in /r/{subreddit}: {post_title}.'
- BODY = '''There has been a new post in /r/Robin_Redbreast by /u/{user}:\n\n>{preview}\n\n---\n\n'''
- def addFriends():
- authors = list(set([(c, c.author) for c in r.submission(id=POST_ID).comments if not c.saved]))
- friends = r.user.friends()
- for comment, author in authors:
- comment.save()
- if '!quebec' in comment.body.lower():
- if author not in friends:
- author.friend()
- author.message('Opt-in successful', OPT_IN)
- else:
- author.message('Error', 'You are already opted-in.')
- def removeFriends():
- for message in r.inbox.unread(limit=None):
- message.mark_read()
- if '!optout' in message.body.lower()+message.subject.lower():
- if message.author in r.user.friends():
- message.author.unfriend()
- message.reply(OPT_OUT)
- else:
- message.reply('Error: You are not currently opted-in.')
- def update_friends(post):
- for friend in r.user.friends():
- friend.message(TITLE.format(subreddit=SUBREDDIT, post_title=post.title), BODY.format(user=post.author.name, preview=post.selftext[:300]+'...')+FOOTER.format(plink=post.shortlink))
- def sendMessages():
- for post in r.subreddit(SUBREDDIT).new(limit=5):
- if not post.saved:
- post.save()
- if post.author.name.lower() == USER.lower() and '[ms]' in post.title.lower():
- update_friends(post)
- def main():
- addFriends()
- removeFriends()
- sendMessages()
- if __name__ == '__main__':
- while True:
- try:
- main()
- except Exception as e:
- print(str(e))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement