2222from pyrogram .errors .exceptions .bad_request_400 import UserNotParticipant
2323from pyrogram .types import InlineKeyboardButton , InlineKeyboardMarkup
2424from tgbot_ping import get_runtime
25+ from token_bucket import Limiter , MemoryStorage
2526from youtubesearchpython import VideosSearch
2627
2728from client_init import create_app
28- from config import (AUTHORIZED_USER , ENABLE_CELERY , ENABLE_VIP , OWNER ,
29- REQUIRED_MEMBERSHIP )
29+ from config import (AUTHORIZED_USER , BURST , ENABLE_CELERY , ENABLE_FFMPEG ,
30+ ENABLE_VIP , OWNER , RATE , REQUIRED_MEMBERSHIP )
3031from constant import BotText
3132from db import InfluxDB , MySQL , Redis
3233from limit import VIP , verify_payment
4445
4546logging .info ("Authorized users are %s" , AUTHORIZED_USER )
4647
48+ # rate, capacity
49+ mem = MemoryStorage ()
50+ # 5 minutes, 2 bursts
51+ lim = Limiter (1 / RATE , BURST , mem )
52+
4753
4854def private_use (func ):
4955 def wrapper (client : "Client" , message : "types.Message" ):
@@ -145,7 +151,7 @@ def patch_handler(client: "Client", message: "types.Message"):
145151
146152
147153@app .on_message (filters .command (["uncache" ]))
148- def patch_handler (client : "Client" , message : "types.Message" ):
154+ def uncache_handler (client : "Client" , message : "types.Message" ):
149155 username = message .from_user .username
150156 link = message .text .split ()[1 ]
151157 if username == OWNER :
@@ -169,7 +175,7 @@ def ping_handler(client: "Client", message: "types.Message"):
169175
170176
171177@app .on_message (filters .command (["about" ]))
172- def help_handler (client : "Client" , message : "types.Message" ):
178+ def about_handler (client : "Client" , message : "types.Message" ):
173179 chat_id = message .chat .id
174180 client .send_chat_action (chat_id , "typing" )
175181 client .send_message (chat_id , bot_text .about )
@@ -296,6 +302,11 @@ def download_handler(client: "Client", message: "types.Message"):
296302 message .reply_text ("Channel download is disabled now. Please send me individual video link." , quote = True )
297303 red .update_metrics ("reject_channel" )
298304 return
305+ # non vip user, consume too many token
306+ if (not VIP ().check_vip (chat_id )) and (not lim .consume (str (chat_id ).encode (), 1 )):
307+ red .update_metrics ("rate_limit" )
308+ message .reply_text (bot_text .too_fast , quote = True )
309+ return
299310
300311 red .update_metrics ("video_request" )
301312 text = bot_text .get_receive_link_text ()
@@ -338,9 +349,13 @@ def download_resolution_callback(client: "Client", callback_query: types.Callbac
338349
339350@app .on_callback_query (filters .regex (r"convert" ))
340351def audio_callback (client : "Client" , callback_query : types .CallbackQuery ):
352+ if not ENABLE_FFMPEG :
353+ callback_query .answer ("Audio conversion is disabled now." )
354+ callback_query .message .reply_text ("Audio conversion is disabled now." )
355+ return
356+
341357 callback_query .answer (f"Converting to audio...please wait patiently" )
342358 Redis ().update_metrics ("audio_request" )
343-
344359 vmsg = callback_query .message
345360 audio_entrance (vmsg , client )
346361
0 commit comments