pyrofork: filters: Add support for fragments usernames

Signed-off-by: wulan17 <wulan17@komodos.id>
This commit is contained in:
wulan17 2025-06-06 18:01:38 +07:00
parent 2a84c33607
commit 47b054c996
No known key found for this signature in database
GPG key ID: 737814D4B5FF0420

View file

@ -890,7 +890,12 @@ def command(commands: Union[str, List[str]], prefixes: Union[str, List[str]] = "
command_re = re.compile(r"([\"'])(.*?)(?<!\\)\1|(\S+)") command_re = re.compile(r"([\"'])(.*?)(?<!\\)\1|(\S+)")
async def func(flt, client: pyrogram.Client, message: Message): async def func(flt, client: pyrogram.Client, message: Message):
usernames = []
username = client.me.username or "" username = client.me.username or ""
if client.me.usernames:
usernames.append(username)
for user in client.me.usernames:
usernames.append(user.username)
text = message.text or message.caption text = message.text or message.caption
message.command = None message.command = None
@ -904,6 +909,24 @@ def command(commands: Union[str, List[str]], prefixes: Union[str, List[str]] = "
without_prefix = text[len(prefix):] without_prefix = text[len(prefix):]
for cmd in flt.commands: for cmd in flt.commands:
if usernames:
for username in usernames:
if not re.match(rf"^(?:{cmd}(?:@?{username})?)(?:\s|$)", without_prefix,
flags=re.IGNORECASE if not flt.case_sensitive else 0):
continue
without_command = re.sub(rf"{cmd}(?:@?{username})?\s?", "", without_prefix, count=1,
flags=re.IGNORECASE if not flt.case_sensitive else 0)
# match.groups are 1-indexed, group(1) is the quote, group(2) is the text
# between the quotes, group(3) is unquoted, whitespace-split text
# Remove the escape character from the arguments
message.command = [cmd] + [
re.sub(r"\\([\"'])", r"\1", m.group(2) or m.group(3) or "")
for m in command_re.finditer(without_command)
]
return True
if not re.match(rf"^(?:{cmd}(?:@?{username})?)(?:\s|$)", without_prefix, if not re.match(rf"^(?:{cmd}(?:@?{username})?)(?:\s|$)", without_prefix,
flags=re.IGNORECASE if not flt.case_sensitive else 0): flags=re.IGNORECASE if not flt.case_sensitive else 0):
continue continue
@ -1011,12 +1034,22 @@ class user(Filter, set):
) )
async def __call__(self, _, message: Message): async def __call__(self, _, message: Message):
is_usernames_in_filters = False
if message.from_user.usernames:
for username in message.from_user.usernames:
if (
username.username in self
or username.username.lower() in self
):
is_usernames_in_filters = True
break
return (message.from_user return (message.from_user
and (message.from_user.id in self and (message.from_user.id in self
or (message.from_user.username or (message.from_user.username
and message.from_user.username.lower() in self) and message.from_user.username.lower() in self)
or ("me" in self or ("me" in self
and message.from_user.is_self))) and message.from_user.is_self))
or is_usernames_in_filters)
# noinspection PyPep8Naming # noinspection PyPep8Naming
@ -1044,6 +1077,15 @@ class chat(Filter, set):
async def __call__(self, _, message: Union[Message, Story]): async def __call__(self, _, message: Union[Message, Story]):
if isinstance(message, Story): if isinstance(message, Story):
is_usernames_in_filters = False
if message.sender_chat.usernames:
for username in message.sender_chat.usernames:
if (
username.username in self
or username.username.lower() in self
):
is_usernames_in_filters = True
break
return ( return (
message.sender_chat message.sender_chat
and ( and (
@ -1062,8 +1104,17 @@ class chat(Filter, set):
and message.from_user.username.lower() in self and message.from_user.username.lower() in self
) )
) )
) ) or is_usernames_in_filters
else: else:
is_usernames_in_filters = False
if message.chat.usernames:
for username in message._chat.usernames:
if (
username.username in self
or username.username.lower() in self
):
is_usernames_in_filters = True
break
return (message.chat return (message.chat
and (message.chat.id in self and (message.chat.id in self
or (message.chat.username or (message.chat.username
@ -1071,7 +1122,10 @@ class chat(Filter, set):
or ("me" in self or ("me" in self
and message.from_user and message.from_user
and message.from_user.is_self and message.from_user.is_self
and not message.outgoing))) and not message.outgoing))
or (is_usernames_in_filters
and not message.outgoing)
)
# noinspection PyPep8Naming # noinspection PyPep8Naming