160 lines
4.1 KiB
Python
160 lines
4.1 KiB
Python
import asyncio
|
|
import logging
|
|
|
|
from bot import bot
|
|
from balethon.conditions import equals, regex
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
LINK_REGEX = (
|
|
r"(https?:\/\/[^\s]+|"
|
|
r"ftp:\/\/[^\s]+|"
|
|
r"www\.[^\s]+|"
|
|
r"[a-zA-Z0-9-]+\.[a-zA-Z]{2,}(?:\/[^\s]*)?)"
|
|
)
|
|
|
|
GROUP_ID_PATTERNS = [
|
|
r"@[a-zA-Z0-9_]{5,}",
|
|
r"id:\s*\d{5,}",
|
|
r"آیدی:\s*\d{5,}",
|
|
r"شناسه:\s*\d{5,}",
|
|
]
|
|
|
|
|
|
async def is_admin(chat_id, user_id):
|
|
member = await bot.get_chat_member(chat_id, user_id)
|
|
return member.status in ("creator", "administrator")
|
|
|
|
|
|
async def is_bot_admin(chat_id):
|
|
me = await bot.get_me()
|
|
return await is_admin(chat_id, me.id)
|
|
|
|
|
|
async def guard(message):
|
|
if not await is_admin(message.chat.id, message.author.id):
|
|
await message.reply("⛔ فقط ادمینها مجاز هستند.")
|
|
return False
|
|
|
|
if not await is_bot_admin(message.chat.id):
|
|
await message.reply("⛔ ربات باید ادمین گروه باشد.")
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
def extract_target(message):
|
|
if message.reply_to_message:
|
|
return message.reply_to_message.author.id
|
|
return None
|
|
|
|
|
|
async def validate_target(message):
|
|
target = extract_target(message)
|
|
|
|
if not target:
|
|
await message.reply("📝 روی پیام کاربر ریپلای کنید.")
|
|
return None
|
|
|
|
me = await bot.get_me()
|
|
|
|
if target == me.id:
|
|
await message.reply("❌ نمیتوانید ربات را هدف قرار دهید.")
|
|
return None
|
|
|
|
if await is_admin(message.chat.id, target):
|
|
await message.reply("❌ نمیتوانید ادمین را هدف قرار دهید.")
|
|
return None
|
|
|
|
return target
|
|
|
|
|
|
# =========================
|
|
# Commands
|
|
# =========================
|
|
|
|
@bot.on_message(equals("/start"))
|
|
async def start(message):
|
|
if message.chat.type == "private":
|
|
await message.reply(
|
|
"👋 سلام\n\n"
|
|
"🤖 ربات مدیریت گروه\n\n"
|
|
"🎯 قابلیتها:\n"
|
|
"• خوشآمدگویی خودکار\n"
|
|
"• حذف لینکهای تبلیغاتی\n"
|
|
"• اخراج کاربر\n"
|
|
"• سکوت کاربر\n"
|
|
"• لغو سکوت کاربر\n\n"
|
|
"💡 ربات را به گروه اضافه و ادمین کنید."
|
|
)
|
|
else:
|
|
await message.reply("✅ ربات مدیریت گروه فعال شد.")
|
|
|
|
|
|
@bot.on_message(equals("/help"))
|
|
async def help_cmd(message):
|
|
await message.reply(
|
|
"🤖 راهنمای ربات مدیریت گروه\n\n"
|
|
"🔹 /mute [minute]\n"
|
|
"🔹 /unmute\n"
|
|
"🔹 /ban\n"
|
|
"🔹 /help\n\n"
|
|
"📝 همه دستورات باید روی پیام کاربر ریپلای شوند."
|
|
)
|
|
|
|
|
|
@bot.on_message(equals("/ban"))
|
|
async def ban(message):
|
|
if not await guard(message):
|
|
return
|
|
|
|
target = await validate_target(message)
|
|
|
|
if not target:
|
|
return
|
|
|
|
try:
|
|
await bot.ban_chat_member(
|
|
chat_id=message.chat.id,
|
|
user_id=target,
|
|
)
|
|
|
|
await message.reply("✅ کاربر از گروه اخراج شد.")
|
|
|
|
except Exception as e:
|
|
logger.error(e)
|
|
await message.reply("⚠️ خطا در اخراج کاربر.")
|
|
|
|
|
|
# =========================
|
|
# Normal Handlers (Always Last)
|
|
# =========================
|
|
|
|
@bot.on_message(regex(LINK_REGEX))
|
|
async def delete_links(message):
|
|
try:
|
|
await message.delete()
|
|
logger.info(
|
|
f"Advertisement deleted | "
|
|
f"chat={message.chat.id} "
|
|
f"user={message.author.id}"
|
|
)
|
|
except Exception as e:
|
|
logger.error(e)
|
|
|
|
@bot.on_message()
|
|
async def welcome(message):
|
|
|
|
if not message.new_chat_members:
|
|
return
|
|
|
|
for member in message.new_chat_members:
|
|
|
|
if member.is_bot:
|
|
continue
|
|
|
|
await message.reply(
|
|
f"👋 سلام {member.first_name} عزیز\n"
|
|
f"به گروه {message.chat.title} خوش آمدید.\n"
|
|
"لطفاً قوانین گروه را رعایت کنید."
|
|
) |