commit fed129192bf1cb053ba0380c78abaaa5f3b40d4b
parent be98ec9dd105d711dcd577f08d1436531014dba8
Author: Asher Morgan <59518073+ashermorgan@users.noreply.github.com>
Date: Sun, 31 Mar 2024 12:43:53 -0700
Move settings from settings.json to .env
Diffstat:
6 files changed, 36 insertions(+), 46 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -3,7 +3,7 @@
__pycache__
*.pyc
-settings.json
*.db
*.sqlite3
log.txt
+.env
diff --git a/README.md b/README.md
@@ -11,24 +11,23 @@ A Discord bot that facilitates countdowns and generates detailed countdown analy
2. Go to the [Discord Developer Portal](https://discord.com/developers/) and create an application and a bot
-3. Run `setup.py`
+3. Create `.env` file and add settings:
```
- python setup.py
+ TOKEN=...
+ PREFIX=!
+ DATABASE=sqlite:///data.sqlite3
+ LOG_FILE=log.txt
+ LOG_LEVEL=INFO
```
-4. Open `settings.json` (which was generated by `setup.py`) and add your bot's token
- ```json
- {"token": "YOUR_TOKEN_HERE", "prefixes": ["c."], "database": "sqlite:///data.sqlite3"}
- ```
-
-5. Run the bot
+4. Run the bot
```
python run.py
```
-6. Add the bot to your server
+5. Add the bot to your server
```
https://discordapp.com/oauth2/authorize?client_id=BOT_ID_HERE&scope=bot&permissions=101440
```
-7. Send `c.help` to the bot get a list of commands and a description of the bot's behavior
+6. Send `!help` to the bot get a list of commands and a description of the bot's behavior
diff --git a/requirements.txt b/requirements.txt
@@ -1,3 +1,4 @@
discord
matplotlib
+python-dotenv
sqlalchemy
diff --git a/run.py b/run.py
@@ -1,19 +1,27 @@
# Import dependencies
-import json
+from dotenv import load_dotenv
+import logging
import os
# Import modules
from src import CountdownBot
-
-
+from src.models import getSessionMaker
# Load settings
-settings = {}
-with open(os.path.join(os.path.dirname(__file__), "settings.json"), "a+") as f:
- f.seek(0)
- settings = json.load(f)
+load_dotenv()
+# Setup logging
+logger = logging.getLogger()
+logger.setLevel(getattr(logging, os.environ.get("LOG_LEVEL", "INFO")))
+logging.basicConfig(
+ format = "[{asctime}] [{levelname:<8}] {name}: {message}",
+ style="{",
+ filename = os.environ.get("LOG_FILE", "log.txt"),
+)
+# Connect to database
+databaseSessionMaker = getSessionMaker(os.environ.get("DATABASE"))
-# Run countdown-bot
-CountdownBot(settings).run(settings["token"])
+# Run bot
+bot = CountdownBot(databaseSessionMaker, [os.environ.get("PREFIX", "!")])
+bot.run(os.environ.get("TOKEN"))
diff --git a/setup.py b/setup.py
@@ -1,14 +0,0 @@
-# Import dependencies
-import json
-import os
-
-# Write to settings.json
-with open(os.path.join(os.path.dirname(__file__), "settings.json"), "w") as f:
- data = {
- "token": "YOUR_TOKEN_HERE",
- "prefixes": ["c."],
- "database": "sqlite:///data.sqlite3",
- "log": "log.txt",
- "log_level": "info",
- }
- json.dump(data, f)
diff --git a/src/bot.py b/src/bot.py
@@ -7,27 +7,23 @@ import logging
# Import modules
from src import analyticsCog, utilitiesCog
from src.botUtilities import addMessage, COLORS, CountdownNotFound, ContributorNotFound, CommandError, getCountdown, getPrefix
-from src.models import getSessionMaker, EmptyCountdownError
+from src.models import EmptyCountdownError
class CountdownBot(commands.Bot):
- def __init__(self, settings):
+ def __init__(self, databaseSessionMaker, prefixes):
+ # Set properties
+ self.databaseSessionMaker = databaseSessionMaker
+ self.prefixes = prefixes
+ self.logger = logging.getLogger(__name__)
+
# Get intents
intents = discord.Intents.default()
intents.message_content = True
# Initialize bot
- commands.Bot.__init__(self, command_prefix=lambda bot, ctx: getPrefix(self.databaseSessionMaker, ctx, self.prefixes), intents=intents)
-
- # Set properties
- self.databaseSessionMaker = getSessionMaker(settings["database"])
- self.prefixes = settings["prefixes"]
-
- # Initialize logger
- logging.basicConfig(format="%(asctime)s %(levelname)s %(message)s", handlers=[logging.FileHandler(settings["log"], "a+", "utf-8"), logging.StreamHandler()])
- self.logger = logging.getLogger()
- self.logger.setLevel(getattr(logging, settings["log_level"].upper()))
+ super().__init__(command_prefix=lambda bot, ctx: getPrefix(self.databaseSessionMaker, ctx, self.prefixes), intents=intents)