mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Refactor TextFilter and its tests
This commit is contained in:
parent
f4339d10b0
commit
277eb8b701
2 changed files with 133 additions and 247 deletions
|
|
@ -84,9 +84,9 @@ class Command(Filter):
|
|||
|
||||
if not ignore_mention and mention and (await message.bot.me).username.lower() != mention.lower():
|
||||
return False
|
||||
elif prefix not in prefixes:
|
||||
if prefix not in prefixes:
|
||||
return False
|
||||
elif (command.lower() if ignore_case else command) not in commands:
|
||||
if (command.lower() if ignore_case else command) not in commands:
|
||||
return False
|
||||
|
||||
return {'command': Command.CommandObj(command=command, prefix=prefix, mention=mention)}
|
||||
|
|
@ -271,19 +271,26 @@ class Text(Filter):
|
|||
|
||||
if self.ignore_case:
|
||||
text = text.lower()
|
||||
_pre_process_func = lambda s: str(s).lower()
|
||||
else:
|
||||
_pre_process_func = str
|
||||
|
||||
# now check
|
||||
if self.equals is not None:
|
||||
self.equals = list(map(lambda s: str(s).lower() if self.ignore_case else str(s), self.equals))
|
||||
return text in self.equals
|
||||
elif self.contains is not None:
|
||||
self.contains = list(map(lambda s: str(s).lower() if self.ignore_case else str(s), self.contains))
|
||||
return all(map(text.__contains__, self.contains))
|
||||
elif self.startswith is not None:
|
||||
self.startswith = list(map(lambda s: str(s).lower() if self.ignore_case else str(s), self.startswith))
|
||||
return any(map(text.startswith, self.startswith))
|
||||
elif self.endswith is not None:
|
||||
self.endswith = list(map(lambda s: str(s).lower() if self.ignore_case else str(s), self.endswith))
|
||||
return any(map(text.endswith, self.endswith))
|
||||
equals = list(map(_pre_process_func, self.equals))
|
||||
return text in equals
|
||||
|
||||
if self.contains is not None:
|
||||
contains = list(map(_pre_process_func, self.contains))
|
||||
return all(map(text.__contains__, contains))
|
||||
|
||||
if self.startswith is not None:
|
||||
startswith = list(map(_pre_process_func, self.startswith))
|
||||
return any(map(text.startswith, startswith))
|
||||
|
||||
if self.endswith is not None:
|
||||
endswith = list(map(_pre_process_func, self.endswith))
|
||||
return any(map(text.endswith, endswith))
|
||||
|
||||
return False
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue