diff --git a/plugins/service.py b/plugins/chat_member_update.py similarity index 50% rename from plugins/service.py rename to plugins/chat_member_update.py index b06cce1..74b6b5f 100644 --- a/plugins/service.py +++ b/plugins/chat_member_update.py @@ -1,45 +1,70 @@ import contextlib - from datetime import datetime, timedelta -from pyrogram.enums import MessageServiceType, ChatMemberStatus +from cashews import cache from pyrogram import filters - -from sticker.scheduler import add_delete_message_job -from sticker.single_utils import Client, Message -from sticker import bot, log +from pyrogram.enums import ChatMemberStatus +from pyrogram.types import ChatMemberUpdated from pyromod.utils.errors import TimeoutConversationError +from sticker.scheduler import add_delete_message_job +from sticker.single_utils import Client +from sticker import bot, log +MSG_PUBLIC = """您好,我发现此群组为公开群组,您需要联系创建者打开 `管理员批准后才能入群` 功能,我就能更好地工作。""" +MSG_SUCCESS = """验证成功,您已经成为群组的一员了!""" +MSG_FAILURE = """验证失败,请重试。""" MSG = """您好 %s ,当前群组开启了验证功能。 您需要在 30 秒内发送任意一个 贴纸 来完成验证。""" ADMIN_MSG = """管理员邀请,自动放行。""" -@bot.on_message(filters.service) -async def chat_members_handle(client: Client, message: Message): - if message.service != MessageServiceType.NEW_CHAT_MEMBERS: +@bot.on_chat_member_updated() +async def invite(client: Client, chat_member_updated: ChatMemberUpdated): + chat = chat_member_updated.chat + old_chat_member = chat_member_updated.old_chat_member + if user := chat_member_updated.new_chat_member and not old_chat_member: + if not user.user: + return + if user.user.is_self: + with contextlib.suppress(Exception): + await log(chat, user.invited_by, "NEW_GROUP") + if chat.username: + with contextlib.suppress(Exception): + await client.send_message(chat.id, MSG_PUBLIC) + if await cache.get(f"cid:{chat.id}"): return - chat = message.chat - if message.new_chat_members: - for i in message.new_chat_members: - if i.is_self: - return - user = message.new_chat_members[0] if message.new_chat_members else message.from_user + 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 if user.is_self or user.is_verified or user.is_bot or user.is_deleted or user.is_support: return - if message.new_chat_members and message.from_user and ( - await bot.get_chat_member(chat.id, message.from_user.id) + if member.status not in {ChatMemberStatus.MEMBER}: + return + 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 and chat_member_updated.from_user and ( + await bot.get_chat_member(chat.id, chat_member_updated.from_user.id) ).status in {ChatMemberStatus.ADMINISTRATOR, ChatMemberStatus.OWNER}: try: - msg = await message.reply(ADMIN_MSG) + msg = await client.send_message(chat.id, ADMIN_MSG) except Exception: return add_delete_message_job(msg) return try: - msg = await message.reply(MSG % user.mention) + msg = await client.send_message(chat.id, MSG % user.mention) except Exception: return try: @@ -49,8 +74,6 @@ async def chat_members_handle(client: Client, message: Message): with contextlib.suppress(Exception): await msg.delete() if not msg_.sticker: - with contextlib.suppress(Exception): - await message.delete() with contextlib.suppress(Exception): await bot.ban_chat_member(chat.id, user.id, datetime.now() + timedelta(minutes=5)) with contextlib.suppress(Exception): @@ -63,8 +86,6 @@ async def chat_members_handle(client: Client, message: Message): except TimeoutConversationError: with contextlib.suppress(Exception): await msg.delete() - with contextlib.suppress(Exception): - await message.delete() with contextlib.suppress(Exception): await bot.ban_chat_member(chat.id, user.id, datetime.now() + timedelta(minutes=5)) with contextlib.suppress(Exception): diff --git a/plugins/invite.py b/plugins/invite.py deleted file mode 100644 index cea0d9f..0000000 --- a/plugins/invite.py +++ /dev/null @@ -1,22 +0,0 @@ -import contextlib - -from pyrogram.types import ChatMemberUpdated - -from sticker.single_utils import Client -from sticker import bot, log - -MSG_PUBLIC = """您好,我发现此群组为公开群组,您需要联系创建者打开 `管理员批准后才能入群` 功能,我就能更好地工作。""" -MSG_SUCCESS = """验证成功,您已经成为群组的一员了!""" -MSG_FAILURE = """验证失败,请重试。""" - - -@bot.on_chat_member_updated() -async def invite(client: Client, chat_member_updated: ChatMemberUpdated): - chat = chat_member_updated.chat - if user := chat_member_updated.new_chat_member: - if user.user.is_self: - with contextlib.suppress(Exception): - await log(chat, user.invited_by, "NEW_GROUP") - if user.user.is_self and chat.username: - with contextlib.suppress(Exception): - await client.send_message(chat.id, MSG_PUBLIC) diff --git a/plugins/new_member.py b/plugins/new_member.py index f9533a7..e3201ee 100644 --- a/plugins/new_member.py +++ b/plugins/new_member.py @@ -1,5 +1,6 @@ import contextlib +from cashews import cache from pyrogram.types import ChatJoinRequest from pyrogram import filters @@ -20,6 +21,7 @@ MSG_FAILURE = """验证失败,请重试。""" @bot.on_chat_join_request() async def new_member(client: Client, chat_join_request: ChatJoinRequest): chat = chat_join_request.chat + await cache.set(f"cid:{chat.id}", "True", expire=3600, exist=True) user = chat_join_request.from_user add_decline_request_job(chat_join_request) try: diff --git a/plugins/reverify.py b/plugins/reverify.py index dc58c91..76af0db 100644 --- a/plugins/reverify.py +++ b/plugins/reverify.py @@ -38,7 +38,7 @@ async def re_verify(client: Client, message: Message): await message.delete() try: msg = await message.reply_to_message.reply(MSG % user.mention) - except Exception as e: + except Exception as _: return try: msg_ = await client.listen(chat.id, filters=filters.user(user.id), timeout=30) diff --git a/requirements.txt b/requirements.txt index 616106e..6f1228d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,8 @@ pyrogram==2.0.106 TgCrypto>=1.2.3 -PyYAML>=6.0 +PyYAML==6.0.1 coloredlogs>=15.0.1 -sqlitedict==2.0.0 -apscheduler==3.9.1 +sqlitedict==2.1.0 +apscheduler==3.10.4 +cashews==6.2.0 pytz diff --git a/sticker/__init__.py b/sticker/__init__.py index f2f735a..6a31a85 100644 --- a/sticker/__init__.py +++ b/sticker/__init__.py @@ -1,14 +1,17 @@ import contextlib -from coloredlogs import ColoredFormatter +import sys +from cashews import cache from datetime import datetime, timezone from logging import getLogger, StreamHandler, CRITICAL, INFO, basicConfig, DEBUG -from sticker.config import Config -from sticker.scheduler import scheduler +from coloredlogs import ColoredFormatter import pyromod.listen from pyrogram import Client -import sys +from sticker.config import Config +from sticker.scheduler import scheduler + +cache.setup("mem://") logs = getLogger(__name__) logging_format = "%(levelname)s [%(asctime)s] [%(name)s] %(message)s" logging_handler = StreamHandler()