feat: remove fail user service message

This commit is contained in:
xtaodada 2023-10-18 23:50:27 +08:00
parent b67926c83a
commit ba68b86dbe
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
3 changed files with 62 additions and 0 deletions

View File

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

31
plugins/service.py Normal file
View File

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

View File

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