Skip to content

Instantly share code, notes, and snippets.

@notesbot
Last active July 28, 2024 00:41
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.

Revisions

  1. notesbot revised this gist Aug 22, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion find_last_content.py
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ def run_bot():
    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):
    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
  2. notesbot created this gist Aug 22, 2022.
    42 changes: 42 additions & 0 deletions find_last_content.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    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()