Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.75 KB | None | 0 0
  1. import json
  2. import logging
  3. import os
  4. import glob
  5.  
  6. default_settings = ('{"TRIVIA_ADMIN_ONLY": false, "EDIT_CC_ADMIN_ONLY": false, "PASSWORD": "PASSWORDHERE", "FILTER": true, "CUSTOMCOMMANDS": true, ' +
  7.                     '"TRIVIA_MAX_SCORE": 10, "TRIVIA_DELAY": 15, "LOGGING": true, "EMAIL": "EMAILHERE", "ADMINROLE": "Transistor", "DOWNLOADMODE" : true, ' +
  8.                     '"VOLUME": 0.20, "TRIVIA_BOT_PLAYS" : false, "TRIVIA_TIMEOUT" : 120, "DEBUG_ID" : "IgnoreThis", "POLL_DURATION" : 60, "PREFIX" : "!"}')
  9.                    
  10. default_apis = ('{"IMGFLIP_USERNAME": "USERNAMEHERE", "IMGFLIP_PASSWORD": "PASSWORDHERE", "MYAPIFILMS_TOKEN" : "TOKENHERE"}')
  11.  
  12. logger = logging.getLogger("__main__")
  13.  
  14.  
  15. def fileIO(filename, IO, data=None):
  16.     if IO == "save" and data != None:
  17.         with open(filename, encoding='utf-8', mode="w") as f:
  18.             f.write(json.dumps(data))
  19.     elif IO == "load" and data == None:
  20.         with open(filename, encoding='utf-8', mode="r") as f:
  21.             return json.loads(f.read())
  22.     elif IO == "check" and data == None:
  23.         try:
  24.             with open(filename, encoding='utf-8', mode="r") as f:
  25.                 return True
  26.         except:
  27.             return False
  28.     else:
  29.         logger.info("Invalid fileIO call")
  30.  
  31. def loadProverbs():
  32.     with open("proverbs.txt", encoding='utf-8', mode="r") as f:
  33.         data = f.readlines()
  34.     return data
  35.  
  36. def loadAndCheckSettings():
  37.     to_delete = []
  38.     try:
  39.         current_settings = fileIO("json/settings.json", "load")
  40.         default = json.loads(default_settings)
  41.         if current_settings.keys() != default.keys():
  42.             logger.warning("Something wrong detected with settings.json. Starting check...")
  43.             for field in default:
  44.                 if field not in current_settings:
  45.                     logger.info("Adding " + field + " field.")
  46.                     current_settings[field] = default[field]
  47.             for field in current_settings:
  48.                 if field not in default:
  49.                     logger.info("Removing " + field + " field.")
  50.                     to_delete.append(field)
  51.             for field in to_delete:
  52.                 del current_settings[field]
  53.             logger.warning("Your settings.json was deprecated (missing or useless fields detected). I fixed it. " +
  54.                            "If the file was missing any field I've added it and put default values. You might want to check it.")
  55.         fileIO("json/settings.json", "save", current_settings)
  56.         return current_settings
  57.     except IOError:
  58.         fileIO("json/settings.json", "save", json.loads(default_settings))
  59.         logger.error("Your settings.json is missing. I've created a new one. Edit it with your settings and restart me.")
  60.         exit(1)
  61.     except:
  62.         logger.error("Your settings.json seems to be invalid. Check it. If you're unable to fix it delete it and I'll create a new one the next start.")
  63.         exit(1)
  64.  
  65. def migration():
  66.     if not os.path.exists("json/"):
  67.         os.makedirs("json")
  68.         logger.info("Creating json folder...")
  69.  
  70.     if not os.path.exists("cache/"): #Stores youtube audio for DOWNLOADMODE
  71.         os.makedirs("cache")
  72.  
  73.     if not os.path.exists("trivia/"):
  74.         os.makedirs("trivia")
  75.    
  76.     files = glob.glob("*.json")
  77.     if files != []:
  78.         logger.info("Moving your json files into the json folder...")
  79.         for f in files:
  80.             logger.info("Moving {}...".format(f))
  81.             os.rename(f, "json/" + f)
  82.  
  83. def createEmptyFiles():
  84.     files = {"twitch.json": [], "commands.json": {}, "economy.json" : {}, "filter.json" : {}, "regex_filter.json" : {}, "shushlist.json" : [], "blacklist.json" : []}
  85.     games = ["Multi Theft Auto", "her Turn()", "Tomb Raider II", "some music.", "NEO Scavenger", "Python", "World Domination", "with your heart."]
  86.     files["games.json"] = games
  87.     for f, data in files.items() :
  88.         if not os.path.isfile("json/" + f):
  89.             logger.info("Missing {}. Creating it...".format(f))
  90.             fileIO("json/" + f, "save", data)
  91.     if not os.path.isfile("json/settings.json"):
  92.         logger.info("Missing settings.json. Creating it...\n")
  93.         fileIO("json/settings.json", "save", json.loads(default_settings))
  94.         print("You have to configure your settings. If you'd like to do it manually, close this window.\nOtherwise type your bot's account email. DO NOT use your own account for the bot, make a new one.\n\nEmail:")
  95.         email = input(">")
  96.         print("Now enter the password.")
  97.         password = input(">")
  98.         print("Admin role? Leave empty for default (Transistor)")
  99.         admin_role = input(">")
  100.         if admin_role == "":
  101.             admin_role = "Transistor"
  102.         print("Command prefix? Leave empty for default, '!'. Maximum 1 character.")
  103.         prefix = input(">")
  104.         if len(prefix) != 1 or prefix == " ":
  105.             print("Invalid prefix. Setting prefix as '!'...")
  106.             prefix = "!"
  107.         new_settings = json.loads(default_settings)
  108.         new_settings["EMAIL"] = email
  109.         new_settings["PASSWORD"] = password
  110.         new_settings["ADMINROLE"] = admin_role
  111.         new_settings["PREFIX"] = prefix
  112.         fileIO("json/settings.json", "save", new_settings )
  113.         logger.info("Settings have been saved.")
  114.  
  115.     if not os.path.isfile("json/apis.json"):
  116.         logger.info("Missing apis.json. Creating it...\n")
  117.         fileIO("json/apis.json", "save", json.loads(default_apis))
  118.         print("\nIt's now time to configure optional services\nIf you're not interested, leave empty and keep pressing enter.\nMemes feature: create an account on https://imgflip.com/.\nimgflip username:")
  119.         imgflip_username = input(">")
  120.         print("Now enter the imgflip password.")
  121.         imgflip_password = input(">")
  122.         if imgflip_username == "": imgflip_username = "USERNAMEHERE"
  123.         if imgflip_password == "": imgflip_password = "PASSWORDHERE"
  124.         print("\n!imdb configuration. Get your token here http://www.myapifilms.com/token.do\nOr just press enter if you're not interested.")
  125.         imdb_token = input(">")
  126.         if imdb_token == "": imdb_token = "TOKENHERE"
  127.         new_settings = json.loads(default_apis)
  128.         new_settings["IMGFLIP_USERNAME"] = imgflip_username
  129.         new_settings["IMGFLIP_PASSWORD"] = imgflip_password
  130.         new_settings["MYAPIFILMS_TOKEN"] = imdb_token
  131.         fileIO("json/apis.json", "save", new_settings )
  132.         logger.info("API Settings have been saved.\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement