mirror of
https://gitflic.ru/project/alt-gnome/karkas.git
synced 2025-10-08 21:53:15 +03:00
feat: add karkas_lite
This commit is contained in:
@@ -5,18 +5,22 @@ from karkas_core.modules_system.public_api import set_my_commands
|
||||
commands = dict()
|
||||
|
||||
|
||||
def register_command(command, description, role="USER"):
|
||||
def register_command(command, description, long_description=None, role="USER"):
|
||||
if long_description is None:
|
||||
long_description = description
|
||||
|
||||
if role not in commands:
|
||||
commands[role] = dict()
|
||||
commands[role][command] = {
|
||||
"description": description,
|
||||
"long_description": long_description,
|
||||
}
|
||||
|
||||
|
||||
async def set_user_commands():
|
||||
async def set_commands(role="USER"):
|
||||
bot_commands = []
|
||||
if "USER" in commands:
|
||||
user_commands = commands["USER"]
|
||||
if role in commands:
|
||||
user_commands = commands[role]
|
||||
for command in user_commands:
|
||||
bot_commands.append(
|
||||
BotCommand(
|
||||
@@ -25,18 +29,24 @@ async def set_user_commands():
|
||||
)
|
||||
)
|
||||
|
||||
# log(bot_commands)
|
||||
|
||||
await set_my_commands(
|
||||
bot_commands,
|
||||
)
|
||||
|
||||
|
||||
def get_user_commands():
|
||||
if "USER" in commands:
|
||||
return commands["USER"].copy()
|
||||
def get_commands(role="USER"):
|
||||
if role in commands:
|
||||
return commands[role].copy()
|
||||
return {}
|
||||
|
||||
|
||||
async def set_user_commands():
|
||||
await set_commands("USER")
|
||||
|
||||
|
||||
def get_user_commands():
|
||||
return get_commands("USER")
|
||||
|
||||
|
||||
async def module_late_init():
|
||||
await set_user_commands()
|
||||
|
@@ -2,6 +2,7 @@ from .filters import (
|
||||
ChatIDFilter,
|
||||
ChatModerOrAdminFilter,
|
||||
ChatNotInApproveFilter,
|
||||
SimpleAdminFilter,
|
||||
chat_not_in_approve,
|
||||
module_init,
|
||||
)
|
||||
|
@@ -3,6 +3,7 @@ from typing import TYPE_CHECKING, Any, Awaitable, Callable, Dict
|
||||
from aiogram import BaseMiddleware, Bot
|
||||
from aiogram.filters import BaseFilter
|
||||
from aiogram.types import Message, TelegramObject
|
||||
from aiogram.utils.chat_member import ADMINS
|
||||
from typing_extensions import deprecated
|
||||
|
||||
from karkas_core.modules_system.public_api import (
|
||||
@@ -121,3 +122,9 @@ class ChatModerOrAdminFilter(BaseFilter):
|
||||
or await roles.check_moderator_permission(user_id)
|
||||
or any(user_id == admin.user.id for admin in admins)
|
||||
)
|
||||
|
||||
|
||||
class SimpleAdminFilter(BaseFilter):
|
||||
async def __call__(self, message: Message, bot: Bot) -> bool:
|
||||
member = await bot.get_chat_member(message.chat.id, message.from_user.id)
|
||||
return isinstance(member, ADMINS)
|
||||
|
@@ -42,7 +42,7 @@ FOOTER = """===============
|
||||
def format_commands(commands_dict):
|
||||
formatted_commands = []
|
||||
for command, details in commands_dict.items():
|
||||
formatted_commands.append(f"/{command} - {details['description']}")
|
||||
formatted_commands.append(f"/{command} - {details['long_description']}")
|
||||
return "\n".join(formatted_commands)
|
||||
|
||||
|
||||
|
@@ -0,0 +1,29 @@
|
||||
from piccolo.apps.migrations.auto.migration_manager import MigrationManager
|
||||
|
||||
ID = "2024-10-03T19:24:33:522177"
|
||||
VERSION = "1.16.0"
|
||||
DESCRIPTION = ""
|
||||
|
||||
|
||||
async def forwards():
|
||||
manager = MigrationManager(
|
||||
migration_id=ID, app_name="standard.spam", description=DESCRIPTION
|
||||
)
|
||||
|
||||
manager.drop_column(
|
||||
table_class_name="SpamLog",
|
||||
tablename="spam_log",
|
||||
column_name="chat_id",
|
||||
db_column_name="chat_id",
|
||||
schema=None,
|
||||
)
|
||||
|
||||
manager.drop_column(
|
||||
table_class_name="SpamLog",
|
||||
tablename="spam_log",
|
||||
column_name="user_id",
|
||||
db_column_name="user_id",
|
||||
schema=None,
|
||||
)
|
||||
|
||||
return manager
|
@@ -1,10 +1,8 @@
|
||||
from piccolo.columns import JSON, Date, Integer, Text
|
||||
from piccolo.columns import JSON, Date, 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()
|
||||
|
@@ -1,4 +1,4 @@
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import TYPE_CHECKING, Type
|
||||
|
||||
from aiogram import Bot, Router
|
||||
from aiogram.filters import Command
|
||||
@@ -17,6 +17,8 @@ except Exception:
|
||||
if TYPE_CHECKING:
|
||||
from aiogram.types import Message
|
||||
|
||||
from karkas_blocks.standard.filters import SimpleAdminFilter as ISimpleAdminFilter
|
||||
|
||||
|
||||
async def spam(message: "Message", bot: "Bot"):
|
||||
if not message.reply_to_message:
|
||||
@@ -41,8 +43,6 @@ async def spam(message: "Message", bot: "Bot"):
|
||||
}
|
||||
|
||||
spam_log = SpamLog(
|
||||
chat_id=chat_id,
|
||||
user_id=spam_message.from_user.id,
|
||||
message_text=spam_message.text,
|
||||
attachments=attachments,
|
||||
)
|
||||
@@ -67,15 +67,23 @@ async def spam(message: "Message", bot: "Bot"):
|
||||
|
||||
def module_init():
|
||||
register_app_config = get_module("standard.database", "register_app_config")
|
||||
SimpleAdminFilter: "Type[ISimpleAdminFilter]" = get_module(
|
||||
"standard.filters", "SimpleAdminFilter"
|
||||
)
|
||||
from .db import APP_CONFIG
|
||||
|
||||
register_app_config(APP_CONFIG)
|
||||
|
||||
router = Router()
|
||||
|
||||
router.message.register(spam, Command("spam"))
|
||||
router.message.register(spam, SimpleAdminFilter(), Command("spam"))
|
||||
|
||||
register_router(router)
|
||||
|
||||
if COMMAND_HELPER_MODULE_LOADED:
|
||||
register_command("spam", "Спам", role="ADMIN")
|
||||
register_command(
|
||||
"spam",
|
||||
"Удалить спам и забанить пользователя",
|
||||
long_description="Удалить спам и забанить пользователя. "
|
||||
"Собирает обезличенные данные для дальнейшего создания алгоритма",
|
||||
)
|
||||
|
Reference in New Issue
Block a user