goober/modules/settings.py

59 lines
1.5 KiB
Python
Raw Permalink Normal View History

2025-07-23 15:47:29 +02:00
import json
import os
from typing import List, TypedDict
import copy
class MiscBotOptions(TypedDict):
ping_line: str
active_song: str
positive_gifs: List[str]
block_profanity: bool
class BotSettings(TypedDict):
prefix: str
owner_ids: List[int]
blacklisted_users: List[int]
user_training: bool
allow_show_mem_command: bool
react_to_messages: bool
misc: MiscBotOptions
enabled_cogs: List[str]
active_memory: str
class SettingsType(TypedDict):
bot: BotSettings
locale: str
name: str
auto_update: bool
disable_checks: bool
splash_text_loc: str
class Settings:
def __init__(self) -> None:
self.path = os.path.join(".", "settings", "settings.json")
if not os.path.exists(self.path):
raise FileNotFoundError("settings.json file does not exist!")
with open(self.path, "r") as f:
self._kv_store = json.load(f)
self.settings: SettingsType = self._kv_store # type: ignore
self.original_settings = copy.deepcopy(self.settings)
def get_locale(self) -> str:
# Return locale or None if missing
return self.settings.get("locale", None)
def commit(self) -> None:
with open(self.path, "w") as f:
json.dump(self.settings, f, indent=4)
self.original_settings = copy.deepcopy(self.settings)
def discard(self) -> None:
self.settings = copy.deepcopy(self.original_settings)
# Usage
instance = Settings()
locale = instance.get_locale()
print("Locale:", locale)