aboutsummaryrefslogtreecommitdiff
path: root/shared/settings.py
diff options
context:
space:
mode:
Diffstat (limited to 'shared/settings.py')
-rw-r--r--shared/settings.py49
1 files changed, 23 insertions, 26 deletions
diff --git a/shared/settings.py b/shared/settings.py
index 560f8f8..bff7ff9 100644
--- a/shared/settings.py
+++ b/shared/settings.py
@@ -11,7 +11,7 @@ class GenConfig(BaseModel):
11 chance: int = Field( 11 chance: int = Field(
12 10, 12 10,
13 description="Шанс с которым бот сгенерирует сообщение", 13 description="Шанс с которым бот сгенерирует сообщение",
14 ge=1, 14 ge=0,
15 le=100, 15 le=100,
16 ) 16 )
17 reply: bool = Field( 17 reply: bool = Field(
@@ -22,16 +22,6 @@ class GenConfig(BaseModel):
22 True, 22 True,
23 description="Включить/Выключить удаление /gen команды", 23 description="Включить/Выключить удаление /gen команды",
24 ) 24 )
25 min_word_count: int | str | None = Field(
26 None,
27 description="Минимальное количество слов в сгенерированном предложении",
28 ge=1,
29 )
30 max_word_count: int | None = Field(
31 None,
32 description="Максимальное количество слов в сгенерированном предложении",
33 ge=1,
34 )
35 25
36 26
37class PollConfig(BaseModel): 27class PollConfig(BaseModel):
@@ -60,22 +50,29 @@ class Config(BaseModel):
60 ) 50 )
61 51
62 52
63class Chats(BaseModel): 53class Chats:
64 chats: dict[int, Config] = {} 54 file_name: str
55 configs: dict[int, Config]
56
57 def __init__(self, file_name: str) -> None:
58 self.file_name = file_name
59 self.configs = {}
65 60
66 @classmethod 61 def load(self) -> None:
67 def load(cls, file_name: str) -> "Chats": 62 with open(self.file_name, "r") as file:
68 with open(file_name, "r") as file: 63 self.configs = {
69 return cls.parse_obj(load(file)) 64 id_: Config.parse_obj(config) for id_, config in load(file).items()
65 }
70 66
71 def save(self, file_name: str) -> None: 67 def save(self) -> None:
72 with open(file_name, "w") as file: 68 with open(self.file_name, "w") as file:
73 dump(self.schema(), file) 69 dump({id_: config.dict() for id_, config in self.configs.items()}, file)
74 70
75 def get_config(self, chat_id: int) -> Config: 71 def get(self, chat_id: int) -> Config:
76 if chat_id not in self.chats: 72 if chat_id not in self.configs:
77 self.chats[chat_id] = Config() 73 self.configs[chat_id] = Config()
78 return self.chats[chat_id] 74 return self.configs[chat_id]
79 75
80 def set_config(self, chat_id: int, config: Config) -> None: 76 def set(self, chat_id: int, config: Config) -> None:
81 self.chats[chat_id] = config 77 self.configs[chat_id] = config
78 self.save()