From 18a12f3242cfe338e86bc264a8f19a93f8caecd6 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Sun, 17 Nov 2024 21:14:22 +0800 Subject: [PATCH] fix: delete service message --- plugins/chat_member_update.py | 5 ++--- plugins/service.py | 5 +---- pyromod/listen/listen.py | 18 +++++++++++------- sticker/__init__.py | 2 +- sticker/functions/service_message.py | 6 ++---- sticker/scheduler.py | 7 +++++-- 6 files changed, 22 insertions(+), 21 deletions(-) diff --git a/plugins/chat_member_update.py b/plugins/chat_member_update.py index 224678e..51ca745 100644 --- a/plugins/chat_member_update.py +++ b/plugins/chat_member_update.py @@ -1,6 +1,5 @@ import asyncio import contextlib -from asyncio import sleep from time import time from typing import Dict @@ -62,7 +61,7 @@ async def start_verify(client: "Client", chat, user): ) msg = await cache.get(key2) if msg: - add_delete_message_id_job(chat.id, msg) + add_delete_message_id_job(chat.id, msg, 1) await msg_.delay_delete(1) if not msg_.sticker: add_ban_chat_member_job(chat.id, user.id) @@ -74,7 +73,7 @@ async def start_verify(client: "Client", chat, user): except TimeoutConversationError: msg = await cache.get(key2) if msg: - add_delete_message_id_job(chat.id, msg) + add_delete_message_id_job(chat.id, msg, 1) add_ban_chat_member_job(chat.id, user.id) await log(chat, user, LogAction.FAIL_TIMEOUT) await ServiceMessage.try_delete(user.id, chat.id) diff --git a/plugins/service.py b/plugins/service.py index 27e497a..4f39123 100644 --- a/plugins/service.py +++ b/plugins/service.py @@ -1,15 +1,12 @@ from pyrogram import filters -from pyrogram.enums import MessageServiceType from sticker import bot from sticker.functions.service_message import ServiceMessage from sticker.single_utils import Client, Message -@bot.on_message(filters.service) +@bot.on_message(filters.new_chat_members, group=2) 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: diff --git a/pyromod/listen/listen.py b/pyromod/listen/listen.py index 46e3049..a280b18 100644 --- a/pyromod/listen/listen.py +++ b/pyromod/listen/listen.py @@ -31,6 +31,7 @@ from ..utils.errors import ListenerCanceled, TimeoutConversationError pyrogram.errors.ListenerCanceled = ListenerCanceled LOCK = asyncio.Lock() DONE = [] +DONE_MAP = {} @patch(pyrogram.client.Client) @@ -96,14 +97,15 @@ class MessageHandler: global LOCK, DONE async with LOCK: listener = client.listening.get(message.chat.id) - if listener: + if listener and DONE_MAP.get(id(self)) == id(listener): with contextlib.suppress(ValueError): DONE.remove(listener) - if listener and not listener["future"].done(): - listener["future"].set_result(message) - return - if listener and listener["future"].done(): - client.clear_listener(message.chat.id, listener["future"]) + del DONE_MAP[id(self)] + if not listener["future"].done(): + listener["future"].set_result(message) + return + if listener["future"].done(): + client.clear_listener(message.chat.id, listener["future"]) await self.user_callback(client, message, *args) @patchable @@ -116,9 +118,11 @@ class MessageHandler: result = await listener["filters"](client, update) if result: DONE.append(listener) - return result + DONE_MAP[id(self)] = id(listener) + return True else: DONE.append(listener) + DONE_MAP[id(self)] = id(listener) return True return await self.filters(client, update) if callable(self.filters) else True diff --git a/sticker/__init__.py b/sticker/__init__.py index 51f9022..04c3af9 100644 --- a/sticker/__init__.py +++ b/sticker/__init__.py @@ -21,7 +21,7 @@ root_logger = getLogger() root_logger.setLevel(DEBUG if Config.DEBUG else CRITICAL) root_logger.addHandler(logging_handler) pyro_logger = getLogger("pyrogram") -pyro_logger.setLevel(CRITICAL) +pyro_logger.setLevel(INFO if Config.DEBUG else CRITICAL) pyro_logger.addHandler(logging_handler) basicConfig(level=DEBUG if Config.DEBUG else INFO) logs.setLevel(DEBUG if Config.DEBUG else INFO) diff --git a/sticker/functions/service_message.py b/sticker/functions/service_message.py index f16284f..3a11f25 100644 --- a/sticker/functions/service_message.py +++ b/sticker/functions/service_message.py @@ -1,9 +1,8 @@ -import contextlib from typing import List from cashews import cache -from sticker import bot +from sticker.scheduler import add_delete_message_id_job class ServiceMessage: @@ -22,5 +21,4 @@ class ServiceMessage: 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)) + add_delete_message_id_job(cid, list(mid), 1) diff --git a/sticker/scheduler.py b/sticker/scheduler.py index d7f34be..31fe3bf 100644 --- a/sticker/scheduler.py +++ b/sticker/scheduler.py @@ -1,5 +1,6 @@ import contextlib import datetime +from typing import Union, List import pytz from apscheduler.schedulers.asyncio import AsyncIOScheduler @@ -18,7 +19,7 @@ async def delete_message(message: Message) -> bool: return False -async def delete_message_id(chat_id: int, message_id: int) -> bool: +async def delete_message_id(chat_id: int, message_id: Union[int, List[int]]) -> bool: with contextlib.suppress(Exception): from sticker.bot import bot @@ -45,7 +46,9 @@ async def ban_chat_member(chat_id: int, user_id: int): return False -def add_delete_message_id_job(chat_id: int, message_id: int, delete_seconds: int = 60): +def add_delete_message_id_job( + chat_id: int, message_id: Union[int, List[int]], delete_seconds: int = 60 +): scheduler.add_job( delete_message_id, "date",