sticker-captcha-bot/plugins/chat_member_update.py

103 lines
3.6 KiB
Python
Raw Normal View History

2022-07-05 08:29:02 +00:00
import contextlib
from datetime import datetime, timedelta
2023-09-11 12:48:52 +00:00
from cashews import cache
2022-07-05 08:29:02 +00:00
from pyrogram import filters
2023-09-11 12:48:52 +00:00
from pyrogram.enums import ChatMemberStatus
from pyrogram.types import ChatMemberUpdated
2022-07-05 08:29:02 +00:00
2023-09-11 12:48:52 +00:00
from pyromod.utils.errors import TimeoutConversationError
2024-01-22 13:06:47 +00:00
from sticker.languages import MSG_PUBLIC, ADMIN_MSG, MSG, VERIFY_TIME
2023-05-12 14:09:43 +00:00
from sticker.scheduler import add_delete_message_job
2023-10-18 15:50:27 +00:00
from sticker.service_message import ServiceMessage
2023-09-11 12:48:52 +00:00
from sticker.single_utils import Client
2022-07-05 09:16:16 +00:00
from sticker import bot, log
2022-07-05 08:29:02 +00:00
2023-09-11 12:48:52 +00:00
@bot.on_chat_member_updated()
async def invite(client: Client, chat_member_updated: ChatMemberUpdated):
chat = chat_member_updated.chat
if await cache.get(f"cid:{chat.id}"):
2022-07-05 08:29:02 +00:00
return
2023-09-11 12:48:52 +00:00
member = chat_member_updated.new_chat_member
old_member = chat_member_updated.old_chat_member
if not member:
return
if not member.user:
return
user = member.user
old_user = old_member.user if old_member else None
2023-09-11 13:05:50 +00:00
if user.is_verified or user.is_bot or user.is_deleted or user.is_support:
2022-07-05 08:29:02 +00:00
return
2023-09-11 12:48:52 +00:00
if member.status not in {ChatMemberStatus.MEMBER}:
return
2023-09-11 13:05:50 +00:00
if (
old_user
and old_user.id == user.id
2023-10-31 05:17:44 +00:00
and old_member.status
2023-09-11 13:05:50 +00:00
in {
ChatMemberStatus.ADMINISTRATOR,
ChatMemberStatus.OWNER,
ChatMemberStatus.MEMBER,
ChatMemberStatus.RESTRICTED,
}
):
return
if user.is_self:
with contextlib.suppress(Exception):
await log(chat, chat_member_updated.from_user, "NEW_GROUP")
if chat.username:
with contextlib.suppress(Exception):
await client.send_message(chat.id, MSG_PUBLIC)
2023-09-11 12:48:52 +00:00
return
2023-09-11 13:05:50 +00:00
from_user = chat_member_updated.from_user
if from_user and from_user.id == user.id:
from_user = None
2023-09-23 03:37:40 +00:00
if from_user and from_user.is_self:
2023-09-13 14:34:14 +00:00
return
2023-09-11 13:05:50 +00:00
if (
user
and from_user
and (await bot.get_chat_member(chat.id, from_user.id)).status
in {ChatMemberStatus.ADMINISTRATOR, ChatMemberStatus.OWNER}
):
2023-05-12 14:09:43 +00:00
try:
2023-09-11 12:48:52 +00:00
msg = await client.send_message(chat.id, ADMIN_MSG)
2023-05-12 14:09:43 +00:00
except Exception:
return
add_delete_message_job(msg)
return
2022-07-05 08:29:02 +00:00
try:
2024-01-22 13:06:47 +00:00
msg = await client.send_message(chat.id, MSG % (user.mention, user.mention))
2023-05-12 14:09:43 +00:00
except Exception:
2022-07-05 09:16:16 +00:00
return
try:
2022-07-06 15:20:06 +00:00
with contextlib.suppress(Exception):
await log(chat, user, "REQUEST")
2024-01-22 13:01:59 +00:00
msg_ = await client.listen(chat.id, filters=filters.user(user.id), timeout=VERIFY_TIME)
2022-07-05 08:29:02 +00:00
with contextlib.suppress(Exception):
await msg.delete()
if not msg_.sticker:
with contextlib.suppress(Exception):
2023-09-11 13:05:50 +00:00
await bot.ban_chat_member(
chat.id, user.id, datetime.now() + timedelta(minutes=5)
)
2022-07-05 09:16:16 +00:00
with contextlib.suppress(Exception):
await log(chat, user, "FAIL_ERROR")
2023-10-18 15:50:27 +00:00
await ServiceMessage.try_delete(user.id, chat.id)
2022-07-05 10:42:47 +00:00
else:
with contextlib.suppress(Exception):
await log(chat, user, "ACCEPT")
2022-07-05 09:16:16 +00:00
with contextlib.suppress(Exception):
await msg_.delete()
2022-07-05 08:29:02 +00:00
except TimeoutConversationError:
2022-07-05 09:16:16 +00:00
with contextlib.suppress(Exception):
await msg.delete()
2022-07-05 08:29:02 +00:00
with contextlib.suppress(Exception):
2023-09-11 13:05:50 +00:00
await bot.ban_chat_member(
chat.id, user.id, datetime.now() + timedelta(minutes=5)
)
2022-07-05 09:16:16 +00:00
with contextlib.suppress(Exception):
await log(chat, user, "FAIL_TIMEOUT")
2023-10-18 15:50:27 +00:00
await ServiceMessage.try_delete(user.id, chat.id)