From 67e3492f84ca2aefb7ea8386bcf7c0949d3548d4 Mon Sep 17 00:00:00 2001 From: rf_tar_railt <3165388245@qq.com> Date: Thu, 4 Jul 2024 18:12:16 +0800 Subject: [PATCH] feat(qqapi): new at-user & at-everyone --- avilla/qqapi/capability.py | 5 +++-- avilla/qqapi/perform/action/message.py | 2 +- avilla/qqapi/perform/message/serialize.py | 4 ++-- avilla/qqapi/utils.py | 11 +++++++++-- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/avilla/qqapi/capability.py b/avilla/qqapi/capability.py index 13c94538..8dcfa854 100644 --- a/avilla/qqapi/capability.py +++ b/avilla/qqapi/capability.py @@ -11,7 +11,7 @@ from avilla.standard.core.application.event import AvillaLifecycleEvent from graia.ryanvk import Fn, PredicateOverload, SimpleOverload, TypeOverload -from .utils import handle_text +from .utils import handle_text, remove_empty class QQAPICapability((m := ApplicationCollector())._): @@ -74,7 +74,8 @@ async def serialize(self, message: MessageChain): if isinstance(elem, str): content += elem else: - res[elem[0]] = elem[1] + other = elem[1] + res[elem[0]] = remove_empty(other) if isinstance(other, dict) else other if content: res["content"] = content return res diff --git a/avilla/qqapi/perform/action/message.py b/avilla/qqapi/perform/action/message.py index 396926e6..f3ee8416 100644 --- a/avilla/qqapi/perform/action/message.py +++ b/avilla/qqapi/perform/action/message.py @@ -233,7 +233,7 @@ async def send_group_msg( expire=timedelta(minutes=5), ) # TODO: wait for api upgrade - msg["content"] = unescape(msg["content"]) + # msg["content"] = unescape(msg["content"]) method, data = form_data(msg) try: result = await self.account.connection.call_http(method, f"v2/groups/{target.pattern['group']}/messages", data) diff --git a/avilla/qqapi/perform/message/serialize.py b/avilla/qqapi/perform/message/serialize.py index c7ba26d0..829e6433 100644 --- a/avilla/qqapi/perform/message/serialize.py +++ b/avilla/qqapi/perform/message/serialize.py @@ -39,11 +39,11 @@ async def face(self, element: Face): async def notice(self, element: Notice): if element.target.last_key == "channel": return f"<#{element.target['channel']}>" - return f"<@{element.target['member']}>" + return f'' @m.entity(QQAPICapability.serialize_element, element=NoticeAll) async def notice_all(self, element: NoticeAll): - return "@everyone" + return "" @m.entity(QQAPICapability.serialize_element, element=Picture) async def picture(self, element: Picture): diff --git a/avilla/qqapi/utils.py b/avilla/qqapi/utils.py index f95fd11d..e54b86a8 100644 --- a/avilla/qqapi/utils.py +++ b/avilla/qqapi/utils.py @@ -13,8 +13,9 @@ def unescape(s: str) -> str: def handle_text(msg: str): text_begin = 0 msg = msg.replace("@everyone", "") + msg = re.sub(r"\", "", msg) for embed in re.finditer( - r"\<(?P(?:@|#|emoji:))!?(?P\w+?)\>", + r"\<(?P(?:@|#|emoji:))!?(?P\w+?)\>|\<(?Pqqbot-at-user) id=\"(?P\w+)\"\s/\>", msg, ): if content := msg[text_begin : embed.pos + embed.start()]: @@ -24,8 +25,10 @@ def handle_text(msg: str): yield {"type": "mention_user", "user_id": embed.group("id")} elif embed["type"] == "#": yield {"type": "mention_channel", "channel_id": embed.group("id")} - else: + elif embed["type"] == "emoji": yield {"type": "emoji", "id": embed.group("id")} + elif embed["type1"] == "qqbot-at-user": + yield {"type": "mention_user", "user_id": embed.group("id1")} if content := msg[text_begin:]: yield {"type": "text", "text": unescape(content)} @@ -45,3 +48,7 @@ def form_data(message: dict): else: data_[key] = value return "multipart", {"files": files, "data": data_} + + +def remove_empty(d: dict): + return {k: (remove_empty(v) if isinstance(v, dict) else v) for k, v in d.items() if v is not None}