Search code examples
pythonapireddit

PRAW: Comment Submitter's Username


I'm developing a reddit bot that needs to know which user submitted a comment.

According to the PRAW API wrapper docs, there's no specific way to get the username of a Comment object's author. Ideally I could directly get the username back. If that's not possible, is there a way to get the fullname of the author and then convert it to a username?


Solution

  • I'm a maintainer for PRAW. Where does it say that you cannot get the username of a Comment objects author? Because that is incorrect and needs to be fixed.

    Anyway, a Comment has a author attribute, which is a Redditor instance of the author.

    import praw
    
    r = praw.Reddit(UNIQUE_AND_DESCRIPTIVE_USERAGENT)
    submission = r.get_submission("http://www.reddit.com/r/redditdev/comments/16m0uu/praw_20_is_coming_release_in_2_days/")
    comment = submission.comments[0]
    author = comment.author  # This returns a ``Redditor`` object.
    print(author.name)  # The username