Merge pull request #124 from pushraxret/dev-test

fix: properly prepare CONSTRUCTORS dict for use, change lambda to async
pull/127/head
Who? 2025-03-12 17:37:56 +07:00 committed by GitHub
commit ac7d3112b7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 23 deletions

View File

@ -45,19 +45,12 @@ GROUPS = [
CONSTRUCTORS = { CONSTRUCTORS = {
(lambda x: x[0].lower() + x[1:])( (entity_name[0].lower() + entity_name[1:]).rsplit("Request", 1)[0]: getattr(cur_entity, "CONSTRUCTOR_ID")
method.__class__.__name__.rsplit("Request", 1)[0] for group in GROUPS
): method.CONSTRUCTOR_ID for entity_name in dir(getattr(functions, group))
for method in utils.array_sum( if hasattr((cur_entity := getattr(getattr(functions, group), entity_name)), "__bases__")
[ and TLRequest in cur_entity.__bases__
[ and hasattr(cur_entity, "CONSTRUCTOR_ID")
method
for method in dir(getattr(functions, group))
if isinstance(method, TLRequest)
]
for group in GROUPS
]
)
} }
@ -101,18 +94,16 @@ class APIRatelimiterMod(loader.Module):
"importChatInvite", "importChatInvite",
] ]
), ),
on_change=lambda: self._client.forbid_constructors( on_change=self.on_forbidden_methods_update
map(
lambda x: CONSTRUCTORS[x],
self.config["forbidden_constructors"],
)
),
), ),
) )
async def client_ready(self): async def client_ready(self):
asyncio.ensure_future(self._install_protection()) 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): async def _install_protection(self):
await asyncio.sleep(30) # Restart lock await asyncio.sleep(30) # Restart lock
if hasattr(self._client._call, "_old_call_rewritten"): if hasattr(self._client._call, "_old_call_rewritten"):

View File

@ -685,23 +685,25 @@ class CustomTelegramClient(TelegramClient):
flood_sleep_threshold, 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): def forbid_constructor(self, constructor: int):
""" """
Forbids the given constructor to be called Forbids the given constructor to be called
:param constructor: Constructor id to forbid :param constructor: Constructor id to forbid
""" """
self._forbidden_constructors.extend([constructor]) self._internal_forbid_ctor([constructor])
self._forbidden_constructors = list(set(self._forbidden_constructors))
def forbid_constructors(self, constructors: list): def forbid_constructors(self, constructors: list):
""" """
Forbids the given constructors to be called. Forbids the given constructors to be called.
All existing forbidden constructors will be removed
:param constructors: Constructor ids to forbid :param constructors: Constructor ids to forbid
""" """
self._forbidden_constructors = list(set(constructors)) self._internal_forbid_ctor(constructors)
def _handle_update( def _handle_update(
self: "CustomTelegramClient", self: "CustomTelegramClient",