Skip to main content
r/redditdev icon

r/redditdev

members
online

Late to the party, need quick latest up to date info about the actual effective rules applied regarding dev with reddit. Late to the party, need quick latest up to date info about the actual effective rules applied regarding dev with reddit.
Reddit API

So I was searching about reddit automation, and heard of devvit

Then I somehow landed on this post because I was learning about the "reddit api": https://www.reddit.com/r/redditdev/comments/1oug31u/introducing_the_responsible_builder_policy_new/

First time i hear of the 3 layers or tiers of developement: normal dev, research, and company pro. Fine I am only interested by normal dev for now. My goal is to make a bot that interact with a subreddit community posts or comments in a certain way.

Questions:

  1. That post explained that now any new dev (unfortunatley I am late to the party) needs an approval to get the api to reddit? If yes: how is the form to fill, does it actually need from you to use your name and stuff like other dev websites (such as google etc)? In reddit we usually don't use real names, even the ADMIN on that post used usernames, reddit should understand that we don't necessarily want to use real names, even if its a private form to fill. So what does that api applying process work?

  2. Can I learn more about the 2 other categories? What is research about? Is it only for PhD students? I can't imagine lot of Phd students having a relation with reddit environement? And what is that commercial licence thing? Is it super out of reach in term or pricing? Only for usa? etc etc Thanks


A match made in futures heaven: Combine 600+ CME Group futures contracts, including popular choices like S&P 500 E-mini (ES), Crude Oil (CL), & Gold (GC) — with our lower margin rates to gain additional market exposure using less capital.
Image A match made in futures heaven: Combine 600+ CME Group futures contracts, including popular choices like S&P 500 E-mini (ES), Crude Oil (CL), & Gold (GC) — with our lower margin rates to gain additional market exposure using less capital.


PRAW - Changing user flairs not working as expected. PRAW - Changing user flairs not working as expected.
PRAW
PRAW - Changing user flairs not working as expected.

[SOLVED] I've been using this code to update user flairs:

def update_flair(username, new_line):
    flair = next(subreddit.flair(redditor=username), None)

    if flair and flair.get("flair_text") is not None:
        base_text = flair.get("flair_text") or ""
        template_id = flair.get("flair_template_id")
    else:
        base_text = ""
        template_id = DEFAULT_FLAIR_TEMPLATE_ID

    lines = [
        l for l in base_text.split("\n")
        if not l.lower().startswith("streak -")
    ]

    lines.append(new_line)
    final_text = "\n".join(lines)

    subreddit.flair.set(
        username,
        text=final_text,
        flair_template_id=template_id
    )

It's part of a larger code for a bot that I'm working on. I'm attaching the expectations vs outcome in the comments. Please let me know what went wrong. Thank you :)

Edit - I've finally managed to solve the issue. The problem is with subreddit.flair(redditor=username). It returns an object like {'flair_css_class': None, 'user': Redditor(name='username'), 'flair_text': 'text'}. It does NOT contain flair_template_id. So, the solution is to get the flair text from the flair first. Then, get all the user flairs. These include both flair text & template ID. Then, go through the list until you find the flair which has the same flair text as yours & get its ID. This is the work-around that I've found. The complete code is given below:

def get_template_text(subreddit, template_id):
    for tpl in subreddit.flair.templates:
        if tpl["id"] == template_id:
            return tpl.get("text") or ""
    return ""

def get_user_flair(subreddit, username):
    try:
        redditor = reddit.redditor(username)
        flair = next(subreddit.flair(redditor=redditor), None)
        return flair
    except Exception as e:
        print("Error fetching flair:", e)
        return None
    
def get_template_id_from_flair_text(subreddit, flair_text):
    if not flair_text:
        return None

    for tpl in subreddit.flair.templates:
        tpl_text = (tpl.get("text") or "").strip()
        if tpl_text == flair_text:
            return tpl["id"]
    return None

def update_flair(username, new_line):
    flair = get_user_flair(subreddit, username)

    if flair:
        base_text = flair.get("flair_text") or ""
        template_id = flair.get("flair_template_id")
        if template_id is None:
            template_id = get_template_id_from_flair_text(subreddit, base_text)

        if template_id is None:
            template_id = DEFAULT_FLAIR_TEMPLATE_ID
            base_text = get_template_text(subreddit, template_id)
        subreddit.flair.delete(username)
    else:
        template_id = DEFAULT_FLAIR_TEMPLATE_ID
        base_text = get_template_text(subreddit, template_id)

    lines = [
        l for l in base_text.split("\n")
        if not l.lower().startswith("streak -")
    ]
    lines.append(new_line)
    final_text = "\n".join(lines)

    subreddit.flair.set(
        username,
        flair_template_id=template_id
    )

    subreddit.flair.set(
        username,
        flair_template_id=template_id,
        text=final_text
    )

replace_more() in CommentForest replace_more() in CommentForest
PRAW

My code looks like this:

comment = [...].reddit.comment(comment_id)
comment.refresh()
comment.replies.replace_more()
tree = comment.replies.list()

But when I run this on a comment with some "More Comments" I get:

praw.exceptions.DuplicateReplaceException: A duplicate comment has been detected. Are you attempting to call 'replace_more_comments' more than once?

https://praw.readthedocs.io/en/stable/code_overview/other/commentforest.html#praw.models.comment_forest.CommentForest.replace_more

Docs suggest using the refresh() + replace_more() way but it seems like that's not the correct thing to do?