From 85aba9e36f92a3c8d6f2a1124445f04bf953456a Mon Sep 17 00:00:00 2001 From: Mix Date: Sun, 7 Feb 2021 12:17:21 +0800 Subject: [PATCH] :bug: fix bug founded during test in mirai adapter --- nonebot/adapters/mirai/bot.py | 5 ++--- nonebot/adapters/mirai/message.py | 8 +++++--- nonebot/adapters/mirai/utils.py | 3 ++- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/nonebot/adapters/mirai/bot.py b/nonebot/adapters/mirai/bot.py index 7bd40968..900e3ec5 100644 --- a/nonebot/adapters/mirai/bot.py +++ b/nonebot/adapters/mirai/bot.py @@ -253,10 +253,9 @@ class Bot(BaseBot): * ``message: Union[MessageChain, MessageSegment, str]``: 要发送的消息 * ``at_sender: bool``: 是否 @ 事件主体 """ - if isinstance(message, MessageSegment): + print(event, message, at_sender) + if not isinstance(message, MessageChain): message = MessageChain(message) - elif isinstance(message, str): - message = MessageChain(MessageSegment.plain(message)) if isinstance(event, FriendMessage): return await self.send_friend_message(target=event.sender.id, message_chain=message) diff --git a/nonebot/adapters/mirai/message.py b/nonebot/adapters/mirai/message.py index 2285ceda..f6af1ab6 100644 --- a/nonebot/adapters/mirai/message.py +++ b/nonebot/adapters/mirai/message.py @@ -273,12 +273,14 @@ class MessageChain(BaseMessage): """ @overrides(BaseMessage) - def __init__(self, message: Union[List[Dict[str, Any]], - Iterable[MessageSegment], MessageSegment], - **kwargs): + def __init__(self, message: Union[List[Dict[str, + Any]], Iterable[MessageSegment], + MessageSegment, str], **kwargs): super().__init__(**kwargs) if isinstance(message, MessageSegment): self.append(message) + elif isinstance(message, str): + self.append(MessageSegment.plain(text=message)) elif isinstance(message, Iterable): self.extend(self._construct(message)) else: diff --git a/nonebot/adapters/mirai/utils.py b/nonebot/adapters/mirai/utils.py index bc7aa7dc..c9c9c143 100644 --- a/nonebot/adapters/mirai/utils.py +++ b/nonebot/adapters/mirai/utils.py @@ -149,6 +149,7 @@ def process_nick(bot: "Bot", event: GroupMessage) -> GroupMessage: nick_regex = '|'.join(filter(lambda x: x, bot.config.nickname)) matched = re.search(rf"^({nick_regex})([\s,,]*|$)", text, re.IGNORECASE) if matched is not None: + event.to_me = True nickname = matched.group(1) Log.info(f'User is calling me {nickname}') plain.data['text'] = text[matched.end():] @@ -159,7 +160,7 @@ def process_nick(bot: "Bot", event: GroupMessage) -> GroupMessage: def process_reply(bot: "Bot", event: GroupMessage) -> GroupMessage: reply = event.message_chain.extract_first(MessageType.QUOTE) if reply is not None: - if reply.data['sender_id'] == event.self_id: + if reply.data['senderId'] == event.self_id: event.to_me = True else: event.message_chain.insert(0, reply)