From ba68b86dbef1e604a1f210660a70bb7d48bfce8a Mon Sep 17 00:00:00 2001 From: xtaodada Date: Wed, 18 Oct 2023 23:50:27 +0800 Subject: [PATCH] feat: remove fail user service message --- plugins/chat_member_update.py | 3 +++ plugins/service.py | 31 +++++++++++++++++++++++++++++++ sticker/service_message.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 plugins/service.py create mode 100644 sticker/service_message.py diff --git a/plugins/chat_member_update.py b/plugins/chat_member_update.py index 80ad730..5e3ff49 100644 --- a/plugins/chat_member_update.py +++ b/plugins/chat_member_update.py @@ -8,6 +8,7 @@ from pyrogram.types import ChatMemberUpdated from pyromod.utils.errors import TimeoutConversationError from sticker.scheduler import add_delete_message_job +from sticker.service_message import ServiceMessage from sticker.single_utils import Client from sticker import bot, log @@ -90,6 +91,7 @@ async def invite(client: Client, chat_member_updated: ChatMemberUpdated): ) with contextlib.suppress(Exception): await log(chat, user, "FAIL_ERROR") + await ServiceMessage.try_delete(user.id, chat.id) else: with contextlib.suppress(Exception): await log(chat, user, "ACCEPT") @@ -104,3 +106,4 @@ async def invite(client: Client, chat_member_updated: ChatMemberUpdated): ) with contextlib.suppress(Exception): await log(chat, user, "FAIL_TIMEOUT") + await ServiceMessage.try_delete(user.id, chat.id) diff --git a/plugins/service.py b/plugins/service.py new file mode 100644 index 0000000..f544840 --- /dev/null +++ b/plugins/service.py @@ -0,0 +1,31 @@ +from pyrogram import filters +from pyrogram.enums import MessageServiceType + +from sticker import bot +from sticker.service_message import ServiceMessage +from sticker.single_utils import Client, Message + + +@bot.on_message(filters.service) +async def service_message_handle(_: Client, message: Message): + if message.service != MessageServiceType.NEW_CHAT_MEMBERS: + return + cid = message.chat.id + if message.new_chat_members: + for i in message.new_chat_members: + if i.is_self: + return + user = ( + message.new_chat_members[0] if message.new_chat_members else message.from_user + ) + if ( + user.is_self + or user.is_verified + or user.is_bot + or user.is_deleted + or user.is_support + ): + return + uid = user.id + mid = message.id + await ServiceMessage.set_cache(uid, cid, mid) diff --git a/sticker/service_message.py b/sticker/service_message.py new file mode 100644 index 0000000..a2eb422 --- /dev/null +++ b/sticker/service_message.py @@ -0,0 +1,28 @@ +import contextlib +from typing import List + +from cashews import cache + +from sticker import bot + + +class ServiceMessage: + @staticmethod + async def set_cache(uid: int, cid: int, mid: int): + old = await ServiceMessage.get_cache(uid, cid) + old.append(mid) + await cache.set(f"service_message:{uid}:{cid}", old, expire=600) + + @staticmethod + async def get_cache(uid: int, cid: int) -> List[int]: + data = await cache.get(f"service_message:{uid}:{cid}") + if data: + return data + return [] + + @staticmethod + async def try_delete(uid: int, cid: int): + mid = await ServiceMessage.get_cache(uid, cid) + if mid: + with contextlib.suppress(Exception): + await bot.delete_messages(cid, list(mid))