🐛 fix message construct typing and plugins loading

This commit is contained in:
yanyongyu 2020-12-31 14:00:59 +08:00
parent a98417a878
commit 938b5bf275
4 changed files with 35 additions and 16 deletions

View File

@ -10,7 +10,7 @@ from copy import copy
from typing_extensions import Literal from typing_extensions import Literal
from functools import reduce, partial from functools import reduce, partial
from dataclasses import dataclass, field from dataclasses import dataclass, field
from typing import Any, Dict, Union, TypeVar, Optional, Callable, Iterable, Awaitable, TYPE_CHECKING from typing import Any, Dict, Union, TypeVar, Mapping, Optional, Callable, Iterable, Iterator, Awaitable, TYPE_CHECKING
from pydantic import BaseModel from pydantic import BaseModel
@ -211,8 +211,8 @@ class Message(list, abc.ABC):
"""消息数组""" """消息数组"""
def __init__(self, def __init__(self,
message: Union[str, list, dict, T_MessageSegment, T_Message, message: Union[str, Mapping, Iterable[Mapping],
Any] = None, T_MessageSegment, T_Message, Any] = None,
*args, *args,
**kwargs): **kwargs):
""" """
@ -242,7 +242,8 @@ class Message(list, abc.ABC):
@staticmethod @staticmethod
@abc.abstractmethod @abc.abstractmethod
def _construct( def _construct(
msg: Union[str, list, dict, Any]) -> Iterable[T_MessageSegment]: msg: Union[str, Mapping, Iterable[Mapping], Any]
) -> Iterable[T_MessageSegment]:
raise NotImplementedError raise NotImplementedError
def __add__(self: T_Message, other: Union[str, T_MessageSegment, def __add__(self: T_Message, other: Union[str, T_MessageSegment,
@ -257,10 +258,20 @@ class Message(list, abc.ABC):
return result return result
def __radd__(self: T_Message, other: Union[str, T_MessageSegment, def __radd__(self: T_Message, other: Union[str, T_MessageSegment,
T_Message]): T_Message]) -> T_Message:
result = self.__class__(other) result = self.__class__(other)
return result.__add__(self) return result.__add__(self)
def __iadd__(self: T_Message, other: Union[str, T_MessageSegment,
T_Message]) -> T_Message:
if isinstance(other, str):
self.extend(self._construct(other))
elif isinstance(other, MessageSegment):
self.append(other)
elif isinstance(other, Message):
self.extend(other)
return self
def append(self: T_Message, obj: Union[str, T_MessageSegment]) -> T_Message: def append(self: T_Message, obj: Union[str, T_MessageSegment]) -> T_Message:
""" """
:说明: :说明:

View File

@ -1,5 +1,5 @@
import re import re
from typing import Any, Dict, Union, Tuple, Iterable, Optional from typing import Any, Dict, Union, Tuple, Mapping, Iterable, Optional
from nonebot.typing import overrides from nonebot.typing import overrides
from nonebot.adapters import Message as BaseMessage, MessageSegment as BaseMessageSegment from nonebot.adapters import Message as BaseMessage, MessageSegment as BaseMessageSegment
@ -212,11 +212,13 @@ class Message(BaseMessage):
@staticmethod @staticmethod
@overrides(BaseMessage) @overrides(BaseMessage)
def _construct(msg: Union[str, dict, list]) -> Iterable[MessageSegment]: def _construct(
if isinstance(msg, dict): msg: Union[str, Mapping,
Iterable[Mapping]]) -> Iterable[MessageSegment]:
if isinstance(msg, Mapping):
yield MessageSegment(msg["type"], msg.get("data") or {}) yield MessageSegment(msg["type"], msg.get("data") or {})
return return
elif isinstance(msg, list): elif isinstance(msg, Iterable) and not isinstance(msg, str):
for seg in msg: for seg in msg:
yield MessageSegment(seg["type"], seg.get("data") or {}) yield MessageSegment(seg["type"], seg.get("data") or {})
return return

View File

@ -1,5 +1,5 @@
from copy import copy from copy import copy
from typing import Any, Dict, Union, Iterable from typing import Any, Dict, Union, Mapping, Iterable
from nonebot.typing import overrides from nonebot.typing import overrides
from nonebot.adapters import Message as BaseMessage, MessageSegment as BaseMessageSegment from nonebot.adapters import Message as BaseMessage, MessageSegment as BaseMessageSegment
@ -133,17 +133,20 @@ class Message(BaseMessage):
@staticmethod @staticmethod
@overrides(BaseMessage) @overrides(BaseMessage)
def _construct(msg: Union[str, dict, list]) -> Iterable[MessageSegment]: def _construct(
if isinstance(msg, dict): msg: Union[str, Mapping,
Iterable[Mapping]]) -> Iterable[MessageSegment]:
if isinstance(msg, Mapping):
yield MessageSegment(msg["type"], msg.get("data") or {}) yield MessageSegment(msg["type"], msg.get("data") or {})
elif isinstance(msg, list):
for seg in msg:
yield MessageSegment(seg["type"], seg.get("data") or {})
elif isinstance(msg, str): elif isinstance(msg, str):
yield MessageSegment.text(msg) yield MessageSegment.text(msg)
elif isinstance(msg, Iterable):
for seg in msg:
yield MessageSegment(seg["type"], seg.get("data") or {})
def _produce(self) -> dict: def _produce(self) -> dict:
data = {} data = {}
segment: MessageSegment
for segment in self: for segment in self:
# text 可以和 text 合并 # text 可以和 text 合并
if segment.type == "text" and data.get("msgtype") == 'text': if segment.type == "text" and data.get("msgtype") == 'text':

View File

@ -897,7 +897,10 @@ def load_plugins(*plugin_dir: str) -> Set[Plugin]:
return None return None
spec = module_info.module_finder.find_spec(name, None) spec = module_info.module_finder.find_spec(name, None)
if spec.name in plugins: if not spec:
logger.warning(
f"Module {name} cannot be loaded! Check module name first.")
elif spec.name in plugins:
return None return None
elif spec.name in sys.modules: elif spec.name in sys.modules:
logger.warning( logger.warning(