132 lines
3.6 KiB
Python
132 lines
3.6 KiB
Python
import json
|
||
import logging
|
||
import os
|
||
import sys
|
||
from .helperClasses import Options
|
||
|
||
FORMAT = " %(asctime)s | %(name)-16s | %(levelname)-8s | %(message)s"
|
||
PRINTFORMAT = "%(asctime)s - %(message)s"
|
||
DATEFORMAT = "%Y-%m-%d %H:%M:%S"
|
||
|
||
logging.addLevelName(25, "PRINT")
|
||
logging.basicConfig(format=FORMAT, datefmt=DATEFORMAT, level=logging.INFO, filename="gwendolyn.log")
|
||
logger = logging.getLogger("Gwendolyn")
|
||
printer = logging.getLogger("printer")
|
||
handler = logging.StreamHandler(sys.stdout)
|
||
handler.setFormatter(logging.Formatter(fmt = PRINTFORMAT, datefmt=DATEFORMAT))
|
||
printer.addHandler(handler)
|
||
printer.propagate = False
|
||
|
||
def getParams():
|
||
with open("resources/slashParameters.json", "r") as f:
|
||
params = json.load(f)
|
||
|
||
options = Options()
|
||
|
||
if options.testing:
|
||
for p in params:
|
||
params[p]["guild_ids"] = options.guildIds
|
||
|
||
return params
|
||
|
||
def logThis(messages, channel : str = "", level : int = 20):
|
||
channel = channel.replace("Direct Message with ","")
|
||
if type(messages) is str:
|
||
messages = [messages]
|
||
|
||
printMessage = messages[0]
|
||
|
||
for x, msg in enumerate(messages):
|
||
if channel != "":
|
||
messages[x] = f"{msg} - ({channel})"
|
||
|
||
if len(messages) > 1:
|
||
printMessage += " (details in log)"
|
||
|
||
if level >= 25:
|
||
printer.log(level, printMessage)
|
||
|
||
for logMessage in messages:
|
||
logger.log(level, logMessage)
|
||
|
||
# Capitalizes all words except some of them
|
||
def cap(s):
|
||
no_caps_list = ["of","the"]
|
||
# Capitalizes a strink like a movie title
|
||
word_number = 0
|
||
lst = s.split()
|
||
res = ''
|
||
for word in lst:
|
||
word_number += 1
|
||
if word not in no_caps_list or word_number == 1:
|
||
word = word.capitalize()
|
||
res += word+" "
|
||
res = res[:-1]
|
||
return res
|
||
|
||
def makeFiles():
|
||
def makeJsonFile(path,content):
|
||
# Creates json file if it doesn't exist
|
||
if not os.path.isfile(path):
|
||
logThis(path.split("/")[-1]+" didn't exist. Making it now.")
|
||
with open(path,"w") as f:
|
||
json.dump(content,f,indent = 4)
|
||
|
||
def makeTxtFile(path,content):
|
||
# Creates txt file if it doesn't exist
|
||
if not os.path.isfile(path):
|
||
logThis(path.split("/")[-1]+" didn't exist. Making it now.")
|
||
with open(path,"w") as f:
|
||
f.write(content)
|
||
|
||
def directory(path):
|
||
if not os.path.isdir(path):
|
||
os.makedirs(path)
|
||
logThis("The "+path.split("/")[-1]+" directory didn't exist")
|
||
|
||
with open("resources/startingFiles.json") as f:
|
||
data = json.load(f)
|
||
|
||
for path, content in data["json"].items():
|
||
makeJsonFile(path,content)
|
||
|
||
for path, content in data["txt"].items():
|
||
makeTxtFile(path,content)
|
||
|
||
for path in data["folder"]:
|
||
directory(path)
|
||
|
||
# Replaces multiple things with the same thing
|
||
def replaceMultiple(mainString, toBeReplaces, newString):
|
||
# Iterate over the strings to be replaced
|
||
for elem in toBeReplaces :
|
||
# Check if string is in the main string
|
||
if elem in mainString :
|
||
# Replace the string
|
||
mainString = mainString.replace(elem, newString)
|
||
|
||
return mainString
|
||
|
||
def emojiToCommand(emoji):
|
||
if emoji == "1️⃣":
|
||
return 1
|
||
elif emoji == "2️⃣":
|
||
return 2
|
||
elif emoji == "3️⃣":
|
||
return 3
|
||
elif emoji == "4️⃣":
|
||
return 4
|
||
elif emoji == "5️⃣":
|
||
return 5
|
||
elif emoji == "6️⃣":
|
||
return 6
|
||
elif emoji == "7️⃣":
|
||
return 7
|
||
elif emoji == "🎲":
|
||
return "roll"
|
||
elif emoji == "❌":
|
||
return "none"
|
||
elif emoji == "✔️":
|
||
return 1
|
||
else: return ""
|