2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 13:27:47 +00:00

Give Filters.user superpowers

It can now add and remove users at runtime
This commit is contained in:
Dan 2018-09-14 15:29:36 +02:00
parent 4e293f23a9
commit 31578ddb33

View File

@ -236,30 +236,33 @@ class Filters:
return create("Regex", f, p=re.compile(pattern, flags)) return create("Regex", f, p=re.compile(pattern, flags))
@staticmethod class user(Filter, set):
def user(users: int or str or list = None): """Filter messages coming from one or more users.
"""Filter messages coming from one or more specific users.
You can use `set bound methods <https://docs.python.org/3/library/stdtypes.html#set>`_ to manipulate the
users container.
Args: Args:
users (``int`` | ``str`` | ``list``): users (``int`` | ``str`` | ``list``):
Pass one or more user ids/usernames to filter the users. 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. Defaults to None (no users).
To add or remove users dynamically, simply manipulate the inner set.
Defaults to None (empty set).
""" """
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 _.users 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 _.users))), if type(users) is list else
users=( {users.lower().strip("@") if type(users) is str else users}
set() if users is None )
else {users.lower().strip("@") if type(users) is str else users}
if not isinstance(users, list) def __call__(self, message):
else {i.lower().strip("@") if type(i) is str else i for i in users} 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 @staticmethod
def chat(chat: int or str or list): def chat(chat: int or str or list):