diff --git a/pyrogram/client/filters/filters.py b/pyrogram/client/filters/filters.py index 4cd4b191..79ab0f5f 100644 --- a/pyrogram/client/filters/filters.py +++ b/pyrogram/client/filters/filters.py @@ -236,47 +236,63 @@ 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. + # noinspection PyPep8Naming + class user(Filter, set): + """Filter messages coming from one or more users. + + You can use `set bound methods `_ to manipulate the + users container. 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 users. + Defaults to None (no users). """ - return create( - "User", - lambda _, m: bool(m.from_user - and (m.from_user.id in _.u - 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} - ) - ) - @staticmethod - def chat(chat: int or str or list): - """Filter messages coming from specific chats. + 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)) + ) + + # 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 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", 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 ) diff --git a/pyrogram/connection/connection.py b/pyrogram/connection/connection.py index b19997e3..fe8b5d5d 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 = 2): + self.dc_id = dc_id self.ipv6 = ipv6 self.proxy = proxy self.address = DataCenter(dc_id, test_mode, ipv6) @@ -57,7 +58,8 @@ class Connection: self.protocol.close() await asyncio.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__ ))