From 499e51e996fbd7e2a33af9c3605184438dda4b39 Mon Sep 17 00:00:00 2001 From: HibiKier <775757368@qq.com> Date: Tue, 27 Feb 2024 02:30:01 +0800 Subject: [PATCH] =?UTF-8?q?perf=F0=9F=91=8C:=20=E6=B7=BB=E5=8A=A0shortcut?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 1 + zhenxun/builtin_plugins/admin/ban/__init__.py | 96 ++++++++++++++----- .../builtin_plugins/admin/ban/_data_source.py | 40 ++++---- .../admin/plugin_switch/__init__.py | 30 ++++-- .../admin/plugin_switch/command.py | 2 - .../chat_history/chat_message_handle.py | 15 ++- zhenxun/models/chat_history.py | 21 ++-- 7 files changed, 140 insertions(+), 65 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 1a22fedc..6d295644 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -15,6 +15,7 @@ "nonebot", "onebot", "tobytes", + "unban", "userinfo", "zhenxun" ], diff --git a/zhenxun/builtin_plugins/admin/ban/__init__.py b/zhenxun/builtin_plugins/admin/ban/__init__.py index 289ef6b6..0aa3c57d 100644 --- a/zhenxun/builtin_plugins/admin/ban/__init__.py +++ b/zhenxun/builtin_plugins/admin/ban/__init__.py @@ -29,12 +29,31 @@ __plugin_meta__ = PluginMetadata( name="封禁用户/群组", description="你被逮捕了!丢进小黑屋!封禁用户以及群组,屏蔽消息", usage=""" - .ban [at] ?[小时] ?[分钟] - .unban - 示例:.ban @user - 示例:.ban @user 6 - 示例:.ban @user 3 10 - 示例:.unban @user + 普通管理员 + 格式: + ban [At用户] [时长] + + 示例: + ban @用户 : 永久拉黑用户 + ban @用户 100 : 拉黑用户100分钟 + unban @用户 : 从小黑屋中拉出来 + + 超级管理员额外命令 + 格式: + ban [At用户/用户Id] [时长] + ban列表: 获取所有Ban数据 + 群组ban列表: 获取群组Ban数据 + 用户ban列表: 获取用户Ban数据 + + 私聊下: + 示例: + ban 123456789 : 永久拉黑用户123456789 + ban 123456789 100 : 拉黑用户123456789 100分钟 + + ban -g 999999 : 拉黑群组为999999的群组 + + unban 123456789 : 从小黑屋中拉出来 + unban -g 999999 : 将群组9999999从小黑屋中拉出来 """.strip(), extra=PluginExtraData( author="HibiKier", @@ -54,19 +73,22 @@ __plugin_meta__ = PluginMetadata( ) -_matcher = on_alconna( +_ban_matcher = on_alconna( Alconna( - "ban-console", - Subcommand( - "ban", - Args["user?", [str, At]]["duration?", int], - Option("-g|--group", Args["group_id", str]), - ), - Subcommand( - "unban", - Args["user?", [str, At]], - Option("-g|--group", Args["group_id", str]), - ), + "ban", + Args["user?", [str, At]]["duration?", int], + Option("-g|--group", Args["group_id", str]), + ), + rule=admin_check("ban", "BAN_LEVEL"), + priority=5, + block=True, +) + +_unban_matcher = on_alconna( + Alconna( + "unban", + Args["user?", [str, At]], + Option("-g|--group", Args["group_id", str]), ), rule=admin_check("ban", "BAN_LEVEL"), priority=5, @@ -76,14 +98,35 @@ _matcher = on_alconna( _status_matcher = on_alconna( Alconna( "ban-status", - Option("-u|--user", Args["user_id", str]), - Option("-g|--group", Args["group_id", str]), + Option("-u|--user", action=store_true, help_text="过滤用户"), + Option("-g|--group", action=store_true, help_text="过滤群组"), ), permission=SUPERUSER, priority=1, block=True, ) -# TODO: shortcut + +_status_matcher.shortcut( + "ban列表", + command="ban-status", + arguments=[], + prefix=True, +) + + +_status_matcher.shortcut( + "用户ban列表", + command="ban-status", + arguments=["--user"], + prefix=True, +) + +_status_matcher.shortcut( + "群组ban列表", + command="ban-status", + arguments=["--group"], + prefix=True, +) @_status_matcher.handle() @@ -94,15 +137,20 @@ async def _( user_id: Match[str], group_id: Match[str], ): + filter_type = None + if arparma.find("user"): + filter_type = "user" + if arparma.find("group"): + filter_type = "group" _user_id = user_id.result if user_id.available else None _group_id = group_id.result if group_id.available else None - if image := await BanManage.build_ban_image(_user_id, _group_id): + if image := await BanManage.build_ban_image(filter_type): await Image(image.pic2bs4()).finish(reply=True) else: await Text("数据为空捏...").finish(reply=True) -@_matcher.assign("ban") +@_ban_matcher.handle() async def _( bot: Bot, session: EventSession, @@ -150,7 +198,7 @@ async def _( await Text(f"对 {at_msg} 狠狠惩戒了一番,一脚踢进了小黑屋!").finish(reply=True) -@_matcher.assign("unban") +@_unban_matcher.handle() async def _( bot: Bot, session: EventSession, diff --git a/zhenxun/builtin_plugins/admin/ban/_data_source.py b/zhenxun/builtin_plugins/admin/ban/_data_source.py index 7418eb47..4264258d 100644 --- a/zhenxun/builtin_plugins/admin/ban/_data_source.py +++ b/zhenxun/builtin_plugins/admin/ban/_data_source.py @@ -1,33 +1,35 @@ import time +from typing import Literal from nonebot_plugin_session import EventSession from zhenxun.models.ban_console import BanConsole from zhenxun.models.level_user import LevelUser -from zhenxun.utils.image_utils import ImageTemplate +from zhenxun.utils.image_utils import BuildImage, ImageTemplate class BanManage: @classmethod - async def build_ban_image(cls, user_id: str | None, group_id: str | None): + async def build_ban_image( + cls, + filter_type: Literal["group", "user"] | None, + ) -> BuildImage | None: + """构造Ban列表图片 + + 参数: + filter_type: 过滤类型 + + 返回: + BuildImage | None: Ban列表图片 + """ data_list = None - if not user_id and not group_id: + if not filter_type: data_list = await BanConsole.all() - elif user_id: - if group_id: - data_list = await BanConsole.filter( - user_id=user_id, group_id=group_id - ).all() - else: - data_list = await BanConsole.filter( - user_id=user_id, group_id__isnull=True - ).all() - else: - if group_id: - data_list = await BanConsole.filter( - user_id__isnull=True, group_id=group_id - ).all() + elif filter_type == "user": + data_list = await BanConsole.filter(group_id__isnull=True).all() + elif filter_type == "group": + data_list = await BanConsole.filter(user_id__isnull=True).all() if not data_list: return None column_name = [ @@ -41,8 +43,8 @@ class BanManage: row_data = [] for data in data_list: duration = int((data.ban_time + data.duration - time.time()) / 60) - if duration < 0: - duration = 0 + if data.duration < 0: + duration = "∞" row_data.append( [ data.id, diff --git a/zhenxun/builtin_plugins/admin/plugin_switch/__init__.py b/zhenxun/builtin_plugins/admin/plugin_switch/__init__.py index 81a7bff8..f183c354 100644 --- a/zhenxun/builtin_plugins/admin/plugin_switch/__init__.py +++ b/zhenxun/builtin_plugins/admin/plugin_switch/__init__.py @@ -19,11 +19,28 @@ __plugin_meta__ = PluginMetadata( name="功能开关", description="对群组内的功能限制,超级用户可以对群组以及全局的功能被动开关限制", usage=""" - 开启/关闭[功能] - 群被动状态 - 开启全部被动 - 关闭全部被动 - 醒来/休息吧 + 普通管理员 + 格式: + 开启/关闭[功能名称] : 开关功能 + 群被动状态 : 查看被动技能开关状态 + 醒来 : 结束休眠 + 休息吧 : 群组休眠, 不会再响应命令 + + 示例: + 开启签到 : 开启签到 + 关闭签到 : 关闭签到 + + 超级管理员额外命令 + 格式: + 插件列表 + 开启/关闭[功能名称] ?[-t ["private", "p", "group", "g"](关闭类型)] ?[-g 群组Id] + + 私聊下: + 示例: + 开启签到 : 全局开启签到 + 关闭签到 : 全局关闭签到 + 关闭签到 p : 全局私聊关闭签到 + 关闭签到 -g 12345678 : 关闭群组12345678的签到功能(普通管理员无法开启) """.strip(), extra=PluginExtraData( author="HibiKier", @@ -43,9 +60,6 @@ __plugin_meta__ = PluginMetadata( ) -# TODO: shortcut - - @_status_matcher.assign("$main") async def _(bot: Bot, session: EventSession, arparma: Arparma): image = None diff --git a/zhenxun/builtin_plugins/admin/plugin_switch/command.py b/zhenxun/builtin_plugins/admin/plugin_switch/command.py index 0ed95b66..41e1811a 100644 --- a/zhenxun/builtin_plugins/admin/plugin_switch/command.py +++ b/zhenxun/builtin_plugins/admin/plugin_switch/command.py @@ -40,8 +40,6 @@ _status_matcher = on_alconna( block=True, ) -# TODO: shortcut - _group_status_matcher = on_alconna( Alconna("group-status", Args["status", ["sleep", "wake"]]), rule=admin_check("admin_bot_manage", "CHANGE_GROUP_SWITCH_LEVEL") diff --git a/zhenxun/builtin_plugins/chat_history/chat_message_handle.py b/zhenxun/builtin_plugins/chat_history/chat_message_handle.py index 8b8cb697..d9e17111 100644 --- a/zhenxun/builtin_plugins/chat_history/chat_message_handle.py +++ b/zhenxun/builtin_plugins/chat_history/chat_message_handle.py @@ -5,10 +5,12 @@ from nonebot.adapters import Bot from nonebot.plugin import PluginMetadata from nonebot_plugin_alconna import ( Alconna, + AlconnaMatch, Args, Arparma, Match, Option, + Query, on_alconna, store_true, ) @@ -39,13 +41,20 @@ __plugin_meta__ = PluginMetadata( _matcher = on_alconna( Alconna( "消息排行", - Option("--des", default=False, action=store_true), + Option("--des", action=store_true, help_text="逆序"), Args["type?", ["日", "周", "月", "年"]]["count?", int, 10], ), priority=5, block=True, ) +_matcher.shortcut( + r"(?P.+)?消息排行", + command="消息排行", + arguments=["type", "{type}"], + prefix=True, +) + @_matcher.handle() async def _( @@ -53,11 +62,9 @@ async def _( session: EventSession, arparma: Arparma, type: Match[str], - count: Match[int], + count: Query[int] = Query("count", 10), ): group_id = session.id3 or session.id2 - if not group_id: - await Text("群组id为空...").finish() time_now = datetime.now() date_scope = None zero_today = time_now - timedelta( diff --git a/zhenxun/models/chat_history.py b/zhenxun/models/chat_history.py index 117397ce..02425987 100644 --- a/zhenxun/models/chat_history.py +++ b/zhenxun/models/chat_history.py @@ -34,7 +34,7 @@ class ChatHistory(Model): @classmethod async def get_group_msg_rank( cls, - gid: str, + gid: str | None, limit: int = 10, order: str = "DESC", date_scope: tuple[datetime, datetime] | None = None, @@ -48,7 +48,7 @@ class ChatHistory(Model): date_scope: 日期范围 """ o = "-" if order == "DESC" else "" - query = cls.filter(group_id=gid) + query = cls.filter(group_id=gid) if gid else cls if date_scope: query = query.filter(create_time__range=date_scope) return list( @@ -60,18 +60,23 @@ class ChatHistory(Model): ) # type: ignore @classmethod - async def get_group_first_msg_datetime(cls, group_id: str) -> datetime | None: + async def get_group_first_msg_datetime( + cls, group_id: str | None + ) -> datetime | None: """获取群第一条记录消息时间 参数: group_id: 群组id """ - if ( - message := await cls.filter(group_id=group_id) - .order_by("create_time") - .first() - ): + if group_id: + message = ( + await cls.filter(group_id=group_id).order_by("create_time").first() + ) + else: + message = await cls.all().order_by("create_time").first() + if message: return message.create_time + return None @classmethod async def get_message(