Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import praw
- import time
- from termcolor import colored
- import pickle as pl
- from collections import OrderedDict
- #define constants
- SUBREDDIT = 'ShittyRainbow6'
- THRESHOLD = 80
- global oldtop
- oldtop = ''
- #Initialize Reddit instance, the only perms
- #the bot needs are flair and post
- r = praw.Reddit(client_id='UMYNU5MhRF-jPg',
- client_secret='SdZvR_VXfXeJwaO_NioflliewhI',
- password='1lafot12XinyL5r6XsttX14a2',
- user_agent='User flair points for for /r/ShittyRainbow6',
- username='RainbowPointsBot')
- #Check if the user already has a flair
- def hasFlair(user):
- uflair = [x for x in r.subreddit(SUBREDDIT).flair(str(user))][0]['flair_text']
- print(uflair)
- if uflair == None or 'Points:' not in uflair or uflair == 'None' or str(uflair) == 'None':
- return False
- else:
- return True
- #add flair to new user
- def addFlair(user):
- r.subreddit(SUBREDDIT).flair.set(user, 'Points: 0', css_class='memer')
- def saveChanges(user, score):
- allUsers = pl.load(open('save.p','rb'))
- allUsers[user] = score
- pl.dump(allUsers, open('save.p', 'wb'))
- def top5():
- sdict = pl.load(open('save.p','rb'))
- fDict = OrderedDict()
- tUsers = sorted(sdict, key=sdict.get)
- x = 0
- for user in tUsers[::-1]:
- x += 1
- if x > 5:
- pass
- else:
- fDict[user] = sdict[user]
- return fDict
- def updateSidebar(topusers):
- print(topusers)
- sidebar_current = r.subreddit(SUBREDDIT).mod.settings()['description'].split('[MemePointsLeaderBoards](#noshow)')
- print(sidebar_current)
- sidebar_current[1] = '''[MemePointsLeaderBoards](#noshow)\n\n
- User | Score
- ---|---
- {}
- [MemePointsLeaderBoards](#noshow)'''.format('\n'.join(['/u/' + list(topusers.keys())[x] + ' | ' + str(list(topusers.values())[x]) for x in range(len(topusers))]))
- sidebar_final = ''.join(sidebar_current)
- r.subreddit(SUBREDDIT).mod.update(description=sidebar_final)
- #increment the flair of a user
- def incrementFlair(user, post):
- uflair = [x for x in r.subreddit(SUBREDDIT).flair(str(user))][0]['flair_text']
- while uflair == None or 'Points:' not in uflair or uflair == 'None' or str(uflair) == 'None':
- uflair = [x for x in r.subreddit(SUBREDDIT).flair(str(user))][0]['flair_text']
- print('UFLAIR HERE:')
- print(uflair)
- num = str(int(uflair.strip('Points: '))+1)
- r.subreddit(SUBREDDIT).flair.set(user, 'Points: {}'.format(num), css_class='memer')
- #myComment = post.reply('''Hey, {}! Congratulations on getting over {} points on this post. I've added **1 point** to your user flair as a reward.'''.format(str(post.author), str(THRESHOLD)))
- #myComment.mod.distinguish(how='yes', sticky=True)
- saveChanges(str(post.author), int(num))
- try:
- print(colored('Updated flair of {} to {}'.format(str(post.author), str(num)), 'blue'))
- except:
- pass
- newtop = top5()
- if oldtop != newtop:
- global oldtop
- oldtop = newtop
- updateSidebar(newtop)
- #check score of the post
- def checkScore(post):
- return True if post.score >= THRESHOLD else False
- def checkPosts():
- for post in [post for post in r.subreddit(SUBREDDIT).new(limit=100) if post.id not in open('aldone.txt','r').read().split('\n')]:
- if checkScore(post) and hasFlair(post.author):
- open('aldone.txt','a').write(post.id + '\n')
- incrementFlair(post.author, post)
- elif checkScore(post):
- open('aldone.txt','a').write(post.id + '\n')
- if not hasFlair(post.author):
- print('CREATING FLAIR')
- addFlair(post.author)
- while not hasFlair(post.author):
- time.sleep(0.01)
- incrementFlair(post.author, post)
- def undoAll():
- for post in [post for post in r.subreddit(SUBREDDIT).new(limit=100) if post.score >= 80]:
- r.subreddit(SUBREDDIT).flair.set(post.author, '', css_class='')
- while True:
- time.sleep(30)
- try:
- checkPosts()
- except Exception as e:
- print(colored(str(e), 'red'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement