From 68406fee5380a082d1b38a8bb0fec6880c01d1c9 Mon Sep 17 00:00:00 2001 From: qualimock Date: Mon, 5 Feb 2024 15:51:50 +0300 Subject: [PATCH 1/6] Fix E302 --- src/core/main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/core/main.py b/src/core/main.py index dad0530..ed06471 100644 --- a/src/core/main.py +++ b/src/core/main.py @@ -5,6 +5,7 @@ from src.modules.standard.database.api import connect_database from asyncio import run from aiogram import Bot, Dispatcher + async def main(): try: database = connect_database() From d71fef0fed7391e8b5eee66f0301c539092b1eb1 Mon Sep 17 00:00:00 2001 From: ilyazheprog Date: Mon, 12 Feb 2024 11:48:55 +0700 Subject: [PATCH 2/6] =?UTF-8?q?=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D1=81=D1=82=D0=B5=D0=BA:=20Python=203.11.6=20=D0=B8=D0=BB?= =?UTF-8?q?=D0=B8=20=D0=B2=D1=8B=D1=88=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- poetry.lock | 2 +- pyproject.toml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) 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/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..28f414e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ authors = ["Your Name "] readme = "README.md" [tool.poetry.dependencies] -python = "3.11.6" +python = "^3.11.6" aiogram = "^3.2.0" peewee = "^3.17.0" pyyaml = "^6.0.1" From 88f40c7f820b64784d33661e417f948c46dcf3fd Mon Sep 17 00:00:00 2001 From: ilyazheprog Date: Mon, 12 Feb 2024 14:55:14 +0700 Subject: [PATCH 3/6] =?UTF-8?q?=D0=90=D0=B4=D0=BC=D0=B8=D0=BD=20=D0=BC?= =?UTF-8?q?=D0=BE=D0=B4=D1=83=D0=BB=D1=8C:=20=D1=83=D0=B4=D0=B0=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B9=20=D0=9C=D0=BE=D0=B4=D1=83=D0=BB=D1=8C=20?= =?UTF-8?q?=D1=84=D0=B8=D0=BB=D1=8C=D1=82=D1=80=D0=BE=D0=B2=20=D0=94=D0=BE?= =?UTF-8?q?=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D1=8B=20=D1=80=D0=BE=D1=83?= =?UTF-8?q?=D1=82=D0=B5=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/__init__.py | 2 +- src/core/main.py | 18 ++++++++++-------- src/core/routers.py | 11 +++++++++++ src/modules/standard/admin/__init__.py | 1 + src/modules/standard/admin/handlers.py | 10 ++++++++++ src/modules/standard/admin/info.json | 6 ++++++ src/modules/standard/admin/routers.py | 8 ++++++++ src/modules/standard/config/config.py | 10 +++++++--- src/modules/standard/database/api.py | 2 ++ src/modules/standard/filters/__init__.py | 0 src/modules/standard/filters/admin.py | 11 +++++++++++ src/modules/standard/filters/info.json | 6 ++++++ 12 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 src/core/routers.py create mode 100644 src/modules/standard/admin/__init__.py create mode 100644 src/modules/standard/admin/handlers.py create mode 100644 src/modules/standard/admin/info.json create mode 100644 src/modules/standard/admin/routers.py create mode 100644 src/modules/standard/filters/__init__.py create mode 100644 src/modules/standard/filters/admin.py create mode 100644 src/modules/standard/filters/info.json diff --git a/src/__init__.py b/src/__init__.py index bcc746e..fa7075c 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -1 +1 @@ -import service \ No newline at end of file +import src.service \ No newline at end of file diff --git a/src/core/main.py b/src/core/main.py index ed06471..a7e7422 100644 --- a/src/core/main.py +++ b/src/core/main.py @@ -1,18 +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.api import connect_database +from src.core.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: @@ -21,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..863f091 --- /dev/null +++ b/src/modules/standard/admin/handlers.py @@ -0,0 +1,10 @@ +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 delete_message_error_access(message: Message, bot: Bot): + await message.reply("Вы не админ/модератор") \ 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..45cb557 --- /dev/null +++ b/src/modules/standard/admin/routers.py @@ -0,0 +1,8 @@ +from aiogram import Router, F + +from src.modules.standard.admin.handlers import delete_message, delete_message_error_access +from src.modules.standard.filters.admin import ChatModerOrAdminFilter + +router = Router() +router.message.register(delete_message, ChatModerOrAdminFilter(), F.text == '/rm') +router.message.register(delete_message_error_access, F.text == '/rm') \ No newline at end of file diff --git a/src/modules/standard/config/config.py b/src/modules/standard/config/config.py index 49e37da..f53c62f 100644 --- a/src/modules/standard/config/config.py +++ b/src/modules/standard/config/config.py @@ -12,14 +12,18 @@ 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"] diff --git a/src/modules/standard/database/api.py b/src/modules/standard/database/api.py index 2c0ff88..5663d6d 100644 --- a/src/modules/standard/database/api.py +++ b/src/modules/standard/database/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..f0c1c14 --- /dev/null +++ b/src/modules/standard/filters/admin.py @@ -0,0 +1,11 @@ +from aiogram.filters import BaseFilter +from aiogram.types import Message + +from src.modules.standard.roles.api import Roles + +class ChatModerOrAdminFilter(BaseFilter): + async def __call__(self, message: Message) -> bool: + user_id = message.from_user.id + roles = Roles() + return await roles.check_admin_permission(user_id) or \ + await roles.check_moderator_permission(user_id) 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 From 0dc216d9f4e43e4c3f4dea52a90d8c9a6da58fd5 Mon Sep 17 00:00:00 2001 From: fiersik Date: Sat, 24 Feb 2024 21:13:51 +0400 Subject: [PATCH 4/6] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20how-to=20install=20deps?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + how-to install deps.md | 46 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 5 deletions(-) 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/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 From 4d6ee647312b2dce83e5f49aa536c0c00275d82e Mon Sep 17 00:00:00 2001 From: fiersik Date: Sat, 24 Feb 2024 23:49:50 +0400 Subject: [PATCH 5/6] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20pyproject?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pyproject.toml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 28f414e..a8ccac3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,8 +2,15 @@ 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" From 4d2f61b1c2ce5cbf926b1f8623aeb9d8212bc42d Mon Sep 17 00:00:00 2001 From: ilyazheprog Date: Sun, 25 Feb 2024 03:59:26 +0700 Subject: [PATCH 6/6] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20/chatID=20=D0=9F=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B0?= =?UTF-8?q?=20=D0=B0=D0=B4=D0=BC=D0=B8=D0=BD=20=D0=BF=D1=80=D0=B0=D0=B2=20?= =?UTF-8?q?=D0=B8=D1=81=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D1=83=D1=8F=20=D1=82?= =?UTF-8?q?=D0=B5=D0=BB=D0=B5=D0=B3=D1=80=D0=B0=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/__init__.py | 3 ++- src/core/main.py | 2 +- src/modules/standard/admin/handlers.py | 7 +++++-- src/modules/standard/admin/routers.py | 7 +++++-- src/modules/standard/filters/admin.py | 6 ++++-- 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/__init__.py b/src/__init__.py index fa7075c..2c9e6c4 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -1 +1,2 @@ -import src.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 a7e7422..ea4a2bd 100644 --- a/src/core/main.py +++ b/src/core/main.py @@ -1,4 +1,4 @@ -from src.core.routers import include_routers +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 diff --git a/src/modules/standard/admin/handlers.py b/src/modules/standard/admin/handlers.py index 863f091..ef6afa2 100644 --- a/src/modules/standard/admin/handlers.py +++ b/src/modules/standard/admin/handlers.py @@ -6,5 +6,8 @@ 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 delete_message_error_access(message: Message, bot: Bot): - await message.reply("Вы не админ/модератор") \ No newline at end of file +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/routers.py b/src/modules/standard/admin/routers.py index 45cb557..63df570 100644 --- a/src/modules/standard/admin/routers.py +++ b/src/modules/standard/admin/routers.py @@ -1,8 +1,11 @@ from aiogram import Router, F -from src.modules.standard.admin.handlers import delete_message, delete_message_error_access +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(delete_message_error_access, F.text == '/rm') \ No newline at end of file +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/filters/admin.py b/src/modules/standard/filters/admin.py index f0c1c14..2b5547a 100644 --- a/src/modules/standard/filters/admin.py +++ b/src/modules/standard/filters/admin.py @@ -1,11 +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) -> bool: + 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) + await roles.check_moderator_permission(user_id) or any(user_id == admin.user.id for admin in admins)