From ed4e63d6787fcae694b1ce7b5e5bb1ce0e782921 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Tue, 5 Jul 2022 17:16:16 +0800 Subject: [PATCH] support log --- config.gen.yml | 1 + plugins/invite.py | 7 +++++-- plugins/new_member.py | 6 +++++- plugins/service.py | 17 +++++++++++++++-- sticker/__init__.py | 15 +++++++++++++++ sticker/config.py | 1 + 6 files changed, 42 insertions(+), 5 deletions(-) diff --git a/config.gen.yml b/config.gen.yml index da679ab..d0b2de1 100644 --- a/config.gen.yml +++ b/config.gen.yml @@ -7,6 +7,7 @@ bot_token: "TOKEN_HERE" # Either debug logging is enabled or not debug: "False" +log_channel: "0" # socks5 proxy_addr: "" diff --git a/plugins/invite.py b/plugins/invite.py index f94b9b7..cea0d9f 100644 --- a/plugins/invite.py +++ b/plugins/invite.py @@ -3,9 +3,9 @@ import contextlib from pyrogram.types import ChatMemberUpdated from sticker.single_utils import Client -from sticker import bot +from sticker import bot, log -MSG_PUBLIC = """您好,我发现此群组为公开群组,您需要联系创建者打开 `管理员批准后才能入群` 功能,我才能正常工作。""" +MSG_PUBLIC = """您好,我发现此群组为公开群组,您需要联系创建者打开 `管理员批准后才能入群` 功能,我就能更好地工作。""" MSG_SUCCESS = """验证成功,您已经成为群组的一员了!""" MSG_FAILURE = """验证失败,请重试。""" @@ -14,6 +14,9 @@ MSG_FAILURE = """验证失败,请重试。""" 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 c553f87..2ccfd0f 100644 --- a/plugins/new_member.py +++ b/plugins/new_member.py @@ -4,7 +4,7 @@ from pyrogram.types import ChatJoinRequest from pyrogram import filters from sticker.single_utils import Client -from sticker import bot +from sticker import bot, log from pyromod.utils.errors import TimeoutConversationError @@ -25,7 +25,11 @@ async def new_member(client: Client, chat_join_request: ChatJoinRequest): with contextlib.suppress(Exception): await client.send_message(user.id, MSG_SUCCESS) await chat_join_request.approve() + with contextlib.suppress(Exception): + await log(chat, user, "ACCEPT") except TimeoutConversationError: with contextlib.suppress(Exception): await client.send_message(user.id, MSG_FAILURE) await chat_join_request.decline() + with contextlib.suppress(Exception): + await log(chat, user, "FAIL_TIMEOUT") diff --git a/plugins/service.py b/plugins/service.py index afc0322..558fcc3 100644 --- a/plugins/service.py +++ b/plugins/service.py @@ -6,7 +6,7 @@ from pyrogram.enums import MessageServiceType from pyrogram import filters from sticker.single_utils import Client, Message -from sticker import bot +from sticker import bot, log from pyromod.utils.errors import TimeoutConversationError @@ -25,7 +25,10 @@ async def chat_members_handle(client: Client, message: Message): return try: msg = await message.reply(MSG % user.mention) - msg_ = await client.listen(message.chat, filters=filters.user(user.id), timeout=30) + except Exception as e: + return + try: + msg_ = await client.listen(chat.id, filters=filters.user(user.id), timeout=30) with contextlib.suppress(Exception): await msg.delete() if not msg_.sticker: @@ -33,8 +36,18 @@ async def chat_members_handle(client: Client, message: Message): 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): + await log(chat, user, "FAIL_ERROR") + with contextlib.suppress(Exception): + await msg_.delete() + with contextlib.suppress(Exception): + await log(chat, user, "ACCEPT") 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): + await log(chat, user, "FAIL_TIMEOUT") diff --git a/sticker/__init__.py b/sticker/__init__.py index b46b3cc..cb35eab 100644 --- a/sticker/__init__.py +++ b/sticker/__init__.py @@ -46,3 +46,18 @@ bot = Client("sticker", ipv6=Config.IPV6, proxy=Config.PROXY, plugins={"root": "plugins"}) + + +async def log(chat, user, action): + if not Config.LOG_CHANNEL: + return + me = await bot.get_me() + event = {"FAIL_ERROR": "回答错误", "FAIL_TIMEOUT": "回答超时", "ACCEPT": "通过验证", "NEW_GROUP": "加入群组"} + msg = """#%s +群组: %s +群组id: %s +用户: #id%s +OPBot: #bot%s +事件: %s""" + msg %= (action, chat.title, chat.id, user.id if user else "", me.id, event[action]) + await bot.send_message(Config.LOG_CHANNEL, msg) diff --git a/sticker/config.py b/sticker/config.py index 6649f9a..243b39b 100644 --- a/sticker/config.py +++ b/sticker/config.py @@ -34,6 +34,7 @@ class Config: API_ID = int(os.environ.get("API_ID", config["api_id"])) API_HASH = os.environ.get("API_HASH", config["api_hash"]) BOT_TOKEN = os.environ.get("BOT_TOKEN", config["bot_token"]) + LOG_CHANNEL = int(os.environ.get("LOG_CHANNEL", config["log_channel"])) STRING_SESSION = os.environ.get("STRING_SESSION") DEBUG = strtobool(os.environ.get("PGM_DEBUG", config["debug"])) IPV6 = strtobool(os.environ.get("PGM_IPV6", config["ipv6"]))