diff --git a/astrbot/core/platform/astr_message_event.py b/astrbot/core/platform/astr_message_event.py index 25a99347b..058d42950 100644 --- a/astrbot/core/platform/astr_message_event.py +++ b/astrbot/core/platform/astr_message_event.py @@ -4,7 +4,7 @@ from .astrbot_message import AstrBotMessage from .platform_metadata import PlatformMetadata from astrbot.core.message.message_event_result import MessageEventResult, MessageChain from astrbot.core.platform.message_type import MessageType -from typing import List +from typing import List, Union from astrbot.core.message.components import Plain, Image, BaseMessageComponent, Face, At, AtAll, Forward from astrbot.core.utils.metrics import Metric @@ -124,7 +124,7 @@ class AstrMessageEvent(abc.ABC): ''' return self.message_obj.sender.nickname - def set_result(self, result: MessageEventResult): + def set_result(self, result: Union[MessageEventResult, str]): '''设置消息事件的结果。 Note: @@ -145,6 +145,8 @@ class AstrMessageEvent(abc.ABC): return ``` ''' + if isinstance(result, str): + result = MessageEventResult().message(result) self._result = result def stop_event(self): diff --git a/astrbot/core/star/context.py b/astrbot/core/star/context.py index 39c3ba46a..130b9097d 100644 --- a/astrbot/core/star/context.py +++ b/astrbot/core/star/context.py @@ -9,7 +9,7 @@ from astrbot.core.platform.astr_message_event import MessageSesion from astrbot.core.message.message_event_result import MessageChain from astrbot.core.provider.manager import ProviderManager from astrbot.core.platform.manager import PlatformManager -from .star import star_registry, star_map, StarMetadata +from .star import star_registry, StarMetadata from .star_handler import star_handlers_registry, star_handlers_map, StarHandlerMetadata from .filter.command import CommandFilter from .filter.regex import RegexFilter @@ -42,8 +42,10 @@ class Context: self._db = db def get_registered_star(self, star_name: str) -> StarMetadata: - return star_map.get(star_name, None) - + for star in star_registry: + if star.name == star_name: + return star + def get_all_stars(self) -> List[StarMetadata]: return star_registry diff --git a/astrbot/core/star/filter/command.py b/astrbot/core/star/filter/command.py index b37c66d97..72b8b160b 100644 --- a/astrbot/core/star/filter/command.py +++ b/astrbot/core/star/filter/command.py @@ -17,7 +17,6 @@ class CommandFilter(HandlerFilter, ParameterValidationMixin): def print_types(self): result = "" - print(self.handler_params) for k, v in self.handler_params.items(): if isinstance(v, type): result += f"{k}({v.__name__})," diff --git a/astrbot/core/star/register/star.py b/astrbot/core/star/register/star.py index 53b7c950d..c3d098512 100644 --- a/astrbot/core/star/register/star.py +++ b/astrbot/core/star/register/star.py @@ -1,6 +1,24 @@ from ..star import star_registry, StarMetadata, star_map def register_star(name: str, author: str, desc: str, version: str, repo: str = None): + '''注册一个插件(Star)。 + + Args: + name: 插件名称。 + author: 作者。 + desc: 插件的简述。 + version: 版本号。 + repo: 仓库地址。如果没有填写仓库地址,将无法更新这个插件。 + + 如果需要为插件填写帮助信息,请使用如下格式: + + ```python + class MyPlugin(star.Star): + \'\'\'这是帮助信息\'\'\' + ... + + 帮助信息会被自动提取。使用 `/plugin <插件名> 可以查看帮助信息。` + ''' def decorator(cls): star_metadata = StarMetadata( name=name, @@ -9,7 +27,7 @@ def register_star(name: str, author: str, desc: str, version: str, repo: str = N version=version, repo=repo, star_cls_type=cls, - module_path=cls.__module__ + module_path=cls.__module__, ) star_registry.append(star_metadata) star_map[cls.__module__] = star_metadata diff --git a/astrbot/core/utils/param_validation_mixin.py b/astrbot/core/utils/param_validation_mixin.py index db931c9b6..5c72b8961 100644 --- a/astrbot/core/utils/param_validation_mixin.py +++ b/astrbot/core/utils/param_validation_mixin.py @@ -6,7 +6,6 @@ class ParameterValidationMixin: '''将参数列表 params 根据 param_type 转换为参数字典。 ''' result = {} - print(params, param_type) for i, (param_name, param_type_or_default_val) in enumerate(param_type.items()): if i >= len(params): if isinstance(param_type_or_default_val, Type) or param_type_or_default_val is inspect.Parameter.empty: @@ -18,7 +17,7 @@ class ParameterValidationMixin: else: # 尝试强制转换 try: - if param_type_or_default_val == None: + if param_type_or_default_val is None: if params[i].isdigit(): result[param_name] = int(params[i]) else: @@ -27,5 +26,4 @@ class ParameterValidationMixin: result[param_name] = param_type_or_default_val(params[i]) except ValueError: raise ValueError(f"参数 {param_name} 类型错误") - print(result) return result \ No newline at end of file diff --git a/packages/astrbot/main.py b/packages/astrbot/main.py index bb6c9d581..9d1232353 100644 --- a/packages/astrbot/main.py +++ b/packages/astrbot/main.py @@ -30,7 +30,7 @@ class Main(star.Star): msg = "已注册的 AstrBot 内置指令:\n" msg += f"""[System] -/plugin: 插件管理 +/plugin: 查看注册的插件、插件帮助 /t2i: 开启/关闭文本转图片模式 /sid: 获取当前会话的 ID /op : 授权管理员 @@ -82,14 +82,24 @@ class Main(star.Star): event.set_result(MessageEventResult().message(f"停用工具 {tool_name} 失败,未找到此工具。")) @filter.command("plugin") - async def plugin(self, event: AstrMessageEvent): - plugin_list_info = "已加载的插件:\n" - for plugin in self.context.get_all_stars(): - plugin_list_info += f"- `{plugin.name}` By {plugin.author}: {plugin.desc}\n" - if plugin_list_info.strip() == "": - plugin_list_info = "没有加载任何插件。" - - event.set_result(MessageEventResult().message(f"{plugin_list_info}")) + async def plugin(self, event: AstrMessageEvent, oper: str = None): + if oper is None: + plugin_list_info = "已加载的插件:\n" + for plugin in self.context.get_all_stars(): + plugin_list_info += f"- `{plugin.name}` By {plugin.author}: {plugin.desc}\n" + if plugin_list_info.strip() == "": + plugin_list_info = "没有加载任何插件。" + + plugin_list_info += "\n使用 /plugin <插件名> 查看插件帮助。" + event.set_result(MessageEventResult().message(f"{plugin_list_info}").use_t2i(False)) + else: + plugin = self.context.get_registered_star(oper) + if plugin is None: + event.set_result(MessageEventResult().message("未找到此插件。")) + else: + help_msg = plugin.star_cls.__doc__ if plugin.star_cls.__doc__ else "该插件未提供帮助信息" + ret = f"插件 {oper} 帮助信息:\n" + help_msg + event.set_result(MessageEventResult().message(ret).use_t2i(False)) @filter.command("t2i") async def t2i(self, event: AstrMessageEvent): diff --git a/packages/web_searcher/main.py b/packages/web_searcher/main.py index bcebffab0..4d4a192b5 100644 --- a/packages/web_searcher/main.py +++ b/packages/web_searcher/main.py @@ -14,6 +14,7 @@ from .engines.config import HEADERS, USER_AGENTS @star.register(name="astrbot-web-searcher", desc="让 LLM 具有网页检索能力", author="Soulter", version="1.14.514") class Main(star.Star): + '''使用 /websearch on 或者 off 开启或者关闭网页搜索功能''' def __init__(self, context: star.Context) -> None: self.context = context