Merge branch 'develop' into asyncio

# Conflicts:
#	pyrogram/connection/connection.py
This commit is contained in:
Dan 2018-09-15 22:19:39 +02:00
commit f4a8848603
5 changed files with 55 additions and 37 deletions

View File

@ -236,46 +236,62 @@ class Filters:
return create("Regex", f, p=re.compile(pattern, flags)) return create("Regex", f, p=re.compile(pattern, flags))
@staticmethod # noinspection PyPep8Naming
def user(user: int or str or list): class user(Filter, set):
"""Filter messages coming from specific users. """Filter messages coming from one or more users.
You can use `set bound methods <https://docs.python.org/3/library/stdtypes.html#set>`_ to manipulate the
users container.
Args: Args:
user (``int`` | ``str`` | ``list``): users (``int`` | ``str`` | ``list``):
The user or list of user IDs (int) or usernames (str) the filter should look for. Pass one or more user ids/usernames to filter users.
Defaults to None (no users).
""" """
return create(
"User", def __init__(self, users: int or str or list = None):
lambda _, m: bool(m.from_user users = [] if users is None else users if type(users) is list else [users]
and (m.from_user.id in _.u super().__init__(
or (m.from_user.username {i.lower().strip("@") if type(i) is str else i for i in users}
and m.from_user.username.lower() in _.u))), if type(users) is list else
u=( {users.lower().strip("@") if type(users) is str else users}
{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 __call__(self, message):
def chat(chat: int or str or list): return bool(
"""Filter messages coming from specific chats. 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 <https://docs.python.org/3/library/stdtypes.html#set>`_ to manipulate the
chats container.
Args: Args:
chat (``int`` | ``str`` | ``list``): chats (``int`` | ``str`` | ``list``):
The chat or list of chat IDs (int) or usernames (str) the filter should look for. Pass one or more chat ids/usernames to filter chats.
Defaults to None (no chats).
""" """
return create(
"Chat", def __init__(self, chats: int or str or list = None):
lambda _, m: bool(m.chat chats = [] if chats is None else chats if type(chats) is list else [chats]
and (m.chat.id in _.c super().__init__(
or (m.chat.username {i.lower().strip("@") if type(i) is str else i for i in chats}
and m.chat.username.lower() in _.c))), if type(chats) is list else
c=( {chats.lower().strip("@") if type(chats) is str else chats}
{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 __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 = create(

View File

@ -49,6 +49,6 @@ class CallbackQueryHandler(Handler):
def check(self, callback_query): def check(self, callback_query):
return ( return (
self.filters(callback_query) self.filters(callback_query)
if self.filters if callable(self.filters)
else True else True
) )

View File

@ -50,6 +50,6 @@ class DeletedMessagesHandler(Handler):
def check(self, messages): def check(self, messages):
return ( return (
self.filters(messages.messages[0]) self.filters(messages.messages[0])
if self.filters if callable(self.filters)
else True else True
) )

View File

@ -50,6 +50,6 @@ class MessageHandler(Handler):
def check(self, message): def check(self, message):
return ( return (
self.filters(message) self.filters(message)
if self.filters if callable(self.filters)
else True else True
) )

View File

@ -38,6 +38,7 @@ class Connection:
} }
def __init__(self, dc_id: int, test_mode: bool, ipv6: bool, proxy: dict, mode: int = 2): 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.ipv6 = ipv6
self.proxy = proxy self.proxy = proxy
self.address = DataCenter(dc_id, test_mode, ipv6) self.address = DataCenter(dc_id, test_mode, ipv6)
@ -57,7 +58,8 @@ class Connection:
self.protocol.close() self.protocol.close()
await asyncio.sleep(1) await asyncio.sleep(1)
else: else:
log.info("Connected! IPv{} - {}".format( log.info("Connected! DC{} - IPv{} - {}".format(
self.dc_id,
"6" if self.ipv6 else "4", "6" if self.ipv6 else "4",
self.mode.__name__ self.mode.__name__
)) ))