mirror of
https://github.com/pyrogram/pyrogram
synced 2025-09-02 07:15:23 +00:00
Allow Filters.command to pass command and arguments
This is done by using the new "command" field in the Message type
This commit is contained in:
@@ -120,25 +120,35 @@ class Filters:
|
|||||||
"""Filter service messages for pinned messages."""
|
"""Filter service messages for pinned messages."""
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def command(command: str or list):
|
def command(command: str or list, prefix: str = "/", separator: str = " "):
|
||||||
"""Filter commands, i.e.: text messages starting with '/'.
|
"""Filter commands, i.e.: text messages starting with "/" or any other custom prefix.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
command (``str`` | ``list``):
|
command (``str`` | ``list``):
|
||||||
The command or list of commands as strings the filter should look for.
|
The command or list of commands as string the filter should look for.
|
||||||
|
Examples: "start", ["start", "help", "settings"]. When a message text containing
|
||||||
|
a command arrives, the command itself and its arguments will be stored in the *command*
|
||||||
|
field of the :class:`Message <pyrogram.Message>`.
|
||||||
|
|
||||||
|
prefix (``str``):
|
||||||
|
The command prefix. Defaults to "/".
|
||||||
|
Examples: /start, .help, !settings.
|
||||||
|
|
||||||
|
separator (``str``):
|
||||||
|
The command arguments separator. Defaults to " " (white space).
|
||||||
|
Examples: /start first second, /start-first-second, /start.first.second.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def f(_, m):
|
||||||
|
if m.text and m.text.startswith(_.p):
|
||||||
|
c = m.text[1:].split(_.s)[0]
|
||||||
|
m.command = ([c] + m.text.split(_.s)[1:]) if c in _.c else None
|
||||||
|
|
||||||
|
return bool(m.command)
|
||||||
|
|
||||||
return build(
|
return build(
|
||||||
"Command",
|
"Command", f, c={command} if not isinstance(command, list) else {c for c in command},
|
||||||
lambda _, m: bool(
|
p=prefix, s=separator
|
||||||
m.text
|
|
||||||
and m.text.startswith("/")
|
|
||||||
and (m.text[1:].split()[0] in _.c)
|
|
||||||
),
|
|
||||||
c=(
|
|
||||||
{command}
|
|
||||||
if not isinstance(command, list)
|
|
||||||
else {c for c in command}
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@@ -178,6 +178,11 @@ class Message(Object):
|
|||||||
matches (``list``):
|
matches (``list``):
|
||||||
A list containing all `Match Objects <https://docs.python.org/3/library/re.html#match-objects>`_ that match
|
A list containing all `Match Objects <https://docs.python.org/3/library/re.html#match-objects>`_ that match
|
||||||
the text of this message. Only applicable when using :obj:`Filters.regex <pyrogram.Filters.regex>`.
|
the text of this message. Only applicable when using :obj:`Filters.regex <pyrogram.Filters.regex>`.
|
||||||
|
|
||||||
|
command (``list``):
|
||||||
|
A list containing the command and its arguments, if any.
|
||||||
|
E.g.: "/start 1 2 3" would produce ["start", "1", "2", "3"].
|
||||||
|
Only applicable when using :obj:`Filters.command <pyrogram.Filters.command>`.
|
||||||
"""
|
"""
|
||||||
ID = 0xb0700003
|
ID = 0xb0700003
|
||||||
|
|
||||||
@@ -227,7 +232,8 @@ class Message(Object):
|
|||||||
connected_website=None,
|
connected_website=None,
|
||||||
views: int = None,
|
views: int = None,
|
||||||
via_bot=None,
|
via_bot=None,
|
||||||
matches: list = None
|
matches: list = None,
|
||||||
|
command: list = None
|
||||||
):
|
):
|
||||||
self.message_id = message_id # int
|
self.message_id = message_id # int
|
||||||
self.date = date # int
|
self.date = date # int
|
||||||
@@ -274,3 +280,4 @@ class Message(Object):
|
|||||||
self.views = views # flags.39?int
|
self.views = views # flags.39?int
|
||||||
self.via_bot = via_bot # flags.40?User
|
self.via_bot = via_bot # flags.40?User
|
||||||
self.matches = matches
|
self.matches = matches
|
||||||
|
self.command = command
|
||||||
|
Reference in New Issue
Block a user