From 8be2514ef2d7f5d3ea48b777b8ec197260be4132 Mon Sep 17 00:00:00 2001
From: omg-xtao <100690902+omg-xtao@users.noreply.github.com>
Date: Fri, 7 Oct 2022 00:32:29 +0800
Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=94=AF=E6=8C=81=E7=AE=A1?=
=?UTF-8?q?=E7=90=86=E5=91=98=E8=8E=B7=E5=8F=96=E7=BE=A4=E7=BB=84=E8=AF=A6?=
=?UTF-8?q?=E7=BB=86=E4=BF=A1=E6=81=AF?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
plugins/system/admin.py | 11 +++----
plugins/system/get_chat.py | 63 ++++++++++++++++++++++++++++++++++++++
plugins/system/log.py | 2 +-
3 files changed, 68 insertions(+), 8 deletions(-)
create mode 100644 plugins/system/get_chat.py
diff --git a/plugins/system/admin.py b/plugins/system/admin.py
index 639851c1..8a94ddca 100644
--- a/plugins/system/admin.py
+++ b/plugins/system/admin.py
@@ -1,3 +1,4 @@
+import contextlib
from telegram import Update
from telegram.error import BadRequest, Forbidden
from telegram.ext import CallbackContext, CommandHandler
@@ -60,15 +61,11 @@ class AdminPlugin(Plugin):
await message.reply_text("输入错误")
return
try:
- try:
- char = await context.bot.get_chat(chat_id)
- await message.reply_text(f"正在尝试退出群 {char.title}[{char.id}]")
- except (BadRequest, Forbidden):
- pass
+ with contextlib.suppress(BadRequest, Forbidden):
+ chat = await context.bot.get_chat(chat_id)
+ await message.reply_text(f"正在尝试退出群 {chat.title}[{chat.id}]")
await context.bot.leave_chat(chat_id)
except (BadRequest, Forbidden) as exc:
- logger.error(f"退出 chat_id[{chat_id}] 发生错误! 错误信息为 \n")
- logger.exception(exc)
await message.reply_text(f"退出 chat_id[{chat_id}] 发生错误! 错误信息为 {str(exc)}")
return
await message.reply_text(f"退出 chat_id[{chat_id}] 成功!")
diff --git a/plugins/system/get_chat.py b/plugins/system/get_chat.py
new file mode 100644
index 00000000..49b2fbea
--- /dev/null
+++ b/plugins/system/get_chat.py
@@ -0,0 +1,63 @@
+from typing import List
+
+from telegram import Update, Chat, ChatMember, ChatMemberOwner, ChatMemberAdministrator
+from telegram.error import BadRequest, Forbidden
+from telegram.ext import CommandHandler, CallbackContext
+
+from core.plugin import Plugin, handler
+from utils.bot import get_all_args
+from utils.decorators.admins import bot_admins_rights_check
+from utils.log import logger
+
+
+class GetChat(Plugin):
+ @staticmethod
+ def parse_chat(chat: Chat, admins: List[ChatMember]) -> str:
+ text = f"群 ID:{chat.id}
\n" \
+ f"群名称:{chat.title}
\n"
+ if chat.username:
+ text += f"群用户名:{chat.username}
\n"
+ if chat.description:
+ text += f"群简介:{chat.description}
\n"
+ if admins:
+ for admin in admins:
+ text += f"{admin.user.full_name} "
+ if isinstance(admin, ChatMemberAdministrator):
+ text += "C" if admin.can_change_info else "_"
+ text += "D" if admin.can_delete_messages else "_"
+ text += "R" if admin.can_restrict_members else "_"
+ text += "I" if admin.can_invite_users else "_"
+ text += "P" if admin.can_pin_messages else "_"
+ text += "V" if admin.can_manage_video_chats else "_"
+ text += "N" if admin.can_promote_members else "_"
+ text += "A" if admin.is_anonymous else "_"
+ elif isinstance(admin, ChatMemberOwner):
+ text += "创建者"
+ text += "\n"
+ return text
+
+ @handler(CommandHandler, command="get_chat", block=False)
+ @bot_admins_rights_check
+ async def get_chat(self, update: Update, context: CallbackContext):
+ user = update.effective_user
+ logger.info(f"用户 {user.full_name}[{user.id}] get_chat 命令请求")
+ message = update.effective_message
+ args = get_all_args(context)
+ if not args:
+ await message.reply_text("参数错误,请指定群 id !")
+ return
+ try:
+ chat_id = int(args[0])
+ if chat_id > 0:
+ await message.reply_text("参数错误,请指定群 id !")
+ return
+ except ValueError:
+ await message.reply_text("参数错误,请指定群 id !")
+ return
+ try:
+ chat = await message.get_bot().get_chat(args[0])
+ admins = await chat.get_administrators()
+ await message.reply_text(self.parse_chat(chat, admins), parse_mode="HTML")
+ except (BadRequest, Forbidden) as exc:
+ await message.reply_text(f"获取群信息失败,API 返回:{exc}")
+ return
diff --git a/plugins/system/log.py b/plugins/system/log.py
index dc4ac501..3f366e80 100644
--- a/plugins/system/log.py
+++ b/plugins/system/log.py
@@ -18,7 +18,7 @@ class Log(Plugin):
@bot_admins_rights_check
async def send_log(self, update: Update, _: CallbackContext):
user = update.effective_user
- logger.info(f"用户 {user.full_name}[{user.id}] send_log命令请求")
+ logger.info(f"用户 {user.full_name}[{user.id}] send_log 命令请求")
message = update.effective_message
if os.path.exists(error_log):
await message.reply_document(open(error_log, mode='rb+'), caption="Error Log")