diff --git a/src/karkas_blocks/karkas_blocks/standard/spam/__init__.py b/src/karkas_blocks/karkas_blocks/standard/spam/__init__.py new file mode 100644 index 0000000..c8fccb0 --- /dev/null +++ b/src/karkas_blocks/karkas_blocks/standard/spam/__init__.py @@ -0,0 +1 @@ +from .main import module_init diff --git a/src/karkas_blocks/karkas_blocks/standard/spam/db/__init__.py b/src/karkas_blocks/karkas_blocks/standard/spam/db/__init__.py new file mode 100644 index 0000000..e3e4a0b --- /dev/null +++ b/src/karkas_blocks/karkas_blocks/standard/spam/db/__init__.py @@ -0,0 +1 @@ +from .piccolo_app import APP_CONFIG diff --git a/src/karkas_blocks/karkas_blocks/standard/spam/db/piccolo_app.py b/src/karkas_blocks/karkas_blocks/standard/spam/db/piccolo_app.py new file mode 100644 index 0000000..525c4cd --- /dev/null +++ b/src/karkas_blocks/karkas_blocks/standard/spam/db/piccolo_app.py @@ -0,0 +1,15 @@ +import os + +from karkas_piccolo.conf.apps import AppConfig + +from .tables import SpamLog + +CURRENT_DIRECTORY = os.path.dirname(os.path.abspath(__file__)) + +APP_CONFIG = AppConfig( + app_name="standard.spam", + migrations_folder_path=os.path.join(CURRENT_DIRECTORY, "piccolo_migrations"), + table_classes=[SpamLog], + migration_dependencies=[], + commands=[], +) diff --git a/src/karkas_blocks/karkas_blocks/standard/spam/db/piccolo_migrations/standardspam_2024_10_03t17_43_50_559465.py b/src/karkas_blocks/karkas_blocks/standard/spam/db/piccolo_migrations/standardspam_2024_10_03t17_43_50_559465.py new file mode 100644 index 0000000..6f4a6d9 --- /dev/null +++ b/src/karkas_blocks/karkas_blocks/standard/spam/db/piccolo_migrations/standardspam_2024_10_03t17_43_50_559465.py @@ -0,0 +1,125 @@ +from piccolo.apps.migrations.auto.migration_manager import MigrationManager +from piccolo.columns.column_types import JSON, Date, Integer, Text +from piccolo.columns.defaults.date import DateNow +from piccolo.columns.indexes import IndexMethod + +ID = "2024-10-03T17:43:50:559465" +VERSION = "1.16.0" +DESCRIPTION = "" + + +async def forwards(): + manager = MigrationManager( + migration_id=ID, app_name="standard.spam", description=DESCRIPTION + ) + + manager.add_table( + class_name="SpamLog", tablename="spam_log", schema=None, columns=None + ) + + manager.add_column( + table_class_name="SpamLog", + tablename="spam_log", + column_name="chat_id", + db_column_name="chat_id", + column_class_name="Integer", + column_class=Integer, + params={ + "default": 0, + "null": False, + "primary_key": False, + "unique": False, + "index": False, + "index_method": IndexMethod.btree, + "choices": None, + "db_column_name": None, + "secret": False, + }, + schema=None, + ) + + manager.add_column( + table_class_name="SpamLog", + tablename="spam_log", + column_name="user_id", + db_column_name="user_id", + column_class_name="Integer", + column_class=Integer, + params={ + "default": 0, + "null": False, + "primary_key": False, + "unique": False, + "index": False, + "index_method": IndexMethod.btree, + "choices": None, + "db_column_name": None, + "secret": False, + }, + schema=None, + ) + + manager.add_column( + table_class_name="SpamLog", + tablename="spam_log", + column_name="message_text", + db_column_name="message_text", + column_class_name="Text", + column_class=Text, + params={ + "default": "", + "null": True, + "primary_key": False, + "unique": False, + "index": False, + "index_method": IndexMethod.btree, + "choices": None, + "db_column_name": None, + "secret": False, + }, + schema=None, + ) + + manager.add_column( + table_class_name="SpamLog", + tablename="spam_log", + column_name="attachments", + db_column_name="attachments", + column_class_name="JSON", + column_class=JSON, + params={ + "default": "{}", + "null": False, + "primary_key": False, + "unique": False, + "index": False, + "index_method": IndexMethod.btree, + "choices": None, + "db_column_name": None, + "secret": False, + }, + schema=None, + ) + + manager.add_column( + table_class_name="SpamLog", + tablename="spam_log", + column_name="created_at", + db_column_name="created_at", + column_class_name="Date", + column_class=Date, + params={ + "default": DateNow(), + "null": False, + "primary_key": False, + "unique": False, + "index": False, + "index_method": IndexMethod.btree, + "choices": None, + "db_column_name": None, + "secret": False, + }, + schema=None, + ) + + return manager diff --git a/src/karkas_blocks/karkas_blocks/standard/spam/db/tables.py b/src/karkas_blocks/karkas_blocks/standard/spam/db/tables.py new file mode 100644 index 0000000..735e015 --- /dev/null +++ b/src/karkas_blocks/karkas_blocks/standard/spam/db/tables.py @@ -0,0 +1,10 @@ +from piccolo.columns import JSON, Date, Integer, Text +from piccolo.table import Table + + +class SpamLog(Table): + chat_id = Integer() + user_id = Integer() + message_text = Text(null=True) + attachments = JSON() + created_at = Date() diff --git a/src/karkas_blocks/karkas_blocks/standard/spam/info.json b/src/karkas_blocks/karkas_blocks/standard/spam/info.json new file mode 100644 index 0000000..697c659 --- /dev/null +++ b/src/karkas_blocks/karkas_blocks/standard/spam/info.json @@ -0,0 +1,17 @@ +{ + "id": "standard.spam", + "name": "Spam", + "description": "Модуль для удаления спама", + "author": "Karkas Team", + "version": "1.0.0", + "privileged": true, + "dependencies": { + "required": { + "standard.config": "^1.0.0" + }, + "optional": { + "standard.command_helper": "^1.0.0", + "standard.filters": "^1.0.0" + } + } + } diff --git a/src/karkas_blocks/karkas_blocks/standard/spam/main.py b/src/karkas_blocks/karkas_blocks/standard/spam/main.py new file mode 100644 index 0000000..f91b276 --- /dev/null +++ b/src/karkas_blocks/karkas_blocks/standard/spam/main.py @@ -0,0 +1,81 @@ +from typing import TYPE_CHECKING + +from aiogram import Bot, Router +from aiogram.filters import Command + +from karkas_core.modules_system.public_api import get_module, register_router + +from .db.tables import SpamLog + +try: + register_command = get_module("standard.command_helper", "register_command") + COMMAND_HELPER_MODULE_LOADED = True +except Exception: + COMMAND_HELPER_MODULE_LOADED = False + pass + +if TYPE_CHECKING: + from aiogram.types import Message + + +async def spam(message: "Message", bot: "Bot"): + if not message.reply_to_message: + return + + if message.reply_to_message.from_user.id in (message.from_user.id, bot.id): + return + + spam_message = message.reply_to_message + chat_id = message.chat.id + + message.reply_to_message.media_group_id + + attachments = { + "version": 1, + "photo": ( + [size.model_dump() for size in spam_message.photo] + if spam_message.photo + else [] + ), + "gif": spam_message.animation.model_dump() if spam_message.animation else [], + } + + spam_log = SpamLog( + chat_id=chat_id, + user_id=spam_message.from_user.id, + message_text=spam_message.text, + attachments=attachments, + ) + + await bot.delete_message( + chat_id=chat_id, + message_id=spam_message.message_id, + ) + + await bot.delete_message( + chat_id=chat_id, + message_id=message.message_id, + ) + + await bot.ban_chat_member( + chat_id=chat_id, + user_id=spam_message.from_user.id, + ) + + await SpamLog.insert(spam_log) + + +def module_init(): + register_app_config = get_module("standard.database", "register_app_config") + from .db import APP_CONFIG + + register_app_config(APP_CONFIG) + + router = Router() + + router.message.register(spam, Command("spam")) + + register_router(router) + + if COMMAND_HELPER_MODULE_LOADED: + register_command("spam", "Спам", role="ADMIN")