Skip to content

Commit

Permalink
feat(qqapi): new at-user & at-everyone
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Jul 4, 2024
1 parent e946565 commit 67e3492
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
5 changes: 3 additions & 2 deletions avilla/qqapi/capability.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())._):
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion avilla/qqapi/perform/action/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions avilla/qqapi/perform/message/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'<qqbot-at-user id="{element.target["member"]}" />'

@m.entity(QQAPICapability.serialize_element, element=NoticeAll)
async def notice_all(self, element: NoticeAll):
return "@everyone"
return "<qqbot-at-everyone />"

@m.entity(QQAPICapability.serialize_element, element=Picture)
async def picture(self, element: Picture):
Expand Down
11 changes: 9 additions & 2 deletions avilla/qqapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"\<qqbot-at-everyone\s/\>", "", msg)
for embed in re.finditer(
r"\<(?P<type>(?:@|#|emoji:))!?(?P<id>\w+?)\>",
r"\<(?P<type>(?:@|#|emoji:))!?(?P<id>\w+?)\>|\<(?P<type1>qqbot-at-user) id=\"(?P<id1>\w+)\"\s/\>",
msg,
):
if content := msg[text_begin : embed.pos + embed.start()]:
Expand All @@ -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)}

Expand All @@ -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}

0 comments on commit 67e3492

Please sign in to comment.