Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Welcome to the official source code of Willard Points Bot.
- This is version 1.3, which is the first stable version of
- the bot with (probably) no bugs.
- Remember that you can earn Willard Points by simply saying
- "shut up exe", or by making good posts in Townofsalemgame.
- The bot checks for good posts twice a day. To get that sweet
- WP, you must be in the top 12 hot posts in the subreddit and
- have more than 50 upvotes on it. Pinned posts do not qualify,
- for now. Sorry.
- Thank's for taking the time to look at the source code! I'll
- let you in on a little secret: You can order a can of
- executioner spray from NateNate60 now for a discounted price.
- Just tell me that you saw the source code in a PM and you can
- order a can for the low price of 19.99 WP (+0.01 WP S&H)
- Released on 6 February 2019. Coded by NateNate60.
- """
- import prawcore.exceptions as e
- while True :
- try :
- # NateNate60's Willard Points Bot
- version = "1.3"
- print ("Starting Willard Points Bot version", version)
- # Module importation
- print ("Importing modules...", end='')
- import praw
- import config4 as config
- import time as t
- import datetime
- import os.path
- print ('done')
- print ("Loading features...", end='')
- # On or Off. You can still run it if off, but nothing will actually happen
- on = True
- # Set the tick
- tick = 0
- # Login function
- def login() :
- print ("Connecting to Reddit")
- print ("Authenticating...", end='')
- r = praw.Reddit(username = config.username,
- password = config.password,
- client_id = config.client_id,
- client_secret = config.client_secret,
- user_agent = "WillardPointsBot" + version)
- print ('done')
- return r
- """
- Core running functionality
- """
- # Retrieve the queue of transactions to be processed
- def retrieve() :
- with open ("transactions.txt", "r") as queuefile :
- queue = queuefile.read()
- queue = queue.split('\n')
- open('transactions.txt', 'w').close()
- return queue
- #Set the signature
- signature = "\n \n ^NateNate60's ^Willard ^Points ^Bot ^v" + version + ". ^Don't ^have ^an ^account? ^Use ^!newaccount"
- # The acutal code
- def run_bot(r, tick, timen, queue, replied_to, time) :
- if on == True :
- for trans in queue :
- trans = str(trans)
- if "~" in trans and "+" not in trans :
- trans = trans.split('~')
- user = trans[0]
- if os.path.isfile (user + ".txt") :
- with open (user + ".txt", 'r') as u :
- balance = u.read()
- balance = int(balance) - int(trans[1])
- if balance < 0 :
- balance = balance + int(trans[1])
- notify (r, user, balance, trans[1], 0)
- with open (user + ".txt", 'w') as s :
- s.write(str(balance))
- with open ('log.txt', 'a') as log :
- log.write ("\n" + time + ' ' + user + " paid " + trans[1] + " WP.")
- print (time + ' ' + user + " paid " + trans[1] + " WP.")
- notify(r, user, balance, trans[1], '~')
- elif "+" in trans and "~" not in trans :
- trans = trans.split('+')
- user = trans[0]
- if os.path.isfile (user + ".txt") :
- with open (user + ".txt", 'r') as u :
- balance = u.read()
- balance = int(balance) + int(trans[1])
- with open (user + ".txt", 'w') as s :
- s.write(str(balance))
- with open ('log.txt', 'a') as log :
- log.write ("\n" + time + ' ' + user + " gained " + trans[1] + " WP.")
- print (time + ' ' + user + " gained " + trans[1] + " WP.")
- notify(r, user, balance, trans[1], '+')
- if tick%5 == 0 :
- for message in r.inbox.unread(limit = 5) :
- #For opening new accounts
- if "!newacc" in message.body or "!openacc" in message.body or "!createacc" in message.body :
- accname = str(message.author)
- if not os.path.isfile(accname + ".txt") :
- accowner = accname
- with open (accname + ".txt", 'w') as newacc :
- newacc.write ("0")
- message.reply ('Account creation successful.'
- + 'You will recieve a one-time "signing" bonus of 10 Willard Points.' + signature)
- with open ('log.txt', 'a') as log :
- log.write ("\n" + time + ' ' + accowner + " opened an account under the name " + accname)
- print (time + ' ' + accowner + " opened an account under the name " + accname)
- with open ('transactions.txt', 'a') as transa :
- transa.write ('\n' + str(accname) + '+10')
- else :
- message.reply ('Something went wrong. Account creation failed. You probably already have an account. If you continue to receive this error, contact NateNate60. You can always try again.'
- + signature)
- #For moderators adding or removing money from people's accounts administratively.
- elif "+" in message.body or '~' in message.body :
- if message.author not in config.approved :
- message.reply ('You are not authorised to make that command.' + signature)
- else :
- trans = message.body
- if "~" in trans :
- trans = trans.split('~')
- user = trans[0]
- if os.path.isfile(user + ".txt") :
- with open (user + ".txt", 'r') as u :
- balance = u.read()
- balance = int(balance) - int(trans[1])
- if balance < 0 :
- balance = 0
- message.reply ("That user does not have enough WP, so their balance was set to zero." + signature)
- else :
- message.reply ("The command completed successfully. \n \n"+ " ^Willard ^Points ^Bot ^v" + version)
- with open (user + ".txt", 'w') as s :
- s.write(str(balance))
- with open ('log.txt', 'a') as log :
- log.write ("\n" + time + ' ' + user + " was docked " + trans[1] + " WP by " + str(message.author))
- print (time + ' ' + user + " was docked " + trans[1] + " WP by " + str(message.author))
- notify(r, user, balance, trans[1], '~~')
- else :
- message.reply ("That account does not exist. Please contact NateNate60 for help." + signature)
- elif "+" in trans :
- trans = trans.split('+')
- user = trans[0]
- if os.path.isfile(user + ".txt") :
- with open (user + ".txt", 'r') as u :
- balance = u.read()
- balance = int(balance) + int(trans[1])
- message.reply ("The command completed successfully. " + signature)
- with open (user + ".txt", 'w') as s :
- s.write(str(balance))
- with open ('log.txt', 'a') as log :
- log.write ("\n" + time + ' ' + user + " was awarded " + trans[1] + " WP by " + str(message.author))
- notify(r, user, balance, trans[1], '++')
- else :
- message.reply ("That account does not exist. Please contact NateNate60 for help." + signature)
- #Balance checking
- elif "!bal" in message.body :
- payload = message.body.split(" ")
- if len(payload) != 2 :
- message.reply ('Invalid syntax. Try !balance [username]')
- else :
- if os.path.isfile(payload[1] + ".txt") :
- with open (payload[1] + ".txt") as acc :
- bal = acc.read()
- message.reply("Your account currently has " + bal + " Willard Points." + signature)
- else :
- message.reply ("That account does not exist. Use !newaccount" + signature)
- # Transferring
- elif "!trans" in message.body :
- payload = message.body
- payload = payload.split(' ')
- to = payload [1]
- amt = payload [2]
- fromfile = str(message.author) + ".txt"
- if os.path.isfile(fromfile) :
- if os.path.isfile(to + ".txt") :
- with open (fromfile, 'r') as f :
- balance = f.read()
- balance = balance - amt
- if balance < 0 :
- balance = balance + amt
- notify (r, message.author, balance, None, '0')
- else :
- with open ('transactions.txt', 'a') as trans :
- trans.write("\n" + str(message.author) + "~" + amt)
- trans.write('\n' + to + "+" + amt)
- else :
- message.reply ("Invalid recepient. Make sure you spelled the recepient's username correctly." + signature)
- else :
- message.reply ("Invalid sender. You do not have an account. Use !newaccount to create a new account." + signature)
- elif "shut up" in message.body.lower() :
- message.reply ('no u' + signature)
- elif "!isup" in message.body.lower() or "!stat" in message.body.lower() :
- message.reply ("Online." + signature)
- # Mark the message as read.
- r.inbox.mark_read([message])
- #For awarding WP to good posts
- timen = int(timen) / 10
- timen = round (timen)
- timen = timen * 10
- if timen%43200 == 0 :
- for post in r.subreddit('townofsalemgame').hot(limit = 12) :
- if post.stickied :
- continue
- elif post.score > 50 and post.score < 100 :
- if post.id not in replied_to :
- post.reply ("It looks like this is a mildly good post. It's reached the Framer tier! \n \n" +
- "Willard Points issued at the Framer tier are equal to 1 WP for every 10 upvotes at the time of checking. The longer it stays on the front page, the more you'll get.\n \n" +
- "^WillardPointsBot ^version " + "^" + version + " ^Contact ^NateNate60 ^for ^information. ^Don't ^have ^an ^account? ^Reply ^!newaccount")
- award = post.score // 10
- print (time, post.author, "gained", str(award), "for FRAMER.")
- with open ("transactions.txt", 'a') as trans :
- trans.write (str(post.author) + "+" + str(award))
- replied_to.append (post.id)
- elif post.score > 100 and post.score < 200 :
- if post.id in replied_to :
- for comment in post.comments :
- if comment.author != r.user.me() :
- continue
- else :
- comment.edit ("It looks like this is a pretty good post. It's reached the Mafioso tier! \n \n" +
- "Willard Points issued at the Mafioso tier are equal to 1 WP for every 15 upvotes at the time of checking. The longer it stays on the front page, the more you'll get.\n \n" +
- "^WillardPointsBot ^version " + "^" + version + " ^Contact ^NateNate60 ^for ^information. ^Don't ^have ^an ^account? ^Reply ^!newaccount")
- else :
- post.reply ("It looks like this is a pretty good post. It's reached the Mafioso tier! Have some more Willard Points! \n \n" +
- "Willard Points issued at the Mafioso tier are equal to 1 WP for every 20 upvotes (plus 5 WP) at the time of checking. The longer it stays on the front page, the more you'll get.\n \n" +
- "^WillardPointsBot ^version " + "^" + version + " ^Contact ^NateNate60 ^for ^information. ^Don't ^have ^an ^account? ^Reply ^!newaccount")
- award = post.score // 15
- award = award + 5
- print (time, post.author, "gained", str(award), "for MAFIOSO.")
- with open ("transactions.txt", 'a') as trans :
- trans.write ('\n' + str(post.author) + "+" + str(award))
- elif post.score > 200 and post.score < 500 :
- if post.id in replied_to :
- for comment in post.comments :
- if comment.author != r.user.me() :
- continue
- else :
- comment.edit ("It looks like this is a really dank post. It's reached the Blackmailer tier! \n \n" +
- "Willard Points issued at the Blackmailer tier are equal to 1 WP for every 20 upvotes. at the time of checking. The longer it stays on the front page, the more you'll get.\n \n" +
- "^WillardPointsBot ^version " + "^" + version + " ^Contact ^NateNate60 ^for ^information. ^Don't ^have ^an ^account? ^Reply ^!newaccount")
- else :
- post.reply ("It looks like this is a really dank post. It's reached the Blackmailer tier! Have some more Willard Points! \n \n" +
- "Willard Points issued at the Blackmailer tier are equal to 1 WP for every 20 upvotes. (plus 10 WP) at the time of checking. The longer it stays on the front page, the more you'll get.\n \n" +
- "^WillardPointsBot ^version " + "^" + version + " ^Contact ^NateNate60 ^for ^information. ^Don't ^have ^an ^account? ^Reply ^!newaccount")
- replied_to.append (post.id)
- award = post.score // 20
- award = award + 10
- print (time, post.author, "gained", str(award), "for BLACKMAILER.")
- with open ("transactions.txt", 'a') as trans :
- trans.write ('\n' + str(post.author) + "+" + str(award))
- elif post.score > 500 and post.score < 1000 :
- for comment in post.comments :
- if comment.author != r.user.me() :
- continue
- else :
- comment.edit ("It looks like this is an extremely good post. It's reached the Consigliere tier! Have some more Willard Points! \n \n" +
- "Willard Points issued at the Consigliere tier are equal to 1 WP for every 20 upvotes. (plus 15 WP) at the time of checking. The longer it stays on the front page, the more you'll get.\n \n" +
- "^WillardPointsBot ^version " + "^" + version + " ^Contact ^NateNate60 ^for ^information. ^Don't ^have ^an ^account? ^Reply ^!newaccount")
- award = post.score // 20
- award = award + 15
- print (time, post.author, "gained", str(award), "for CONSIGLIERE.")
- with open ("transactions.txt", 'a') as trans :
- trans.write ('\n' + str(post.author) + "+" + str(award))
- elif post.score > 1000 :
- for comment in post.comments :
- if comment.author != r.user.me() :
- continue
- else :
- comment.edit ("It looks like this is one of the best posts of all time! Congratulations! It's reached the Godfather tier! Have some more Willard Points! \n \n" +
- "Willard Points issued at the Godfather tier are flat. You get 50 WP for every 12 hours this stays in the top 10. \n \n" +
- "^WillardPointsBot ^version " + "^" + version + " ^Contact ^NateNate60 ^for ^information. ^Don't ^have ^an ^account? ^Reply ^!newaccount")
- award = 50
- print (time, post.author, "gained 50 for GODFATHER.")
- with open ("transactions.txt", 'a') as trans :
- trans.write ('\n' + str(post.author) + "+" + str(award))
- write_comment_list(replied_to)
- def notify (r, user, balance, amt, sign) :
- if sign == '~' :
- r.redditor(user).message('Willard Points were debited from your account', str(amt) + " Willard Points were deducted from your account. \n \n"
- + 'You have ' + str(balance) + " Willard Points left.")
- elif sign == '+' :
- r.redditor(user).message('Willard Points were credited to your account', str(amt) + " Willard Points were added to your account. \n \n"
- + 'You have ' + str(balance) + " Willard Points now.")
- elif sign == '~~' :
- r.redditor(user).message('Willard Points were debited to your account', str(amt) + " Willard Points were administratively removed from your account. \n \n"
- + 'You have ' + str(balance) + " Willard Points left.")
- elif sign == '++' :
- r.redditor(user).message('Willard Points were credited to your account', str(amt) + " Willard Points were administratively added to your account. \n \n"
- + 'You have ' + str(balance) + " Willard Points now.")
- elif sign == '0' :
- r.redditor(user).message('Attempted overdraw detected', "An attempt was made to overdraw your account. No points were deducted and the transaction has been cancelled. You have " + str(balance) + " WP.")
- def get_comment_list() :
- with open ("posts.txt", "r") as f :
- comments_replied_to = f.read()
- comments_replied_to = comments_replied_to.split("\n")
- return comments_replied_to
- def write_comment_list(replied_to) :
- with open ('posts.txt', 'w') as file :
- for i in replied_to :
- file.write (i)
- print ('done')
- replied_to = get_comment_list()
- r = login()
- while True :
- tick += 1
- timen = int(t.time())
- time = datetime.datetime.fromtimestamp(t.time()).strftime('%Y-%m-%d %H:%M:%S')
- queue = retrieve()
- run_bot(r, tick, timen, queue, replied_to, time)
- if tick == 1 :
- print (time + ": The bot has successfully completed one cycle.")
- elif tick == 5 :
- print (time + ": The bot has successfully completed five cycles.")
- elif tick%10 == 0 and tick < 99 :
- print (time + ": The bot has successfully completed " + str(tick) + " cycles.")
- elif tick%100 == 0 and tick < 999 :
- print (time + ": The bot has successfully completed " + str(tick) + " cycles.")
- elif tick%1000 == 0 and tick > 999:
- print (time + ": The bot has successfully completed " + str(tick) + " cycles.")
- replied_to = get_comment_list()
- t.sleep(10)
- except e.RequestException :
- print ('The bot crashed with RequestException. Restarting...')
- continue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement