diff options
Diffstat (limited to 'shared')
| -rw-r--r-- | shared/__init__.py | 3 | ||||
| -rw-r--r-- | shared/commands.py | 23 | ||||
| -rw-r--r-- | shared/config.py | 28 | ||||
| -rw-r--r-- | shared/database.py | 17 | ||||
| -rw-r--r-- | shared/instances.py | 17 | ||||
| -rw-r--r-- | shared/settings.py | 69 |
6 files changed, 117 insertions, 40 deletions
diff --git a/shared/__init__.py b/shared/__init__.py index 9153c94..f708bfe 100644 --- a/shared/__init__.py +++ b/shared/__init__.py | |||
| @@ -1,4 +1,5 @@ | |||
| 1 | # isort: skip_file | 1 | # isort: skip_file |
| 2 | from . import config | 2 | from . import settings |
| 3 | from . import instances | 3 | from . import instances |
| 4 | from . import database | ||
| 4 | from . import commands | 5 | from . import commands |
diff --git a/shared/commands.py b/shared/commands.py index 136f782..684997c 100644 --- a/shared/commands.py +++ b/shared/commands.py | |||
| @@ -1,19 +1,24 @@ | |||
| 1 | from aiogram.types import BotCommand as cmd | 1 | from aiogram.types import BotCommand as cmd |
| 2 | from aiogram.types import BotCommandScopeAllChatAdministrators as admin | ||
| 2 | from aiogram.types import BotCommandScopeAllGroupChats as group | 3 | from aiogram.types import BotCommandScopeAllGroupChats as group |
| 3 | from aiogram.types import BotCommandScopeAllPrivateChats as private | 4 | from aiogram.types import BotCommandScopeAllPrivateChats as private |
| 4 | 5 | ||
| 5 | commands = { | 6 | commands = { |
| 6 | group(): [ | 7 | group(): [ |
| 7 | cmd("gen", "Покакать текстом"), | 8 | cmd("gen", "Сгенерировать сообщение"), |
| 8 | cmd("del", "Убрать говно"), | 9 | cmd("del", "Удалить сообщение бота"), |
| 9 | cmd("void", "Лоботомия"), | 10 | cmd("pin", "Создать опрос для закрепления сообщения"), |
| 10 | cmd("pin", "Закрепить говно"), | 11 | ], |
| 11 | cmd("chance", "Установить шанс покакать в туалет"), | 12 | admin(): [ |
| 13 | cmd("gen", "Сгенерировать сообщение"), | ||
| 14 | cmd("del", "Удалить сообщение бота"), | ||
| 15 | cmd("pin", "Создать опрос для закрепления сообщения"), | ||
| 16 | cmd("void", "Отчистить связи для генерации сообщений"), | ||
| 17 | cmd("config", "Открыть настройки чата"), | ||
| 12 | ], | 18 | ], |
| 13 | private(): [ | 19 | private(): [ |
| 14 | cmd("gen", "Покакать текстом"), | 20 | cmd("gen", "Сгенерировать сообщение"), |
| 15 | cmd("del", "Убрать говно"), | 21 | cmd("del", "Удалить сообщение бота"), |
| 16 | cmd("void", "Лоботомия"), | 22 | cmd("void", "Отчистить связи для генерации сообщений"), |
| 17 | cmd("chance", "Установить шанс покакать в туалет"), | ||
| 18 | ], | 23 | ], |
| 19 | } | 24 | } |
diff --git a/shared/config.py b/shared/config.py deleted file mode 100644 index 09ced45..0000000 --- a/shared/config.py +++ /dev/null | |||
| @@ -1,28 +0,0 @@ | |||
| 1 | import logging | ||
| 2 | from copy import deepcopy | ||
| 3 | from json import dump, load | ||
| 4 | from os import environ as env | ||
| 5 | from os import path | ||
| 6 | from typing import Any | ||
| 7 | |||
| 8 | logging.info("Load configs") | ||
| 9 | |||
| 10 | if not path.exists("data/settings.json"): | ||
| 11 | with open("data/settings.json", "w") as f: | ||
| 12 | f.write("{}") | ||
| 13 | fields: dict[str, Any] = { | ||
| 14 | "chances": {}, | ||
| 15 | } | ||
| 16 | |||
| 17 | settings: dict[str, Any] = load(open("data/settings.json", "r")) | ||
| 18 | for key, default in fields.items(): | ||
| 19 | settings[key] = settings.get(key, deepcopy(default)) | ||
| 20 | |||
| 21 | |||
| 22 | def save(): | ||
| 23 | dump(settings, open("data/settings.json", "w")) | ||
| 24 | |||
| 25 | |||
| 26 | # Configs | ||
| 27 | token = env["TOKEN"] | ||
| 28 | chances: dict[str, int] = settings["chances"] | ||
diff --git a/shared/database.py b/shared/database.py new file mode 100644 index 0000000..9dfe868 --- /dev/null +++ b/shared/database.py | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column | ||
| 2 | |||
| 3 | from shared.instances import engine | ||
| 4 | |||
| 5 | |||
| 6 | class Base(DeclarativeBase): | ||
| 7 | pass | ||
| 8 | |||
| 9 | |||
| 10 | class Message(Base): | ||
| 11 | __tablename__ = "messages" | ||
| 12 | chat_id: Mapped[int] = mapped_column(primary_key=True) | ||
| 13 | message_id: Mapped[int] = mapped_column(primary_key=True) | ||
| 14 | message: Mapped[str] | ||
| 15 | |||
| 16 | |||
| 17 | Base.metadata.create_all(engine) | ||
diff --git a/shared/instances.py b/shared/instances.py index 9ca0b2c..fb511b3 100644 --- a/shared/instances.py +++ b/shared/instances.py | |||
| @@ -1,6 +1,19 @@ | |||
| 1 | from os.path import exists | ||
| 2 | |||
| 1 | from aiogram import Bot, Dispatcher | 3 | from aiogram import Bot, Dispatcher |
| 4 | from sqlalchemy import create_engine | ||
| 5 | from sqlalchemy.orm import Session, sessionmaker | ||
| 6 | |||
| 7 | from shared.settings import Chats, Settings | ||
| 2 | 8 | ||
| 3 | from shared.config import token | 9 | settings = Settings() |
| 10 | config = Chats() | ||
| 11 | if not exists("data/config.json"): | ||
| 12 | config.save("data/config.json") | ||
| 13 | config.load("data/config.json") | ||
| 4 | 14 | ||
| 5 | bot = Bot(token=token) | 15 | bot = Bot(token=settings.token) |
| 6 | dp = Dispatcher(bot) | 16 | dp = Dispatcher(bot) |
| 17 | |||
| 18 | engine = create_engine("sqlite:///data/database.sqlite") | ||
| 19 | session = sessionmaker(engine, Session) | ||
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 @@ | |||
| 1 | from json import dump, load | ||
| 2 | |||
| 3 | from pydantic import BaseModel, BaseSettings, Field | ||
| 4 | |||
| 5 | |||
| 6 | class Settings(BaseSettings): | ||
| 7 | token: str | ||
| 8 | |||
| 9 | |||
| 10 | class 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 | |||
| 29 | class 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 | |||
| 40 | class Config(BaseModel): | ||
| 41 | gen: GenConfig = Field( | ||
| 42 | GenConfig(), | ||
| 43 | description="Настройки генерации сообщений", | ||
| 44 | ) | ||
| 45 | commands: CommandsConfig = Field( | ||
| 46 | CommandsConfig(), | ||
| 47 | description="Настройки команд бота", | ||
| 48 | ) | ||
| 49 | |||
| 50 | |||
| 51 | class 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 | ||
