fix: delete service message

This commit is contained in:
xtaodada 2024-11-17 21:14:22 +08:00
parent 18b57480a4
commit 18a12f3242
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
6 changed files with 22 additions and 21 deletions

View File

@ -1,6 +1,5 @@
import asyncio import asyncio
import contextlib import contextlib
from asyncio import sleep
from time import time from time import time
from typing import Dict from typing import Dict
@ -62,7 +61,7 @@ async def start_verify(client: "Client", chat, user):
) )
msg = await cache.get(key2) msg = await cache.get(key2)
if msg: if msg:
add_delete_message_id_job(chat.id, msg) add_delete_message_id_job(chat.id, msg, 1)
await msg_.delay_delete(1) await msg_.delay_delete(1)
if not msg_.sticker: if not msg_.sticker:
add_ban_chat_member_job(chat.id, user.id) add_ban_chat_member_job(chat.id, user.id)
@ -74,7 +73,7 @@ async def start_verify(client: "Client", chat, user):
except TimeoutConversationError: except TimeoutConversationError:
msg = await cache.get(key2) msg = await cache.get(key2)
if msg: 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) add_ban_chat_member_job(chat.id, user.id)
await log(chat, user, LogAction.FAIL_TIMEOUT) await log(chat, user, LogAction.FAIL_TIMEOUT)
await ServiceMessage.try_delete(user.id, chat.id) await ServiceMessage.try_delete(user.id, chat.id)

View File

@ -1,15 +1,12 @@
from pyrogram import filters from pyrogram import filters
from pyrogram.enums import MessageServiceType
from sticker import bot from sticker import bot
from sticker.functions.service_message import ServiceMessage from sticker.functions.service_message import ServiceMessage
from sticker.single_utils import Client, Message 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): async def service_message_handle(_: Client, message: Message):
if message.service != MessageServiceType.NEW_CHAT_MEMBERS:
return
cid = message.chat.id cid = message.chat.id
if message.new_chat_members: if message.new_chat_members:
for i in message.new_chat_members: for i in message.new_chat_members:

View File

@ -31,6 +31,7 @@ from ..utils.errors import ListenerCanceled, TimeoutConversationError
pyrogram.errors.ListenerCanceled = ListenerCanceled pyrogram.errors.ListenerCanceled = ListenerCanceled
LOCK = asyncio.Lock() LOCK = asyncio.Lock()
DONE = [] DONE = []
DONE_MAP = {}
@patch(pyrogram.client.Client) @patch(pyrogram.client.Client)
@ -96,14 +97,15 @@ class MessageHandler:
global LOCK, DONE global LOCK, DONE
async with LOCK: async with LOCK:
listener = client.listening.get(message.chat.id) listener = client.listening.get(message.chat.id)
if listener: if listener and DONE_MAP.get(id(self)) == id(listener):
with contextlib.suppress(ValueError): with contextlib.suppress(ValueError):
DONE.remove(listener) DONE.remove(listener)
if listener and not listener["future"].done(): del DONE_MAP[id(self)]
listener["future"].set_result(message) if not listener["future"].done():
return listener["future"].set_result(message)
if listener and listener["future"].done(): return
client.clear_listener(message.chat.id, listener["future"]) if listener["future"].done():
client.clear_listener(message.chat.id, listener["future"])
await self.user_callback(client, message, *args) await self.user_callback(client, message, *args)
@patchable @patchable
@ -116,9 +118,11 @@ class MessageHandler:
result = await listener["filters"](client, update) result = await listener["filters"](client, update)
if result: if result:
DONE.append(listener) DONE.append(listener)
return result DONE_MAP[id(self)] = id(listener)
return True
else: else:
DONE.append(listener) DONE.append(listener)
DONE_MAP[id(self)] = id(listener)
return True return True
return await self.filters(client, update) if callable(self.filters) else True return await self.filters(client, update) if callable(self.filters) else True

View File

@ -21,7 +21,7 @@ root_logger = getLogger()
root_logger.setLevel(DEBUG if Config.DEBUG else CRITICAL) root_logger.setLevel(DEBUG if Config.DEBUG else CRITICAL)
root_logger.addHandler(logging_handler) root_logger.addHandler(logging_handler)
pyro_logger = getLogger("pyrogram") pyro_logger = getLogger("pyrogram")
pyro_logger.setLevel(CRITICAL) pyro_logger.setLevel(INFO if Config.DEBUG else CRITICAL)
pyro_logger.addHandler(logging_handler) pyro_logger.addHandler(logging_handler)
basicConfig(level=DEBUG if Config.DEBUG else INFO) basicConfig(level=DEBUG if Config.DEBUG else INFO)
logs.setLevel(DEBUG if Config.DEBUG else INFO) logs.setLevel(DEBUG if Config.DEBUG else INFO)

View File

@ -1,9 +1,8 @@
import contextlib
from typing import List from typing import List
from cashews import cache from cashews import cache
from sticker import bot from sticker.scheduler import add_delete_message_id_job
class ServiceMessage: class ServiceMessage:
@ -22,5 +21,4 @@ class ServiceMessage:
async def try_delete(uid: int, cid: int): async def try_delete(uid: int, cid: int):
mid = await ServiceMessage.get_cache(uid, cid) mid = await ServiceMessage.get_cache(uid, cid)
if mid: if mid:
with contextlib.suppress(Exception): add_delete_message_id_job(cid, list(mid), 1)
await bot.delete_messages(cid, list(mid))

View File

@ -1,5 +1,6 @@
import contextlib import contextlib
import datetime import datetime
from typing import Union, List
import pytz import pytz
from apscheduler.schedulers.asyncio import AsyncIOScheduler from apscheduler.schedulers.asyncio import AsyncIOScheduler
@ -18,7 +19,7 @@ async def delete_message(message: Message) -> bool:
return False 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): with contextlib.suppress(Exception):
from sticker.bot import bot from sticker.bot import bot
@ -45,7 +46,9 @@ async def ban_chat_member(chat_id: int, user_id: int):
return False 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( scheduler.add_job(
delete_message_id, delete_message_id,
"date", "date",