From 91e84dddf65a2c6f657bb8883cc6efaec764cae7 Mon Sep 17 00:00:00 2001 From: Alessandro Bellia Date: Fri, 13 Jan 2023 16:28:55 +0100 Subject: [PATCH] Added the possibility to configure SQLITE db path Now, the SQLITE db path for the user API keys can be configured through the USER_KEY_DB_PATH env variable Signed-off-by: Alessandro Bellia --- README.md | 6 +++++- cogs/gpt_3_commands_and_converser.py | 13 ++++++++++++- models/env_service_model.py | 13 +++++++++++++ sample.env | 5 ++++- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cd9c260..8e7b0de 100644 --- a/README.md +++ b/README.md @@ -178,7 +178,9 @@ USER_INPUT_API_KEYS="True" Then, restart the bot, and it will set up the system for everyone to input their own API keys. -The bot will use SQLite to store API keys for the users, each user's key will be saved with a USER_ID <> API_KEY mapping in SQLite, and will be persistent across restarts. All the data will be saved in a file called `user_key_db.sqlite` in the current working directory of the bot. +The bot will use SQLite to store API keys for the users, each user's key will be saved with a USER_ID <> API_KEY mapping in SQLite, and will be persistent across restarts. All the data will be saved in a file called `user_key_db.sqlite` in the current working directory of the bot (by default). + +You can configure the location of the sqlite database by changing the `USER_KEY_DB_PATH` variable in your `.env` file (check the `sample.env` file for reference). With this feature enabled, any attempt to use a GPT3 or DALL-E command without a valid API key set for the user will pop up the following modal for them to enter their API key: @@ -239,6 +241,8 @@ GPT_ROLES="openai,gpt" WELCOME_MESSAGE="Hi There! Welcome to our Discord server. We hope you'll enjoy our server and we look forward to engaging with you!" # This is a fallback message if gpt3 fails to generate a welcome message. # This is the channel that auto-moderation alerts will be sent to MODERATIONS_ALERT_CHANNEL="977697652147892304" +# User API key db path configuration. This is where the user API keys will be stored. +USER_KEY_DB_PATH = user_key_db.sqlite ``` **Permissions** diff --git a/cogs/gpt_3_commands_and_converser.py b/cogs/gpt_3_commands_and_converser.py index 7a81d40..3572ef8 100644 --- a/cogs/gpt_3_commands_and_converser.py +++ b/cogs/gpt_3_commands_and_converser.py @@ -41,7 +41,18 @@ if USER_INPUT_API_KEYS: print( "This server was configured to enforce user input API keys. Doing the required database setup now" ) - USER_KEY_DB = SqliteDict("user_key_db.sqlite") + # Get USER_KEY_DB from the environment variable + USER_KEY_DB_PATH = EnvService.get_user_key_db_path() + # Check if USER_KEY_DB_PATH is valid + if not USER_KEY_DB_PATH: + print("No user key database path was provided. Defaulting to user_key_db.sqlite") + USER_KEY_DB_PATH = "user_key_db.sqlite" + else: + # append "user_key_db.sqlite" to USER_KEY_DB_PATH if it doesn't already end with .sqlite + if not USER_KEY_DB_PATH.match("*.sqlite"): + # append "user_key_db.sqlite" to USER_KEY_DB_PATH + USER_KEY_DB_PATH = USER_KEY_DB_PATH / "user_key_db.sqlite" + USER_KEY_DB = SqliteDict(USER_KEY_DB_PATH) print("Retrieved/created the user key database") diff --git a/models/env_service_model.py b/models/env_service_model.py index 80caddc..c6f10e7 100644 --- a/models/env_service_model.py +++ b/models/env_service_model.py @@ -1,6 +1,8 @@ import os import sys from pathlib import Path +from typing import Union + from dotenv import load_dotenv @@ -193,3 +195,14 @@ class EnvService: return False except: return False + + @staticmethod + def get_user_key_db_path() -> Union[Path, None]: + try: + user_key_db_path = os.getenv("USER_KEY_DB_PATH") + if user_key_db_path is None: + return None + else: + return Path(user_key_db_path) + except: + return None diff --git a/sample.env b/sample.env index 3bd1ed1..9648ca5 100644 --- a/sample.env +++ b/sample.env @@ -18,4 +18,7 @@ WELCOME_MESSAGE = "Hi There! Welcome to our Discord server. We hope you'll enjoy USER_INPUT_API_KEYS="False" # If True, users must use their own API keys for OpenAI. If False, the bot will use the API key in the .env file. # Moderations Service alert channel, this is where moderation alerts will be sent as a default if enabled -MODERATIONS_ALERT_CHANNEL = "977697652147892304" \ No newline at end of file +MODERATIONS_ALERT_CHANNEL = "977697652147892304" + +# User API key db path configuration. This is where the user API keys will be stored. +USER_KEY_DB_PATH = user_key_db.sqlite \ No newline at end of file