feat: add spam block

This commit is contained in:
Maxim Slipenko 2024-10-03 18:16:38 +03:00
parent a6c0433569
commit 7724a60f8c
No known key found for this signature in database
GPG Key ID: 6DEA9FA7DE98C7D6
7 changed files with 250 additions and 0 deletions

View File

@ -0,0 +1 @@
from .main import module_init

View File

@ -0,0 +1 @@
from .piccolo_app import APP_CONFIG

View File

@ -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=[],
)

View File

@ -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

View File

@ -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()

View File

@ -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"
}
}
}

View File

@ -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")