@@ -43,9 +43,10 @@ def __or__(self, other):
4343class InvertFilter (Filter ):
4444 def __init__ (self , base ):
4545 self .base = base
46+ self ._base_is_async = inspect .iscoroutinefunction (base .__call__ )
4647
4748 async def __call__ (self , client : "pyrogram.Client" , update : Update ):
48- if inspect . iscoroutinefunction ( self .base . __call__ ) :
49+ if self ._base_is_async :
4950 x = await self .base (client , update )
5051 else :
5152 x = await client .loop .run_in_executor (
@@ -61,9 +62,11 @@ class AndFilter(Filter):
6162 def __init__ (self , base , other ):
6263 self .base = base
6364 self .other = other
65+ self ._base_is_async = inspect .iscoroutinefunction (base .__call__ )
66+ self ._other_is_async = inspect .iscoroutinefunction (other .__call__ )
6467
6568 async def __call__ (self , client : "pyrogram.Client" , update : Update ):
66- if inspect . iscoroutinefunction ( self .base . __call__ ) :
69+ if self ._base_is_async :
6770 x = await self .base (client , update )
6871 else :
6972 x = await client .loop .run_in_executor (
@@ -72,11 +75,10 @@ async def __call__(self, client: "pyrogram.Client", update: Update):
7275 client , update
7376 )
7477
75- # short circuit
7678 if not x :
7779 return False
7880
79- if inspect . iscoroutinefunction ( self .other . __call__ ) :
81+ if self ._other_is_async :
8082 y = await self .other (client , update )
8183 else :
8284 y = await client .loop .run_in_executor (
@@ -85,16 +87,18 @@ async def __call__(self, client: "pyrogram.Client", update: Update):
8587 client , update
8688 )
8789
88- return x and y
90+ return y
8991
9092
9193class OrFilter (Filter ):
9294 def __init__ (self , base , other ):
9395 self .base = base
9496 self .other = other
97+ self ._base_is_async = inspect .iscoroutinefunction (base .__call__ )
98+ self ._other_is_async = inspect .iscoroutinefunction (other .__call__ )
9599
96100 async def __call__ (self , client : "pyrogram.Client" , update : Update ):
97- if inspect . iscoroutinefunction ( self .base . __call__ ) :
101+ if self ._base_is_async :
98102 x = await self .base (client , update )
99103 else :
100104 x = await client .loop .run_in_executor (
@@ -103,11 +107,10 @@ async def __call__(self, client: "pyrogram.Client", update: Update):
103107 client , update
104108 )
105109
106- # short circuit
107110 if x :
108111 return True
109112
110- if inspect . iscoroutinefunction ( self .other . __call__ ) :
113+ if self ._other_is_async :
111114 y = await self .other (client , update )
112115 else :
113116 y = await client .loop .run_in_executor (
@@ -116,7 +119,7 @@ async def __call__(self, client: "pyrogram.Client", update: Update):
116119 client , update
117120 )
118121
119- return x or y
122+ return y
120123
121124
122125CUSTOM_FILTER_NAME = "CustomFilter"
@@ -146,7 +149,7 @@ def create(func: Callable, name: str = None, **kwargs) -> Filter:
146149 :meth:`~pyrogram.filters.command` or :meth:`~pyrogram.filters.regex`.
147150 """
148151 return type (
149- name or func . __name__ or CUSTOM_FILTER_NAME ,
152+ name or getattr ( func , " __name__" , None ) or CUSTOM_FILTER_NAME ,
150153 (Filter ,),
151154 {"__call__" : func , ** kwargs }
152155 )()
@@ -923,11 +926,11 @@ async def paid_message_filter(_, __, m: Message):
923926
924927# region linked_channel_filter
925928async def linked_channel_filter (_ , __ , m : Message ):
926- return bool (
927- m . forward_origin and
928- m . forward_origin . type == enums . MessageOriginType . CHANNEL and
929- m . forward_origin . chat == m . sender_chat
930- )
929+ origin = m . forward_origin
930+ if not ( origin and origin . type == enums . MessageOriginType . CHANNEL and m . sender_chat ):
931+ return False
932+ origin_chat = getattr ( origin , " chat" , None )
933+ return origin_chat is not None and origin_chat . id == m . sender_chat . id
931934
932935
933936linked_channel = create (linked_channel_filter )
@@ -999,7 +1002,7 @@ def command(commands: Union[str, List[str]], prefixes: Optional[Union[str, List[
9991002 command_re = re .compile (r"([\"'])(.*?)(?<!\\)\1|(\S+)" )
10001003
10011004 async def func (flt , client : pyrogram .Client , message : Message ):
1002- username = client .me .username or ""
1005+ username = client .me .username if client . me else ""
10031006 text = message .text or message .caption
10041007 message .command = None
10051008
@@ -1013,11 +1016,12 @@ async def func(flt, client: pyrogram.Client, message: Message):
10131016 without_prefix = text [len (prefix ):]
10141017
10151018 for cmd in flt .commands :
1016- if not re .match (rf"^(?:{ cmd } (?:@?{ username } )?)(?:\s|$)" , without_prefix ,
1019+ escaped_cmd = re .escape (cmd )
1020+ if not re .match (rf"^(?:{ escaped_cmd } (?:@?{ username } )?)(?:\s|$)" , without_prefix ,
10171021 flags = re .IGNORECASE if not flt .case_sensitive else 0 ):
10181022 continue
10191023
1020- without_command = re .sub (rf"{ cmd } (?:@?{ username } )?\s?" , "" , without_prefix , count = 1 ,
1024+ without_command = re .sub (rf"{ escaped_cmd } (?:@?{ username } )?\s?" , "" , without_prefix , count = 1 ,
10211025 flags = re .IGNORECASE if not flt .case_sensitive else 0 )
10221026
10231027 # match.groups are 1-indexed, group(1) is the quote, group(2) is the text
@@ -1087,6 +1091,8 @@ async def func(flt, _, update: Update):
10871091
10881092 if value :
10891093 update .matches = list (flt .p .finditer (value )) or None
1094+ else :
1095+ update .matches = None
10901096
10911097 return bool (update .matches )
10921098
0 commit comments