mirror of
https://github.com/djimboy/djimbo_template_aio3.git
synced 2026-07-25 09:44:29 +00:00
Update
This commit is contained in:
+3
-2
@@ -1,6 +1,7 @@
|
|||||||
APScheduler==3.9.1
|
APScheduler==3.9.1
|
||||||
aiogram==3.0.0b5
|
aiogram==3.0.0b6
|
||||||
colorlog==6.7.0
|
colorlog==6.7.0
|
||||||
typing==3.7.4.3
|
typing==3.7.4.3
|
||||||
aiohttp==3.8.3
|
aiohttp==3.8.3
|
||||||
colorama==0.4.5
|
colorama==0.4.5
|
||||||
|
cachetools==5.2.0
|
||||||
@@ -1,56 +1,58 @@
|
|||||||
# - *- coding: utf- 8 - *-
|
# - *- coding: utf- 8 - *-
|
||||||
import time
|
import time
|
||||||
from typing import Any, Awaitable, Callable, Dict
|
from typing import Any, Awaitable, Callable, Dict, Union
|
||||||
|
|
||||||
from aiogram import BaseMiddleware
|
from aiogram import BaseMiddleware
|
||||||
from aiogram.dispatcher.event.handler import HandlerObject
|
|
||||||
from aiogram.dispatcher.flags import get_flag
|
from aiogram.dispatcher.flags import get_flag
|
||||||
from aiogram.types import Message
|
from aiogram.types import Message, User
|
||||||
|
from cachetools import TTLCache
|
||||||
from tgbot.services.api_sqlite import get_userx
|
|
||||||
|
|
||||||
|
|
||||||
# Антиспам
|
# Антиспам
|
||||||
class ThrottlingMiddleware(BaseMiddleware):
|
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.default_rate = default_rate
|
||||||
self.now_rate = default_rate
|
|
||||||
|
|
||||||
self.last_throttled = int(time.time())
|
self.users = TTLCache(maxsize=10_000, ttl=600)
|
||||||
self.count_throttled = 0
|
|
||||||
|
|
||||||
async def __call__(self, handler: Callable[[Message, Dict[str, Any]], Awaitable[Any]], event: Message, data):
|
async def __call__(self, handler: Callable[[Message, Dict[str, Any]], Awaitable[Any]], event: Message, data):
|
||||||
real_handler: HandlerObject = data['handler']
|
this_user: User = data.get("event_from_user")
|
||||||
this_user = data.get("event_from_user")
|
|
||||||
|
|
||||||
if not this_user.is_bot:
|
|
||||||
data['get_user'] = get_userx(user_id=this_user.id)
|
|
||||||
|
|
||||||
if get_flag(data, "rate") is not None:
|
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:
|
if this_user.id not in self.users:
|
||||||
self.last_throttled = int(time.time())
|
self.users[this_user.id] = {
|
||||||
self.now_rate = self.default_rate
|
'last_throttled': int(time.time()),
|
||||||
self.count_throttled = 0
|
'count_throttled': 0,
|
||||||
|
'now_rate': self.default_rate,
|
||||||
|
}
|
||||||
|
|
||||||
return await handler(event, data)
|
return await handler(event, data)
|
||||||
else:
|
else:
|
||||||
if self.count_throttled == 0:
|
if int(time.time()) - self.users[this_user.id]['last_throttled'] >= self.users[this_user.id]['now_rate']:
|
||||||
self.count_throttled += 1
|
self.users.pop(this_user.id)
|
||||||
self.now_rate += self.default_rate * 2
|
|
||||||
|
|
||||||
return await handler(event, data)
|
return await handler(event, data)
|
||||||
elif self.count_throttled == 1:
|
|
||||||
self.count_throttled += 1
|
|
||||||
self.now_rate += self.default_rate * 2
|
|
||||||
|
|
||||||
await event.reply("<b>❗ Пожалуйста, не спамьте.</b>")
|
|
||||||
else:
|
else:
|
||||||
self.now_rate = 3
|
self.users[this_user.id]['last_throttled'] = int(time.time())
|
||||||
|
|
||||||
if self.count_throttled == 2:
|
if self.users[this_user.id]['count_throttled'] == 0:
|
||||||
self.count_throttled = 3
|
self.users[this_user.id]['count_throttled'] = 1
|
||||||
await event.reply("<b>❗ Бот не будет отвечать до прекращения спама.</b>")
|
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("<b>❗ Пожалуйста, не спамьте.\n"
|
||||||
|
"❗ Please, do not spam.</b>")
|
||||||
|
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("<b>❗ Бот не будет отвечать до прекращения спама.\n"
|
||||||
|
"❗ The bot will not respond until the spam stops.</b>")
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
|
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
|
|
||||||
|
|||||||
@@ -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:
|
elif message.sticker is not None:
|
||||||
await bot.send_sticker(user_id, message.sticker.file_id, reply_markup=reply)
|
await bot.send_sticker(user_id, message.sticker.file_id, reply_markup=reply)
|
||||||
elif message.dice is not None:
|
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:
|
elif message.location is not None:
|
||||||
await bot.send_location(user_id, latitude=message.location.latitude, longitude=message.location.longitude,
|
await bot.send_location(user_id, latitude=message.location.latitude, longitude=message.location.longitude,
|
||||||
reply_markup=reply)
|
reply_markup=reply)
|
||||||
@@ -92,7 +92,7 @@ def ded(get_text: str) -> str:
|
|||||||
return get_text
|
return get_text
|
||||||
|
|
||||||
|
|
||||||
# Очистка HTML тэгов
|
# Очистка текста от HTML тэгов
|
||||||
def clear_html(get_text: str) -> str:
|
def clear_html(get_text: str) -> str:
|
||||||
if get_text is not None:
|
if get_text is not None:
|
||||||
if "<" in get_text: get_text = get_text.replace("<", "*")
|
if "<" in get_text: get_text = get_text.replace("<", "*")
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user