Create requirements, config-sample, and config reader

This commit is contained in:
Flare Starfall 2025-09-19 10:36:35 +02:00
parent d3f3b608d6
commit b2efaf340e
5 changed files with 58 additions and 0 deletions

2
.gitignore vendored
View File

@ -174,3 +174,5 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/ #.idea/
# ---> StarfallBot
config.toml

7
app.py Normal file
View File

@ -0,0 +1,7 @@
from starfall.config import Config
if __name__ == "__main__":
try:
Config.load()
except FileNotFoundError:
pass

16
config-sample.toml Normal file
View File

@ -0,0 +1,16 @@
# == StarfallBot configuration file ==
[web]
# What port the web server uses.
port = 5000
# Connection database url.
# sqlite is only production safe to a point - consult the SQLAlchemy
# documentation to see how to set up other databases.
database_url = "sqlite:///starfallbot.db"
[discord]
token = "YOUR BOT TOKEN HERE"
[twitch]
oauth_token = "oauth:TWITCH OAUTH TOKEN"
channel = "TWITCH CHANNEL NAME"

4
requirements.txt Normal file
View File

@ -0,0 +1,4 @@
flask
flask-sqlalchemy
discord-py-interactions
twitchio

29
starfall/config.py Normal file
View File

@ -0,0 +1,29 @@
import os
import tomllib
from typing import Annotated, Optional
class Config:
data: Annotated[Optional[dict],
"Configuration imported from config.toml"] = None
@classmethod
def load(cls):
"""Loads the contents of the config.toml file into the class.
Raises:
FileNotFoundError: If the file does not exist.
"""
if not cls.file_exists():
raise FileNotFoundError("config.toml does not exist")
with open("config.toml", "r") as f:
cls.data = tomllib.load(f)
@classmethod
def file_exists(cls) -> bool:
"""Whether the config.toml file exists or needs to be created.
Returns:
bool: True if path exists and is a file, False otherwise.
"""
return os.path.exists("config.toml") and os.path.isfile("config.toml")