@@ -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" ,
0 commit comments