aboutsummaryrefslogtreecommitdiff
path: root/shared/settings.py
diff options
context:
space:
mode:
Diffstat (limited to 'shared/settings.py')
-rw-r--r--shared/settings.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/shared/settings.py b/shared/settings.py
new file mode 100644
index 0000000..20f3f76
--- /dev/null
+++ b/shared/settings.py
@@ -0,0 +1,69 @@
1from json import dump, load
2
3from pydantic import BaseModel, BaseSettings, Field
4
5
6class Settings(BaseSettings):
7 token: str
8
9
10class GenConfig(BaseModel):
11 chance: int = Field(
12 10,
13 description="Шанс с которым бот ответит на сообщение",
14 ge=1,
15 le=100,
16 )
17 min_word_count: int | str | None = Field(
18 None,
19 description="Минимальное количество слов в сгенерированном предложении",
20 ge=1,
21 )
22 max_word_count: int | None = Field(
23 None,
24 description="Максимальное количество слов в сгенерированном предложении",
25 ge=1,
26 )
27
28
29class CommandsConfig(BaseModel):
30 pin_answers_count: int = Field(
31 4,
32 description="Минимальное количество голосов для проверки опроса на закрепление сообщения",
33 )
34 accept_member_answers_count: int = Field(
35 5,
36 description="Минимальное количество голосов для проверки опроса на принятия человека в группу",
37 )
38
39
40class Config(BaseModel):
41 gen: GenConfig = Field(
42 GenConfig(),
43 description="Настройки генерации сообщений",
44 )
45 commands: CommandsConfig = Field(
46 CommandsConfig(),
47 description="Настройки команд бота",
48 )
49
50
51class Chats(BaseModel):
52 chats: dict[int, Config] = {}
53
54 @classmethod
55 def load(cls, file_name: str) -> "Chats":
56 with open(file_name, "r") as file:
57 return cls.parse_obj(load(file))
58
59 def save(self, file_name: str) -> None:
60 with open(file_name, "w") as file:
61 dump(self.schema(), file)
62
63 def get_config(self, chat_id: int) -> Config:
64 if chat_id not in self.chats:
65 self.chats[chat_id] = Config()
66 return self.chats[chat_id]
67
68 def set_config(self, chat_id: int, config: Config) -> None:
69 self.chats[chat_id] = config