Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.17 KB | None | 0 0
  1. import praw
  2. import config
  3. import random
  4. import firebase_admin
  5. import time
  6. import os
  7. import re
  8. from firebase_admin import credentials, firestore, db
  9.  
  10. print("Initializing Firebase...")
  11. cred = credentials.Certificate("credentials.json")
  12. app = firebase_admin.initialize_app(cred)
  13. db = firestore.client()
  14. print("OK!")
  15.  
  16. print("Logging into Reddit...")
  17. reddit = praw.Reddit(user_agent='KickOpenTheDoor bot by u/RPG_Ivan and u/VirtuousVermin',
  18.                   client_id=config.client_id,
  19.                   client_secret=config.client_secret,
  20.                   username=config.username,
  21.                   password=config.password)
  22. print("OK!")
  23.  
  24.  
  25. #Use kiot as a test sub for now.
  26. subreddit = reddit.subreddit('kickopenthedoor')
  27.  
  28.  
  29. nu = 0
  30. x = 0
  31.  
  32. #COMMANDS VIA COMMENTING
  33. comments = subreddit.stream.comments(pause_after=None, skip_existing=True)
  34. try:
  35.     def run():
  36.         for comment in comments:
  37.             if comment is None:
  38.                 break
  39.             #NEW USER CREATION
  40.             users_ref = db.collection('users')
  41.             users = users_ref.get()
  42.             author = comment.author.name   
  43.             docs = []
  44.             for document in users:
  45.                 docs.append(document.id)
  46.             if author not in docs:
  47.                 print("Generating...")
  48.                 doc = db.collection('users').document(author)
  49.                 doc.set({
  50.                     'inventory': {},
  51.                     'discord_id':'',
  52.                     'race':'',
  53.                     'gold':'0'
  54.                     })
  55.                 print('Generated new profile.')
  56.                 reddit.redditor(author).message('Welcome to r/KickOpenTheDoor!', '#Welcome to r/KickOpenTheDoor!'+'\n\n'+'Your user profile has been created. You have been granted:'+'\n\n'+'*0 Gold'+'\n\n'+'Have fun slaying them monsters!')
  57.             if comment.body == '!balance' and comment.id not in processed:
  58.                 if comment is None:
  59.                     pass
  60.                 else:
  61.                     print('Getting balance...')
  62.                     user_ref = db.collection('users').document(str(comment.author))
  63.                     user = user_ref.get()
  64.                     gold = user.get('gold')
  65.                     comment.reply(f'Your balance is: {gold} gold.')
  66.                     f = open("processed.txt", "a")
  67.                     f.write(comment.id + ", ")
  68.             #ATTACK SYSTEM
  69.             monsters_ref = db.collection('monsters')
  70.             monsters = monsters_ref.get()
  71.             post_id = comment.link_id[3:]
  72.             basedamage = random.randint(1, 3)
  73.             #SHOP SYSTEM
  74.             shop2 = db.collection(u'shop').get()
  75.             items = []
  76.             items2 = []
  77.             for document in shop2:
  78.                 items.append(document.id)
  79.             comment_buying = []
  80.             random_low_item = db.collection('lowtieritems').get()
  81.             for document in random_low_item:
  82.                 items2.append(document.id)
  83.             #Command parsing goes here
  84.             content = comment.body.lower()
  85.             if "!attack" in content and comment.id not in processed:
  86.                 for document in monsters:
  87.                     if document.id == post_id:
  88.                         #Normal attacking (without a weapon)
  89.                         weaponname = content[8:]
  90.                         monster_ref = monsters_ref.document(post_id)
  91.                         monster = monster_ref.get()
  92.                         name = monster.get('name')
  93.                         oldhp = monster.get('hp')
  94.                         attacked = monster.get('attacked')
  95.                         if comment.author.name in attacked:
  96.                             comment.reply('You have already attacked this monster!')
  97.                         elif comment.author.name not in attacked:
  98.                             if weaponname == "":
  99.                                 if oldhp > 0:
  100.                                     while True:
  101.                                         try:
  102.                                             user_ref = db.collection('users').document(comment.author.name)
  103.                                             user = user_ref.get()
  104.                                             oldgold = user.get('gold')
  105.                                             damage = basedamage
  106.                                             cash = 10*damage
  107.                                             user_ref.update({
  108.                                                 'gold':oldgold+cash
  109.                                                 })
  110.                                             monster_ref.update({
  111.                                                 'hp': oldhp - damage,
  112.                                                 'attacked': attacked + [comment.author.name],
  113.                                                 })
  114.                                             break
  115.                                         except AttributeError:
  116.                                             continue
  117.                                 monster = monster_ref.get()
  118.                                 hp = str(monster.get('hp'))
  119.                                 if int(hp) <= 0:
  120.                                     comment.submission.mod.flair("Slain!")
  121.                                     comment.reply(f'You attack {name}! You deal {basedamage} base damage! It has {hp} health remaining! The beast has been slain!'+'\n\n'+f'You earned {cash} gold! To view your balance, reply to this message with !balance.')                               
  122.                                 elif int(hp) > 0:
  123.                                     comment.submission.mod.flair(f'{hp} HP remaining!')
  124.                                     comment.reply(f'You attack {name}! You deal {basedamage} base damage! It has {hp} health remaining!'+'\n\n'+f'You earned {cash} gold! To view your balance, reply to this message with !balance.')
  125.                                 elif oldhp <= 0:
  126.                                     comment.reply('Sorry, but this beast has already been slain!')
  127.                             else:
  128.                                 inventory_ref = db.collection('users').document(comment.author.name)
  129.                                 inventory = inventory_ref.get()
  130.                                 inv = inventory_ref.collection('inventory').document(weaponname)
  131.                                 try:
  132.                                     print(weaponname)
  133.                                     weapon = inv.get(weaponname)
  134.                                     wname = inv.get('name')
  135.                                     if oldhp > 0:
  136.                                         user_ref = db.collection('users').document(comment.author.name)
  137.                                         user = user_ref.get()
  138.                                         oldgold = user.get('gold')
  139.                                         extradamage = weapon.get('damage')
  140.                                         damage = basedamage+extradamage
  141.                                         cash = 10*damage
  142.                                         user_ref.update({
  143.                                             'gold':oldgold+cash
  144.                                             })
  145.                                         monster_ref.update({
  146.                                             'hp': oldhp - damage,
  147.                                             'attacked': attacked + [comment.author.name],
  148.                                             })
  149.                                     monster = monster_ref.get()
  150.                                     hp = str(monster.get('hp'))
  151.                                     if int(hp) <= 0:
  152.                                         comment.submission.mod.flair("Slain!")
  153.                                         comment.reply(f'You attack {name}! You deal {basedamage} base damage and using {wname} deals an extra {extradamage} damage! It has {hp} health remaining! The beast has been slain!'+'\n\n'+f'You earned {cash} gold! To view your balance, reply to this message with !balance.')                             
  154.                                     elif int(hp) > 0:
  155.                                         comment.submission.mod.flair(f'{hp} HP remaining!')
  156.                                         comment.reply(f'You attack {name}! You deal {basedamage} base damage and using {wname} deals an extra {extradamage} damage! It has {hp} health remaining!'+'\n\n'+f'You earned {cash} gold! To view your balance, reply to this message with !balance.')
  157.                                     elif oldhp <= 0:
  158.                                         comment.reply('Sorry, but this beast has already been slain!')
  159.                                 except (KeyError, AttributeError):
  160.                                     print('Error')
  161.                                     comment.reply('Either you don\'t have that weapon, or it doesn\'t exist!')
  162.  
  163.                         print('Comment processed!')
  164.                         f = open("processed.txt", "a")
  165.                         f.write(comment.id + ", ")
  166.             if "!buy" in comment.body and comment.id not in processed:
  167.                 while True:
  168.                     print('Buy Request Found!')
  169.                     global nu
  170.                     global x
  171.                     #Used for checking if item being purchased exists, if not add it to the excluded list and break out of while True:
  172.                     if nu > len(items):
  173.                         nu=0
  174.                         x = x + 1
  175.                     if x > 1:
  176.                         f = open("processed", "a")
  177.                         f.write(comment.id + ", ")
  178.                         x = 0
  179.                         comment.reply('That item does not exist. Please try one of these: patrician, plebian.')
  180.                         break
  181.                     #Defining more variables
  182.                     all_items = ''.join(str(e) for e in items[-nu])
  183.                     item_in_comment = re.findall(all_items, content)
  184.                     comment_buying.extend(item_in_comment)
  185.                     #Checking if there's an item in the comment, it iterates through the list one at a time
  186.                     if bool(item_in_comment) is False:
  187.                         nu = nu + 1
  188.                         print(nu)
  189.                         continue
  190.                     else:
  191.                         #More variables
  192.                         comment_shop = ''.join(str(e) for e in comment_buying[-1])
  193.                         all_users = db.collection('users').document(str(comment.author))
  194.                         total_users = all_users.get()
  195.                         gold = total_users.get('gold')
  196.                         shop_ref = db.collection('shop').document(comment_shop)
  197.                         shop = shop_ref.get()
  198.                         price = shop.get('price')
  199.                     #Putting the item in the user's inventory & subtracting the price
  200.                     if int(gold) < int(price):
  201.                         print('Transaction Declined!')
  202.                         comment.reply(f'You do not have enough gold for the {comment_shop} treasure card!')
  203.                     else:
  204.                         print('Item Bought!')
  205.                         if 'plebian' in comment_shop:
  206.                             low_tier_items = ''.join(str(e) for e in items2[random.randint(0,8)])
  207.                             low_tier_refs = db.collection('lowtieritems').document(low_tier_items)
  208.                             low_tier = low_tier_refs.get()
  209.                             low_items = low_tier.get('name')
  210.                             low_items_dmg = low_tier.get('dmg')
  211.                             inventory = all_users.collection('inventory').document(low_items)
  212.                             inventory.set({'name': low_items,
  213.                                                 'damage': int(low_items_dmg),
  214.                                                 'amount': 1,
  215.                                                 'uses': 10,
  216.                                                 'equipped': False}, merge=True)
  217.                             comment.reply(f'Ta-Da! You have recieved the {low_items}. It deals {low_items_dmg} bonus damage!')
  218.  
  219.                             ammount_final = gold - price
  220.                             all_users.update({'gold':ammount_final})
  221.                         if 'patrician' in comment_shop:
  222.                             high_tier_items = ''.join(str(e) for e in items2[random.randint(0,8)])
  223.                             high_tier_refs = db.collection('hightieritems').document(high_tier_items)
  224.                             high_tier = high_tier_refs.get()
  225.                             high_items = high_tier.get('name')
  226.                             high_items_dmg = high_tier.get('dmg')
  227.                             inventory = all_users.collection('inventory').document(high_items)
  228.                             inventory.set({'name': high_items,
  229.                                                 'damage': int(high_items_dmg),
  230.                                                 'amount': 1,
  231.                                                 'uses': 10,
  232.                                                 'equipped': False}, merge=True)
  233.                             comment.reply(f'Ta-Da! You have recieved the {high_items}. It deals {high_items_dmg} bonus damage!')
  234.                             ammount_final = gold - price
  235.                             all_users.update({'gold':ammount_final})
  236.                         if 'tuba' in comment_shop:
  237.                                 pass
  238.                     f = open("processed", "a")
  239.                     f.write(comment.id + ", ")
  240.                     break
  241.  
  242.             if '!inventory' in comment.body and comment.id not in processed:
  243.                 list3 = []
  244.                 try:
  245.                     user_inventory_ref = db.collection('users').document(comment.author.name)
  246.                     user_inventory = user_inventory_ref.collection('inventory').get()
  247.                     for document in user_inventory:
  248.                         list3.append(document.id)
  249.                     print(list3)
  250.                     comment.reply(f'You have the items: {list3}')
  251.                 except (AttributeError, KeyError):
  252.                     comment.reply('VirtuousVermin messed something up, let me go end him')
  253.             #Ending the while True:
  254.             if comment.id in processed:
  255.                 break
  256. except:
  257.     pass
  258.  
  259.  
  260.  
  261.  
  262. f = open("processed.txt", "r")
  263. processed = f.read()
  264. pfixed = processed.split(', ')
  265.  
  266.  
  267. while True:
  268.     run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement