🐛 Fix client ask and listen check

This commit is contained in:
xtaodada 2024-08-13 18:56:42 +08:00
parent 299c8db5dd
commit f75fb07f44
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
2 changed files with 66 additions and 35 deletions

28
.git-blame-ignore-revs Normal file
View File

@ -0,0 +1,28 @@
# This file contains a list of commits with
# mass changes for exclusion by `git blame`.
#
# Passing `--ignore-revs-file .git-blame-ignore-revs` as a flag will
# tell git to "ignore changes made by the revision when assigning
# blame, as if the change never happened".
#
# For example:
# git blame --ignore-revs-file .git-blame-ignore-revs ...
#
# For extra hot sauce, ignore white space changes too:
# git blame -w --ignore-revs-file .git-blame-ignore-revs ...
#
# You can make this a default for your repo using:
# git config blame.ignoreRevsFile .git-blame-ignore-revs
#
# Also, if you use the GitLens extension for Visual Studio Code, you
# can add "-w" as a value to "gitlens.advanced.blame.customArguments".
#
# Note that `git blame` does not use any file by default, and
# the filename `.git-blame-ignore-revs` is just a convention.
299c8db5dd9b59cfe450cb7dc425205406acf015
# Author: xtaodada <xtao@xtaolink.cn>
# Date: Tue Aug 13 18:38:13 2024 +0800
#
# :lipstick: Format with black 24.x.x
#

View File

@ -38,6 +38,8 @@ from ..utils.conversation import Conversation
from ..utils.errors import TimeoutConversationError, ListenerCanceled from ..utils.errors import TimeoutConversationError, ListenerCanceled
pyrogram.errors.ListenerCanceled = ListenerCanceled # noqa pyrogram.errors.ListenerCanceled = ListenerCanceled # noqa
LOCK = asyncio.Lock()
DONE = []
@patch(pyrogram.client.Client) @patch(pyrogram.client.Client)
@ -108,35 +110,51 @@ class Client:
@patch(pyrogram.handlers.message_handler.MessageHandler) @patch(pyrogram.handlers.message_handler.MessageHandler)
class MessageHandler: class MessageHandler(pyrogram.handlers.message_handler.MessageHandler):
@patchable @patchable
def __init__(self, callback: callable, filters=None): def __init__(self, callback: callable, filters=None):
self.user_callback = callback self.user_callback = callback
self.old__init__(self.resolve_listener, filters) self.old__init__(self.resolve_listener, filters)
@patchable @staticmethod
async def resolve_listener(self, client, message, *args): async def resolve_listener_(self, client, message, *args):
global LOCK, DONE
async with LOCK:
listener = client.listening.get(message.chat.id) listener = client.listening.get(message.chat.id)
if listener:
with contextlib.suppress(ValueError):
DONE.remove(listener)
if listener and not listener["future"].done(): if listener and not listener["future"].done():
listener["future"].set_result(message) listener["future"].set_result(message)
else: return
if listener and listener["future"].done(): if listener and listener["future"].done():
client.clear_listener(message.chat.id, listener["future"]) client.clear_listener(message.chat.id, listener["future"])
await self.user_callback(client, message, *args) await self.user_callback(client, message, *args)
@patchable @patchable
async def check(self, client, update): async def resolve_listener(self, client, message, *args):
await MessageHandler.resolve_listener_(self, client, message, *args)
@staticmethod
async def check_(self, client, update):
global LOCK, DONE
async with LOCK:
listener = client.listening.get(update.chat.id) listener = client.listening.get(update.chat.id)
if listener and (listener not in DONE) and (not listener["future"].done()):
if listener and not listener["future"].done(): if callable(listener["filters"]):
return ( result = await listener["filters"](client, update)
await listener["filters"](client, update) if result:
if callable(listener["filters"]) DONE.append(listener)
else True return result
) else:
DONE.append(listener)
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
@patchable
async def check(self, client, update):
return await MessageHandler.check_(self, client, update)
@patch(pyrogram.handlers.edited_message_handler.EditedMessageHandler) @patch(pyrogram.handlers.edited_message_handler.EditedMessageHandler)
class EditedMessageHandler: class EditedMessageHandler:
@ -147,26 +165,11 @@ class EditedMessageHandler:
@patchable @patchable
async def resolve_listener(self, client, message, *args): async def resolve_listener(self, client, message, *args):
listener = client.listening.get(message.chat.id) await MessageHandler.resolve_listener_(self, client, message, *args)
if listener and not listener["future"].done():
listener["future"].set_result(message)
else:
if listener and listener["future"].done():
client.clear_listener(message.chat.id, listener["future"])
await self.user_callback(client, message, *args)
@patchable @patchable
async def check(self, client, update): async def check(self, client, update):
listener = client.listening.get(update.chat.id) return await MessageHandler.check_(self, client, update)
if listener and not listener["future"].done():
return (
await listener["filters"](client, update)
if callable(listener["filters"])
else True
)
return await self.filters(client, update) if callable(self.filters) else True
@patch(pyrogram.types.user_and_chats.chat.Chat) @patch(pyrogram.types.user_and_chats.chat.Chat)