Skip to content

Commit f4a8848

Browse files
committed
Merge branch 'develop' into asyncio
# Conflicts: # pyrogram/connection/connection.py
2 parents dbd6076 + edfdf9d commit f4a8848

5 files changed

Lines changed: 54 additions & 36 deletions

File tree

pyrogram/client/filters/filters.py

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -236,47 +236,63 @@ def f(_, m):
236236

237237
return create("Regex", f, p=re.compile(pattern, flags))
238238

239-
@staticmethod
240-
def user(user: int or str or list):
241-
"""Filter messages coming from specific users.
239+
# noinspection PyPep8Naming
240+
class user(Filter, set):
241+
"""Filter messages coming from one or more users.
242+
243+
You can use `set bound methods <https://docs.python.org/3/library/stdtypes.html#set>`_ to manipulate the
244+
users container.
242245
243246
Args:
244-
user (``int`` | ``str`` | ``list``):
245-
The user or list of user IDs (int) or usernames (str) the filter should look for.
247+
users (``int`` | ``str`` | ``list``):
248+
Pass one or more user ids/usernames to filter users.
249+
Defaults to None (no users).
246250
"""
247-
return create(
248-
"User",
249-
lambda _, m: bool(m.from_user
250-
and (m.from_user.id in _.u
251-
or (m.from_user.username
252-
and m.from_user.username.lower() in _.u))),
253-
u=(
254-
{user.lower().strip("@") if type(user) is str else user}
255-
if not isinstance(user, list)
256-
else {i.lower().strip("@") if type(i) is str else i for i in user}
251+
252+
def __init__(self, users: int or str or list = None):
253+
users = [] if users is None else users if type(users) is list else [users]
254+
super().__init__(
255+
{i.lower().strip("@") if type(i) is str else i for i in users}
256+
if type(users) is list else
257+
{users.lower().strip("@") if type(users) is str else users}
257258
)
258-
)
259259

260-
@staticmethod
261-
def chat(chat: int or str or list):
262-
"""Filter messages coming from specific chats.
260+
def __call__(self, message):
261+
return bool(
262+
message.from_user
263+
and (message.from_user.id in self
264+
or (message.from_user.username
265+
and message.from_user.username.lower() in self))
266+
)
267+
268+
# noinspection PyPep8Naming
269+
class chat(Filter, set):
270+
"""Filter messages coming from one or more chats.
271+
272+
You can use `set bound methods <https://docs.python.org/3/library/stdtypes.html#set>`_ to manipulate the
273+
chats container.
263274
264275
Args:
265-
chat (``int`` | ``str`` | ``list``):
266-
The chat or list of chat IDs (int) or usernames (str) the filter should look for.
276+
chats (``int`` | ``str`` | ``list``):
277+
Pass one or more chat ids/usernames to filter chats.
278+
Defaults to None (no chats).
267279
"""
268-
return create(
269-
"Chat",
270-
lambda _, m: bool(m.chat
271-
and (m.chat.id in _.c
272-
or (m.chat.username
273-
and m.chat.username.lower() in _.c))),
274-
c=(
275-
{chat.lower().strip("@") if type(chat) is str else chat}
276-
if not isinstance(chat, list)
277-
else {i.lower().strip("@") if type(i) is str else i for i in chat}
280+
281+
def __init__(self, chats: int or str or list = None):
282+
chats = [] if chats is None else chats if type(chats) is list else [chats]
283+
super().__init__(
284+
{i.lower().strip("@") if type(i) is str else i for i in chats}
285+
if type(chats) is list else
286+
{chats.lower().strip("@") if type(chats) is str else chats}
287+
)
288+
289+
def __call__(self, message):
290+
return bool(
291+
message.chat
292+
and (message.chat.id in self
293+
or (message.chat.username
294+
and message.chat.username.lower() in self))
278295
)
279-
)
280296

281297
service = create(
282298
"Service",

pyrogram/client/handlers/callback_query_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ def __init__(self, callback: callable, filters=None):
4949
def check(self, callback_query):
5050
return (
5151
self.filters(callback_query)
52-
if self.filters
52+
if callable(self.filters)
5353
else True
5454
)

pyrogram/client/handlers/deleted_messages_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ def __init__(self, callback: callable, filters=None):
5050
def check(self, messages):
5151
return (
5252
self.filters(messages.messages[0])
53-
if self.filters
53+
if callable(self.filters)
5454
else True
5555
)

pyrogram/client/handlers/message_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ def __init__(self, callback: callable, filters=None):
5050
def check(self, message):
5151
return (
5252
self.filters(message)
53-
if self.filters
53+
if callable(self.filters)
5454
else True
5555
)

pyrogram/connection/connection.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class Connection:
3838
}
3939

4040
def __init__(self, dc_id: int, test_mode: bool, ipv6: bool, proxy: dict, mode: int = 2):
41+
self.dc_id = dc_id
4142
self.ipv6 = ipv6
4243
self.proxy = proxy
4344
self.address = DataCenter(dc_id, test_mode, ipv6)
@@ -57,7 +58,8 @@ async def connect(self):
5758
self.protocol.close()
5859
await asyncio.sleep(1)
5960
else:
60-
log.info("Connected! IPv{} - {}".format(
61+
log.info("Connected! DC{} - IPv{} - {}".format(
62+
self.dc_id,
6163
"6" if self.ipv6 else "4",
6264
self.mode.__name__
6365
))

0 commit comments

Comments
 (0)