Fully converted to slash commands

This commit is contained in:
NikolajDanger
2021-03-31 00:38:51 +02:00
parent a8a7e5eabd
commit b345720468
50 changed files with 1102 additions and 1111 deletions

View File

@ -1,5 +1,6 @@
"""A collections of utilities used by Gwendolyn and her functions"""
__all__ = ["Options", "Credentials"]
__all__ = ["Options", "Credentials", "databaseFuncs", "getParams", "logThis", "cap", "makeFiles", "replaceMultiple", "emojiToCommand"]
from .helperClasses import Options, Credentials
from .helperClasses import Options, Credentials, databaseFuncs
from .utilFunctions import getParams, logThis, cap, makeFiles, replaceMultiple, emojiToCommand

View File

@ -1,3 +1,4 @@
import re, git, os, json
def sanitize(data : str, options : bool = False):
data = data.splitlines()
@ -19,7 +20,7 @@ def sanitize(data : str, options : bool = False):
lineValues[1] = (lineValues[1] == "true")
dct[lineValues[0]] = lineValues[1]
return dct
class Options():
@ -43,4 +44,89 @@ class Credentials():
self.mongoDBPassword = data["mongodb password"]
self.wolfKey = data["wolframalpha appid"]
self.radarrKey = data["radarr api key"]
self.sonarrKey = data["sonarr api key"]
self.sonarrKey = data["sonarr api key"]
class databaseFuncs():
def __init__(self, bot):
self.bot = bot
def getName(self, userID):
user = self.bot.database["users"].find_one({"_id":userID})
if user != None:
return user["user name"]
elif userID == "Gwendolyn":
return userID
else:
self.bot.log("Couldn't find user "+userID)
return userID
def getID(self,userName):
user = self.bot.database["users"].find_one({"user name":re.compile(userName, re.IGNORECASE)})
if user != None:
return user["_id"]
else:
self.bot.log("Couldn't find user "+userName)
return None
def deleteGame(self, gameType,channel):
self.bot.database[gameType].delete_one({"_id":channel})
def stopServer(self):
self.bot.database["trivia questions"].delete_many({})
self.bot.database["blackjack games"].delete_many({})
self.bot.database["connect 4 games"].delete_many({})
self.bot.database["hangman games"].delete_many({})
if not self.bot.options.testing:
g = git.cmd.Git("")
g.pull()
def connectFourReactionTest(self,channel,message,user):
game = self.bot.database["connect 4 games"].find_one({"_id":str(channel.id)})
with open("resources/games/oldImages/connectFour"+str(channel.id), "r") as f:
oldImage = int(f.read())
if message.id == oldImage:
self.bot.log("They reacted to the connectFour game", level = 10)
turn = game["turn"]
if user == game["players"][turn]:
return True, turn+1
else:
self.bot.log("It wasn't their turn")
return False, 0
else:
return False, 0
def hangmanReactionTest(self, channel,message):
try:
with open("resources/games/oldImages/hangman"+str(channel.id), "r") as f:
oldMessages = f.read().splitlines()
except:
return False
gameMessage = False
for oldMessage in oldMessages:
oldMessageID = int(oldMessage)
if message.id == oldMessageID:
self.bot.log("They reacted to the hangman game", level = 10)
gameMessage = True
return gameMessage
def bedreNetflixReactionTest(self, channel, message):
if os.path.isfile(f"resources/bedreNetflix/oldMessage{str(channel.id)}"):
with open("resources/bedreNetflix/oldMessage"+str(channel.id),"r") as f:
data = json.load(f)
else:
return False, None, None
if data["messageID"] == message.id:
if "imdbIds" in data:
return True, True, data["imdbIds"]
else:
return True, False, data["imdbNames"]
else:
return False, None, None

121
utils/utilFunctions.py Normal file
View File

@ -0,0 +1,121 @@
import json
import time
import logging
import os
from .helperClasses import Options
logging.basicConfig(filename="gwendolyn.log", level=logging.INFO)
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):
localtime = time.asctime(time.localtime(time.time()))
channel = channel.replace("Direct Message with ","")
if type(messages) is str:
messages = [messages]
for x, msg in enumerate(messages):
if channel == "":
messages[x] = localtime+" - "+msg
else:
messages[x] = localtime+" ("+channel+") - "+msg
if len(messages) > 1:
messages[0] += " (details in log)"
if level != 10:
print(messages[0])
for logMessage in messages:
logging.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 ""