mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 03:08:55 -05:00
118 lines
5.0 KiB
Python
118 lines
5.0 KiB
Python
import json
|
|
import logging
|
|
import os
|
|
import glob
|
|
|
|
default_settings = ('{"TRIVIA_ADMIN_ONLY": false, "EDIT_CC_ADMIN_ONLY": false, "PASSWORD": "PASSWORDHERE", "FILTER": true, "CUSTOMCOMMANDS": true, ' +
|
|
'"TRIVIA_MAX_SCORE": 10, "TRIVIA_DELAY": 15, "LOGGING": true, "EMAIL": "EMAILHERE", "ADMINROLE": "Transistor", "DOWNLOADMODE" : true, ' +
|
|
'"VOLUME": 0.20, "TRIVIA_BOT_PLAYS" : false, "TRIVIA_TIMEOUT" : 120, "DEBUG_ID" : "IgnoreThis", "POLL_DURATION" : 60}')
|
|
default_apis = ('{"IMGFLIP_USERNAME": "USERNAMEHERE", "IMGFLIP_PASSWORD": "PASSWORDHERE"}')
|
|
logger = logging.getLogger("__main__")
|
|
|
|
|
|
def fileIO(filename, IO, data=None):
|
|
if IO == "save" and data != None:
|
|
with open(filename, encoding='utf-8', mode="w") as f:
|
|
f.write(json.dumps(data))
|
|
elif IO == "load" and data == None:
|
|
with open(filename, encoding='utf-8', mode="r") as f:
|
|
return json.loads(f.read())
|
|
elif IO == "check" and data == None:
|
|
try:
|
|
with open(filename, encoding='utf-8', mode="r") as f:
|
|
return True
|
|
except:
|
|
return False
|
|
else:
|
|
logger.info("Invalid fileIO call")
|
|
|
|
def loadProverbs():
|
|
with open("proverbs.txt", encoding='utf-8', mode="r") as f:
|
|
data = f.readlines()
|
|
return data
|
|
|
|
def loadAndCheckSettings():
|
|
to_delete = []
|
|
try:
|
|
current_settings = fileIO("json/settings.json", "load")
|
|
default = json.loads(default_settings)
|
|
if current_settings.keys() != default.keys():
|
|
logger.warning("Something wrong detected with settings.json. Starting check...")
|
|
for field in default:
|
|
if field not in current_settings:
|
|
logger.info("Adding " + field + " field.")
|
|
current_settings[field] = default[field]
|
|
for field in current_settings:
|
|
if field not in default:
|
|
logger.info("Removing " + field + " field.")
|
|
to_delete.append(field)
|
|
for field in to_delete:
|
|
del current_settings[field]
|
|
logger.warning("Your settings.json was deprecated (missing or useless fields detected). I fixed it. " +
|
|
"If the file was missing any field I've added it and put default values. You might want to check it.")
|
|
fileIO("json/settings.json", "save", current_settings)
|
|
return current_settings
|
|
except IOError:
|
|
fileIO("json/settings.json", "save", json.loads(default_settings))
|
|
logger.error("Your settings.json is missing. I've created a new one. Edit it with your settings and restart me.")
|
|
exit(1)
|
|
except:
|
|
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.")
|
|
exit(1)
|
|
|
|
def migration():
|
|
if not os.path.exists("json/"):
|
|
os.makedirs("json")
|
|
logger.info("Creating json folder...")
|
|
|
|
if not os.path.exists("cache/"): #Stores youtube audio for DOWNLOADMODE
|
|
os.makedirs("cache")
|
|
|
|
if not os.path.exists("trivia/"):
|
|
os.makedirs("trivia")
|
|
|
|
files = glob.glob("*.json")
|
|
if files != []:
|
|
logger.info("Moving your json files into the json folder...")
|
|
for f in files:
|
|
logger.info("Moving {}...".format(f))
|
|
os.rename(f, "json/" + f)
|
|
|
|
def createEmptyFiles():
|
|
files = {"twitch.json": [], "commands.json": {}, "economy.json" : {}, "filter.json" : {}, "regex_filter.json" : {}, "shushlist.json" : [], "blacklist.json" : []}
|
|
games = ["Multi Theft Auto", "her Turn()", "Tomb Raider II", "some music.", "NEO Scavenger", "Python", "World Domination", "with your heart."]
|
|
files["games.json"] = games
|
|
for f, data in files.items() :
|
|
if not os.path.isfile("json/" + f):
|
|
logger.info("Missing {}. Creating it...".format(f))
|
|
fileIO("json/" + f, "save", data)
|
|
if not os.path.isfile("json/settings.json"):
|
|
logger.info("Missing settings.json. Creating it...\n")
|
|
fileIO("json/settings.json", "save", json.loads(default_settings))
|
|
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:")
|
|
email = input(">")
|
|
print("Now enter the password.")
|
|
password = input(">")
|
|
print("Admin role? Leave empty for default (Transistor)")
|
|
admin_role = input(">")
|
|
if admin_role == "":
|
|
admin_role = "Transistor"
|
|
new_settings = json.loads(default_settings)
|
|
new_settings["EMAIL"] = email
|
|
new_settings["PASSWORD"] = password
|
|
new_settings["ADMINROLE"] = admin_role
|
|
fileIO("json/settings.json", "save", new_settings )
|
|
logger.info("Settings have been saved.")
|
|
if not os.path.isfile("json/apis.json"):
|
|
logger.info("Missing apis.json. Creating it...\n")
|
|
fileIO("json/apis.json", "save", json.loads(default_apis))
|
|
print("You can configure your apis settings to add extra features to your bot e.g. creating memes.\n If you don't have an account, you can create one on https://imgflip.com/.\n If you'd like to do it manually or do it later, close this window.\n\nimgflip username:")
|
|
imgflip_username = input(">")
|
|
print("Now enter the imgflip password.")
|
|
imgflip_password = input(">")
|
|
new_settings = json.loads(default_apis)
|
|
new_settings["IMGFLIP_USERNAME"] = imgflip_username
|
|
new_settings["IMGFLIP_PASSWORD"] = imgflip_password
|
|
fileIO("json/apis.json", "save", new_settings )
|
|
logger.info("API Settings have been saved.\n")
|