diff --git a/src/melobot/plugin/base.py b/src/melobot/plugin/base.py index 9c53fdd..ca33cf8 100644 --- a/src/melobot/plugin/base.py +++ b/src/melobot/plugin/base.py @@ -4,7 +4,7 @@ from dataclasses import dataclass from enum import Enum from pathlib import Path -from typing import Iterable, final +from typing import Iterable, final, overload from .._hook import HookBus from ..ctx import BotCtx @@ -16,6 +16,7 @@ Callable, P, SingletonBetterABCMeta, + T, abstractattr, deprecate_warn, ) @@ -112,6 +113,27 @@ def wrapped(func: AsyncCallable[P, None]) -> AsyncCallable[P, None]: return wrapped + @overload + def use(self, obj: Flow) -> Flow: ... + @overload + def use(self, obj: SyncShare[T]) -> SyncShare[T]: ... # type: ignore[overload-overlap] + @overload + def use(self, obj: AsyncShare[T]) -> AsyncShare[T]: ... # type: ignore[overload-overlap] + @overload + def use(self, obj: Callable[P, T]) -> Callable[P, T]: ... + + @final + def use(self, obj: T) -> T: + if isinstance(obj, Flow): + self.flows.append(obj) + elif isinstance(obj, (SyncShare, AsyncShare)): + self.shares.append(obj) + elif callable(obj): + self.funcs.append(obj) + else: + raise PluginLoadError(f"插件无法使用并使用 {type(obj)} 类型的对象") + return obj + @final def _build(self, name: str) -> Plugin: if not self._built: