From 246fc3baa998128a727bffe7ed24385799a783f4 Mon Sep 17 00:00:00 2001 From: pushraxret <65623597+pushraxret@users.noreply.github.com> Date: Wed, 12 Mar 2025 13:25:09 +0300 Subject: [PATCH 1/2] fix: properly prepare CONSTRUCTOR dict for use, change lambda to async --- hikka/modules/api_protection.py | 10 ++++------ hikka/tl_cache.py | 10 ++++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/hikka/modules/api_protection.py b/hikka/modules/api_protection.py index 475e56f..fa857c2 100644 --- a/hikka/modules/api_protection.py +++ b/hikka/modules/api_protection.py @@ -101,17 +101,15 @@ class APIRatelimiterMod(loader.Module): "importChatInvite", ] ), - on_change=lambda: self._client.forbid_constructors( - map( - lambda x: CONSTRUCTORS[x], - self.config["forbidden_constructors"], - ) - ), + on_change=self.on_forbidden_methods_update ), ) async def client_ready(self): asyncio.ensure_future(self._install_protection()) + + async def on_forbidden_methods_update(self): + self._client.forbid_constructors(list(map(lambda x: CONSTRUCTORS[x], self.config['forbidden_methods'], ))) async def _install_protection(self): await asyncio.sleep(30) # Restart lock diff --git a/hikka/tl_cache.py b/hikka/tl_cache.py index 7891a45..a77a935 100644 --- a/hikka/tl_cache.py +++ b/hikka/tl_cache.py @@ -685,23 +685,25 @@ class CustomTelegramClient(TelegramClient): flood_sleep_threshold, ) + def _internal_forbid_ctor(self, constructors: list): + self._forbidden_constructors.extend(constructors) + self._forbidden_constructors = list(set(self._forbidden_constructors)) + def forbid_constructor(self, constructor: int): """ Forbids the given constructor to be called :param constructor: Constructor id to forbid """ - self._forbidden_constructors.extend([constructor]) - self._forbidden_constructors = list(set(self._forbidden_constructors)) + self._internal_forbid_ctor([constructor]) def forbid_constructors(self, constructors: list): """ Forbids the given constructors to be called. - All existing forbidden constructors will be removed :param constructors: Constructor ids to forbid """ - self._forbidden_constructors = list(set(constructors)) + self._internal_forbid_ctor(constructors) def _handle_update( self: "CustomTelegramClient", From c428fe9298d506da18d6351690a22ab3453c27ac Mon Sep 17 00:00:00 2001 From: pushraxret <65623597+pushraxret@users.noreply.github.com> Date: Wed, 12 Mar 2025 13:26:54 +0300 Subject: [PATCH 2/2] forgotten stash --- hikka/modules/api_protection.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/hikka/modules/api_protection.py b/hikka/modules/api_protection.py index fa857c2..7702b6a 100644 --- a/hikka/modules/api_protection.py +++ b/hikka/modules/api_protection.py @@ -45,19 +45,12 @@ GROUPS = [ CONSTRUCTORS = { - (lambda x: x[0].lower() + x[1:])( - method.__class__.__name__.rsplit("Request", 1)[0] - ): method.CONSTRUCTOR_ID - for method in utils.array_sum( - [ - [ - method - for method in dir(getattr(functions, group)) - if isinstance(method, TLRequest) - ] - for group in GROUPS - ] - ) + (entity_name[0].lower() + entity_name[1:]).rsplit("Request", 1)[0]: getattr(cur_entity, "CONSTRUCTOR_ID") + for group in GROUPS + for entity_name in dir(getattr(functions, group)) + if hasattr((cur_entity := getattr(getattr(functions, group), entity_name)), "__bases__") + and TLRequest in cur_entity.__bases__ + and hasattr(cur_entity, "CONSTRUCTOR_ID") }