Skip to content

Commit 43b0b57

Browse files
committed
Implement global prefix handler
1 parent 5826bce commit 43b0b57

2 files changed

Lines changed: 42 additions & 6 deletions

File tree

pyrogram/client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ class Client(Methods):
222222
223223
link_preview_options (:obj:`~pyrogram.types.LinkPreviewOptions`, *optional*):
224224
Global link preview options for the client.
225+
226+
prefixes (``str`` | ``list``, *optional*):
227+
Global command prefix or list of prefixes for the client, used as the default by
228+
:func:`~pyrogram.filters.command` whenever a handler doesn't pass its own
229+
``prefixes`` argument. Defaults to "/" (slash). Examples: "!", ["/", "!", "."].
230+
Pass None or "" (empty string) to allow commands with no prefix at all by default.
225231
226232
fetch_replies (``bool``, *optional*):
227233
Pass True to automatically fetch replies for messages.
@@ -310,6 +316,7 @@ def __init__(
310316
storage_engine: Optional[Storage] = None,
311317
client_platform: "enums.ClientPlatform" = enums.ClientPlatform.OTHER,
312318
link_preview_options: Optional[LinkPreviewOptions] = None,
319+
prefixes: Optional[Union[str, List[str]]] = "/",
313320
fetch_replies: Optional[bool] = True,
314321
fetch_topics: Optional[bool] = True,
315322
fetch_stories: Optional[bool] = True,
@@ -353,6 +360,10 @@ def __init__(
353360
self.max_topic_cache_size = max_topic_cache_size
354361
self.client_platform = client_platform
355362
self.link_preview_options = link_preview_options
363+
364+
_prefixes = [] if prefixes is None else prefixes
365+
_prefixes = _prefixes if isinstance(_prefixes, list) else [_prefixes]
366+
self.prefixes = set(_prefixes) if _prefixes else {""}
356367
self.fetch_replies = fetch_replies
357368
self.fetch_topics = fetch_topics
358369
self.fetch_stories = fetch_stories

pyrogram/filters.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,21 @@ async def gift_offer_rejected_filter(_, __, m: Message):
985985
# endregion
986986

987987
# region command_filter
988-
def command(commands: Union[str, List[str]], prefixes: Optional[Union[str, List[str]]] = "/", case_sensitive: bool = False):
988+
class _NotSet:
989+
def __repr__(self):
990+
return "<default client prefixes>"
991+
992+
993+
COMMAND_PREFIX_NOT_SET = _NotSet()
994+
"""Sentinel used by :func:`~pyrogram.filters.command` to detect that *prefixes* was not
995+
explicitly passed, so the filter should fall back to the ``Client(prefixes=...)`` value."""
996+
997+
998+
def command(
999+
commands: Union[str, List[str]],
1000+
prefixes: Optional[Union[str, List[str]]] = COMMAND_PREFIX_NOT_SET,
1001+
case_sensitive: bool = False
1002+
):
9891003
"""Filter commands, i.e.: text messages starting with "/" or any other custom prefix.
9901004
9911005
Parameters:
@@ -997,7 +1011,9 @@ def command(commands: Union[str, List[str]], prefixes: Optional[Union[str, List[
9971011
9981012
prefixes (``str`` | ``list``, *optional*):
9991013
A prefix or a list of prefixes as string the filter should look for.
1000-
Defaults to "/" (slash). Examples: ".", "!", ["/", "!", "."], list(".:!").
1014+
Defaults to the prefixes configured on the :obj:`~pyrogram.Client` (via the
1015+
``prefixes`` parameter of :meth:`~pyrogram.Client`), which itself defaults to "/"
1016+
(slash) when not set. Examples: ".", "!", ["/", "!", "."], list(".:!").
10011017
Pass None or "" (empty string) to allow commands with no prefix at all.
10021018
10031019
case_sensitive (``bool``, *optional*):
@@ -1014,7 +1030,13 @@ async def func(flt, client: pyrogram.Client, message: Message):
10141030
if not text:
10151031
return False
10161032

1017-
for prefix in flt.prefixes:
1033+
prefixes = flt.prefixes
1034+
1035+
if prefixes is COMMAND_PREFIX_NOT_SET:
1036+
client_prefixes = getattr(client, "prefixes", None)
1037+
prefixes = client_prefixes if client_prefixes else {"/"}
1038+
1039+
for prefix in prefixes:
10181040
if not text.startswith(prefix):
10191041
continue
10201042

@@ -1044,9 +1066,12 @@ async def func(flt, client: pyrogram.Client, message: Message):
10441066
commands = commands if isinstance(commands, list) else [commands]
10451067
commands = {c if case_sensitive else c.lower() for c in commands}
10461068

1047-
prefixes = [] if prefixes is None else prefixes
1048-
prefixes = prefixes if isinstance(prefixes, list) else [prefixes]
1049-
prefixes = set(prefixes) if prefixes else {""}
1069+
if prefixes is COMMAND_PREFIX_NOT_SET:
1070+
prefixes = COMMAND_PREFIX_NOT_SET
1071+
else:
1072+
prefixes = [] if prefixes is None else prefixes
1073+
prefixes = prefixes if isinstance(prefixes, list) else [prefixes]
1074+
prefixes = set(prefixes) if prefixes else {""}
10501075

10511076
return create(
10521077
func,

0 commit comments

Comments
 (0)