aboutsummaryrefslogtreecommitdiff
path: root/shared/settings.py
blob: 731fade968bd4f081e1168de5c3135916e58e6ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from json import dump, load

from pydantic import BaseModel, BaseSettings, Field


class Settings(BaseSettings):
    token: str


class GenConfig(BaseModel):
    chance: int = Field(
        10,
        description="Шанс с которым бот ответит на сообщение",
        ge=1,
        le=100,
    )
    min_word_count: int | str | None = Field(
        None,
        description="Минимальное количество слов в сгенерированном предложении",
        ge=1,
    )
    max_word_count: int | None = Field(
        None,
        description="Максимальное количество слов в сгенерированном предложении",
        ge=1,
    )


class PollConfig(BaseModel):
    answer_count: int = Field(
        4,
        description="Минимальное количество голосов для проверки опроса",
    )
    anonym: bool = Field(
        False,
        description="Включить/Выключить анонимный опрос",
    )


class Config(BaseModel):
    gen: GenConfig = Field(
        GenConfig(),
        description="Настройки генерации сообщений",
    )
    pin: PollConfig = Field(
        PollConfig(),
        description="Настройки закрепления сообщений",
    )
    members: PollConfig = Field(
        PollConfig(),
        description="Настройки принятия людей в группу",
    )


class Chats(BaseModel):
    chats: dict[int, Config] = {}

    @classmethod
    def load(cls, file_name: str) -> "Chats":
        with open(file_name, "r") as file:
            return cls.parse_obj(load(file))

    def save(self, file_name: str) -> None:
        with open(file_name, "w") as file:
            dump(self.schema(), file)

    def get_config(self, chat_id: int) -> Config:
        if chat_id not in self.chats:
            self.chats[chat_id] = Config()
        return self.chats[chat_id]

    def set_config(self, chat_id: int, config: Config) -> None:
        self.chats[chat_id] = config