diff --git a/.gitignore b/.gitignore index cd8d226..344a92f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .vscode .env env +.venv venv __pycache__ OCAB.db diff --git a/README.md b/README.md index 14d8465..69be112 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ OCAB - это бот для Telegram, который призван помочь ## Технологический стек -* Python 3.11.6 - основной язык программирования. +* Python 3.11.6 или выше - основной язык программирования. * SQLite 3 - база данных для хранения информации о чате и пользователях. * [Poetry](https://gitflic.ru/project/armatik/ocab/blob?file=how-to%20install%20deps.md&branch=OCAB-V2) - менеджер зависимостей. * aiogram 3 - библиотека для работы с Telegram API. diff --git a/how-to install deps.md b/how-to install deps.md index 79c77e0..9415845 100644 --- a/how-to install deps.md +++ b/how-to install deps.md @@ -1,9 +1,45 @@ -Ставим poetry: -```bash -pip install poetry +## Poetry + +### Установка с официального сайта + +```shell +curl -sSL https://install.python-poetry.org | python3 - ``` -Ставим зависимости: -```bash +### Установка с PyPi + +```shell +python3 -m pip install poetry +``` + +## Зависимости + +### Добавление зависимости +```shell +poetry add NAME +``` +`NAME` - название зависимости. + +### Установка зависимостей +```shell poetry install +``` + +### Обновление зависимостей +```shell +poetry update +``` + +## Виртуальное окружение + +### Создание/активация +```shell +poetry shell +``` + +### Настройка + +Хранить окружение внутри проекта +```shell +poetry config virtualenvs.in-project true ``` \ No newline at end of file diff --git a/poetry.lock b/poetry.lock index ade0aa5..355811a 100644 --- a/poetry.lock +++ b/poetry.lock @@ -824,5 +824,5 @@ multidict = ">=4.0" [metadata] lock-version = "2.0" -python-versions = "3.11.6" +python-versions = "^3.11.6" content-hash = "dea2f6ada13efef77325b219721fda6515a4c203c31e47dcafe8726e5d5f9ac6" diff --git a/pyproject.toml b/pyproject.toml index 48b1512..a8ccac3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,11 +2,18 @@ name = "ocab" version = "0.1.0" description = "" -authors = ["Your Name "] +license = "GPL-3.0-only" +authors = ["Семён Фомченков "] +maintainers = [ + "Илья Женецкий ", + "qualimock ", + "Кирилл Уницаев fiersik.kouji@yandex.ru", +] readme = "README.md" +repository = "https://gitflic.ru/project/armatik/ocab" [tool.poetry.dependencies] -python = "3.11.6" +python = "^3.11.6" aiogram = "^3.2.0" peewee = "^3.17.0" pyyaml = "^6.0.1" diff --git a/src/__init__.py b/src/__init__.py index bcc746e..2c9e6c4 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -1 +1,2 @@ -import service \ No newline at end of file +import src.service +import src.core \ No newline at end of file diff --git a/src/core/main.py b/src/core/main.py index caeb799..ea4a2bd 100644 --- a/src/core/main.py +++ b/src/core/main.py @@ -1,17 +1,19 @@ -from src.modules.standard.config.config import get_config -from src.modules.standard.database.models.base import database -from src.modules.standard.database.db_api import connect_database +from routers import include_routers +from src.modules.standard.config.config import get_telegram_token +from src.modules.standard.database.api import connect_database, create_tables from asyncio import run from aiogram import Bot, Dispatcher -async def main(): + +async def main(bot: Bot): try: - database = connect_database() - bot = Bot(token=get_config()["TELEGRAM"]["TOKEN"]) + database, path = connect_database() + database.connect() + create_tables(database) dp = Dispatcher() - + await include_routers(dp) await dp.start_polling(bot) finally: @@ -20,4 +22,5 @@ async def main(): if __name__ == "__main__": - run(main()) + bot = Bot(token=get_telegram_token()) + run(main(bot)) diff --git a/src/core/routers.py b/src/core/routers.py new file mode 100644 index 0000000..a0390b0 --- /dev/null +++ b/src/core/routers.py @@ -0,0 +1,11 @@ +from aiogram import Dispatcher + +from src.modules.standard.admin.routers import router as admin_router + + +async def include_routers(dp: Dispatcher): + """ + Подключение роутеров в бота + dp.include_router() + """ + dp.include_router(admin_router) \ No newline at end of file diff --git a/src/modules/standard/admin/__init__.py b/src/modules/standard/admin/__init__.py new file mode 100644 index 0000000..62a5d54 --- /dev/null +++ b/src/modules/standard/admin/__init__.py @@ -0,0 +1 @@ +from . import routers diff --git a/src/modules/standard/admin/handlers.py b/src/modules/standard/admin/handlers.py new file mode 100644 index 0000000..ef6afa2 --- /dev/null +++ b/src/modules/standard/admin/handlers.py @@ -0,0 +1,13 @@ +from aiogram import Bot +from aiogram.types import Message + + +async def delete_message(message: Message, bot: Bot): + reply_message_id = message.reply_to_message.message_id + await bot.delete_message(message.chat.id, reply_message_id) + +async def error_access(message: Message, bot: Bot): + await message.reply("Вы не админ/модератор") + +async def get_chat_id(message: Message, bot: Bot): + await message.reply(f"ID данного чата: `{message.chat.id}`", parse_mode="MarkdownV2") \ No newline at end of file diff --git a/src/modules/standard/admin/info.json b/src/modules/standard/admin/info.json new file mode 100644 index 0000000..bd45d98 --- /dev/null +++ b/src/modules/standard/admin/info.json @@ -0,0 +1,6 @@ +{ + "name": "Admin", + "description": "Модуль для работы с админкой", + "author": "OCAB Team", + "version": "1.0" +} \ No newline at end of file diff --git a/src/modules/standard/admin/routers.py b/src/modules/standard/admin/routers.py new file mode 100644 index 0000000..63df570 --- /dev/null +++ b/src/modules/standard/admin/routers.py @@ -0,0 +1,11 @@ +from aiogram import Router, F + +from src.modules.standard.admin.handlers import delete_message, error_access, get_chat_id +from src.modules.standard.filters.admin import ChatModerOrAdminFilter + +router = Router() +router.message.register(get_chat_id, ChatModerOrAdminFilter(), F.text == '/chatID') + +router.message.register(delete_message, ChatModerOrAdminFilter(), F.text == '/rm') +router.message.register(error_access, F.text == '/rm') +router.message.register(error_access, F.text == '/chatID') \ No newline at end of file diff --git a/src/modules/standard/config/config.py b/src/modules/standard/config/config.py index c58ba57..4ceb131 100644 --- a/src/modules/standard/config/config.py +++ b/src/modules/standard/config/config.py @@ -12,15 +12,19 @@ def get_config(is_test: bool = False) -> dict: with open(path, 'r') as file: return yaml.full_load(file) +config = get_config() + +def get_telegram_token() -> str: + return config["TELEGRAM"]["TOKEN"] def get_yandexgpt_token() -> str: - return get_config()["YANDEX_GPT_TOKEN"] + return config["YANDEX_GPT_TOKEN"] def get_yandexgpt_catalog_id() -> str: - return get_config()["YANDEX_GPT_CATALOG_ID"] + return config["YANDEX_GPT_CATALOG_ID"] def get_yandexgpt_prompt() -> str: - return get_config()["YANDEX_GPT_PROMPT"] + return config["YANDEX_GPT_PROMPT"] def get_access_rights() -> dict: return get_config()["ACCESS_RIGHTS"] diff --git a/src/modules/standard/database/db_api.py b/src/modules/standard/database/db_api.py index 2c0ff88..5663d6d 100644 --- a/src/modules/standard/database/db_api.py +++ b/src/modules/standard/database/db_api.py @@ -22,6 +22,8 @@ def connect_database(is_test: bool = False, module: str | None = None): Chats._meta.database = _database Messages._meta.database = _database Users._meta.database = _database + UserStats._meta.database = _database + ChatStats._meta.database = _database return _database, f"{db_path}/OCAB.db" diff --git a/src/modules/standard/filters/__init__.py b/src/modules/standard/filters/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/modules/standard/filters/admin.py b/src/modules/standard/filters/admin.py new file mode 100644 index 0000000..2b5547a --- /dev/null +++ b/src/modules/standard/filters/admin.py @@ -0,0 +1,13 @@ +from aiogram.filters import BaseFilter +from aiogram.types import Message +from aiogram import Bot + +from src.modules.standard.roles.api import Roles + +class ChatModerOrAdminFilter(BaseFilter): + async def __call__(self, message: Message, bot: Bot) -> bool: + user_id = message.from_user.id + roles = Roles() + admins = await bot.get_chat_administrators(message.chat.id) + return await roles.check_admin_permission(user_id) or \ + await roles.check_moderator_permission(user_id) or any(user_id == admin.user.id for admin in admins) diff --git a/src/modules/standard/filters/info.json b/src/modules/standard/filters/info.json new file mode 100644 index 0000000..59f6d44 --- /dev/null +++ b/src/modules/standard/filters/info.json @@ -0,0 +1,6 @@ +{ + "name": "Filters", + "description": "Модуль с фильтрами", + "author": "OCAB Team", + "version": "1.0" +} \ No newline at end of file