From 017d21c8d831eb683e46f21617d310a14ca8921c Mon Sep 17 00:00:00 2001 From: HibiKier <775757368@qq.com> Date: Mon, 10 Feb 2025 21:10:07 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=B7=BB=E5=8A=A0=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=90=8C=E6=84=8F=E7=BE=A4=E7=BB=84=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- poetry.lock | 10 ++-- pyproject.toml | 1 + zhenxun/builtin_plugins/record_request.py | 51 +++++++++++++++++-- .../builtin_plugins/sign_in/_data_source.py | 2 +- .../superuser/request_manage.py | 17 +++++-- .../web_ui/api/tabs/dashboard/data_source.py | 14 +++-- .../web_ui/api/tabs/main/data_source.py | 17 +++++-- zhenxun/models/fg_request.py | 17 ++++--- zhenxun/models/sign_user.py | 18 +++++++ zhenxun/utils/http_utils.py | 4 +- 10 files changed, 120 insertions(+), 31 deletions(-) diff --git a/poetry.lock b/poetry.lock index 046b43e8..0413cfe5 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2319,14 +2319,14 @@ reference = "aliyun" [[package]] name = "nonebot-plugin-waiter" -version = "0.8.0" +version = "0.8.1" description = "An alternative for got-and-reject in Nonebot" optional = false python-versions = ">=3.9" groups = ["main"] files = [ - {file = "nonebot_plugin_waiter-0.8.0-py3-none-any.whl", hash = "sha256:eabf284f783a1e534591a552139a747623e621453a790b554854bc8a386f783f"}, - {file = "nonebot_plugin_waiter-0.8.0.tar.gz", hash = "sha256:b750f2a3b255ebe9ab7ce75056c1e829281d898c6da8d81552147df2b1686270"}, + {file = "nonebot_plugin_waiter-0.8.1-py3-none-any.whl", hash = "sha256:3e1afc8f134496d3a4ecefd9c3a2a98d6ef28a5318268cb22b99a0ef61a44080"}, + {file = "nonebot_plugin_waiter-0.8.1.tar.gz", hash = "sha256:5e54213dfea1fd8a1e20dbe6d93b7881f35cbeedf80005148cdc39c1fd2ccc0f"}, ] [package.dependencies] @@ -5057,4 +5057,8 @@ reference = "aliyun" [metadata] lock-version = "2.1" python-versions = "^3.10" +<<<<<<< Updated upstream content-hash = "7f22f8300bad17b4c821d401391663d328aee17ffc2be9230b45cb9c27f212ea" +======= +content-hash = "5409b38a3c23d8e2e6d464db225217f1301e0319c6019f4cbd3b354218dd191c" +>>>>>>> Stashed changes diff --git a/pyproject.toml b/pyproject.toml index 40cc4512..1b3685a0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -44,6 +44,7 @@ py-cpuinfo = "^9.0.0" nonebot-plugin-alconna = "^0.54.0" tenacity = "^9.0.0" nonebot-plugin-uninfo = ">0.4.1" +nonebot-plugin-waiter = "^0.8.1" [tool.poetry.group.dev.dependencies] nonebug = "^0.4" diff --git a/zhenxun/builtin_plugins/record_request.py b/zhenxun/builtin_plugins/record_request.py index 9cd2d952..d4b0c694 100644 --- a/zhenxun/builtin_plugins/record_request.py +++ b/zhenxun/builtin_plugins/record_request.py @@ -1,4 +1,6 @@ +import asyncio from datetime import datetime +import random import time from nonebot import on_message, on_request @@ -40,7 +42,15 @@ __plugin_meta__ = PluginMetadata( help="是否自动同意好友添加", type=bool, default_value=False, - ) + ), + RegisterConfig( + module="invite_manager", + key="AUTO_ADD_GROUP", + value=False, + help="是否自动同意邀请入群", + type=bool, + default_value=False, + ), ], ).to_dict(), ) @@ -81,6 +91,7 @@ async def _(bot: v12Bot | v11Bot, event: FriendRequestEvent, session: EventSessi "好友请求", target=event.user_id, ) + await asyncio.sleep(random.randint(1, 10)) await bot.set_friend_add_request(flag=event.flag, approve=True) await FriendUser.create( user_id=str(user["user_id"]), user_name=user["nickname"] @@ -104,7 +115,7 @@ async def _(bot: v12Bot | v11Bot, event: FriendRequestEvent, session: EventSessi await PlatformUtils.send_superuser( bot, f"*****一份好友申请*****\n" - f"ID: {f.id}" + f"ID: {f.id}\n" f"昵称:{nickname}({event.user_id})\n" f"自动同意:{'√' if base_config.get('AUTO_ADD_FRIEND') else '×'}\n" f"日期:{str(datetime.now()).split('.')[0]}\n" @@ -118,10 +129,10 @@ async def _(bot: v12Bot | v11Bot, event: FriendRequestEvent, session: EventSessi async def _(bot: v12Bot | v11Bot, event: GroupRequestEvent, session: EventSession): if event.sub_type != "invite": return - if str(event.user_id) in bot.config.superusers: + if str(event.user_id) in bot.config.superusers or base_config.get("AUTO_ADD_GROUP"): try: logger.debug( - "超级用户自动同意加入群聊", + "超级用户自动同意加入群聊或开启自动同意入群", "群聊请求", session=event.user_id, target=event.group_id, @@ -154,12 +165,42 @@ async def _(bot: v12Bot | v11Bot, event: GroupRequestEvent, session: EventSessio ) except ActionFailed as e: logger.error( - "超级用户自动同意加入群聊发生错误", + "超超级用户自动同意加入群聊或开启自动同意入群,加入群组发生错误", "群聊请求", session=event.user_id, target=event.group_id, e=e, ) + if str(event.user_id) not in bot.config.superusers and base_config.get( + "AUTO_ADD_GROUP" + ): + # 非超级用户邀请自动加入群组 + nickname = await FriendUser.get_user_name(str(event.user_id)) + f = await FgRequest.create( + request_type=RequestType.GROUP, + platform=session.platform, + bot_id=bot.self_id, + flag=event.flag, + user_id=str(event.user_id), + nickname=nickname, + group_id=str(event.group_id), + handle_type=RequestHandleType.APPROVE, + ) + await PlatformUtils.send_superuser( + bot, + f"*****一份入群申请*****\n" + f"ID:{f.id}\n" + f"申请人:{nickname}({event.user_id})\n群聊:" + f"{event.group_id}\n邀请日期:{datetime.now().replace(microsecond=0)}\n" + "注: 该请求已自动同意", + ) + await asyncio.sleep(random.randint(1, 5)) + await bot.send_private_msg( + user_id=event.user_id, + message=f"管理员已开启自动同意群组邀请,请不要让{BotConfig.self_nickname}受委屈哦(狠狠监控)" + "\n在群组中 群组管理员与群主 允许使用管理员帮助" + "(包括ban与功能开关等)\n请在群组中发送 '管理员帮助'", + ) elif Timer.check(f"{event.user_id}:{event.group_id}"): logger.debug( f"收录 用户[{event.user_id}] 群聊[{event.group_id}] 群聊请求", diff --git a/zhenxun/builtin_plugins/sign_in/_data_source.py b/zhenxun/builtin_plugins/sign_in/_data_source.py index 8e4a56e4..607d135b 100644 --- a/zhenxun/builtin_plugins/sign_in/_data_source.py +++ b/zhenxun/builtin_plugins/sign_in/_data_source.py @@ -83,7 +83,7 @@ class SignManage: ) data_list.append( [ - f"{i+1}", + f"{i + 1}", (bytes, 30, 30) if user[3] == "qq" else "", uid2name.get(user[0]), user[1], diff --git a/zhenxun/builtin_plugins/superuser/request_manage.py b/zhenxun/builtin_plugins/superuser/request_manage.py index 7419ec77..23b235bf 100644 --- a/zhenxun/builtin_plugins/superuser/request_manage.py +++ b/zhenxun/builtin_plugins/superuser/request_manage.py @@ -16,6 +16,7 @@ from nonebot_plugin_alconna import ( ) from nonebot_plugin_session import EventSession +from zhenxun.configs.config import BotConfig from zhenxun.configs.path_config import IMAGE_PATH from zhenxun.configs.utils import PluginExtraData from zhenxun.models.fg_request import FgRequest @@ -134,14 +135,15 @@ async def _( "r": RequestHandleType.REFUSED, "i": RequestHandleType.IGNORE, } + req = None handle_type = type_dict[handle[-1]] try: if handle_type == RequestHandleType.APPROVE: - await FgRequest.approve(bot, id) + req = await FgRequest.approve(bot, id) if handle_type == RequestHandleType.REFUSED: - await FgRequest.refused(bot, id) + req = await FgRequest.refused(bot, id) if handle_type == RequestHandleType.IGNORE: - await FgRequest.ignore(id) + req = await FgRequest.ignore(id) except NotFoundError: await MessageUtils.build_message("未发现此id的请求...").finish(reply_to=True) except Exception: @@ -149,7 +151,14 @@ async def _( reply_to=True ) logger.info("处理请求", arparma.header_result, session=session) - await MessageUtils.build_message("成功处理请求!").finish(reply_to=True) + await MessageUtils.build_message("成功处理请求!").send(reply_to=True) + if req and handle_type == RequestHandleType.APPROVE: + await bot.send_private_msg( + user_id=req.user_id, + message=f"管理员已同意此次群组邀请,请不要让{BotConfig.self_nickname}受委屈哦(狠狠监控)" + "\n在群组中 群组管理员与群主 允许使用管理员帮助" + "(包括ban与功能开关等)\n请在群组中发送 '管理员帮助'", + ) @_read_matcher.handle() diff --git a/zhenxun/builtin_plugins/web_ui/api/tabs/dashboard/data_source.py b/zhenxun/builtin_plugins/web_ui/api/tabs/dashboard/data_source.py index 23584f4c..25fe6758 100644 --- a/zhenxun/builtin_plugins/web_ui/api/tabs/dashboard/data_source.py +++ b/zhenxun/builtin_plugins/web_ui/api/tabs/dashboard/data_source.py @@ -11,6 +11,7 @@ from zhenxun.configs.config import BotConfig from zhenxun.models.bot_connect_log import BotConnectLog from zhenxun.models.chat_history import ChatHistory from zhenxun.models.statistics import Statistics +from zhenxun.services.log import logger from zhenxun.utils.platform import PlatformUtils from ....base_model import BaseResultModel, QueryModel @@ -63,10 +64,15 @@ class ApiDataSource: bot_info = BotInfo( self_id=bot.self_id, nickname=nickname, ava_url=ava_url, platform=platform ) - group_list, _ = await PlatformUtils.get_group_list(bot, True) - friend_list, _ = await PlatformUtils.get_friend_list(bot) - bot_info.group_count = len(group_list) - bot_info.friend_count = len(friend_list) + try: + group_list, _ = await PlatformUtils.get_group_list(bot, True) + friend_list, _ = await PlatformUtils.get_friend_list(bot) + bot_info.group_count = len(group_list) + bot_info.friend_count = len(friend_list) + except Exception as e: + logger.warning("获取bot好友/群组信息失败...", "WebUi", e=e) + bot_info.group_count = 0 + bot_info.friend_count = 0 bot_info.day_call = await Statistics.filter( create_time__gte=now - timedelta(hours=now.hour, minutes=now.minute), bot_id=bot.self_id, diff --git a/zhenxun/builtin_plugins/web_ui/api/tabs/main/data_source.py b/zhenxun/builtin_plugins/web_ui/api/tabs/main/data_source.py index 5526d516..f9ff6fca 100644 --- a/zhenxun/builtin_plugins/web_ui/api/tabs/main/data_source.py +++ b/zhenxun/builtin_plugins/web_ui/api/tabs/main/data_source.py @@ -110,11 +110,18 @@ class ApiDataSource: create_time__gte=now - timedelta(hours=now.hour), ).count() # 群聊数量 - select_bot.group_count = len(await PlatformUtils.get_group_list(select_bot.bot)) - # 好友数量 - select_bot.friend_count = len( - await PlatformUtils.get_friend_list(select_bot.bot) - ) + try: + select_bot.group_count = len( + (await PlatformUtils.get_group_list(select_bot.bot, True))[0] + ) + # 好友数量 + select_bot.friend_count = len( + (await PlatformUtils.get_friend_list(select_bot.bot))[0] + ) + except Exception as e: + logger.warning("获取bot好友/群组信息失败...", "WebUi", e=e) + select_bot.group_count = 0 + select_bot.friend_count = 0 select_bot.status = await BotConsole.get_bot_status(select_bot.self_id) # 连接时间 select_bot.connect_time = bot_live.get(select_bot.self_id) or 0 diff --git a/zhenxun/models/fg_request.py b/zhenxun/models/fg_request.py index 8d4dc03c..4aee1d73 100644 --- a/zhenxun/models/fg_request.py +++ b/zhenxun/models/fg_request.py @@ -1,3 +1,5 @@ +from typing_extensions import Self + from nonebot.adapters import Bot from tortoise import fields @@ -38,7 +40,7 @@ class FgRequest(Model): table_description = "好友群组请求" @classmethod - async def approve(cls, bot: Bot, id: int): + async def approve(cls, bot: Bot, id: int) -> Self: """同意请求 参数: @@ -48,10 +50,10 @@ class FgRequest(Model): 异常: NotFoundError: 未发现请求 """ - await cls._handle_request(bot, id, RequestHandleType.APPROVE) + return await cls._handle_request(bot, id, RequestHandleType.APPROVE) @classmethod - async def refused(cls, bot: Bot, id: int): + async def refused(cls, bot: Bot, id: int) -> Self: """拒绝请求 参数: @@ -61,10 +63,10 @@ class FgRequest(Model): 异常: NotFoundError: 未发现请求 """ - await cls._handle_request(bot, id, RequestHandleType.REFUSED) + return await cls._handle_request(bot, id, RequestHandleType.REFUSED) @classmethod - async def ignore(cls, id: int): + async def ignore(cls, id: int) -> Self: """忽略请求 参数: @@ -73,7 +75,7 @@ class FgRequest(Model): 异常: NotFoundError: 未发现请求 """ - await cls._handle_request(None, id, RequestHandleType.IGNORE) + return await cls._handle_request(None, id, RequestHandleType.IGNORE) @classmethod async def expire(cls, id: int): @@ -93,7 +95,7 @@ class FgRequest(Model): bot: Bot | None, id: int, handle_type: RequestHandleType, - ): + ) -> Self: """处理请求 参数: @@ -126,3 +128,4 @@ class FgRequest(Model): sub_type="invite", approve=handle_type == RequestHandleType.APPROVE, ) + return req diff --git a/zhenxun/models/sign_user.py b/zhenxun/models/sign_user.py index 1b4e033b..5a9fc234 100644 --- a/zhenxun/models/sign_user.py +++ b/zhenxun/models/sign_user.py @@ -36,6 +36,24 @@ class SignUser(Model): table = "sign_users" table_description = "用户签到数据表" + @classmethod + async def get_user(cls, user_id: str, platform: str | None = None) -> "SignUser": + """获取签到用户 + + 参数: + user_id: 用户id + platform: 平台. + + 返回: + Self: SignUser + """ + user_console = await UserConsole.get_user(user_id, platform) + user, _ = await SignUser.get_or_create( + user_id=user_id, + defaults={"user_console": user_console, "platform": platform}, + ) + return user + @classmethod async def sign( cls, diff --git a/zhenxun/utils/http_utils.py b/zhenxun/utils/http_utils.py index 79149674..962c9e01 100644 --- a/zhenxun/utils/http_utils.py +++ b/zhenxun/utils/http_utils.py @@ -204,9 +204,9 @@ class AsyncHttpx: ) @classmethod - async def get_content(cls, url: str, **kwargs) -> bytes | None: + async def get_content(cls, url: str, **kwargs) -> bytes: res = await cls.get(url, **kwargs) - return res.content if res and res.status_code == 200 else None + return res.content @classmethod async def download_file(