Skip to content

Commit 3307b41

Browse files
committed
Give superpowers to Filters.chat too
It can now add and remove chats at runtime
1 parent 339630d commit 3307b41

1 file changed

Lines changed: 24 additions & 16 deletions

File tree

pyrogram/client/filters/filters.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -265,26 +265,34 @@ def __call__(self, message):
265265
and message.from_user.username.lower() in self))
266266
)
267267

268-
@staticmethod
269-
def chat(chat: int or str or list):
270-
"""Filter messages coming from specific chats.
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.
271274
272275
Args:
273-
chat (``int`` | ``str`` | ``list``):
274-
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 the chats.
278+
Defaults to None (no chats).
275279
"""
276-
return create(
277-
"Chat",
278-
lambda _, m: bool(m.chat
279-
and (m.chat.id in _.c
280-
or (m.chat.username
281-
and m.chat.username.lower() in _.c))),
282-
c=(
283-
{chat.lower().strip("@") if type(chat) is str else chat}
284-
if not isinstance(chat, list)
285-
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))
286295
)
287-
)
288296

289297
service = create(
290298
"Service",

0 commit comments

Comments
 (0)