mirror of
https://gitflic.ru/project/alt-gnome/karkas.git
synced 2024-12-23 16:23:02 +03:00
feat: add spam block
This commit is contained in:
parent
a6c0433569
commit
7724a60f8c
@ -0,0 +1 @@
|
|||||||
|
from .main import module_init
|
@ -0,0 +1 @@
|
|||||||
|
from .piccolo_app import APP_CONFIG
|
@ -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=[],
|
||||||
|
)
|
@ -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
|
10
src/karkas_blocks/karkas_blocks/standard/spam/db/tables.py
Normal file
10
src/karkas_blocks/karkas_blocks/standard/spam/db/tables.py
Normal 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()
|
17
src/karkas_blocks/karkas_blocks/standard/spam/info.json
Normal file
17
src/karkas_blocks/karkas_blocks/standard/spam/info.json
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
81
src/karkas_blocks/karkas_blocks/standard/spam/main.py
Normal file
81
src/karkas_blocks/karkas_blocks/standard/spam/main.py
Normal 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")
|
Loading…
Reference in New Issue
Block a user