Skip to content

Commit 292a6ea

Browse files
committed
Refactor the command filter.
Also allow it to work on media captions as well
1 parent 88078d4 commit 292a6ea

1 file changed

Lines changed: 24 additions & 28 deletions

File tree

pyrogram/client/filters/filters.py

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,16 @@ class Filters:
222222
- poll"""
223223

224224
@staticmethod
225-
def command(command: str or list,
226-
prefix: str or list = "/",
227-
separator: str = " ",
228-
case_sensitive: bool = False):
225+
def command(
226+
commands: str or list,
227+
prefix: str or list = "/",
228+
separator: str = " ",
229+
case_sensitive: bool = False
230+
):
229231
"""Filter commands, i.e.: text messages starting with "/" or any other custom prefix.
230232
231233
Args:
232-
command (``str`` | ``list``):
234+
commands (``str`` | ``list``):
233235
The command or list of commands as string the filter should look for.
234236
Examples: "start", ["start", "help", "settings"]. When a message text containing
235237
a command arrives, the command itself and its arguments will be stored in the *command*
@@ -249,31 +251,25 @@ def command(command: str or list,
249251
Examples: when True, command="Start" would trigger /Start but not /start.
250252
"""
251253

252-
def f(_, m):
253-
if m.text:
254-
for i in _.p:
255-
if m.text.startswith(i):
256-
t = m.text.split(_.s)
257-
c, a = t[0][len(i):], t[1:]
258-
c = c if _.cs else c.lower()
259-
m.command = ([c] + a) if c in _.c else None
254+
def func(flt, message):
255+
text = message.text or message.caption
256+
257+
if text:
258+
for p in flt.p:
259+
if text.startswith(p):
260+
s = text.split(flt.s)
261+
c, a = s[0][len(p):], s[1:]
262+
c = c if flt.cs else c.lower()
263+
message.command = ([c] + a) if c in flt.c else None
260264
break
261265

262-
return bool(m.command)
263-
264-
return create(
265-
"Command",
266-
f,
267-
c={command if case_sensitive
268-
else command.lower()}
269-
if not isinstance(command, list)
270-
else {c if case_sensitive
271-
else c.lower()
272-
for c in command},
273-
p=set(prefix) if prefix else {""},
274-
s=separator,
275-
cs=case_sensitive
276-
)
266+
return bool(message.command)
267+
268+
commands = commands if type(commands) is list else [commands]
269+
commands = {c if case_sensitive else c.lower() for c in commands}
270+
prefixes = set(prefix) if prefix else {""}
271+
272+
return create("Command", func=func, c=commands, p=prefixes, s=separator, cs=case_sensitive)
277273

278274
@staticmethod
279275
def regex(pattern, flags: int = 0):

0 commit comments

Comments
 (0)