From 05b3be1e885c21f2dfd920e9cafa49bc9c6ab303 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 8 Sep 2018 19:33:47 +0200 Subject: [PATCH 1/7] Info log DC number on connection --- pyrogram/connection/connection.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pyrogram/connection/connection.py b/pyrogram/connection/connection.py index b9d4cc87..e10011d1 100644 --- a/pyrogram/connection/connection.py +++ b/pyrogram/connection/connection.py @@ -38,6 +38,7 @@ class Connection: } def __init__(self, dc_id: int, test_mode: bool, ipv6: bool, proxy: dict, mode: int = 1): + self.dc_id = dc_id self.ipv6 = ipv6 self.proxy = proxy self.address = DataCenter(dc_id, test_mode, ipv6) @@ -58,7 +59,8 @@ class Connection: self.connection.close() time.sleep(1) else: - log.info("Connected! IPv{} - {}".format( + log.info("Connected! DC{} - IPv{} - {}".format( + self.dc_id, "6" if self.ipv6 else "4", self.mode.__name__ )) From b893698f1e8bbd04d44dfb4fd6d2a01e98c60908 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 14 Sep 2018 14:37:04 +0200 Subject: [PATCH 2/7] Add ability to add/remove users from the user filter. Use .users to access the inner set of users --- pyrogram/client/filters/filters.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/pyrogram/client/filters/filters.py b/pyrogram/client/filters/filters.py index 4cd4b191..26ef8219 100644 --- a/pyrogram/client/filters/filters.py +++ b/pyrogram/client/filters/filters.py @@ -237,23 +237,27 @@ class Filters: return create("Regex", f, p=re.compile(pattern, flags)) @staticmethod - def user(user: int or str or list): - """Filter messages coming from specific users. + def user(users: int or str or list = None): + """Filter messages coming from one or more specific users. Args: - user (``int`` | ``str`` | ``list``): - The user or list of user IDs (int) or usernames (str) the filter should look for. + users (``int`` | ``str`` | ``list``): + Pass one or more user ids/usernames to filter the users. + The argument passed will be stored as a python set in the *.users* field of the filter instance. + To add or remove users dynamically, simply manipulate the inner set. + Defaults to None (empty set). """ return create( "User", lambda _, m: bool(m.from_user - and (m.from_user.id in _.u + and (m.from_user.id in _.users or (m.from_user.username - and m.from_user.username.lower() in _.u))), - u=( - {user.lower().strip("@") if type(user) is str else user} - if not isinstance(user, list) - else {i.lower().strip("@") if type(i) is str else i for i in user} + and m.from_user.username.lower() in _.users))), + users=( + set() if users is None + else {users.lower().strip("@") if type(users) is str else users} + if not isinstance(users, list) + else {i.lower().strip("@") if type(i) is str else i for i in users} ) ) From 4e293f23a92de671a39b801f02256ec9dca04799 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 14 Sep 2018 15:28:08 +0200 Subject: [PATCH 3/7] Make handlers test whether filters are callable and not if they exist --- pyrogram/client/handlers/callback_query_handler.py | 2 +- pyrogram/client/handlers/deleted_messages_handler.py | 2 +- pyrogram/client/handlers/message_handler.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pyrogram/client/handlers/callback_query_handler.py b/pyrogram/client/handlers/callback_query_handler.py index c5346519..5d09f7d9 100644 --- a/pyrogram/client/handlers/callback_query_handler.py +++ b/pyrogram/client/handlers/callback_query_handler.py @@ -49,6 +49,6 @@ class CallbackQueryHandler(Handler): def check(self, callback_query): return ( self.filters(callback_query) - if self.filters + if callable(self.filters) else True ) diff --git a/pyrogram/client/handlers/deleted_messages_handler.py b/pyrogram/client/handlers/deleted_messages_handler.py index 55d5715f..8f5ef448 100644 --- a/pyrogram/client/handlers/deleted_messages_handler.py +++ b/pyrogram/client/handlers/deleted_messages_handler.py @@ -50,6 +50,6 @@ class DeletedMessagesHandler(Handler): def check(self, messages): return ( self.filters(messages.messages[0]) - if self.filters + if callable(self.filters) else True ) diff --git a/pyrogram/client/handlers/message_handler.py b/pyrogram/client/handlers/message_handler.py index 1b4770b3..e4c3d13f 100644 --- a/pyrogram/client/handlers/message_handler.py +++ b/pyrogram/client/handlers/message_handler.py @@ -50,6 +50,6 @@ class MessageHandler(Handler): def check(self, message): return ( self.filters(message) - if self.filters + if callable(self.filters) else True ) From 31578ddb33cdd29e13a3bee3320d9053be9f2fbf Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 14 Sep 2018 15:29:36 +0200 Subject: [PATCH 4/7] Give Filters.user superpowers It can now add and remove users at runtime --- pyrogram/client/filters/filters.py | 39 ++++++++++++++++-------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/pyrogram/client/filters/filters.py b/pyrogram/client/filters/filters.py index 26ef8219..7636f382 100644 --- a/pyrogram/client/filters/filters.py +++ b/pyrogram/client/filters/filters.py @@ -236,30 +236,33 @@ class Filters: return create("Regex", f, p=re.compile(pattern, flags)) - @staticmethod - def user(users: int or str or list = None): - """Filter messages coming from one or more specific users. + class user(Filter, set): + """Filter messages coming from one or more users. + + You can use `set bound methods `_ to manipulate the + users container. Args: users (``int`` | ``str`` | ``list``): Pass one or more user ids/usernames to filter the users. - The argument passed will be stored as a python set in the *.users* field of the filter instance. - To add or remove users dynamically, simply manipulate the inner set. - Defaults to None (empty set). + Defaults to None (no users). """ - return create( - "User", - lambda _, m: bool(m.from_user - and (m.from_user.id in _.users - or (m.from_user.username - and m.from_user.username.lower() in _.users))), - users=( - set() if users is None - else {users.lower().strip("@") if type(users) is str else users} - if not isinstance(users, list) - else {i.lower().strip("@") if type(i) is str else i for i in users} + + def __init__(self, users: int or str or list = None): + users = [] if users is None else users if type(users) is list else [users] + super().__init__( + {i.lower().strip("@") if type(i) is str else i for i in users} + if type(users) is list else + {users.lower().strip("@") if type(users) is str else users} + ) + + def __call__(self, message): + return bool( + message.from_user + and (message.from_user.id in self + or (message.from_user.username + and message.from_user.username.lower() in self)) ) - ) @staticmethod def chat(chat: int or str or list): From 339630dafb9c0acfe32f3a028fbf47be6c68173a Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 14 Sep 2018 15:29:56 +0200 Subject: [PATCH 5/7] Add noinspection PyPep8Naming for Filters.user --- pyrogram/client/filters/filters.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pyrogram/client/filters/filters.py b/pyrogram/client/filters/filters.py index 7636f382..e218155a 100644 --- a/pyrogram/client/filters/filters.py +++ b/pyrogram/client/filters/filters.py @@ -236,6 +236,7 @@ class Filters: return create("Regex", f, p=re.compile(pattern, flags)) + # noinspection PyPep8Naming class user(Filter, set): """Filter messages coming from one or more users. From 3307b410b4dd2b0fdcebe2d740985a52d90fcc68 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 14 Sep 2018 15:33:32 +0200 Subject: [PATCH 6/7] Give superpowers to Filters.chat too It can now add and remove chats at runtime --- pyrogram/client/filters/filters.py | 40 ++++++++++++++++++------------ 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/pyrogram/client/filters/filters.py b/pyrogram/client/filters/filters.py index e218155a..a573c875 100644 --- a/pyrogram/client/filters/filters.py +++ b/pyrogram/client/filters/filters.py @@ -265,26 +265,34 @@ class Filters: and message.from_user.username.lower() in self)) ) - @staticmethod - def chat(chat: int or str or list): - """Filter messages coming from specific chats. + # noinspection PyPep8Naming + class chat(Filter, set): + """Filter messages coming from one or more chats. + + You can use `set bound methods `_ to manipulate the + chats container. Args: - chat (``int`` | ``str`` | ``list``): - The chat or list of chat IDs (int) or usernames (str) the filter should look for. + chats (``int`` | ``str`` | ``list``): + Pass one or more chat ids/usernames to filter the chats. + Defaults to None (no chats). """ - return create( - "Chat", - lambda _, m: bool(m.chat - and (m.chat.id in _.c - or (m.chat.username - and m.chat.username.lower() in _.c))), - c=( - {chat.lower().strip("@") if type(chat) is str else chat} - if not isinstance(chat, list) - else {i.lower().strip("@") if type(i) is str else i for i in chat} + + def __init__(self, chats: int or str or list = None): + chats = [] if chats is None else chats if type(chats) is list else [chats] + super().__init__( + {i.lower().strip("@") if type(i) is str else i for i in chats} + if type(chats) is list else + {chats.lower().strip("@") if type(chats) is str else chats} + ) + + def __call__(self, message): + return bool( + message.chat + and (message.chat.id in self + or (message.chat.username + and message.chat.username.lower() in self)) ) - ) service = create( "Service", From edfdf9d143279aec29e7034a67163d6a956cd972 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 14 Sep 2018 15:34:00 +0200 Subject: [PATCH 7/7] Small docstring fixes --- pyrogram/client/filters/filters.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyrogram/client/filters/filters.py b/pyrogram/client/filters/filters.py index a573c875..79ab0f5f 100644 --- a/pyrogram/client/filters/filters.py +++ b/pyrogram/client/filters/filters.py @@ -245,7 +245,7 @@ class Filters: Args: users (``int`` | ``str`` | ``list``): - Pass one or more user ids/usernames to filter the users. + Pass one or more user ids/usernames to filter users. Defaults to None (no users). """ @@ -274,7 +274,7 @@ class Filters: Args: chats (``int`` | ``str`` | ``list``): - Pass one or more chat ids/usernames to filter the chats. + Pass one or more chat ids/usernames to filter chats. Defaults to None (no chats). """