From 4d03cbe0b59f5d364d6b0150e26a9070fa678e91 Mon Sep 17 00:00:00 2001 From: RF-Tar-Railt <3165388245@qq.com> Date: Thu, 26 Oct 2023 13:54:03 +0800 Subject: [PATCH] :sparkles: version 0.30.4 --- example/plugins/demo.py | 2 +- src/nonebot_plugin_alconna/__init__.py | 2 +- src/nonebot_plugin_alconna/model.py | 5 +- src/nonebot_plugin_alconna/rule.py | 54 ++++++++++++------- src/nonebot_plugin_alconna/uniseg/__init__.py | 2 +- 5 files changed, 41 insertions(+), 24 deletions(-) diff --git a/example/plugins/demo.py b/example/plugins/demo.py index 66baada..f98f6a1 100644 --- a/example/plugins/demo.py +++ b/example/plugins/demo.py @@ -396,7 +396,7 @@ async def statis_h(): Arg("phone", int, Field(completion=lambda: "请输入手机号")), ) -cmd = on_alconna(alc, comp_config={}) +cmd = on_alconna(alc, comp_config={"disables": {"tab"}}) @cmd.handle() diff --git a/src/nonebot_plugin_alconna/__init__.py b/src/nonebot_plugin_alconna/__init__.py index a0ba865..7eda50c 100644 --- a/src/nonebot_plugin_alconna/__init__.py +++ b/src/nonebot_plugin_alconna/__init__.py @@ -89,7 +89,7 @@ from .consts import ALCONNA_EXEC_RESULT as ALCONNA_EXEC_RESULT from .extension import add_global_extension as add_global_extension -__version__ = "0.30.3" +__version__ = "0.30.4" __plugin_meta__ = PluginMetadata( name="Alconna 插件", diff --git a/src/nonebot_plugin_alconna/model.py b/src/nonebot_plugin_alconna/model.py index bd5a602..cf06160 100644 --- a/src/nonebot_plugin_alconna/model.py +++ b/src/nonebot_plugin_alconna/model.py @@ -1,6 +1,6 @@ from dataclasses import field, dataclass from typing_extensions import NotRequired -from typing import Type, Union, Generic, TypeVar, Optional, TypedDict +from typing import Set, Type, Union, Generic, Literal, TypeVar, Optional, TypedDict from arclet.alconna import Empty, Alconna, Arparma from arclet.alconna.duplication import Duplication @@ -61,4 +61,7 @@ class CompConfig(TypedDict): enter: NotRequired[str] exit: NotRequired[str] timeout: NotRequired[int] + hide_tabs: NotRequired[bool] + hides: NotRequired[Set[Literal["tab", "enter", "exit"]]] + disables: NotRequired[Set[Literal["tab", "enter", "exit"]]] lite: NotRequired[bool] diff --git a/src/nonebot_plugin_alconna/rule.py b/src/nonebot_plugin_alconna/rule.py index cb291fa..c1ec8d8 100644 --- a/src/nonebot_plugin_alconna/rule.py +++ b/src/nonebot_plugin_alconna/rule.py @@ -46,6 +46,7 @@ class AlconnaRule: "_waiter", "_future", "_interface", + "_comp_help", ) def __init__( @@ -96,15 +97,36 @@ def __init__( rule=Rule(lambda: self._session is not None), ) self._waiter.destroy() + self._comp_help = "" if self.comp_config is not None: - _tab = self.comp_config.get("tab", ".tab") - _enter = self.comp_config.get("enter", ".enter") - _exit = self.comp_config.get("exit", ".exit") + _tab = self.comp_config.get("tab") or ".tab" + _enter = self.comp_config.get("enter") or ".enter" + _exit = self.comp_config.get("exit") or ".exit" + disables = self.comp_config.get("disables", set()) + hides = self.comp_config.get("hides", set()) + hide_tabs = self.comp_config.get("hide_tabs", False) + if self.comp_config.get("lite", False): + hide_tabs = True + hides = {"tab", "enter", "exit"} + hides |= disables + if len(hides) < 3: + template = f"\n\n{{}}{{}}{{}}{lang.require('comp/nonebot', 'other')}\n" + self._comp_help = template.format( + (lang.require("comp/nonebot", "tab").format(cmd=_tab) + "\n") + if "tab" not in hides + else "", + (lang.require("comp/nonebot", "enter").format(cmd=_enter) + "\n") + if "enter" not in hides + else "", + (lang.require("comp/nonebot", "exit").format(cmd=_exit) + "\n") + if "exit" not in hides + else "", + ) @self._waiter.handle() async def _waiter_handle(_bot: Bot, _event: Event, content: Message = EventMessage()): msg = str(content) - if msg.startswith(_exit): + if msg.startswith(_exit) and "exit" not in disables: if msg == _exit: self._future.set_result(False) await self._waiter.finish() @@ -115,7 +137,7 @@ async def _waiter_handle(_bot: Bot, _event: Event, content: Message = EventMessa target=msg.replace(_exit, "", 1) ) ) - elif msg.startswith(_enter): + elif msg.startswith(_enter) and "enter" not in disables: if msg == _enter: self._future.set_result(True) await self._waiter.finish() @@ -126,7 +148,7 @@ async def _waiter_handle(_bot: Bot, _event: Event, content: Message = EventMessa target=msg.replace(_enter, "", 1) ) ) - elif msg.startswith(_tab): + elif msg.startswith(_tab) and "tab" not in disables: offset = msg.replace(_tab, "", 1).lstrip() or 1 try: offset = int(offset) @@ -137,12 +159,11 @@ async def _waiter_handle(_bot: Bot, _event: Event, content: Message = EventMessa ) else: self._interface.tab(offset) - if self.comp_config is not None and self.comp_config.get("lite", False): - out = f"* {self._interface.current()}" - else: - out = "\n".join(self._interface.lines()) - self._future.set_result(None) - await self._waiter.pause(out) + await self._waiter.pause( + f"* {self._interface.current()}" + if hide_tabs + else "\n".join(self._interface.lines()) + ) else: self._future.set_result(content) await self._waiter.finish() @@ -176,15 +197,8 @@ async def handle(self, bot: Bot, event: Event, msg: Message) -> Union[Arparma, L error_info=SpecialOptionTriggered("completion"), ) - help_text = ( - f"{lang.require('comp/nonebot', 'tab').format(cmd=self.comp_config.get('tab', '.tab'))}\n" - f"{lang.require('comp/nonebot', 'enter').format(cmd=self.comp_config.get('enter', '.enter'))}\n" - f"{lang.require('comp/nonebot', 'exit').format(cmd=self.comp_config.get('exit', '.exit'))}\n" - f"{lang.require('comp/nonebot', 'other')}\n" - ) - while self._interface.available: - await self.send(f"{str(self._interface)}\n\n{help_text}", bot, event, res) + await self.send(f"{str(self._interface)}{self._comp_help}", bot, event, res) while True: self._future = asyncio.get_running_loop().create_future() try: diff --git a/src/nonebot_plugin_alconna/uniseg/__init__.py b/src/nonebot_plugin_alconna/uniseg/__init__.py index 22c30d4..8da7d8e 100644 --- a/src/nonebot_plugin_alconna/uniseg/__init__.py +++ b/src/nonebot_plugin_alconna/uniseg/__init__.py @@ -24,7 +24,7 @@ from .params import UniversalMessage as UniversalMessage from .params import UniversalSegment as UniversalSegment -__version__ = "0.30.3" +__version__ = "0.30.4" __plugin_meta__ = PluginMetadata( name="Universal Segment 插件",