Files
Gwendolyn/utils/helperClasses.py
2021-06-14 18:28:07 +02:00

150 lines
5.2 KiB
Python

import re, git, os, json, time
def sanitize(data : str, options : bool = False):
data = data.splitlines()
dct = {}
for line in data:
if line[0] != "#" and ":" in line:
lineValues = line.split(":")
lineValues[0] = lineValues[0].lower()
lineValues[1] = lineValues[1].replace(" ", "")
if options:
lineValues[1] = lineValues[1].lower()
if lineValues[0] in ["testing guild ids", "admins"]:
lineValues[1] = lineValues[1].split(",")
if all(i.isnumeric() for i in lineValues[1]):
lineValues[1] = [int(i) for i in lineValues[1]]
if any(i == lineValues[1] for i in ["true", "false"]):
lineValues[1] = (lineValues[1] == "true")
dct[lineValues[0]] = lineValues[1]
return dct
class Options():
def __init__(self):
with open("options.txt","r") as f:
data = sanitize(f.read(), True)
self.testing = data["testing"]
self.guildIds = data["testing guild ids"]
self.admins = data["admins"]
class Credentials():
def __init__(self):
with open("credentials.txt","r") as f:
data = sanitize(f.read())
self.token = data["bot token"]
self.finnhubKey = data["finnhub api key"]
self.wordnikKey = data["wordnik api key"]
self.mongoDBUser = data["mongodb user"]
self.mongoDBPassword = data["mongodb password"]
self.wolfKey = data["wolframalpha appid"]
self.radarrKey = data["radarr 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 userID == f"#{self.bot.user.id}":
return "Gwendolyn"
elif user != None:
return user["user name"]
else:
self.bot.log(f"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({})
self.bot.database["hex 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")
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, user):
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:
game = self.bot.database["hangman games"].find_one({"_id":str(channel.id)})
if user == game["player"]:
gameMessage = True
break
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
async def syncCommands(self):
collection = self.bot.database["last synced"]
lastSynced = collection.find_one()
now = time.time()
if lastSynced["last synced"] < now - 86400:
slashCommandList = await self.bot.slash.to_dict()
self.bot.log(f"Updating commands: {slashCommandList}")
await self.bot.slash.sync_all_commands()
idNumber = lastSynced["_id"]
queryFilter = {"_id" : idNumber}
update = {"$set" : {"last synced" : now}}
collection.update_one(queryFilter, update)