diff --git a/README.md b/README.md index 106ee3b..73b1774 100644 --- a/README.md +++ b/README.md @@ -87,19 +87,27 @@ app.run() 编写插件: ```python -from arclet.entari import Session, MessageCreatedEvent, metadata, listen +from arclet.entari import BasicConfModel, Session, MessageCreatedEvent, plugin -metadata( + +class Config(BasicConfModel): + name: str + + +plugin.metadata( name="Hello, World!", author=["Arclet"], version="0.1.0", - description="A simple plugin that replies 'Hello, World!' to every message." + description="A simple plugin that replies 'Hello, World!' to every message.", + config=Config ) # or __plugin_metadata__ = PluginMetadata(...) -@listen(MessageCreatedEvent) # or plugin.dispatch(MessageCreatedEvent) +config = plugin.get_config(Config) + +@plugin.listen(MessageCreatedEvent) # or plugin.dispatch(MessageCreatedEvent) async def _(session: Session): - await session.send("Hello, World!") + await session.send(f"Hello, World! {config.name}") ``` 加载插件: @@ -108,7 +116,7 @@ async def _(session: Session): from arclet.entari import Entari, WS, load_plugin app = Entari(WS(port=5140, path="satori")) -load_plugin("example_plugin") +load_plugin("example_plugin", {"name": "Entari"}) load_plugin("::echo") load_plugin("::auto_reload", {"watch_dirs": ["plugins"]}) @@ -116,7 +124,6 @@ app.run() ``` - ## 配置文件 ```yaml @@ -130,6 +137,7 @@ basic: log_level: INFO prefix: ["/"] plugins: + $files: ["./plugins"] $prelude: ["::auto_reload"] .record_message: record_send: true @@ -153,6 +161,8 @@ plugins: - `log_level`: 日志等级 - `prefix`: 指令前缀, 可留空 - `plugins`: 插件配置 + - `$files`: 额外的插件配置文件搜索目录 + - `$prelude`: 预加载插件列表 - `.record_message`: 消息日志并配置 - `record_send`: 是否记录发送消息 (默认为 `true`) - `.commands`: 指令插件配置 (适用于所有使用了 `command.on/command.command` 的插件) diff --git a/arclet/entari/__init__.py b/arclet/entari/__init__.py index 9864fbb..338a4f9 100644 --- a/arclet/entari/__init__.py +++ b/arclet/entari/__init__.py @@ -1,5 +1,4 @@ from arclet.letoderea import bind as bind -from arclet.letoderea import es as es from satori import ArgvInteraction as ArgvInteraction from satori import At as At from satori import Audio as Audio @@ -51,8 +50,10 @@ from .plugin import Plugin as Plugin from .plugin import PluginMetadata as PluginMetadata from .plugin import add_service as add_service +from .plugin import collect_disposes as collect_disposes from .plugin import declare_static as declare_static from .plugin import keeping as keeping +from .plugin import listen as listen from .plugin import load_plugin as load_plugin from .plugin import load_plugins as load_plugins from .plugin import metadata as metadata @@ -65,6 +66,5 @@ WS = WebsocketsInfo WH = WebhookInfo filter_ = Filter -listen = es.on __version__ = "0.10.5" diff --git a/arclet/entari/builtins/help.py b/arclet/entari/builtins/help.py index 906266d..4b72a54 100644 --- a/arclet/entari/builtins/help.py +++ b/arclet/entari/builtins/help.py @@ -1,5 +1,5 @@ -import random from dataclasses import field +import random from typing import Optional from arclet.alconna import ( diff --git a/arclet/entari/config.py b/arclet/entari/config.py index c84a935..39d8154 100644 --- a/arclet/entari/config.py +++ b/arclet/entari/config.py @@ -1,7 +1,8 @@ from __future__ import annotations -from dataclasses import dataclass, fields, is_dataclass +from dataclasses import dataclass from dataclasses import field as field +from dataclasses import fields, is_dataclass from inspect import Signature import json import os diff --git a/arclet/entari/plugin/__init__.py b/arclet/entari/plugin/__init__.py index 9b98fa7..47562e9 100644 --- a/arclet/entari/plugin/__init__.py +++ b/arclet/entari/plugin/__init__.py @@ -2,8 +2,9 @@ from os import PathLike from pathlib import Path -from typing import Any, overload +from typing import Any, Callable, overload +from arclet.letoderea import es from tarina import init_spec from ..config import C, EntariConfig, config_model_validate @@ -133,6 +134,9 @@ def plugin_config(model_type: type[C] | None = None): return plugin.config +get_config = plugin_config + + def declare_static(): """声明当前插件为静态插件""" if not (plugin := _current_plugin.get(None)): @@ -148,6 +152,12 @@ def add_service(serv: TS | type[TS]) -> TS: return plugin.service(serv) +def collect_disposes(*disposes: Callable[[], None]): + if not (plugin := _current_plugin.get(None)): + raise LookupError("no plugin context found") + plugin.collect(*disposes) + + def find_plugin(name: str) -> Plugin | None: if name in plugin_service.plugins: return plugin_service.plugins[name] @@ -168,3 +178,6 @@ def find_plugin_by_file(file: str) -> Plugin | None: return plugin path1 = path1.parent return None + + +listen = es.on diff --git a/pyproject.toml b/pyproject.toml index 942701e..6d1e131 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "arclet-entari" -version = "0.10.5" +version = "0.11.0" description = "Simple IM Framework based on satori-python" authors = [ {name = "RF-Tar-Railt",email = "rf_tar_railt@qq.com"},