Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Credit: Rudy Pikulik - for comment validation from RedditSilverRobot
- Reddit_Ribbon: A bot for handing out Reddit participation ribbons
- Copyright 2018 /u/CaulkParty
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- """
- import re
- import praw
- # Create the Reddit instance
- REDDIT = praw.Reddit('reddit_ribbon')
- SUBREDDIT = REDDIT.subreddit('all')
- COMMENTS = SUBREDDIT.stream.comments()
- COMMAND = "!redditribbon"
- def main():
- """
- The main function for running the bot.
- """
- for comment in COMMENTS:
- #print(submission.title)
- if validate_comment(comment):
- # Do a case insensitive search
- if re.search(COMMAND, comment.body.lower(), re.IGNORECASE):
- # Reply to the post
- comment.reply("[Here is your Reddit Participation Ribbon!](https://i.imgur.com/7HcVdqw.jpg)")
- print("Bot replying to : ", comment.author)
- def validate_comment(comment):
- """
- Decides whether or not to reply to a given comment.
- - Must contain command
- - Must not have already replied
- - Must not reply to self
- Args:
- comment - the comment object to validate
- """
- if COMMAND in comment.body.lower():
- # We wrote the comment, don't loop.
- if comment.author.name is "reddit_ribbon":
- print(comment, "Cannot respond to self.")
- return False
- # Parent comment was deleted, don't respond.
- if get_receiver(comment) == '[deleted]':
- print(comment, "Parent comment was deleted!")
- return False
- comment.refresh()
- for child_comment in comment.replies:
- if child_comment.author.name == "reddit_ribbon":
- print(comment, "Already replied to this comment. Will not do it again.")
- return False
- return True
- return False
- def get_receiver(comment):
- """
- Gets the receiver of the comment.
- Args:
- comment - the comment object to get the receiver for.
- """
- text = comment.body.lower().split()
- try:
- # Kind of gross looking code below. Splits the comment exactly once at '!RedditSilver',
- # then figures out if the very next character is a new line. If it is, respond to parent.
- # If it is not a new line, either respond to the designated person or the parent.
- split = comment.body.lower().split(COMMAND, 1)[1].replace(' ', '')
- if split[0] is "\n":
- try:
- receiver = comment.parent().author.name
- except AttributeError:
- receiver = '[deleted]'
- else:
- receiver = text[text.index(COMMAND) + 1]
- # An IndexError is thrown if the user did not specify a recipient.
- except IndexError:
- try:
- receiver = comment.parent().author.name
- except AttributeError:
- receiver = '[deleted]'
- # A ValueError is thrown if '!RedditSilver' is not found. Example: !RedditSilverTest would throw this.
- except ValueError:
- return None
- if '/u/' in receiver:
- receiver = receiver.replace('/u/', '')
- if 'u/' in receiver:
- receiver = receiver.replace('u/', '')
- if '/' in receiver:
- receiver = receiver.replace('/', '')
- # This line is to change the name from all lowercase.
- receiver = REDDIT.redditor(receiver).name
- return receiver
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment