Skip to content

Instantly share code, notes, and snippets.

@notesbot
Last active May 26, 2022 21:18
Show Gist options
  • Save notesbot/0f22f3293eedcdb8828bbc1508923556 to your computer and use it in GitHub Desktop.
Save notesbot/0f22f3293eedcdb8828bbc1508923556 to your computer and use it in GitHub Desktop.
# Automod comment killer bot for r/sourdough
# Import modules needed for the bot to run.
import praw
import sys
import time
# Define subreddit and bot's comment reply
sub_name = 'sourdough'
comment_reply = "YOUR BOT'S REPLY"
# Define how long the bot will sleep before waking up for another pass.
sleep_seconds = 300
# Define the reddit login. Fill in your username, pwd, client id, and client secret.
def reddit_login():
try:
reddit = praw.Reddit(
user_agent = 'Approved Submission Checker for r/sourdough v1.0 by u/buckrowdy',
refresh_token = '',
client_id = '',
client_secret = ''
)
# connect to the subreddit
subreddit = reddit.subreddit(sub_name)
except Exception as e:
print(f'\t### ERROR - Could not login.\n\t{e}')
print(f'Logged in as: {reddit.user.me()}')
return reddit
### Fetch submissions from r/subreddit/new.
def get_latest_submissions(subreddit):
submissions = reddit.subreddit(sub_name).new(limit=100)
return submissions
# Process submissions
def check_submissions(submissions):
try:
print(f"Now processing submissions...")
for submission in submissions:
if submission.approved and not submission.removed:
# Check 100 comments on each submission. If you need to check more, increase the limit. Max is 1000.
submission.comments.replace_more(limit=100)
for comment in submission.comments.list():
# Check to see if any of the comments were made by automoderator
if comment.author == "AutoModerator":
print(f'Removing automod comment on {submission.id}')
comment.mod.remove()
print('Posting mod comment reply...')
mod_comment = submission.reply(comment_reply)
mod_comment.mod.distinguish(how='yes', sticky=True)
except Exception as e:
print(f'\t### ERROR - Something went wrong checking submissions.\n\t{e}')
##############################################
### Bot starts here
if __name__ == '__main__':
try:
# Connect to reddit and return the object
reddit = reddit_login()
# Connect to the subreddit
subreddit = reddit.subreddit(sub_name)
except Exception as e:
print(f'\t\n### ERROR - Could not connect to reddit.\n\t{e}')
sys.exit(1)
# Loop the bot
while True:
try:
# Get the latest submissions after emptying variable
submissions = None
submissions = get_latest_submissions(subreddit)
except Exception as e:
print(f'\t### ERROR - Could not get posts from reddit\n\t{e}')
# If there are posts, start scanning
if not submissions is None:
# Once you have submissions, run the bot function.
check_submissions(submissions)
# Loop every X seconds (5 minutes, currently.)
time.sleep(sleep_seconds)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment