From 1c5f06d9a916c1386a05ecf6fefc951765036153 Mon Sep 17 00:00:00 2001 From: RockChinQ <1010553892@qq.com> Date: Sun, 28 Jul 2024 20:23:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20reply=20=E5=92=8C?= =?UTF-8?q?=20send=5Fmessage=20=E4=B8=A4=E4=B8=AA=E6=8F=92=E4=BB=B6api?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/platform/manager.py | 6 ++--- pkg/plugin/context.py | 53 ++++++++++++++++++++++++++++++++++------- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/pkg/platform/manager.py b/pkg/platform/manager.py index b5f6986..b969642 100644 --- a/pkg/platform/manager.py +++ b/pkg/platform/manager.py @@ -146,9 +146,9 @@ class PlatformManager: if len(self.adapters) == 0: self.ap.logger.warning('未运行平台适配器,请根据文档配置并启用平台适配器。') - async def send(self, event: mirai.MessageEvent, msg: mirai.MessageChain, adapter: msadapter.MessageSourceAdapter, check_quote=True, check_at_sender=True): + async def send(self, event: mirai.MessageEvent, msg: mirai.MessageChain, adapter: msadapter.MessageSourceAdapter): - if check_at_sender and self.ap.platform_cfg.data['at-sender'] and isinstance(event, GroupMessage): + if self.ap.platform_cfg.data['at-sender'] and isinstance(event, GroupMessage): msg.insert( 0, @@ -160,7 +160,7 @@ class PlatformManager: await adapter.reply_message( event, msg, - quote_origin=True if self.ap.platform_cfg.data['quote-origin'] and check_quote else False + quote_origin=True if self.ap.platform_cfg.data['quote-origin'] else False ) async def run(self): diff --git a/pkg/plugin/context.py b/pkg/plugin/context.py index b0e2ef9..42cb6be 100644 --- a/pkg/plugin/context.py +++ b/pkg/plugin/context.py @@ -3,6 +3,7 @@ from __future__ import annotations import typing import abc import pydantic +import mirai from . import events from ..provider.tools import entities as tools_entities @@ -165,11 +166,54 @@ class EventContext: } """ + # ========== 插件可调用的 API ========== + def add_return(self, key: str, ret): """添加返回值""" if key not in self.__return_value__: self.__return_value__[key] = [] self.__return_value__[key].append(ret) + + async def reply(self, message_chain: mirai.MessageChain): + """回复此次消息请求 + + Args: + message_chain (mirai.MessageChain): YiriMirai库的消息链,若用户使用的不是 YiriMirai 适配器,程序也能自动转换为目标消息链 + """ + await self.host.ap.platform_mgr.send( + event=self.event.query.message_event, + msg=message_chain, + adapter=self.event.query.adapter, + ) + + async def send_message( + self, + target_type: str, + target_id: str, + message: mirai.MessageChain + ): + """主动发送消息 + + Args: + target_type (str): 目标类型,`person`或`group` + target_id (str): 目标ID + message (mirai.MessageChain): YiriMirai库的消息链,若用户使用的不是 YiriMirai 适配器,程序也能自动转换为目标消息链 + """ + await self.event.query.adapter.send_message( + target_type=target_type, + target_id=target_id, + message=message + ) + + def prevent_postorder(self): + """阻止后续插件执行""" + self.__prevent_postorder__ = True + + def prevent_default(self): + """阻止默认行为""" + self.__prevent_default__ = True + + # ========== 以下是内部保留方法,插件不应调用 ========== def get_return(self, key: str) -> list: """获取key的所有返回值""" @@ -183,14 +227,6 @@ class EventContext: return self.__return_value__[key][0] return None - def prevent_default(self): - """阻止默认行为""" - self.__prevent_default__ = True - - def prevent_postorder(self): - """阻止后续插件执行""" - self.__prevent_postorder__ = True - def is_prevented_default(self): """是否阻止默认行为""" return self.__prevent_default__ @@ -198,6 +234,7 @@ class EventContext: def is_prevented_postorder(self): """是否阻止后序插件执行""" return self.__prevent_postorder__ + def __init__(self, host: APIHost, event: events.BaseEventModel):