diff --git a/pyrogram/client/filters/filters.py b/pyrogram/client/filters/filters.py index 1c16bf32..a1a52230 100644 --- a/pyrogram/client/filters/filters.py +++ b/pyrogram/client/filters/filters.py @@ -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 `. + + 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 diff --git a/pyrogram/client/types/message.py b/pyrogram/client/types/message.py index 55c3ce05..73ddf720 100644 --- a/pyrogram/client/types/message.py +++ b/pyrogram/client/types/message.py @@ -178,6 +178,11 @@ class Message(Object): matches (``list``): A list containing all `Match Objects `_ that match the text of this message. Only applicable when using :obj:`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 `. """ 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