Advertisement
NateNate60

Willard Points Bot v1.3

Feb 6th, 2019
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 22.71 KB | None | 0 0
  1. """
  2. Welcome to the official source code of Willard Points Bot.
  3. This is version 1.3, which is the first stable version of
  4. the bot with (probably) no bugs.
  5.  
  6. Remember that you can earn Willard Points by simply saying
  7. "shut up exe", or by making good posts in Townofsalemgame.
  8. The bot checks for good posts twice a day. To get that sweet
  9. WP, you must be in the top 12 hot posts in the subreddit and
  10. have more than 50 upvotes on it. Pinned posts do not qualify,
  11. for now. Sorry.
  12.  
  13. Thank's for taking the time to look at the source code! I'll
  14. let you in on a little secret: You can order a can of
  15. executioner spray from NateNate60 now for a discounted price.
  16. Just tell me that you saw the source code in a PM and you can
  17. order a can for the low price of 19.99 WP (+0.01 WP S&H)
  18.  
  19. Released on 6 February 2019. Coded by NateNate60.
  20. """
  21.  
  22. import prawcore.exceptions as e
  23. while True :
  24.     try :
  25.         # NateNate60's Willard Points Bot
  26.         version = "1.3"
  27.  
  28.         print ("Starting Willard Points Bot version", version)
  29.  
  30.         # Module importation
  31.         print ("Importing modules...", end='')
  32.         import praw
  33.         import config4 as config
  34.         import time as t
  35.         import datetime
  36.         import os.path
  37.         print ('done')
  38.         print ("Loading features...", end='')
  39.  
  40.         # On or Off. You can still run it if off, but nothing will actually happen
  41.         on = True
  42.  
  43.         # Set the tick
  44.         tick = 0
  45.  
  46.         # Login function
  47.         def login() :
  48.             print ("Connecting to Reddit")
  49.             print ("Authenticating...", end='')
  50.             r = praw.Reddit(username = config.username,
  51.                             password = config.password,
  52.                             client_id = config.client_id,
  53.                             client_secret = config.client_secret,
  54.                             user_agent = "WillardPointsBot" + version)
  55.             print ('done')
  56.             return r
  57.  
  58.  
  59.         """
  60.        Core running functionality
  61.        """
  62.  
  63.  
  64.         # Retrieve the queue of transactions to be processed
  65.         def retrieve() :
  66.             with open ("transactions.txt", "r") as queuefile :
  67.                 queue = queuefile.read()
  68.                 queue = queue.split('\n')
  69.             open('transactions.txt', 'w').close()
  70.  
  71.             return queue
  72.  
  73.  
  74.         #Set the signature
  75.         signature = "\n \n ^NateNate60's ^Willard ^Points ^Bot ^v" + version + ". ^Don't ^have ^an ^account? ^Use ^!newaccount"
  76.  
  77.  
  78.         # The acutal code
  79.         def run_bot(r, tick, timen, queue, replied_to, time) :
  80.  
  81.            
  82.            
  83.             if on == True :
  84.                 for trans in queue :
  85.                     trans = str(trans)
  86.                     if "~" in trans and "+" not in trans :
  87.                         trans = trans.split('~')
  88.                         user = trans[0]
  89.                         if os.path.isfile (user + ".txt") :
  90.                             with open (user + ".txt", 'r') as u :
  91.                                 balance = u.read()
  92.                             balance = int(balance) - int(trans[1])
  93.                             if balance < 0 :
  94.                                 balance = balance + int(trans[1])
  95.                                 notify (r, user, balance, trans[1], 0)
  96.                             with open (user + ".txt", 'w') as s :
  97.                                 s.write(str(balance))
  98.                             with open ('log.txt', 'a') as log :
  99.                                 log.write ("\n" + time + ' ' + user + " paid " + trans[1] + " WP.")
  100.                                 print (time + ' ' + user + " paid " + trans[1] + " WP.")
  101.                             notify(r, user, balance, trans[1], '~')
  102.  
  103.                     elif "+" in trans and "~" not in trans :
  104.                         trans = trans.split('+')
  105.                         user = trans[0]
  106.                         if os.path.isfile (user + ".txt") :
  107.                             with open (user + ".txt", 'r') as u :
  108.                                 balance = u.read()
  109.                             balance = int(balance) + int(trans[1])
  110.                             with open (user + ".txt", 'w') as s :
  111.                                 s.write(str(balance))
  112.                             with open ('log.txt', 'a') as log :
  113.                                 log.write ("\n" + time + ' ' + user + " gained " + trans[1] + " WP.")
  114.                                 print (time + ' ' + user + " gained " + trans[1] + " WP.")
  115.                             notify(r, user, balance, trans[1], '+')
  116.                 if tick%5 == 0 :
  117.                     for message in r.inbox.unread(limit = 5) :
  118.  
  119.                         #For opening new accounts
  120.                         if "!newacc" in message.body or "!openacc" in message.body or "!createacc" in message.body :
  121.                             accname = str(message.author)
  122.                             if not os.path.isfile(accname + ".txt") :
  123.                                 accowner = accname
  124.                                 with open (accname + ".txt", 'w') as newacc :
  125.                                     newacc.write ("0")
  126.                                 message.reply ('Account creation successful.'
  127.                                                + 'You will recieve a one-time "signing" bonus of 10 Willard Points.' + signature)
  128.                                 with open ('log.txt', 'a') as log :
  129.                                     log.write ("\n" + time + ' ' + accowner + " opened an account under the name " + accname)
  130.                                     print (time + ' ' + accowner + " opened an account under the name " + accname)
  131.                                 with open ('transactions.txt', 'a') as transa :
  132.                                    transa.write ('\n' + str(accname) + '+10')
  133.                             else :
  134.                                 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.'
  135.                                                + signature)
  136.  
  137.                         #For moderators adding or removing money from people's accounts administratively.
  138.                         elif "+" in message.body or '~' in message.body :
  139.                             if message.author not in config.approved :
  140.                                 message.reply ('You are not authorised to make that command.' + signature)
  141.                             else :
  142.                                 trans = message.body
  143.                                 if "~" in trans :
  144.                                     trans = trans.split('~')
  145.                                     user = trans[0]
  146.                                     if os.path.isfile(user + ".txt") :
  147.                                         with open (user + ".txt", 'r') as u :
  148.                                             balance = u.read()
  149.                                         balance = int(balance) - int(trans[1])
  150.                                         if balance < 0 :
  151.                                             balance = 0
  152.                                             message.reply ("That user does not have enough WP, so their balance was set to zero." + signature)
  153.                                         else :
  154.                                             message.reply ("The command completed successfully. \n \n"+ " ^Willard ^Points ^Bot ^v" + version)
  155.                                         with open (user + ".txt", 'w') as s :
  156.                                             s.write(str(balance))
  157.                                         with open ('log.txt', 'a') as log :
  158.                                             log.write ("\n" + time + ' ' + user + " was docked " + trans[1] + " WP by "  + str(message.author))
  159.                                             print (time + ' ' + user + " was docked " + trans[1] + " WP by "  + str(message.author))
  160.                                         notify(r, user, balance, trans[1], '~~')
  161.                                     else :
  162.                                         message.reply ("That account does not exist. Please contact NateNate60 for help." + signature)
  163.                                 elif "+" in trans :
  164.                                     trans = trans.split('+')
  165.                                     user = trans[0]
  166.                                     if os.path.isfile(user + ".txt") :
  167.                                         with open (user + ".txt", 'r') as u :
  168.                                             balance = u.read()
  169.                                         balance = int(balance) + int(trans[1])
  170.                                         message.reply ("The command completed successfully. " + signature)
  171.                                         with open (user + ".txt", 'w') as s :
  172.                                             s.write(str(balance))
  173.                                         with open ('log.txt', 'a') as log :
  174.                                             log.write ("\n" + time + ' ' + user + " was awarded " + trans[1] + " WP by "  + str(message.author))
  175.                                         notify(r, user, balance, trans[1], '++')
  176.                                     else :
  177.                                         message.reply ("That account does not exist. Please contact NateNate60 for help." + signature)
  178.  
  179.                         #Balance checking
  180.                         elif "!bal" in message.body :
  181.                             payload = message.body.split(" ")
  182.                             if len(payload) != 2 :
  183.                                 message.reply ('Invalid syntax. Try !balance [username]')
  184.                             else :
  185.                                 if os.path.isfile(payload[1] + ".txt") :
  186.                                     with open (payload[1] + ".txt") as acc :
  187.                                         bal = acc.read()
  188.                                     message.reply("Your account currently has " + bal + " Willard Points." + signature)
  189.                                 else :
  190.                                     message.reply ("That account does not exist. Use !newaccount" + signature)
  191.  
  192.                         # Transferring
  193.                         elif "!trans" in message.body :
  194.                             payload = message.body
  195.                             payload = payload.split(' ')
  196.                             to = payload [1]
  197.                             amt = payload [2]
  198.                             fromfile = str(message.author) + ".txt"
  199.                             if os.path.isfile(fromfile) :
  200.                                 if os.path.isfile(to + ".txt") :
  201.                                     with open (fromfile, 'r') as f :
  202.                                         balance = f.read()
  203.                                         balance = balance - amt
  204.                                         if balance < 0 :
  205.                                             balance = balance + amt
  206.                                             notify (r, message.author, balance, None, '0')
  207.                                         else :
  208.                                             with open ('transactions.txt', 'a') as trans :
  209.                                                 trans.write("\n" + str(message.author) + "~" + amt)
  210.                                                 trans.write('\n' + to + "+" + amt)
  211.                                 else :
  212.                                     message.reply ("Invalid recepient. Make sure you spelled the recepient's username correctly." + signature)
  213.                             else :
  214.                                 message.reply ("Invalid sender. You do not have an account. Use !newaccount to create a new account." + signature)
  215.                         elif "shut up" in message.body.lower() :
  216.                             message.reply ('no u' + signature)
  217.                         elif "!isup" in message.body.lower() or "!stat" in message.body.lower() :
  218.                             message.reply ("Online." + signature)
  219.                         # Mark the message as read.
  220.                         r.inbox.mark_read([message])
  221.  
  222.  
  223.  
  224.  
  225.                 #For awarding WP to good posts
  226.                 timen = int(timen) / 10
  227.                 timen = round (timen)
  228.                 timen = timen * 10
  229.                 if timen%43200 == 0 :
  230.                     for post in r.subreddit('townofsalemgame').hot(limit = 12) :
  231.                         if post.stickied :
  232.                             continue
  233.                         elif post.score > 50 and post.score < 100 :
  234.                             if post.id not in replied_to :
  235.                                 post.reply ("It looks like this is a mildly good post. It's reached the Framer tier! \n \n" +
  236.                                             "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" +
  237.                                             "^WillardPointsBot ^version " + "^" + version + " ^Contact ^NateNate60 ^for ^information. ^Don't ^have ^an ^account? ^Reply ^!newaccount")
  238.                             award = post.score // 10
  239.                             print (time, post.author, "gained", str(award), "for FRAMER.")
  240.                             with open ("transactions.txt", 'a') as trans :
  241.                                 trans.write (str(post.author) + "+" + str(award))
  242.                                 replied_to.append (post.id)
  243.                         elif post.score > 100 and post.score < 200 :
  244.                             if post.id in replied_to :
  245.                                 for comment in post.comments :
  246.                                     if comment.author != r.user.me() :
  247.                                         continue
  248.                                     else :
  249.                                         comment.edit ("It looks like this is a pretty good post. It's reached the Mafioso tier! \n \n" +
  250.                                                       "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" +
  251.                                                       "^WillardPointsBot ^version " + "^" + version + " ^Contact ^NateNate60 ^for ^information. ^Don't ^have ^an ^account? ^Reply ^!newaccount")
  252.                             else :
  253.                                 post.reply ("It looks like this is a pretty good post. It's reached the Mafioso tier! Have some more Willard Points! \n \n" +
  254.                                             "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" +
  255.                                             "^WillardPointsBot ^version " + "^" + version + " ^Contact ^NateNate60 ^for ^information. ^Don't ^have ^an ^account? ^Reply ^!newaccount")
  256.                             award = post.score // 15
  257.                             award = award + 5
  258.                             print (time, post.author, "gained", str(award), "for MAFIOSO.")
  259.                             with open ("transactions.txt", 'a') as trans :
  260.                                 trans.write ('\n' + str(post.author) + "+" + str(award))
  261.                         elif post.score > 200 and post.score < 500 :
  262.                             if post.id in replied_to :
  263.                                 for comment in post.comments :
  264.                                     if comment.author != r.user.me() :
  265.                                         continue
  266.                                     else :
  267.                                         comment.edit ("It looks like this is a really dank post. It's reached the Blackmailer tier! \n \n" +
  268.                                                       "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" +
  269.                                                       "^WillardPointsBot ^version " + "^" + version + " ^Contact ^NateNate60 ^for ^information. ^Don't ^have ^an ^account? ^Reply ^!newaccount")
  270.                             else :
  271.                                 post.reply ("It looks like this is a really dank post. It's reached the Blackmailer tier! Have some more Willard Points! \n \n" +
  272.                                             "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" +
  273.                                             "^WillardPointsBot ^version " + "^" + version + " ^Contact ^NateNate60 ^for ^information. ^Don't ^have ^an ^account? ^Reply ^!newaccount")
  274.                                 replied_to.append (post.id)
  275.                             award = post.score // 20
  276.                             award = award + 10
  277.                             print (time, post.author, "gained", str(award), "for BLACKMAILER.")
  278.                             with open ("transactions.txt", 'a') as trans :
  279.                                 trans.write ('\n' + str(post.author) + "+" + str(award))
  280.                         elif post.score > 500 and post.score < 1000 :
  281.                             for comment in post.comments :
  282.                                 if comment.author != r.user.me() :
  283.                                     continue
  284.                                 else :
  285.                                     comment.edit ("It looks like this is an extremely good post. It's reached the Consigliere tier! Have some more Willard Points! \n \n" +
  286.                                                   "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" +
  287.                                                   "^WillardPointsBot ^version " + "^" + version + " ^Contact ^NateNate60 ^for ^information. ^Don't ^have ^an ^account? ^Reply ^!newaccount")
  288.                                     award = post.score // 20
  289.                                     award = award + 15
  290.                                     print (time, post.author, "gained", str(award), "for CONSIGLIERE.")
  291.                                     with open ("transactions.txt", 'a') as trans :
  292.                                         trans.write ('\n' + str(post.author) + "+" + str(award))
  293.                         elif post.score > 1000 :
  294.                             for comment in post.comments :
  295.                                 if comment.author != r.user.me() :
  296.                                     continue
  297.                                 else :
  298.                                     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" +
  299.                                                   "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" +
  300.                                                   "^WillardPointsBot ^version " + "^" + version + " ^Contact ^NateNate60 ^for ^information. ^Don't ^have ^an ^account? ^Reply ^!newaccount")
  301.                                     award = 50
  302.                                     print (time, post.author, "gained 50 for GODFATHER.")
  303.                                     with open ("transactions.txt", 'a') as trans :
  304.                                         trans.write ('\n' + str(post.author) + "+" + str(award))
  305.                 write_comment_list(replied_to)
  306.  
  307.  
  308.         def notify (r, user, balance, amt, sign) :
  309.             if sign == '~' :
  310.                 r.redditor(user).message('Willard Points were debited from your account', str(amt) + " Willard Points were deducted from your account. \n \n"
  311.                                          + 'You have ' + str(balance) + " Willard Points left.")
  312.             elif sign == '+' :
  313.                 r.redditor(user).message('Willard Points were credited to your account', str(amt) + " Willard Points were added to your account. \n \n"
  314.                                          + 'You have ' + str(balance) + " Willard Points now.")
  315.             elif sign == '~~' :
  316.                 r.redditor(user).message('Willard Points were debited to your account', str(amt) + " Willard Points were administratively removed from your account. \n \n"
  317.                                          + 'You have ' + str(balance) + " Willard Points left.")
  318.             elif sign == '++' :
  319.                 r.redditor(user).message('Willard Points were credited to your account', str(amt) + " Willard Points were administratively added to your account. \n \n"
  320.                                          + 'You have ' + str(balance) + " Willard Points now.")
  321.             elif sign == '0' :
  322.                 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.")
  323.  
  324.         def get_comment_list() :
  325.             with open ("posts.txt", "r") as f :
  326.                 comments_replied_to = f.read()
  327.                 comments_replied_to = comments_replied_to.split("\n")
  328.             return comments_replied_to
  329.         def write_comment_list(replied_to) :
  330.             with open ('posts.txt', 'w') as file :
  331.                 for i in replied_to :
  332.                     file.write (i)
  333.         print ('done')
  334.  
  335.  
  336.         replied_to = get_comment_list()
  337.         r = login()
  338.         while True :
  339.  
  340.             tick += 1
  341.             timen = int(t.time())
  342.             time = datetime.datetime.fromtimestamp(t.time()).strftime('%Y-%m-%d %H:%M:%S')
  343.             queue = retrieve()
  344.             run_bot(r, tick, timen, queue, replied_to, time)
  345.             if tick == 1 :
  346.                 print (time + ": The bot has successfully completed one cycle.")
  347.             elif tick == 5 :
  348.                 print (time + ": The bot has successfully completed five cycles.")
  349.             elif tick%10 == 0 and tick < 99 :
  350.                 print (time + ": The bot has successfully completed " + str(tick) + " cycles.")
  351.             elif tick%100 == 0 and tick < 999 :
  352.                 print (time + ": The bot has successfully completed " + str(tick) + " cycles.")
  353.             elif tick%1000 == 0 and tick > 999:
  354.                 print (time + ": The bot has successfully completed " + str(tick) + " cycles.")
  355.             replied_to = get_comment_list()
  356.             t.sleep(10)
  357.     except e.RequestException :
  358.         print ('The bot crashed with RequestException. Restarting...')
  359.         continue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement