sticker-captcha-bot/plugins/chat_member_update.py

107 lines
3.8 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
2023-05-12 14:09:43 +00:00
from sticker.scheduler import add_delete_message_job
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
MSG_PUBLIC = """您好,我发现此群组为公开群组,您需要联系创建者打开 `管理员批准后才能入群` 功能,我就能更好地工作。"""
MSG_SUCCESS = """验证成功,您已经成为群组的一员了!"""
MSG_FAILURE = """验证失败,请重试。"""
2022-07-05 08:29:02 +00:00
MSG = """您好 %s ,当前群组开启了验证功能。
您需要在 30 秒内发送任意一个 贴纸 来完成验证"""
2023-05-12 14:09:43 +00:00
ADMIN_MSG = """管理员邀请,自动放行。"""
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
and old_user.status
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:
2023-09-11 12:48:52 +00:00
msg = await client.send_message(chat.id, MSG % 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")
2022-07-05 09:16:16 +00:00
msg_ = await client.listen(chat.id, filters=filters.user(user.id), timeout=30)
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")
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")