Help on adding subreddits my bot is banned from to the blacklist

So I have a bot that's being banned from a lot of subreddits simply for being a bot. In order to not clog up my console with errors and spam reddit, I have to manually check the inbox and paste every subreddit I'm banned from into the blacklist. Is there anyway I can make the bot fetch the inbox, check if I'm banned from any subreddits, and add it to a txt file? Something like:

blacklist = getbannedsubreddits
os.chdir('/Users/Test/Desktop/Reddit Bot')    
f = open("blacklist.txt","w")
x = write(blacklist)
f.close() 

Posted Sep 13, 2015, 3:18 PM UTC

12
GoldenSights•

Is there any way I can make the bot fetch the inbox, check if I'm banned from any subreddits

Of course. How would you perform it in real life? First open your inbox, then read each message, then see if it looks like a ban message.

def get_banned_subreddits():
    bans = []
    messages = r.get_unread(limit=None)
    for message in messages:
        if message.subreddit is None:
            # this came from a User and we can ignore it for this purpose
            continue
        if 'banned from' not in message.subject:
            # this is about something else and is ignored
            continue
        message.mark_as_read()
        bans.append(message.subreddit.display_name)
    return bans

Be careful how you store this information though. If you open a file in 'w' mode, all of the file's contents are deleted. You might want to play with 'a+' mode, which leaves the current contents intact. You'll have to play with the file writing for a little bit first and make sure everything is being written safely and formatted properly. There are some other optimizations you can make such as only opening the file if the bans list is not empty, to cut down on unnecessary file operations.

2
[deleted]•

[removed]

1