اضافه شدن قابلت رفع اسپم
This commit is contained in:
+105
@@ -3,11 +3,14 @@ from bot import bot
|
||||
from balethon.conditions import equals, regex
|
||||
from balethon.objects import ChatPermissions
|
||||
from jsondb import JsonDB
|
||||
import time
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
db_warning = JsonDB('warning.json')
|
||||
|
||||
spam_memory = {}
|
||||
|
||||
|
||||
async def is_admin(chat_id, user_id):
|
||||
member = await bot.get_chat_member(chat_id, user_id)
|
||||
@@ -145,6 +148,30 @@ async def mute_by_reply(message):
|
||||
await bot.restrict_chat_member(message.chat.id, target, perms)
|
||||
|
||||
|
||||
@bot.on_message(regex(r"^(رفع سکوت|unmute)$"))
|
||||
async def mute_by_reply(message):
|
||||
if not await guard(message):
|
||||
return
|
||||
|
||||
target = await validate_target(message)
|
||||
|
||||
perms = ChatPermissions(
|
||||
can_send_messages=True,
|
||||
can_send_photos=True,
|
||||
can_send_videos=True,
|
||||
can_send_audios=True,
|
||||
can_send_documents=True,
|
||||
can_send_other_messages=False,
|
||||
can_send_media_messages=False,
|
||||
can_invite_users=False,
|
||||
can_pin_messages=False,
|
||||
can_change_info=False
|
||||
)
|
||||
|
||||
if not target:
|
||||
return
|
||||
await bot.restrict_chat_member(message.chat.id, target, perms)
|
||||
|
||||
|
||||
@bot.on_message(regex(r"^(اخطار|warning)$"))
|
||||
async def warning(message):
|
||||
@@ -196,6 +223,84 @@ async def warning(message):
|
||||
|
||||
|
||||
|
||||
def get_message_signature(message):
|
||||
if message.text:
|
||||
return message.text.strip()
|
||||
return None
|
||||
'''if getattr(message, 'photo', None):
|
||||
return f"photo_{message.photo[-1].file_id}"
|
||||
if getattr(message, 'video', None):
|
||||
return f"video_{message.video.file_id}"
|
||||
if getattr(message, 'animation', None):
|
||||
return f"gif_{message.animation.file_id}"
|
||||
if getattr(message, 'sticker', None):
|
||||
return f"sticker_{message.sticker.file_id}"
|
||||
return f"other_{time.time()}"'''
|
||||
|
||||
|
||||
@bot.on_message()
|
||||
async def anti_spam(message):
|
||||
# ۱. بررسی ربات و ادمین بودن
|
||||
if message.author.is_bot:
|
||||
return
|
||||
if await is_admin(message.chat.id, message.author.id):
|
||||
return
|
||||
|
||||
chat_id = message.chat.id
|
||||
user_id = message.author.id
|
||||
current_time = time.time()
|
||||
signature = get_message_signature(message)
|
||||
|
||||
# ۲. ساخت دفترچه
|
||||
if chat_id not in spam_memory:
|
||||
spam_memory[chat_id] = {}
|
||||
if user_id not in spam_memory[chat_id]:
|
||||
spam_memory[chat_id][user_id] = {}
|
||||
|
||||
# ۳. ذخیره پیام جدید (کلید: زمان، مقدار: امضا)
|
||||
spam_memory[chat_id][user_id][current_time] = signature
|
||||
|
||||
# ۴. پیدا کردن پیامهای قدیمی برای حذف
|
||||
user_history = spam_memory[chat_id][user_id]
|
||||
old_times_to_delete = [] # لیستی از زمانهایی که باید پاک بشن
|
||||
|
||||
for msg_time in user_history:
|
||||
# اگه زمان پیام از ۱۰ ثانیه پیش کمتر بوده (یعنی قدیمی شده)
|
||||
if current_time - msg_time > 10:
|
||||
old_times_to_delete.append(msg_time)
|
||||
|
||||
# حالا که حلقه تموم شد، پیامهای قدیمی رو حذف میکنیم
|
||||
for old_time in old_times_to_delete:
|
||||
del user_history[old_time]
|
||||
|
||||
# ۵. حالا بشمار ببین چند تا پیام مشابه توی ۱۰ ثانیه اخیر (که مونده) هست
|
||||
similar_count = 0
|
||||
for msg_time, msg_sig in user_history.items():
|
||||
if msg_sig == signature:
|
||||
similar_count += 1
|
||||
|
||||
# ۶. اگر 5 بار یا بیشتر بود -> اسپم!
|
||||
if similar_count >= 5:
|
||||
# دفترچه کاربر رو خالی کن
|
||||
spam_memory[chat_id][user_id].clear()
|
||||
|
||||
try:
|
||||
# مجوزهای سکوت
|
||||
perms = ChatPermissions(
|
||||
can_send_messages=False, can_send_photos=False, can_send_videos=False,
|
||||
can_send_audios=False, can_send_documents=False, can_send_other_messages=False,
|
||||
can_invite_users=False, can_pin_messages=False, can_change_info=False
|
||||
)
|
||||
|
||||
# اعمال سکوت
|
||||
await bot.restrict_chat_member(chat_id, user_id, perms)
|
||||
|
||||
# اطلاعرسانی
|
||||
await message.reply(f"🚨 کاربر {message.author.first_name} به دلیل ارسال پیام تکراری سکوت شد!")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"خطا در ضد اسپم: {e}")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user