From 8592c7cb493cca2c5ca508dfece9f179d2e08105 Mon Sep 17 00:00:00 2001 From: CMHopeSunshine <277073121@qq.com> Date: Sat, 27 Aug 2022 13:23:10 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20`help`=E5=A2=9E=E5=8A=A0=E7=BC=93?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../manager/plugin_manager/__init__.py | 23 ++++++++++++++----- .../manager/plugin_manager/manager.py | 2 +- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/LittlePaimon/manager/plugin_manager/__init__.py b/LittlePaimon/manager/plugin_manager/__init__.py index d47cbea..9cf4583 100644 --- a/LittlePaimon/manager/plugin_manager/__init__.py +++ b/LittlePaimon/manager/plugin_manager/__init__.py @@ -11,6 +11,7 @@ from nonebot.typing import T_State from LittlePaimon import SUPERUSERS, DRIVER from LittlePaimon.utils import logger +from LittlePaimon.utils.message import CommandObjectID from LittlePaimon.database.models import PluginPermission from .manager import PluginManager, hidden_plugins from .draw_help import draw_help @@ -21,11 +22,14 @@ manage_cmd = on_regex(r'^pm (?Pban|unban) (?P([\w ]*)|all) ?(-g (? help_cmd = on_command('help', aliases={'帮助', '菜单', 'pm help'}, priority=1) set_config_cmd = on_command('pm set', priority=1, permission=SUPERUSER) +cache_help = {} + @manage_cmd.handle() -async def _(event: GroupMessageEvent, state: T_State, match: dict = RegexDict()): +async def _(event: GroupMessageEvent, state: T_State, match: dict = RegexDict(), session_id: int = CommandObjectID()): if event.user_id not in SUPERUSERS and event.sender.role not in ['admin', 'owner']: await manage_cmd.finish('你没有权限使用该命令', at_sender=True) + state['session_id'] = session_id state['bool'] = match['func'] == 'unban' state['plugin'] = match['plugin'].strip().split(' ') if not match['group'] or event.user_id not in SUPERUSERS: @@ -36,9 +40,10 @@ async def _(event: GroupMessageEvent, state: T_State, match: dict = RegexDict()) @manage_cmd.handle() -async def _(event: PrivateMessageEvent, state: T_State, match: dict = RegexDict()): +async def _(event: PrivateMessageEvent, state: T_State, match: dict = RegexDict(), session_id: int = CommandObjectID()): if event.user_id not in SUPERUSERS: await manage_cmd.finish('你没有权限使用该命令', at_sender=True) + state['session_id'] = session_id state['bool'] = match['func'] == 'unban' state['plugin'] = match['plugin'].strip().split(' ') state['group'] = [int(group) for group in match['group'].strip().split(' ')] if match['group'] else [] @@ -49,6 +54,8 @@ async def _(event: PrivateMessageEvent, state: T_State, match: dict = RegexDict( async def _(state: T_State): if not state['group'] and state['user']: await manage_cmd.finish('使用ban|unban -g 群号 -u 用户', at_sender=True) + if state['session_id'] in cache_help: + del cache_help[state['session_id']] if state['group'] and not state['user']: for group_id in state['group']: if 'all' in state['plugin']: @@ -82,10 +89,14 @@ async def _(state: T_State): @help_cmd.handle() -async def _(event: MessageEvent): - plugin_list = await plugin_manager.get_plugin_list(event.message_type, event.user_id if isinstance(event, PrivateMessageEvent) else event.group_id if isinstance(event, GroupMessageEvent) else event.guild_id) - img = await draw_help(plugin_list) - await help_cmd.finish(img) +async def _(event: MessageEvent, session_id: int = CommandObjectID()): + if session_id in cache_help: + await help_cmd.finish(cache_help[session_id]) + else: + plugin_list = await plugin_manager.get_plugin_list(event.message_type, event.user_id if isinstance(event, PrivateMessageEvent) else event.group_id if isinstance(event, GroupMessageEvent) else event.guild_id) + img = await draw_help(plugin_list) + cache_help[session_id] = img + await help_cmd.finish(img) @set_config_cmd.handle() diff --git a/LittlePaimon/manager/plugin_manager/manager.py b/LittlePaimon/manager/plugin_manager/manager.py index ab1d3c9..0dc9cf0 100644 --- a/LittlePaimon/manager/plugin_manager/manager.py +++ b/LittlePaimon/manager/plugin_manager/manager.py @@ -119,7 +119,7 @@ class PluginManager: plugin_list = sorted(self.data.values(), key=lambda x: x.priority).copy() plugin_list = [p for p in plugin_list if p.show and p.module_name in load_plugins] for plugin in plugin_list: - if message_type == 'guild': + if message_type != 'guild': plugin_info = await PluginPermission.get_or_none(name=plugin.module_name, session_id=session_id, session_type=message_type) plugin.status = True if plugin_info is None else plugin_info.status