21
21
from swibots .api .common .models import User
22
22
from swibots .api .callback import AppBar , AppPage
23
23
from swibots .api .common .events import Event
24
+ from swibots .rate_limiter import AsyncRateLimiter
24
25
from swibots .utils import (
25
26
DownloadProgress ,
26
27
IOClient ,
@@ -81,8 +82,10 @@ def __init__(
81
82
if email and password :
82
83
auth_result = self .auth_service .login (email , password )
83
84
token = auth_result .access_token
85
+
84
86
if not token :
85
87
raise TokenInvalidError (f"'token' for the bot can't be '{ token } '" )
88
+
86
89
self .token = token
87
90
self ._user_type = Bot
88
91
self ._bot_info : BotInfo | None = None
@@ -95,14 +98,17 @@ def __init__(
95
98
self ._bot_description = bot_description
96
99
self .auto_update_bot = auto_update_bot
97
100
self ._loop = loop or asyncio .get_event_loop ()
98
- try :
99
- self .user = self .auth_service .get_me_sync (user_type = self ._user_type )
100
- self .name = self .user .name
101
- except Exception as er :
102
- logging .error (er )
103
- # In case, if issues with auth service
104
- self .user = self ._user_type (app = self )
105
- self .name = ""
101
+
102
+ self .user = self .auth_service .get_me_sync (user_type = self ._user_type )
103
+ self .name = self .user .name
104
+
105
+ self .rate_limit = AsyncRateLimiter (storage_file = f"{ self .user .id } .pkl" )
106
+
107
+ # Add rate limit functions
108
+ self .update_bot_commands = self .rate_limit .limit (
109
+ "update_bot_commands" , 10 , 15 * 60
110
+ )(self .update_bot_commands )
111
+
106
112
if app_bar is None :
107
113
app_bar = AppBar (self .name , left_icon = self .user .imageurl )
108
114
self .app_bar = app_bar
@@ -253,8 +259,9 @@ async def update_bot_commands(self):
253
259
commands = commands ,
254
260
preview = self ._app_preview ,
255
261
)
256
-
262
+ log . info ( "Updating bot commands..." )
257
263
self ._bot_info = await self .update_bot_info (self ._bot_info )
264
+ log .info ("Bot commands updated" )
258
265
259
266
async def _on_chat_service_start (self , _ ):
260
267
await self .chat_service .subscribe_to_notifications (callback = self .on_chat_event )
@@ -459,5 +466,8 @@ def run(self, task: Callable = None):
459
466
log .exception (e )
460
467
run (self .stop ())
461
468
469
+ def limit (self , key : str , times : int , seconds : int ):
470
+ pass
471
+
462
472
463
473
BotApp = Client
0 commit comments