Give superpowers to Filters.chat too

It can now add and remove chats at runtime
This commit is contained in:
Dan 2018-09-14 15:33:32 +02:00
parent 339630dafb
commit 3307b410b4

View File

@ -265,25 +265,33 @@ class Filters:
and message.from_user.username.lower() in self)) and message.from_user.username.lower() in self))
) )
@staticmethod # noinspection PyPep8Naming
def chat(chat: int or str or list): class chat(Filter, set):
"""Filter messages coming from specific chats. """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 the 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(