2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 05:18:10 +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:
Dan 2018-04-25 10:27:55 +02:00
parent e5de670d40
commit 9173c49938
2 changed files with 32 additions and 15 deletions

View File

@ -120,25 +120,35 @@ class Filters:
"""Filter service messages for pinned messages."""
@staticmethod
def command(command: str or list):
"""Filter commands, i.e.: text messages starting with '/'.
def command(command: str or list, prefix: str = "/", separator: str = " "):
"""Filter commands, i.e.: text messages starting with "/" or any other custom prefix.
Args:
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(
"Command",
lambda _, m: bool(
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}
)
"Command", f, c={command} if not isinstance(command, list) else {c for c in command},
p=prefix, s=separator
)
@staticmethod

View File

@ -178,6 +178,11 @@ class Message(Object):
matches (``list``):
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>`.
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
@ -227,7 +232,8 @@ class Message(Object):
connected_website=None,
views: int = None,
via_bot=None,
matches: list = None
matches: list = None,
command: list = None
):
self.message_id = message_id # int
self.date = date # int
@ -274,3 +280,4 @@ class Message(Object):
self.views = views # flags.39?int
self.via_bot = via_bot # flags.40?User
self.matches = matches
self.command = command