feat: Refactored all cogs implementation
Some checks failed
CI / Run tests (pull_request) Has been cancelled
Some checks failed
CI / Run tests (pull_request) Has been cancelled
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import os
|
||||
import os, pathlib, json
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from setup.logger import LOGGER
|
||||
|
||||
class Config:
|
||||
load_dotenv()
|
||||
@@ -9,6 +9,10 @@ class Config:
|
||||
CONFIG_PATH = str(os.getenv("CONFIG_PATH"))
|
||||
COGS = os.getenv("COGS", "")
|
||||
|
||||
GUILDS_IDS:list[int] = []
|
||||
|
||||
GUILD_CONFIGS:dict[int,dict] = {}
|
||||
|
||||
@classmethod
|
||||
def get_cogs(cls):
|
||||
raw_value = cls.COGS.strip()
|
||||
@@ -16,3 +20,53 @@ class Config:
|
||||
return None
|
||||
return [item.strip().lower() for item in raw_value.split(",") if item.strip()]
|
||||
|
||||
@classmethod
|
||||
def load_guilds_configs(cls):
|
||||
with os.scandir(cls.CONFIG_PATH) as d:
|
||||
guilds_config_files = [f.path for f in d if f.is_file() and f.name.endswith(".json")]
|
||||
for guild_config_file in guilds_config_files:
|
||||
with open(guild_config_file) as f:
|
||||
guild_config = json.load(f)
|
||||
guild_id = guild_config["guild_id"]
|
||||
cls.GUILDS_IDS.append(guild_id)
|
||||
cls.GUILD_CONFIGS[guild_id] = {k:v for k,v in guild_config.items() if k != "guild_id"}
|
||||
cls.GUILD_CONFIGS[guild_id]["config_path"] = guild_config_file
|
||||
|
||||
@classmethod
|
||||
def load_guild_config(cls,guild_id:int):
|
||||
config_path = cls.GUILD_CONFIGS[guild_id]["config_path"]
|
||||
with open(config_path) as f:
|
||||
guild_config = json.load(f)
|
||||
# Preserve config_path when re-loading
|
||||
cls.GUILD_CONFIGS[guild_id] = {k: v for k, v in guild_config.items() if k != "guild_id"}
|
||||
cls.GUILD_CONFIGS[guild_id]["config_path"] = config_path
|
||||
|
||||
@classmethod
|
||||
def save_guilds_configs(cls):
|
||||
for guild_id, guild_config in cls.GUILD_CONFIGS.items():
|
||||
config_path = guild_config.get("config_path")
|
||||
if not config_path:
|
||||
continue
|
||||
with open(config_path, 'w') as file:
|
||||
json_config = {k: v for k, v in guild_config.items() if k != "config_path"}
|
||||
json_config["guild_id"] = guild_id
|
||||
json.dump(json_config, file, ensure_ascii=False, indent=2)
|
||||
|
||||
@classmethod
|
||||
def save_guild_config(cls,guild_id:int):
|
||||
config = cls.GUILD_CONFIGS.get(guild_id)
|
||||
if not config:
|
||||
return
|
||||
config_path = config.get("config_path")
|
||||
if not config_path:
|
||||
return
|
||||
with open(config_path, 'w') as file:
|
||||
json_config = {k: v for k, v in config.items() if k != "config_path"}
|
||||
json_config["guild_id"] = guild_id
|
||||
json.dump(json_config, file, ensure_ascii=False, indent=2)
|
||||
|
||||
@classmethod
|
||||
def get_guild_config(cls,guild_id):
|
||||
return cls.GUILD_CONFIGS[guild_id]
|
||||
|
||||
Config.load_guilds_configs()
|
||||
Reference in New Issue
Block a user