Skip to content

Instantly share code, notes, and snippets.

@Grezzo
Last active May 16, 2017 20:13
Show Gist options
  • Save Grezzo/23b039ef6927e15a6fce0d4460771f9a to your computer and use it in GitHub Desktop.
Save Grezzo/23b039ef6927e15a6fce0d4460771f9a to your computer and use it in GitHub Desktop.
Bot script to aid in automatic removal of inappropriate reddit submissions based on user's votes on a bot's comment
#! /usr/bin/env python3
#TODO: Check for post limit
#TODO: Check for moderator restriction
import datetime
import urllib
import praw
#####################################
####CHANGE THINGS BELOW THIS LINE####
#####################################
bot_account_name = 'XXXX'
bot_account_password = 'XXXX'
client_id = 'XXXX'
client_secret = 'XXXX'
subreddit_name = 'XXXX'
message = '''
Downvote this comment if this post is not relevant to this subreddit and upvote if it is relevant.
If this comment's score drops too low, this post will be automatically deleted.
'''
deletion_message = '''
Your post has been removed because the community has voted that it is not what is expected of a /r/{subreddit} post.
If you feel this is untrue, please appeal by
[messaging the moderators of this subreddit]
(https://www.reddit.com/message/compose?to=%2Fr%2F{subreddit}&subject=Query+about+automatic+removal+of+submission&message=Regarding+[this+submission]\(\):%0A%0A)
why your post should not have been removed.
Thank you.
'''.format(subreddit=subreddit_name)
check_num_new_submissions = XXXX #Number between 1 and 1000
#Submission will be deleted if vote-comment score is lower than min_score and older than min_age
min_score = XXXX
min_age = datetime.timedelta(
days = XXXX,
hours = XXXX,
minutes = XXXX,
seconds = XXXX
)
#############################################
####DON'T CHANGE ANYTHING BELOW THIS LINE####
#############################################
reddit = praw.Reddit(client_id = client_id,
client_secret = client_secret,
user_agent = 'v0.3:{botname}'.format(botname=bot_account_name),
username = bot_account_name,
password = bot_account_password
)
subreddit = reddit.subreddit(subreddit_name)
for submission in subreddit.new(limit=check_num_new_submissions):
print('Processing submission {link}'.format(link=submission.permalink))
#Find a comment left by vote bot user
comments = submission.comments
comments.replace_more(limit=0)
comment = None
for top_level_comment in comments:
if top_level_comment.author == reddit.redditor(name=bot_account_name):
comment = top_level_comment
break #Stop looping through as we've found our comment
if comment:
print(' - Found vote-comment {link}'.format(link=comment.permalink(fast=True)))
comment_created = datetime.datetime.utcfromtimestamp(comment.created_utc)
comment_age = datetime.datetime.utcnow() - comment_created
if (comment.score <= min_score and comment_age >= min_age):
link = submission.permalink
print(' - Deleting (Score: {score}, Age: {age})'.format(
score = comment.score,
age = comment_age
))
subreddit.message('Submission removed', '{link} was removed by {botname}'.format(
link = link,
botname = bot_account_name,
))
submission.mod.remove()
encoded_link = urllib.parse.quote_plus(link)
print(encoded_link)
comment = submission.reply(deletion_message.replace('\(\)', '\({link}\)'.format(link=encoded_link)))
comment.mod.distinguish(sticky=True)
else:
print(' - Keeping (Score: {score}, Age: {age})'.format(
score = comment.score,
age = comment_age
))
else:
print(' - No vote-comment found. Posting new one')
comment = submission.reply(message)
comment.mod.distinguish(sticky=True)
print(' - Vote Comment left {link}'.format(link=comment.permalink(fast=True)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment