Skip to content

Instantly share code, notes, and snippets.

@notesbot
Last active August 22, 2022 03:04
Show Gist options
  • Save notesbot/7f11900867a6940d1798026566b64f9a to your computer and use it in GitHub Desktop.
Save notesbot/7f11900867a6940d1798026566b64f9a to your computer and use it in GitHub Desktop.
def run_bot():
try:
print('Working...')
unread_conversations = reddit.subreddit(sub_name).modmail.conversations(state="highlighted")
for conversation in unread_conversations:
if (len(conversation.authors) > 1 and len(conversation.messages) > 1 and "find_last" in conversation.messages[-1].body_markdown):
print(f'Match Found: {conversation.id}')
username = conversation.user
target_sub = conversation.owner
api = PushshiftAPI()
banned_user = api.search_comments(author=username, subreddit=target_sub, limit = 1)
banned_user_posts = api.search_submissions(author=username, subreddit=target_sub, limit = 1)
comments = [comment.d_ for comment in banned_user]
submissions = [submission.d_ for submission in banned_user_posts]
for comment in comments:
if comment is not None:
body_text = comment["body"]
# Make a multi-line comment linkable via markdown.
replace_comment = body_text[:300]
sliced_comment = "".join(f"{i}" for i in replace_comment.split("\n\n"))
link_text = comment["permalink"]
comment_body = f"\n> [{sliced_comment}...]({link_text})\n\n"
else:
comment_body = 'None'
for submission in submissions:
if submission is not None:
subm_text = submission["title"]
subm_link = comment["permalink"]
submission_title = f'\n> [{subm_text}]({subm_link})'
else:
submission_title = 'None'
response = f"I found the last post and comment for the queried user.\n\nPost: \n\n{submission_title}\n\nLast comment: \n\n{comment_body}"
conversation.reply(body=response, internal=True)
conversation.unhighlight()
except Exception as e:
print("\t\n### ERROR - Modmail couldn't be checked.")
traceback.print_exc()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment