اضافه شدن قابلیت اخطار

This commit is contained in:
2026-11-16 16:34:19 +03:30
parent 8e239041b6
commit 188c6ad662
2 changed files with 49 additions and 20 deletions
+4 -1
View File
@@ -8,4 +8,7 @@ __pycache__/
venv/ venv/
# pycharm # pycharm
.idea/ .idea/
# db
warning.json
+45 -19
View File
@@ -2,10 +2,11 @@ import logging
from bot import bot from bot import bot
from balethon.conditions import equals, regex from balethon.conditions import equals, regex
from balethon.objects import ChatPermissions from balethon.objects import ChatPermissions
from jsondb import JsonDB
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
db_warning = JsonDB('warning.json')
async def is_admin(chat_id, user_id): async def is_admin(chat_id, user_id):
@@ -70,7 +71,8 @@ async def start(message):
"• خوش‌آمدگویی خودکار\n" "• خوش‌آمدگویی خودکار\n"
"• اخراج کاربر\n" "• اخراج کاربر\n"
"• سکوت کاربر\n" "• سکوت کاربر\n"
"• لغو سکوت کاربر\n\n" "• لغو سکوت کاربر\n"
"• اخطار کاربر\n\n"
"💡 ربات را به گروه اضافه و ادمین کنید." "💡 ربات را به گروه اضافه و ادمین کنید."
) )
@@ -82,6 +84,7 @@ async def help_cmd(message):
"🔹 سکوت\n" "🔹 سکوت\n"
"🔹 رفع سکوت\n" "🔹 رفع سکوت\n"
"🔹 اخراج\n" "🔹 اخراج\n"
"🔹 اخطار\n"
"🔹 /help\n\n" "🔹 /help\n\n"
"📝 همه دستورات مدیریتی باید روی پیام کاربر ریپلای شوند." "📝 همه دستورات مدیریتی باید روی پیام کاربر ریپلای شوند."
) )
@@ -143,30 +146,53 @@ async def mute_by_reply(message):
@bot.on_message(regex(r"^(رفع سکوت|unmute)$")) @bot.on_message(regex(r"^(اخطار|warning)$"))
async def mute_by_reply(message): async def warning(message):
if not await guard(message): if not await guard(message):
return return
target = await validate_target(message) target = await validate_target(message)
perms = ChatPermissions(
can_send_messages=True, # اجازه ارسال متن
can_send_photos=False, # بدون عکس
can_send_videos=False, # بدون ویدیو
can_send_audios=False, # بدون صدا
can_send_documents=False, # بدون فایل
can_send_other_messages=False, # ← کلید ممنوعیت گیف، استیکر، بازی
can_invite_users=True, # بدون دعوت دیگران
can_pin_messages=False, # بدون پین کردن پیام
can_change_info=False, # بدون تغییر اطلاعات گروه
)
if not target: if not target:
return return
await bot.restrict_chat_member(message.chat.id, target, perms)
try:
# خواندن دیتابیس
db_data = db_warning.read()
chat_id_str = str(message.chat.id)
target_id_str = str(target) # اخطار برای کاربر هدف (نه ادمین)
# اگر گروه در دیتابیس نبود، بساز
if chat_id_str not in db_data:
db_data[chat_id_str] = {}
# اگر کاربر اخطار قبلی نداشت، صفر بذار
if target_id_str not in db_data[chat_id_str]:
db_data[chat_id_str][target_id_str] = 0
# یک اخطار اضافه کن
db_data[chat_id_str][target_id_str] += 1
current_warnings = db_data[chat_id_str][target_id_str]
# ذخیره در دیتابیس
db_warning.write(db_data)
await message.reply(f"⚠️ اخطار ثبت شد. (اخطار {current_warnings} از ۳)")
# اگر به ۳ رسید، اخراج کن
if current_warnings >= 3:
await bot.ban_chat_member(
chat_id=message.chat.id,
user_id=target,
)
await message.reply("🚫 کاربر به دلیل 3 اخطار از گروه اخراج شد.")
# ریست کردن اخطارهای کاربر بعد از اخراج
db_data[chat_id_str][target_id_str] = 0
db_warning.write(db_data)
except Exception as e:
logger.error(e)
await message.reply("⚠️ خطا در ثبت اخطار.")