:spakles: Database and OOP

This commit is contained in:
NikolajDanger
2020-08-13 16:31:28 +02:00
parent f431c079d1
commit 4127e537a1
31 changed files with 3674 additions and 3731 deletions

View File

@@ -1,142 +1,201 @@
import json
import string
from funcs import logThis, getName
from funcs import logThis
def getCharName(user : str):
logThis("Getting name for "+getName(user)+"'s character")
with open("resources/starWars/swcharacters.json", "r") as f:
data = json.load(f)
if user in data:
logThis("Name is "+data[user]["Name"])
return data[user]["Name"]
else:
logThis("Just using "+getName(user))
return getName(user)
class SwChar():
def __init__(self, bot):
self.bot = bot
def setUpDict(cmd : dict):
logThis("Setting up a dictionary in a nice way")
if bool(cmd):
keys = list(cmd)
values = list(cmd.values())
result = ""
if type(values[0]) is dict:
return ", ".join(values)
def getCharName(self, user : str):
logThis("Getting name for "+self.bot.funcs.getName(user)+"'s character")
userCharacter = self.bot.database["swcharacters"].find_one({"_id":user})
if user != None:
logThis("Name is "+userCharacter["Name"])
return userCharacter["Name"]
else:
for x in range(len(keys)):
if type(keys[x]) is list:
if x%3 != 2:
result += "**" + keys[x] + "**" + ": " + ", ".join(values[x]) + " "
else:
result += "**" + keys[x] + "**" + ": " + ", ".join(values[x]) + "\n"
else:
if x%3 != 2:
result += "**" + keys[x] + "**" + ": " + str(values[x]) + " "
else:
result += "**" + keys[x] + "**" + ": " + str(values[x]) + "\n"
logThis("Returning a dictionary, but well formatted")
return result
else:
logThis("Couldn't find anything")
return "There doesn't seem to be anything here..."
logThis("Just using "+self.bot.funcs.getName(user))
return self.bot.funcs.getName(user)
def lookUp(data : dict, key : str, cmd : str = ""):
if cmd == " ":
cmd = ""
elif cmd != "":
while cmd[0] == " ":
cmd = cmd[1:]
if cmd == "":
break
logThis("Looking up "+key)
if key in data:
logThis(key+" exists")
if cmd == "":
if type(data[key]) is dict and key != "Weapons":
return setUpDict(data[key])
elif key == "Weapons":
logThis("Does this even get used? I'm too scared to delete it")
if bool(data[key]):
logThis("Found "+(", ".join(list(data[key]))))
return ", ".join(list(data[key]))
else:
logThis("There is nothing here")
return "There doesn't seem to be anything here..."
def setUpDict(self, cmd : dict):
logThis("Setting up a dictionary in a nice way")
if bool(cmd):
keys = list(cmd)
values = list(cmd.values())
result = ""
if type(values[0]) is dict:
return ", ".join(values)
else:
if str(data[key]) != "":
logThis("Returning "+str(data[key]))
return data[key]
else:
logThis("There was nothing there")
return "There doesn't seem to be anything here"
elif cmd[0] == '+':
logThis("Trying to add to "+key)
try:
cmd = cmd[1:]
while cmd[0] == ' ':
cmd = cmd[1:]
except:
logThis("Yeah, that fucked up")
return "Can't do that"
for x in range(len(keys)):
if type(keys[x]) is list:
if x%3 != 2:
result += "**" + keys[x] + "**" + ": " + ", ".join(values[x]) + " "
else:
result += "**" + keys[x] + "**" + ": " + ", ".join(values[x]) + "\n"
else:
if x%3 != 2:
result += "**" + keys[x] + "**" + ": " + str(values[x]) + " "
else:
result += "**" + keys[x] + "**" + ": " + str(values[x]) + "\n"
logThis("Returning a dictionary, but well formatted")
return result
else:
logThis("Couldn't find anything")
return "There doesn't seem to be anything here..."
if type(data[key]) is int:
def lookUp(self, data : dict, key : str, cmd : str = ""):
if cmd == " ":
cmd = ""
elif cmd != "":
while cmd[0] == " ":
cmd = cmd[1:]
if cmd == "":
break
logThis("Looking up "+key)
if key in data:
logThis(key+" exists")
if cmd == "":
if type(data[key]) is dict and key != "Weapons":
return self.setUpDict(data[key])
elif key == "Weapons":
logThis("Does this even get used? I'm too scared to delete it")
if bool(data[key]):
logThis("Found "+(", ".join(list(data[key]))))
return ", ".join(list(data[key]))
else:
logThis("There is nothing here")
return "There doesn't seem to be anything here..."
else:
if str(data[key]) != "":
logThis("Returning "+str(data[key]))
return data[key]
else:
logThis("There was nothing there")
return "There doesn't seem to be anything here"
elif cmd[0] == '+':
logThis("Trying to add to "+key)
try:
newValue = data[key] + int(cmd)
data[key] = newValue
logThis("Added "+cmd+" to "+key)
return data
cmd = cmd[1:]
while cmd[0] == ' ':
cmd = cmd[1:]
except:
logThis("Couldn't add "+cmd+" to "+key)
return "Can't add that"
elif type(data[key]) is list:
logThis("Yeah, that fucked up")
return "Can't do that"
if type(data[key]) is int:
try:
data[key].append(cmd)
newValue = data[key] + int(cmd)
data[key] = newValue
logThis("Added "+cmd+" to "+key)
return data
except:
logThis("Couldn't add "+cmd+" to "+key)
return "Can't add that"
else:
logThis("Yeah, I can't add that to "+key)
return "Can't add that"
elif type(data[key]) is list:
try:
data[key].append(cmd)
logThis("Added "+cmd+" to "+key)
return data
except:
logThis("Couldn't add "+cmd+" to "+key)
return "Can't add that"
else:
logThis("Yeah, I can't add that to "+key)
return "Can't add that"
elif cmd[0] == '-':
logThis("Trying to remove/subtract from "+key)
try:
cmd = cmd[1:]
while cmd[0] == ' ':
cmd = cmd[1:]
except:
logThis("Yeah, that fucked up")
return "Can't do that"
if type(data[key]) is int:
elif cmd[0] == '-':
logThis("Trying to remove/subtract from "+key)
try:
newValue = data[key] - int(cmd)
data[key] = newValue
logThis("Subtracted "+cmd+" from "+key)
return data
cmd = cmd[1:]
while cmd[0] == ' ':
cmd = cmd[1:]
except:
logThis("Couldn't subtract "+cmd+" from "+key)
return "Can't remove that"
elif type(data[key]) is list:
logThis("Yeah, that fucked up")
return "Can't do that"
if type(data[key]) is int:
try:
data[key].remove(cmd)
logThis("Removed "+cmd+" from "+key)
newValue = data[key] - int(cmd)
data[key] = newValue
logThis("Subtracted "+cmd+" from "+key)
return data
except:
logThis("Couldn't remove "+cmd+" from "+key)
logThis("Couldn't subtract "+cmd+" from "+key)
return "Can't remove that"
elif type(data[key]) is list:
try:
data[key].remove(cmd)
logThis("Removed "+cmd+" from "+key)
return data
except:
logThis("Couldn't remove "+cmd+" from "+key)
return "Can't remove that"
else:
logThis("Yeah, I can't remove/subtract that from "+key)
return "Can't remove that"
else:
logThis("Yeah, I can't remove/subtract that from "+key)
return "Can't remove that"
else:
while cmd[0] == ' ':
cmd = cmd[1:]
while cmd[0] == ' ':
cmd = cmd[1:]
if type(data[key]) is dict:
if type(data[key]) is dict:
newKey = cmd.split(" ")[0]
cmd = cmd[len(newKey):]
if cmd != "":
while cmd[0] == " ":
cmd = cmd[1:]
if cmd == "":
break
logThis("Looking up "+newKey+" in "+key)
lookUpResult = self.lookUp(data[key],newKey,cmd)
if type(lookUpResult) is dict:
data[key] = lookUpResult
return data
else:
return lookUpResult
elif type(data[key]) != list:
logThis("Trying to change "+key+" to "+cmd)
try:
cmd = type(data[key])(cmd)
data[key] = cmd
logThis("Did that")
return data
except:
logThis("No. That did not work")
return "Wrong data type"
else:
logThis("Yeah, that didn't work")
return "Wrong data type"
else:
logThis("Couldn't find "+key)
logThis("Testing to see if it's a multi-word key")
cmd = key + " " + cmd
words = cmd.split(" ")
search = ""
i = 0
while search not in data:
try:
search += " " + words[i]
i += 1
except:
logThis("It wasn't. "+search+" doesn't exist")
return search + " doesn't exist"
if search[0] == " ":
search = search[1:]
logThis("Yeah! Did that! The key was "+search)
cmd = cmd[len(search):]
if cmd != "":
while cmd[0] == " ":
cmd = cmd[1:]
if cmd == "":
break
if cmd == "":
logThis("Returning "+search)
return self.setUpDict(data[search])
else:
newKey = cmd.split(" ")[0]
cmd = cmd[len(newKey):]
if cmd != "":
@@ -144,394 +203,321 @@ def lookUp(data : dict, key : str, cmd : str = ""):
cmd = cmd[1:]
if cmd == "":
break
logThis("Looking up "+newKey+" in "+key)
lookUpResult = lookUp(data[key],newKey,cmd)
lookUpResult = self.lookUp(data[search],newKey,cmd)
if type(lookUpResult) is dict:
data[key] = lookUpResult
data[search] = lookUpResult
return data
else:
return lookUpResult
elif type(data[key]) != list:
logThis("Trying to change "+key+" to "+cmd)
try:
cmd = type(data[key])(cmd)
data[key] = cmd
logThis("Did that")
return data
except:
logThis("No. That did not work")
return "Wrong data type"
else:
logThis("Yeah, that didn't work")
return "Wrong data type"
else:
logThis("Couldn't find "+key)
logThis("Testing to see if it's a multi-word key")
cmd = key + " " + cmd
words = cmd.split(" ")
search = ""
i = 0
while search not in data:
try:
search += " " + words[i]
i += 1
except:
logThis("It wasn't. "+search+" doesn't exist")
return search + " doesn't exist"
if search[0] == " ":
search = search[1:]
logThis("Yeah! Did that! The key was "+search)
cmd = cmd[len(search):]
if cmd != "":
def characterSheet(self,character : dict):
logThis("Setting up a character sheet for "+character["Name"])
divider = "--------------------\n"
name = character["Name"]
textf = ""
if character["Force-rating"] != 0:
textf = "\n**Force Rating**: "+str(character["Force-rating"])
text1 = "**Species**: "+character["Species"]+"\n**Career**: "+character["Career"]+"\n**Specialization Trees**: "+", ".join(character["Specialization-trees"])+textf+"\n**Soak**: "+str(character["Soak"])
text2 = "\n\n**Wounds**: "+str(character["Wounds"])+"/"+str(character["Wound-threshold"])+"\n**Strain**: "+str(character["Strain"])+"/"+str(character["Strain-threshold"])
text3 = self.setUpDict(character["Characteristics"])
text4 = self.setUpDict(character["Skills"])
text5 = ""
text6 = ""
text7 = ""
text8 = ""
if bool(character["Talents"]):
text5 = "**Talents**: "+", ".join(list(character["Talents"]))+"\n\n"
if bool(character["Force-powers"]):
text6 = "**Force Powers**: "+", ".join(list(character["Force-powers"]))+"\n\n"
text7 = "**Equipment**: "+", ".join(character["Equipment"])+"\n**Credits**: "+str(character["Credits"])+"\n**Weapons**: "+", ".join(list(character["Weapons"]))+"\n"+divider
if bool(character["Obligations"]):
text8 = "**Obligations**: "+",".join(list(character["Obligations"]))
return name, text1+text2+"\n\n"+text3+divider+text4+"\n"+divider+text5+text6+text7+text8
def charData(self,user : str,cmd : str):
userCharacter = self.bot.database["swcharacters"].find_one({"_id":user})
key = string.capwords(cmd.split(" ")[0])
cmd = cmd[len(key):]
if cmd == " ":
cmd = ""
elif cmd != "":
while cmd[0] == " ":
cmd = cmd[1:]
if cmd == "":
break
if cmd == "":
logThis("Returning "+search)
return setUpDict(data[search])
else:
newKey = cmd.split(" ")[0]
cmd = cmd[len(newKey):]
if cmd != "":
while cmd[0] == " ":
cmd = cmd[1:]
logThis("Looking for "+self.bot.funcs.getName(user)+"'s character")
if userCharacter != None:
logThis("Foundt it! Looking for "+key+" in the data")
if key in userCharacter:
logThis("Found it!")
if type(userCharacter[key]) is dict:
logThis("It's a dictionary!")
if cmd == "":
break
lookUpResult = lookUp(data[search],newKey,cmd)
if type(lookUpResult) is dict:
data[search] = lookUpResult
return data
else:
return lookUpResult
def characterSheet(character : dict):
logThis("Setting up a character sheet for "+character["Name"])
divider = "--------------------\n"
name = character["Name"]
textf = ""
if character["Force-rating"] != 0:
textf = "\n**Force Rating**: "+str(character["Force-rating"])
text1 = "**Species**: "+character["Species"]+"\n**Career**: "+character["Career"]+"\n**Specialization Trees**: "+", ".join(character["Specialization-trees"])+textf+"\n**Soak**: "+str(character["Soak"])
text2 = "\n\n**Wounds**: "+str(character["Wounds"])+"/"+str(character["Wound-threshold"])+"\n**Strain**: "+str(character["Strain"])+"/"+str(character["Strain-threshold"])
text3 = setUpDict(character["Characteristics"])
text4 = setUpDict(character["Skills"])
text5 = ""
text6 = ""
text7 = ""
text8 = ""
if bool(character["Talents"]):
text5 = "**Talents**: "+", ".join(list(character["Talents"]))+"\n\n"
if bool(character["Force-powers"]):
text6 = "**Force Powers**: "+", ".join(list(character["Force-powers"]))+"\n\n"
text7 = "**Equipment**: "+", ".join(character["Equipment"])+"\n**Credits**: "+str(character["Credits"])+"\n**Weapons**: "+", ".join(list(character["Weapons"]))+"\n"+divider
if bool(character["Obligations"]):
text8 = "**Obligations**: "+",".join(list(character["Obligations"]))
return name, text1+text2+"\n\n"+text3+divider+text4+"\n"+divider+text5+text6+text7+text8
def charData(user : str,cmd : str):
with open("resources/starWars/swcharacters.json", "r") as f:
data = json.load(f)
key = string.capwords(cmd.split(" ")[0])
cmd = cmd[len(key):]
if cmd == " ":
cmd = ""
elif cmd != "":
while cmd[0] == " ":
cmd = cmd[1:]
if cmd == "":
break
logThis("Looking for "+getName(user)+"'s character")
if user in data:
logThis("Foundt it! Looking for "+key+" in the data")
if key in data[user]:
logThis("Found it!")
if type(data[user][key]) is dict:
logThis("It's a dictionary!")
if cmd == "":
logThis("Retrieving data")
if key == "Weapons":
if bool(data[user][key]):
logThis("Returning a list of weapons")
return ", ".join(list(data[user][key]))
logThis("Retrieving data")
if key == "Weapons":
if bool(userCharacter[key]):
logThis("Returning a list of weapons")
return ", ".join(list(userCharacter[key]))
else:
logThis("The character doesn't have any weapons. Which is probably for the best. Like, who just walks around with weapons? (error code 941)")
return "There doesn't seem to be anything there... (error code 941)"
else:
logThis("The character doesn't have any weapons. Which is probably for the best. Like, who just walks around with weapons? (error code 941)")
return "There doesn't seem to be anything there... (error code 941)"
else:
return setUpDict(data[user][key])
elif cmd[0] == "+":
logThis("Gonna add something!!!")
try:
cmd = cmd[1:]
while cmd[0] == ' ':
cmd = cmd[1:]
except:
logThis("Nope. That didn't happen (error code 942)")
return "Can't do that (error code 942)"
if (key == "Talents" or key == "Force-powers") and "," in cmd:
cmd = cmd.split(",")
while cmd[1][0] == " ":
cmd[1] = cmd[1][1:]
logThis("Adding "+cmd[0]+" to "+key)
data[user][key][cmd[0]] = cmd[1]
with open("resources/starWars/swcharacters.json", "w") as f:
json.dump(data,f,indent = 4)
return cmd[0]+" added to "+key+" for " + data[user]["Name"]
elif key == "Obligations" and "," in cmd:
cmd = cmd.split(",")
while cmd[1][0] == " ":
cmd[1] = cmd[1][1:]
logThis("Adding "+cmd[0]+" to "+key)
return self.setUpDict(userCharacter[key])
elif cmd[0] == "+":
logThis("Gonna add something!!!")
try:
data[user][key][cmd[0]] = int(cmd[1])
cmd = cmd[1:]
while cmd[0] == ' ':
cmd = cmd[1:]
except:
logThis("Fucked that up (error code 949)")
return "Wrong data type (error code 949)"
with open("resources/starWars/swcharacters.json", "w") as f:
json.dump(data,f,indent = 4)
return cmd[0]+" added to "+key+" for " + data[user]["Name"]
elif key == "Weapons":
with open("resources/starWars/swtemplates.json", "r") as f:
templates = json.load(f)
newWeapon = templates["Weapon"]
logThis("Adding "+cmd+" to "+key)
data[user][key][cmd] = newWeapon
logThis("Nope. That didn't happen (error code 942)")
return "Can't do that (error code 942)"
with open("resources/starWars/swcharacters.json", "w") as f:
json.dump(data,f,indent = 4)
return cmd+" added to weapons for " + data[user]["Name"]
else:
logThis("That's not happening (error code 947d)")
return "Can't add that (error code 947d)"
if (key == "Talents" or key == "Force-powers") and "," in cmd:
cmd = cmd.split(",")
while cmd[1][0] == " ":
cmd[1] = cmd[1][1:]
logThis("Adding "+cmd[0]+" to "+key)
self.bot.database["swcharacters"].update_one({"_id":user},
{"$set": {key+"."+cmd[0] : cmd[1]}})
return cmd[0]+" added to "+key+" for " + userCharacter["Name"]
elif cmd[0] == "-":
logThis("Gonna subtract/remove something")
try:
cmd = cmd[1:]
while cmd[0] == ' ':
cmd = cmd[1:]
except:
logThis("AAAAAAAAAAAA (error code 948)")
return "Can't do that (error code 948)"
if key == "Talents" or key == "Force-powers" or key == "Weapons" or key == "Obligations":
logThis("Trying to remove "+cmd+" from "+key)
if cmd in data[user][key]:
del data[user][key][cmd]
with open("resources/starWars/swcharacters.json", "w") as f:
json.dump(data,f,indent = 4)
logThis("I did that")
return cmd+" removed from "+key+" from "+data[user]["Name"]
else:
logThis("Welp. I fucked that up (error code 946e)")
return "Can't remove that (error code 946e)"
else:
logThis("Urgh! (error code 946d)")
return "Can't remove that (error code 946d)"
else:
logThis("Looking up "+cmd+" in "+key)
if key == "Talents" or key == "Force-powers":
newKey = cmd
newcmd = ""
else:
newKey = string.capwords(cmd.split(" ")[0])
newcmd = cmd[len(newKey):]
lookUpResult = lookUp(data[user][key],newKey,newcmd)
if type(lookUpResult) is dict:
data[user][key] = lookUpResult
with open("resources/starWars/swcharacters.json", "w") as f:
json.dump(data,f,indent = 4)
return "Changed " + data[user]["Name"] + "'s " + key
else:
return lookUpResult
else:
if cmd == "":
logThis("Retrieving data")
if type(data[user][key]) is list:
return key+":\n"+", ".join(data[user][key])
else:
return data[user][key]
elif cmd[0] == '+':
logThis("Adding")
try:
cmd = cmd[1:]
while cmd[0] == ' ':
cmd = cmd[1:]
except:
logThis("Error message (error code 948)")
return "Can't do that (error code 948)"
if type(data[user][key]) is int:
try:
logThis("Adding "+cmd+" to "+key)
beforeData = data[user][key]
data[user][key] = beforeData+ int(cmd)
with open("resources/starWars/swcharacters.json", "w") as f:
json.dump(data,f,indent = 4)
return "Added " + cmd + " to " + data[user]["Name"] + "'s " + key
except:
logThis("BITCH SANDWICH (error code 947c)")
return "Can't add that (error code 947c)"
elif type(data[user][key]) is list:
try:
logThis("Adding "+cmd+" to "+key)
data[user][key].append(cmd)
with open("resources/starWars/swcharacters.json", "w") as f:
json.dump(data,f,indent = 4)
return "Added " + cmd + " to " + data[user]["Name"] + "'s " + key
except:
logThis("tstststststs (error code 947b)")
return "Can't add that (error code 947b)"
else:
logThis("Help (error code 947a)")
return "Can't add that (error code 947a)"
elif cmd[0] == '-':
logThis("Removing/subtracting")
try:
cmd = cmd[1:]
while cmd[0] == ' ':
cmd = cmd[1:]
except:
logThis("lalalala (error code 948)")
return "Can't do that (error code 948)"
if type(data[user][key]) is int:
try:
logThis("Subtracting "+cmd+" from "+key)
beforeData = data[user][key]
data[user][key] = beforeData - int(cmd)
with open("resources/starWars/swcharacters.json", "w") as f:
json.dump(data,f,indent = 4)
return "Subtracted " + cmd + " from " + data[user]["Name"] + "'s " + key
except:
logThis("Tried it. Didn't want to (error code 946c)")
return "Can't remove that (error code 946c)"
elif type(data[user][key]) is list:
try:
logThis("removing "+cmd+" from "+key)
beforeData = data[user][key]
elif key == "Obligations" and "," in cmd:
cmd = cmd.split(",")
while cmd[1][0] == " ":
cmd[1] = cmd[1][1:]
logThis("Adding "+cmd[0]+" to "+key)
try:
data[user][key].remove(cmd)
self.bot.database["swcharacters"].update_one({"_id":user},
{"$set": {key+"."+cmd[0] : int(cmd[1])}})
except:
logThis("They can only remove stuff that's actually in the list")
return "Not in list (error code 944b)"
with open("resources/starWars/swcharacters.json", "w") as f:
json.dump(data,f,indent = 4)
return "Removed " + cmd + " from " + data[user]["Name"] + "'s " + key
except:
logThis("nah (error code 946b)")
return "Can't remove that (error code 946b)"
else:
logThis("nyope (error code 946a)")
return "Can't remove that (error code 946a)"
else:
logThis("Changing "+key+" to "+cmd)
if type(data[user][key]) is int:
logThis("Fucked that up (error code 949)")
return "Wrong data type (error code 949)"
return cmd[0]+" added to "+key+" for " + userCharacter["Name"]
elif key == "Weapons":
with open("resources/starWars/swtemplates.json", "r") as f:
templates = json.load(f)
newWeapon = templates["Weapon"]
logThis("Adding "+cmd+" to "+key)
self.bot.database["swcharacters"].update_one({"_id":user},
{"$set": {key+"."+cmd : newWeapon}})
return cmd+" added to weapons for " + userCharacter["Name"]
else:
logThis("That's not happening (error code 947d)")
return "Can't add that (error code 947d)"
elif cmd[0] == "-":
logThis("Gonna subtract/remove something")
try:
data[user][key] = int(cmd)
cmd = cmd[1:]
while cmd[0] == ' ':
cmd = cmd[1:]
except:
logThis("I don't wanna tho (error code 945b)")
return "Can't do that (error code 945b)"
elif type(data[user][key]) is str:
data[user][key] = cmd
logThis("AAAAAAAAAAAA (error code 948)")
return "Can't do that (error code 948)"
if key == "Talents" or key == "Force-powers" or key == "Weapons" or key == "Obligations":
logThis("Trying to remove "+cmd+" from "+key)
if cmd in userCharacter[key]:
self.bot.database["swcharacters"].update_one({"_id":user},
{"$unset": {cmd}})
logThis("I did that")
return cmd+" removed from "+key+" from "+userCharacter["Name"]
else:
logThis("Welp. I fucked that up (error code 946e)")
return "Can't remove that (error code 946e)"
else:
logThis("Urgh! (error code 946d)")
return "Can't remove that (error code 946d)"
else:
logThis("I don't wanna tho (error code 945a)")
return "Can't do that (error code 945a)"
logThis("Looking up "+cmd+" in "+key)
if key == "Talents" or key == "Force-powers":
newKey = cmd
newcmd = ""
else:
newKey = string.capwords(cmd.split(" ")[0])
newcmd = cmd[len(newKey):]
with open("resources/starWars/swcharacters.json", "w") as f:
json.dump(data,f,indent = 4)
return "Changed " + data[user]["Name"] + "'s " + key +" to " + cmd
lookUpResult = self.lookUp(userCharacter[key],newKey,newcmd)
if type(lookUpResult) is dict:
self.bot.database["swcharacters"].update_one({"_id":user},
{"$set": {key : lookUpResult}})
return "Changed " + userCharacter["Name"] + "'s " + key
else:
return lookUpResult
else:
if cmd == "":
logThis("Retrieving data")
if type(userCharacter[key]) is list:
return key+":\n"+", ".join(userCharacter[key])
else:
return userCharacter[key]
elif cmd[0] == '+':
logThis("Adding")
try:
cmd = cmd[1:]
while cmd[0] == ' ':
cmd = cmd[1:]
except:
logThis("Error message (error code 948)")
return "Can't do that (error code 948)"
if type(userCharacter[key]) is int:
try:
logThis("Adding "+cmd+" to "+key)
self.bot.database["swcharacters"].update_one({"_id":user},
{"$inc": {key : int(cmd)}})
return "Added " + cmd + " to " + userCharacter["Name"] + "'s " + key
except:
logThis("BITCH SANDWICH (error code 947c)")
return "Can't add that (error code 947c)"
elif type(userCharacter[key]) is list:
try:
logThis("Adding "+cmd+" to "+key)
self.bot.database["swcharacters"].update_one({"_id":user},
{"$push": {key : cmd}})
return "Added " + cmd + " to " + userCharacter["Name"] + "'s " + key
except:
logThis("tstststststs (error code 947b)")
return "Can't add that (error code 947b)"
else:
logThis("Help (error code 947a)")
return "Can't add that (error code 947a)"
elif cmd[0] == '-':
logThis("Removing/subtracting")
try:
cmd = cmd[1:]
while cmd[0] == ' ':
cmd = cmd[1:]
except:
logThis("lalalala (error code 948)")
return "Can't do that (error code 948)"
if type(userCharacter[key]) is int:
try:
logThis("Subtracting "+cmd+" from "+key)
self.bot.database["swcharacters"].update_one({"_id":user},
{"$inc": {key : -int(cmd)}})
return "Subtracted " + cmd + " from " + userCharacter["Name"] + "'s " + key
except:
logThis("Tried it. Didn't want to (error code 946c)")
return "Can't remove that (error code 946c)"
elif type(userCharacter[key]) is list:
try:
logThis("removing "+cmd+" from "+key)
try:
self.bot.database["swcharacters"].update_one({"_id":user},
{"$pull": {key : cmd}})
except:
logThis("They can only remove stuff that's actually in the list")
return "Not in list (error code 944b)"
return "Removed " + cmd + " from " + userCharacter["Name"] + "'s " + key
except:
logThis("nah (error code 946b)")
return "Can't remove that (error code 946b)"
else:
logThis("nyope (error code 946a)")
return "Can't remove that (error code 946a)"
else:
logThis("Changing "+key+" to "+cmd)
if type(userCharacter[key]) is int:
try:
self.bot.database["swcharacters"].update_one({"_id":user},
{"$set": {key : int(cmd)}})
except:
logThis("I don't wanna tho (error code 945b)")
return "Can't do that (error code 945b)"
elif type(userCharacter[key]) is str:
self.bot.database["swcharacters"].update_one({"_id":user},
{"$set": {key : cmd}})
else:
logThis("I don't wanna tho (error code 945a)")
return "Can't do that (error code 945a)"
return "Changed " + userCharacter["Name"] + "'s " + key +" to " + cmd
else:
logThis(key+" isn't in there (error code 944)")
return "Couldn't find that data. Are you sure you spelled it correctly? (error code 944)"
else:
logThis(key+" isn't in there (error code 944)")
return "Couldn't find that data. Are you sure you spelled it correctly? (error code 944)"
else:
logThis(user+" doesn't have a character (error code 943)")
return "You don't have a character. You can make one with !swchar (error code 943)"
logThis(user+" doesn't have a character (error code 943)")
return "You don't have a character. You can make one with !swchar (error code 943)"
def replaceSpaces(cmd : str):
withSpaces = ["Specialization Trees","Wound Threshold","Strain Threshold","Defense - Ranged","Defense - Melee","Force Rating","Core Worlds","Outer Rim","Piloting - Planetary","Piloting - Space","Ranged - Heavy","Ranged - Light","Lightsaber Characteristic","Critical Injuries","Force Powers"]
withoutSpaces = ["Specialization-trees","Wound-threshold","Strain-threshold","Defense-ranged","Defense-melee","Force-rating","Core-worlds","Outer-rim","Piloting-planetary","Piloting-space","Ranged-heavy","Ranged-light","Lightsaber-characteristic","Critical-injuries","Force-powers"]
def replaceSpaces(self,cmd : str):
withSpaces = ["Specialization Trees","Wound Threshold","Strain Threshold","Defense - Ranged","Defense - Melee","Force Rating","Core Worlds","Outer Rim","Piloting - Planetary","Piloting - Space","Ranged - Heavy","Ranged - Light","Lightsaber Characteristic","Critical Injuries","Force Powers"]
withoutSpaces = ["Specialization-trees","Wound-threshold","Strain-threshold","Defense-ranged","Defense-melee","Force-rating","Core-worlds","Outer-rim","Piloting-planetary","Piloting-space","Ranged-heavy","Ranged-light","Lightsaber-characteristic","Critical-injuries","Force-powers"]
for x in range(len(withoutSpaces)):
cmd = cmd.replace(withSpaces[x],withoutSpaces[x])
return cmd
for x in range(len(withoutSpaces)):
cmd = cmd.replace(withSpaces[x],withoutSpaces[x])
def replaceWithSpaces(cmd : str):
withSpaces = ["Specialization Trees","Wound Threshold","Strain Threshold","Defense - Ranged","Defense - Melee","Force Rating","Core Worlds","Outer Rim","Piloting - Planetary","Piloting - Space","Ranged - Heavy","Ranged - light","Lightsaber Characteristic","Critical Injuries","Force Powers"]
withoutSpaces = ["Specialization-trees","Wound-threshold","Strain-threshold","Defense-ranged","Defense-melee","Force-rating","Core-worlds","Outer-rim","Piloting-planetary","Piloting-space","Ranged-heavy","Ranged-light","Lightsaber-characteristic","Critical-injuries","Force-powers"]
return cmd
for x in range(len(withoutSpaces)):
cmd = cmd.replace(withoutSpaces[x],withSpaces[x])
return cmd
def replaceWithSpaces(self,cmd : str):
withSpaces = ["Specialization Trees","Wound Threshold","Strain Threshold","Defense - Ranged","Defense - Melee","Force Rating","Core Worlds","Outer Rim","Piloting - Planetary","Piloting - Space","Ranged - Heavy","Ranged - light","Lightsaber Characteristic","Critical Injuries","Force Powers"]
withoutSpaces = ["Specialization-trees","Wound-threshold","Strain-threshold","Defense-ranged","Defense-melee","Force-rating","Core-worlds","Outer-rim","Piloting-planetary","Piloting-space","Ranged-heavy","Ranged-light","Lightsaber-characteristic","Critical-injuries","Force-powers"]
def parseChar(user : str, cmd : str):
for x in range(len(withoutSpaces)):
cmd = cmd.replace(withoutSpaces[x],withSpaces[x])
cmd = replaceSpaces(cmd)
return cmd
with open("resources/starWars/swcharacters.json", "r") as f:
data = json.load(f)
def parseChar(self,user : str, cmd : str):
if cmd == " ":
cmd = ""
elif cmd != "":
while cmd[0] == " ":
cmd = cmd[1:]
if cmd == "":
break
cmd = self.replaceSpaces(cmd)
if cmd == "":
if user in data:
text1, text2 = characterSheet(data[user])
return text1, replaceWithSpaces(text2)
userCharacter = self.bot.database["swcharacters"].find_one({"_id":user})
if cmd == " ":
cmd = ""
elif cmd != "":
while cmd[0] == " ":
cmd = cmd[1:]
if cmd == "":
break
if cmd == "":
if userCharacter != None:
text1, text2 = self.characterSheet(userCharacter)
return text1, self.replaceWithSpaces(text2)
else:
logThis("Makin' a character for "+self.bot.funcs.getName(user))
with open("resources/starWars/swtemplates.json", "r") as f:
templates = json.load(f)
newChar = templates["Character"]
print("yeet")
newChar["_id"] = user
print("yeet")
self.bot.database["swcharacters"].insert_one(newChar)
print("yeet")
return "", "Character for " + self.bot.funcs.getName(user) + " created"
else:
logThis("Makin' a character for "+getName(user))
with open("resources/starWars/swtemplates.json", "r") as f:
templates = json.load(f)
newChar = templates["Character"]
data[user] = newChar
with open("resources/starWars/swcharacters.json", "w") as f:
json.dump(data,f,indent = 4)
return "", "Character for " + getName(user) + " created"
else:
if cmd == "Purge":
logThis("Deleting "+getName(user)+"'s character")
del data[user]
with open("resources/starWars/swcharacters.json", "w") as f:
json.dump(data,f,indent = 4)
return "", "Character for " + getName(user) + " deleted"
else:
return "", replaceWithSpaces(str(charData(user,cmd)))
if cmd == "Purge":
logThis("Deleting "+self.bot.funcs.getName(user)+"'s character")
self.bot.database["swcharacters"].remove_one({newChar})
return "", "Character for " + self.bot.funcs.getName(user) + " deleted"
else:
return "", self.replaceWithSpaces(str(self.charData(user,cmd)))
def lightsaberChar(user : str):
with open("resources/starWars/swcharacters.json", "r") as f:
data = json.load(f)
def lightsaberChar(self,user : str):
userCharacter = self.bot.database["swcharacters"].find_one({"_id":user})
if user in data:
return data[user]["Lightsaber-characteristic"]
if userCharacter != None:
return userCharacter["Lightsaber-characteristic"]
def userHasChar(user : str):
with open("resources/starWars/swcharacters.json", "r") as f:
data = json.load(f)
def userHasChar(self,user : str):
userCharacter = self.bot.database["swcharacters"].find_one({"_id":user})
return user in data
return userCharacter != None