diff --git a/Gwendolyn.py b/Gwendolyn.py index cecdf1e..a0e066f 100644 --- a/Gwendolyn.py +++ b/Gwendolyn.py @@ -1,8 +1,8 @@ -import discord, os, finnhub +import discord, os, finnhub, pymongo from discord.ext import commands - -from funcs import logThis, makeFiles +from pymongo import MongoClient +from funcs import logThis, makeFiles, transferUsers commandPrefix = "!" @@ -14,12 +14,24 @@ class Credentials(): self.token = data[0][10:].replace(" ","") self.finnhubKey = data[1][16:].replace(" ","") self.wordnikKey = data[2][16:].replace(" ","") + self.mongoDBUser = data[3][13:].replace(" ","") + self.mongoDBPassword = data[4][17:].replace(" ","") +class Options(): + def __init__(self): + with open("options.txt","r") as f: + data = f.read().splitlines() + + self.prefix = data[0][7:].replace(" ","") + self.testing = bool(data[1][8:].replace(" ","")) class Gwendolyn(commands.Bot): def __init__(self): + self.options = Options() self.credentials = Credentials() self.finnhubClient = finnhub.Client(api_key = self.credentials.finnhubKey) + self.MongoClient = MongoClient(f"mongodb+srv://{self.credentials.mongoDBUser}:{self.credentials.mongoDBPassword}@gwendolyn.qkwfy.mongodb.net/Gwendolyn-Test?retryWrites=true&w=majority") + self.database = self.MongoClient["Gwendolyn-Test"] super().__init__(command_prefix=commandPrefix, case_insensitive=True) # Creates the required files @@ -28,6 +40,9 @@ makeFiles() # Creates the Bot client = Gwendolyn() +# Creates database collections +transferUsers(client.database) + # Logs in @client.event async def on_ready(): diff --git a/funcs/__init__.py b/funcs/__init__.py index 0185f1b..3f67e88 100644 --- a/funcs/__init__.py +++ b/funcs/__init__.py @@ -1,8 +1,8 @@ """A collection of all Gwendolyn functions.""" -__all__ = ["helloFunc", "cap", "imageFunc", "logThis", "findWikiPage", "makeFiles", "emojiToCommand", "fiarReactionTest", "deleteGame", "stopServer", "checkBalance", "giveMoney", "addMoney", "triviaCountPoints", "triviaStart", "triviaAnswer", "spellFunc", "monsterFunc", "nameGen", "tavernGen", "movieFunc", "roll_dice", "parseChar", "parseRoll", "critRoll", "parseDestiny", "addToDict", "getName", "getID", "replaceMultiple", "monopolyReactionTest","parseInvest", "fiar", "runMonopoly", "runHex", "runHangman","hangmanReactionTest", "parseBlackjack"] +__all__ = ["helloFunc", "cap", "imageFunc", "logThis", "findWikiPage", "makeFiles", "emojiToCommand", "fiarReactionTest", "deleteGame", "stopServer", "checkBalance", "giveMoney", "addMoney", "triviaCountPoints", "triviaStart", "triviaAnswer", "spellFunc", "monsterFunc", "nameGen", "tavernGen", "movieFunc", "roll_dice", "parseChar", "parseRoll", "critRoll", "parseDestiny", "addToDict", "getName", "getID", "replaceMultiple", "monopolyReactionTest","parseInvest", "fiar", "runMonopoly", "runHex", "runHangman","hangmanReactionTest", "parseBlackjack","transferUsers"] -from .miscFuncs import helloFunc, cap, imageFunc, logThis, findWikiPage, makeFiles, replaceMultiple, emojiToCommand, fiarReactionTest, deleteGame, stopServer, addToDict, getName, getID, monopolyReactionTest, hangmanReactionTest +from .miscFuncs import helloFunc, cap, imageFunc, logThis, findWikiPage, makeFiles, replaceMultiple, emojiToCommand, fiarReactionTest, deleteGame, stopServer, addToDict, getName, getID, monopolyReactionTest, hangmanReactionTest, transferUsers from .games import checkBalance, giveMoney, addMoney, triviaCountPoints, triviaStart, triviaAnswer, fiar, runMonopoly, runHex, runHangman, parseBlackjack, parseInvest diff --git a/funcs/miscFuncs.py b/funcs/miscFuncs.py index da1d3d2..af8128f 100644 --- a/funcs/miscFuncs.py +++ b/funcs/miscFuncs.py @@ -8,6 +8,7 @@ import logging # Used for... you know... logging import wikia # Used by findWikiPage import os # Used by makeFiles import git # Used by stopServer() +import pymongo # Used by transferUsers logging.basicConfig(filename="gwendolyn.log", level=logging.INFO) @@ -342,3 +343,18 @@ def getID(userName): return userID except: logThis("Error getting ID") + +def transferUsers(database): + collist = database.list_collection_names() + if "users" not in collist and os.path.exists("resources/users.json"): + logThis("Transfering users") + with open("resources/users.json", "r") as f: + data = json.load(f) + + userList = [] + + for key, value in list(data.items()): + user = {"_id":int(key[1:]),"user name":value["user name"],"money":value["money"]} + userList.append(user) + + database["users"].insert_many(userList) diff --git a/resources/startingFiles.json b/resources/startingFiles.json index 1c2ccd6..1badf49 100644 --- a/resources/startingFiles.json +++ b/resources/startingFiles.json @@ -4,7 +4,6 @@ "resources/games/hexGames.json": {}, "resources/games/monopolyGames.json": {}, "resources/games/hangmanGames.json": {}, - "resources/users.json" : {}, "resources/games/investments.json" : {}, "resources/games/games.json" : { "trivia questions":{}, @@ -70,7 +69,8 @@ "resources/starWars/destinyPoints.txt": "", "resources/movies.txt": "The Room", "resources/names.txt": "Gandalf", - "credentials.txt" : "Bot token: TOKEN\nFinnhub API key: KEY\nWordnik API Key: KEY" + "credentials.txt" : "Bot token: TOKEN\nFinnhub API key: KEY\nWordnik API Key: KEY\nMongoDB user: USERNAME\nMongoDB password: PASSWORD", + "options.txt" : "Prefix: !\nTesting: True" }, "folder" : [ "resources/games/blackjackTables",