mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-28 21:07:59 +00:00
Allow defining case sensitive commands with Filters.command
This commit is contained in:
parent
2125415f8f
commit
62d4b2aeee
@ -134,7 +134,10 @@ class Filters:
|
|||||||
# TODO: Add filters for reply markups
|
# TODO: Add filters for reply markups
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def command(command: str or list, prefix: str = "/", separator: str = " "):
|
def command(command: str or list,
|
||||||
|
prefix: str = "/",
|
||||||
|
separator: str = " ",
|
||||||
|
case_sensitive: bool = False):
|
||||||
"""Filter commands, i.e.: text messages starting with "/" or any other custom prefix.
|
"""Filter commands, i.e.: text messages starting with "/" or any other custom prefix.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -144,26 +147,40 @@ class Filters:
|
|||||||
a command arrives, the command itself and its arguments will be stored in the *command*
|
a command arrives, the command itself and its arguments will be stored in the *command*
|
||||||
field of the :class:`Message <pyrogram.Message>`.
|
field of the :class:`Message <pyrogram.Message>`.
|
||||||
|
|
||||||
prefix (``str``):
|
prefix (``str``, *optional*):
|
||||||
The command prefix. Defaults to "/".
|
The command prefix. Defaults to "/" (slash).
|
||||||
Examples: /start, .help, !settings.
|
Examples: /start, .help, !settings.
|
||||||
|
|
||||||
separator (``str``):
|
separator (``str``, *optional*):
|
||||||
The command arguments separator. Defaults to " " (white space).
|
The command arguments separator. Defaults to " " (white space).
|
||||||
Examples: /start first second, /start-first-second, /start.first.second.
|
Examples: /start first second, /start-first-second, /start.first.second.
|
||||||
|
|
||||||
|
case_sensitive (``bool``, *optional*):
|
||||||
|
Pass True if you want your command(s) to be case sensitive. Defaults to False.
|
||||||
|
Examples: when True, command="Start" would trigger /Start but not /start.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def f(_, m):
|
def f(_, m):
|
||||||
if m.text and m.text.startswith(_.p):
|
if m.text and m.text.startswith(_.p):
|
||||||
t = m.text.split(_.s)
|
t = m.text.split(_.s)
|
||||||
c, a = t[0][len(_.p):], t[1:]
|
c, a = t[0][len(_.p):], t[1:]
|
||||||
|
c = c if _.cs else c.lower()
|
||||||
m.command = ([c] + a) if c in _.c else None
|
m.command = ([c] + a) if c in _.c else None
|
||||||
|
|
||||||
return bool(m.command)
|
return bool(m.command)
|
||||||
|
|
||||||
return build(
|
return build(
|
||||||
"Command", f, c={command} if not isinstance(command, list) else {c for c in command},
|
"Command",
|
||||||
p=prefix, s=separator
|
f,
|
||||||
|
c={command if case_sensitive
|
||||||
|
else command.lower()}
|
||||||
|
if not isinstance(command, list)
|
||||||
|
else {c if case_sensitive
|
||||||
|
else c.lower()
|
||||||
|
for c in command},
|
||||||
|
p=prefix,
|
||||||
|
s=separator,
|
||||||
|
cs=case_sensitive
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
Loading…
x
Reference in New Issue
Block a user