diff --git a/requirements.txt b/requirements.txt
index 530fbe9..6de07b3 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,6 +1,7 @@
APScheduler==3.9.1
-aiogram==3.0.0b5
+aiogram==3.0.0b6
colorlog==6.7.0
typing==3.7.4.3
aiohttp==3.8.3
-colorama==0.4.5
\ No newline at end of file
+colorama==0.4.5
+cachetools==5.2.0
\ No newline at end of file
diff --git a/tgbot/middlewares/throttling.py b/tgbot/middlewares/throttling.py
index abf964e..cd5b986 100644
--- a/tgbot/middlewares/throttling.py
+++ b/tgbot/middlewares/throttling.py
@@ -1,56 +1,58 @@
# - *- coding: utf- 8 - *-
import time
-from typing import Any, Awaitable, Callable, Dict
+from typing import Any, Awaitable, Callable, Dict, Union
from aiogram import BaseMiddleware
-from aiogram.dispatcher.event.handler import HandlerObject
from aiogram.dispatcher.flags import get_flag
-from aiogram.types import Message
-
-from tgbot.services.api_sqlite import get_userx
+from aiogram.types import Message, User
+from cachetools import TTLCache
# Антиспам
class ThrottlingMiddleware(BaseMiddleware):
- def __init__(self, default_rate: int = 0.6) -> None:
+ def __init__(self, default_rate: Union[int, float] = 0.6) -> None:
self.default_rate = default_rate
- self.now_rate = default_rate
- self.last_throttled = int(time.time())
- self.count_throttled = 0
+ self.users = TTLCache(maxsize=10_000, ttl=600)
async def __call__(self, handler: Callable[[Message, Dict[str, Any]], Awaitable[Any]], event: Message, data):
- real_handler: HandlerObject = data['handler']
- this_user = data.get("event_from_user")
-
- if not this_user.is_bot:
- data['get_user'] = get_userx(user_id=this_user.id)
+ this_user: User = data.get("event_from_user")
if get_flag(data, "rate") is not None:
- self.now_rate = get_flag(data, "rate")
+ self.default_rate = get_flag(data, "rate")
- if int(time.time()) - self.last_throttled >= self.now_rate:
- self.last_throttled = int(time.time())
- self.now_rate = self.default_rate
- self.count_throttled = 0
+ if this_user.id not in self.users:
+ self.users[this_user.id] = {
+ 'last_throttled': int(time.time()),
+ 'count_throttled': 0,
+ 'now_rate': self.default_rate,
+ }
return await handler(event, data)
else:
- if self.count_throttled == 0:
- self.count_throttled += 1
- self.now_rate += self.default_rate * 2
+ if int(time.time()) - self.users[this_user.id]['last_throttled'] >= self.users[this_user.id]['now_rate']:
+ self.users.pop(this_user.id)
return await handler(event, data)
- elif self.count_throttled == 1:
- self.count_throttled += 1
- self.now_rate += self.default_rate * 2
-
- await event.reply("❗ Пожалуйста, не спамьте.")
else:
- self.now_rate = 3
+ self.users[this_user.id]['last_throttled'] = int(time.time())
- if self.count_throttled == 2:
- self.count_throttled = 3
- await event.reply("❗ Бот не будет отвечать до прекращения спама.")
+ if self.users[this_user.id]['count_throttled'] == 0:
+ self.users[this_user.id]['count_throttled'] = 1
+ self.users[this_user.id]['now_rate'] *= 2
- self.last_throttled = int(time.time())
+ return await handler(event, data)
+ elif self.users[this_user.id]['count_throttled'] == 1:
+ self.users[this_user.id]['count_throttled'] = 2
+ self.users[this_user.id]['now_rate'] *= 2
+
+ await event.reply("❗ Пожалуйста, не спамьте.\n"
+ "❗ Please, do not spam.")
+ elif self.users[this_user.id]['count_throttled'] == 2:
+ self.users[this_user.id]['count_throttled'] = 3
+ self.users[this_user.id]['now_rate'] = 3
+
+ await event.reply("❗ Бот не будет отвечать до прекращения спама.\n"
+ "❗ The bot will not respond until the spam stops.")
+ else:
+ pass
diff --git a/tgbot/services/__init__.py b/tgbot/services/__init__.py
index 8b13789..e69de29 100644
--- a/tgbot/services/__init__.py
+++ b/tgbot/services/__init__.py
@@ -1 +0,0 @@
-
diff --git a/tgbot/utils/__init__.py b/tgbot/utils/__init__.py
index 8b13789..e69de29 100644
--- a/tgbot/utils/__init__.py
+++ b/tgbot/utils/__init__.py
@@ -1 +0,0 @@
-
diff --git a/tgbot/utils/const_functions.py b/tgbot/utils/const_functions.py
index 0a1788e..b85a761 100644
--- a/tgbot/utils/const_functions.py
+++ b/tgbot/utils/const_functions.py
@@ -63,7 +63,7 @@ async def smart_send(bot: Bot, message: types.Message, user_id, text_add=None, r
elif message.sticker is not None:
await bot.send_sticker(user_id, message.sticker.file_id, reply_markup=reply)
elif message.dice is not None:
- await bot.send_dice(user_id, message.dice.emoji, reply_markup=reply)
+ await bot.send_dice(user_id, emoji=message.dice.emoji, reply_markup=reply)
elif message.location is not None:
await bot.send_location(user_id, latitude=message.location.latitude, longitude=message.location.longitude,
reply_markup=reply)
@@ -92,7 +92,7 @@ def ded(get_text: str) -> str:
return get_text
-# Очистка HTML тэгов
+# Очистка текста от HTML тэгов
def clear_html(get_text: str) -> str:
if get_text is not None:
if "<" in get_text: get_text = get_text.replace("<", "*")
diff --git a/tgbot/utils/misc/__init__.py b/tgbot/utils/misc/__init__.py
index 8b13789..e69de29 100644
--- a/tgbot/utils/misc/__init__.py
+++ b/tgbot/utils/misc/__init__.py
@@ -1 +0,0 @@
-