Advertisement
NateNate60

Willard Points Bot v1.7

Feb 12th, 2019
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 25.82 KB | None | 0 0
  1. import prawcore.exceptions as e
  2. while True :
  3.     try :
  4.         # NateNate60's Willard Points Bot
  5.         version = "1.7"
  6.  
  7.         print ("Starting Willard Points Bot version", version)
  8.  
  9.         # Module importation
  10.         print ("Importing modules...", end='')
  11.         import praw
  12.         import config4 as config
  13.         import time as t
  14.         import datetime
  15.         import os.path
  16.         print ('done')
  17.         print ("Loading features...", end='')
  18.  
  19.         # On or Off. You can still run it if off, but nothing will actually happen
  20.         on = True
  21.  
  22.         # Set the tick
  23.         tick = 0
  24.  
  25.         # Login function
  26.         def login() :
  27.             print ("Connecting to Reddit")
  28.             print ("Authenticating...", end='')
  29.             r = praw.Reddit(username = config.username,
  30.                             password = config.password,
  31.                             client_id = config.client_id,
  32.                             client_secret = config.client_secret,
  33.                             user_agent = "WillardPointsBot" + version)
  34.             print ('done')
  35.             return r
  36.  
  37.  
  38.         """
  39.        Core running functionality
  40.        """
  41.  
  42.  
  43.         # Retrieve the queue of transactions to be processed
  44.         def retrieve() :
  45.             with open ("transactions.txt", "r") as queuefile :
  46.                 queue = queuefile.read()
  47.                 queue = queue.split('\n')
  48.             open('transactions.txt', 'w').close()
  49.  
  50.             return queue
  51.  
  52.  
  53.         #Set the signature
  54.         signature = "\n \n ^NateNate60's ^Willard ^Points ^Bot ^v" + version + ". ^Don't ^have ^an ^account? ^Use ^!newaccount. ^!info ^for ^information"
  55.  
  56.  
  57.         # The acutal code
  58.         def run_bot(r, tick, timen, queue, replied_to, time, blacklist) :
  59.  
  60.            
  61.            
  62.             if on == True :
  63.                 for trans in queue :
  64.                     trans = str(trans)
  65.                     if "~" in trans and "+" not in trans :
  66.                         trans = trans.split('~')
  67.                         user = trans[0]
  68.                         if os.path.isfile (user + ".txt") :
  69.                             with open (user + ".txt", 'r') as u :
  70.                                 balance = u.read()
  71.                             balance = int(balance) - int(trans[1])
  72.                             if balance < 0 :
  73.                                 balance = balance + int(trans[1])
  74.                                 notify (r, user, balance, trans[1], 0)
  75.                             with open (user + ".txt", 'w') as s :
  76.                                 s.write(str(balance))
  77.                             with open ('log.txt', 'a') as log :
  78.                                 log.write ("\n" + time + ' ' + user + " paid " + trans[1] + " WP.")
  79.                                 print (time + ' ' + user + " paid " + trans[1] + " WP.")
  80.                             notify(r, user, balance, trans[1], '~')
  81.  
  82.                     elif "+" in trans and "~" not in trans :
  83.                         trans = trans.split('+')
  84.                         user = trans[0]
  85.                         if os.path.isfile (user + ".txt") :
  86.                             with open (user + ".txt", 'r') as u :
  87.                                 balance = u.read()
  88.                             balance = int(balance) + int(trans[1])
  89.                             with open (user + ".txt", 'w') as s :
  90.                                 s.write(str(balance))
  91.                             with open ('log.txt', 'a') as log :
  92.                                 log.write ("\n" + time + ' ' + user + " gained " + trans[1] + " WP.")
  93.                                 print (time + ' ' + user + " gained " + trans[1] + " WP.")
  94.                             notify(r, user, balance, trans[1], '+')
  95.                 if tick%5 == 0 :
  96.                     for message in r.inbox.unread(limit = 5) :
  97.                         #For blacklisting
  98.                         if "!black" in message.body.lower() or "unsub" in message.body.lower() or "ignore" in message.body.lower() :
  99.                             message.reply ("Sorry to see you go. You have been temporarily blacklisted. Due to a technical problem, the bot cannot permanently blacklist you automatically." +
  100.                                            " Pinging u/NateNate60 for permanent blacklisting. This ping WILL NOT WORK if this command was issued in a PM, so please contact NateNate60 yourself." + signature)
  101.                             blacklist.append(message.author)
  102.                             print (time, message.author, "requested to be blacklisted")
  103.                         #For opening new accounts
  104.                         if "!newacc" in message.body or "!openacc" in message.body or "!createacc" in message.body :
  105.                             accname = str(message.author)
  106.                             if not os.path.isfile(accname + ".txt") :
  107.                                 accowner = accname
  108.                                 with open (accname + ".txt", 'w') as newacc :
  109.                                     newacc.write ("0")
  110.                                 message.reply ('Account creation successful. '
  111.                                                + 'You will recieve a one-time "signing" bonus of 10 Willard Points.' + signature)
  112.                                 with open ('log.txt', 'a') as log :
  113.                                     log.write ("\n" + time + ' ' + accowner + " opened an account.")
  114.                                     print (time + ' ' + accowner + " opened an account.")
  115.                                 with open ('transactions.txt', 'a') as transa :
  116.                                    transa.write ('\n' + str(accname) + '+10')
  117.                             else :
  118.                                 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.'
  119.                                                + signature)
  120.  
  121.                                
  122.                         #For querying information
  123.                         elif "!inf" in message.body.lower() or "!help" in message.body.lower() :
  124.                             message.reply ("NateNate60's Willard Points Bot version " + version + "\n \n" +
  125.                                            "Willard Points Bot is a bot created by NateNate60 to conduct transactions in Willard Points. What are Willard Points, you say? WP is a completely useless" +
  126.                                            ' joke currency that is issued to people saying "shut up exe" and making dank memes in r/townofsalemgame.' +
  127.                                            " Commands may be issued to the bot using the NEUTRALEVIL protocol. \n \n" +
  128.                                            "`!createaccount` will open a new account for you. I'll even give you 10 WP just for doing that. \n \n" +
  129.                                            "`!bal [username]` to check [username]'s account balance. For example, `!bal Willard_Points_Bot` will query Willard_Points_Bot's account balance. \n \n" +
  130.                                            "`!transfer [amount] [recepient]` will transfer [amount] Willard Points to [recepient]. \n \n" +
  131.                                            "`[username]+[amount]` and `[username]~[amount]` will add and subtract [amount[ WP from [username] respectively. \n \n" +
  132.                                            "`!blacklist` to unsubscribe yourself from the Willard Points programme. Your account will be deleted in due time and the bot will no longer reply to your posts awarding WP. \n \n" +
  133.                                            "As long as I see the command in my inbox I will process it. You can either reply to one of my messages, or compose a PM to me by clicking the little envelope button next" +
  134.                                            ' to your name and then going to "compose new message". I cannot use the chat function of New Reddit yet. Stay tuned for more updates! If you have any questions, contact NatenNate60.')
  135.  
  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 "bad bot" in message.body.lower() :
  218.                             message.reply ("shut up human" + signature)
  219.                         elif "!isup" in message.body.lower() or "!stat" in message.body.lower() :
  220.                             message.reply ("Online." + signature)
  221.                         #elif "!black" or "!ign
  222.                         # Mark the message as read.
  223.                         r.inbox.mark_read([message])
  224.  
  225.  
  226.  
  227.  
  228.                 #For awarding WP to good posts
  229.                 timen = int(timen) / 10
  230.                 timen = round (timen)
  231.                 timen = timen * 10
  232.                 if timen%21600 == 0 :
  233.                     for post in r.subreddit('townofsalemgame').hot(limit = 12) :
  234.                         if post.stickied or post.author in blacklist :
  235.                             continue
  236.                         elif post.score > 50 and post.score < 100 :
  237.                             if post.id not in replied_to :
  238.                                 post.reply ("It looks like this is a good post. It's reached the Framer tier! \n \n" +
  239.                                             "Willard Points issued at the Framer tier are equal to 1 WP for every 15 upvotes for every 6 hours this stays on the front page. Pinned posts are not eligible. The longer it stays on the front page, the more you'll get.\n \n" +
  240.                                             signature)
  241.                             award = post.score // 15
  242.                             print (time, post.author, "gained", str(award), "for FRAMER.")
  243.                             with open ("transactions.txt", 'a') as trans :
  244.                                 trans.write (str(post.author) + "+" + str(award))
  245.                                 replied_to.append (post.id)
  246.                         elif post.score > 100 and post.score < 200 :
  247.                             if post.id in replied_to :
  248.                                 for comment in post.comments :
  249.                                     if comment.author != r.user.me() :
  250.                                         continue
  251.                                     else :
  252.                                         comment.edit ("It looks like this is a pretty good post. It's reached the Mafioso tier! \n \n" +
  253.                                                       "Willard Points issued at the Mafioso tier are equal to 1 WP for every 15 upvotes (plus 3 WP) for every 6 hours this stays on the front page. Pinned posts are not eligible. The longer it stays on the front page, the more you'll get.\n \n" +
  254.                                                       signature)
  255.                             else :
  256.                                 post.reply ("It looks like this is a pretty good post. It's reached the Mafioso tier! \n \n" +
  257.                                             "Willard Points issued at the Mafioso tier are equal to 1 WP for every 15 upvotes (plus 3 WP) for every 6 hours this stays on the front page. Pinned posts are not eligible. The longer it stays on the front page, the more you'll get.\n \n" +
  258.                                             signature)
  259.                             award = post.score // 20
  260.                             award = award + 3
  261.                             print (time, post.author, "gained", str(award), "for MAFIOSO.")
  262.                             with open ("transactions.txt", 'a') as trans :
  263.                                 trans.write ('\n' + str(post.author) + "+" + str(award))
  264.                         elif post.score > 200 and post.score < 500 :
  265.                             if post.id in replied_to :
  266.                                 for comment in post.comments :
  267.                                     if comment.author != r.user.me() :
  268.                                         continue
  269.                                     else :
  270.                                         comment.edit ("It looks like this is a really dank post. It's reached the Blackmailer tier! \n \n" +
  271.                                                       "Willard Points issued at the Blackmailer tier are equal to 1 WP for every 20 upvotes (plus 5 WP) for every 6 hours this stays on the front page. Pinned posts are not eligible. The longer it stays on the front page, the more you'll get.\n \n" +
  272.                                                       signature)
  273.                             else :
  274.                                 post.reply ("It looks like this is a really dank post. It's reached the Blackmailer tier! \n \n" +
  275.                                             "Willard Points issued at the Blackmailer tier are equal to 1 WP for every 20 upvotes. (plus 5 WP) for every 6 hours this stays on the front page. Pinned posts are not eligible. The longer it stays on the front page, the more you'll get.\n \n" +
  276.                                             signature)
  277.                                 replied_to.append (post.id)
  278.                             award = post.score // 20
  279.                             award = award + 5
  280.                             print (time, post.author, "gained", str(award), "for BLACKMAILER.")
  281.                             with open ("transactions.txt", 'a') as trans :
  282.                                 trans.write ('\n' + str(post.author) + "+" + str(award))
  283.                         elif post.score > 500 and post.score < 1000 :
  284.                             for comment in post.comments :
  285.                                 if comment.author != r.user.me() :
  286.                                     continue
  287.                                 else :
  288.                                     comment.edit ("It looks like this is an extremely good post. It's reached the Consigliere tier! \n \n" +
  289.                                                   "Willard Points issued at the Consigliere tier are equal to 1 WP for every 20 upvotes. (plus 10 WP) for every 6 hours this stays on the front page. Pinned posts are not eligible. The longer it stays on the front page, the more you'll get.\n \n" +
  290.                                                   signature)
  291.                                     award = post.score // 20
  292.                                     award = award + 10
  293.                                     print (time, post.author, "gained", str(award), "for CONSIGLIERE.")
  294.                                     with open ("transactions.txt", 'a') as trans :
  295.                                         trans.write ('\n' + str(post.author) + "+" + str(award))
  296.                         elif post.score > 1000 :
  297.                             for comment in post.comments :
  298.                                 if comment.author != r.user.me() :
  299.                                     continue
  300.                                 else :
  301.                                     comment.edit ("It looks like this is one of the best posts of all time! Congratulations! It's reached the Godfather tier! \n \n" +
  302.                                                   "Willard Points issued at the Godfather tier are equal to 1 WP for ever 20 upvotes (plus 20 WP) for every 6 hours this stays on the front page. Pinned posts are not eligible. The longer it stays on the front page, the more you'll get. \n \n" +
  303.                                                   signature)
  304.                                     award = post.score // 20
  305.                                     award = award + 20
  306.                                     print (time, post.author, "gained 50 for GODFATHER.")
  307.                                     with open ("transactions.txt", 'a') as trans :
  308.                                         trans.write ('\n' + str(post.author) + "+" + str(award))
  309.                         replied_to.append (post.id)
  310.                 write_comment_list(replied_to)
  311.  
  312.         def notify (r, user, balance, amt, sign) :
  313.             if sign == '~' :
  314.                 r.redditor(user).message('Willard Points were debited from your account', str(amt) + " Willard Points were deducted from your account. \n \n"
  315.                                          + 'You have ' + str(balance) + " Willard Points left.")
  316.             elif sign == '+' and int(amt) > 1 :
  317.                 r.redditor(user).message('Willard Points were credited to your account', str(amt) + " Willard Points were added to your account. \n \n"
  318.                                          + 'You have ' + str(balance) + " Willard Points now.")
  319.             elif sign == '~~' :
  320.                 r.redditor(user).message('Willard Points were debited to your account', str(amt) + " Willard Points were administratively removed from your account. \n \n"
  321.                                          + 'You have ' + str(balance) + " Willard Points left.")
  322.             elif sign == '++' :
  323.                 r.redditor(user).message('Willard Points were credited to your account', str(amt) + " Willard Points were administratively added to your account. \n \n"
  324.                                          + 'You have ' + str(balance) + " Willard Points now.")
  325.             elif sign == '0' :
  326.                 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.")
  327.  
  328.         def get_comment_list() :
  329.             with open ("comments.txt", "r") as f :
  330.                 comments_replied_to = f.read()
  331.                 comments_replied_to = comments_replied_to.split("\n")
  332.             return comments_replied_to
  333.         def write_comment_list(replied_to) :
  334.             with open ('comments.txt', 'w') as file :
  335.                 for i in replied_to :
  336.                     file.write (i + '\n')
  337.         print ('done')
  338.         #NOTES
  339.         # [OK] Test run_bot for a debiting statement in the queue file (which should be
  340.         # in the form of 1234-5 where 1234 is debited 5 WP)
  341.         #
  342.         # [OK] Make sure to add a statement for crediting, too.
  343.         #
  344.         # [OK] Account creation should be quick and easy. The bot should periodically
  345.         # check its inbox for messages with !createacc or !openacc or !newacc and make
  346.         # a new account
  347.         #
  348.         # [OK] The bot should honour !credit [amt], !add, !debit, !sub commands only from
  349.         # authorised users. Namely, NateNate60 and the mods of r/townofsalemgame
  350.         #
  351.         # [OK] The bot should be able to facilitate transactions between users with !transfer
  352.         # between the account holder (VERIFY THIS TO STOP FRAUD) to any other account.
  353.         # This may prove to be tricky to implement.
  354.         #
  355.         # [OK] The bot should honour !bal [acc #] commands from anyone, for any account.
  356.         #
  357.         # [OK] USERNAMES MUST NOT CONTAIN "+" OR "-"
  358.         #
  359.         # [OK] Mark read messages as read.
  360.  
  361.         replied_to = get_comment_list()
  362.         r = login()
  363.         blacklist = config.blacklist
  364.         time = datetime.datetime.fromtimestamp(t.time()).strftime('%Y-%m-%d %H:%M:%S')
  365.         print ("Right now, it's PST " + time)
  366.         while True :
  367.             replied_to = get_comment_list()
  368.             tick += 1
  369.             timen = int(t.time())
  370.             time = datetime.datetime.fromtimestamp(t.time()).strftime('%Y-%m-%d %H:%M:%S')
  371.             queue = retrieve()
  372.             run_bot(r, tick, timen, queue, replied_to, time, blacklist)
  373.             if tick == 1 :
  374.                 print (time + ": The bot has successfully completed one cycle.")
  375.             elif tick == 5 :
  376.                 print (time + ": The bot has successfully completed five cycles.")
  377.             elif tick%10 == 0 and tick < 99 :
  378.                 print (time + ": The bot has successfully completed " + str(tick) + " cycles.")
  379.             elif tick%100 == 0 and tick < 999 :
  380.                 print (time + ": The bot has successfully completed " + str(tick) + " cycles.")
  381.             elif tick%1000 == 0 and tick > 999:
  382.                 print (time + ": The bot has successfully completed " + str(tick) + " cycles.")
  383.            
  384.             t.sleep(10)
  385.     except e.RequestException :
  386.         print ('The bot crashed with RequestException. Restarting...')
  387.         continue
  388.     except ValueError :
  389.         print ('The bot crashed with ValueError. Restarting...')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement