Skip to content

Commit

Permalink
✨ add event class for qun bot
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu authored Nov 13, 2023
1 parent 6b82f09 commit 632d3a7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 48 deletions.
9 changes: 4 additions & 5 deletions nonebot/adapters/qq/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from .event import (
Event,
ReadyEvent,
QQMessageEvent,
GuildMessageEvent,
C2CMessageCreateEvent,
DirectMessageCreateEvent,
Expand Down Expand Up @@ -89,7 +90,7 @@

async def _check_reply(
bot: "Bot",
event: Union[GuildMessageEvent, C2CMessageCreateEvent, GroupAtMessageCreateEvent],
event: Union[GuildMessageEvent, QQMessageEvent],
) -> None:
"""检查消息中存在的回复,赋值 `event.reply`, `event.to_me`。
Expand All @@ -112,7 +113,7 @@ async def _check_reply(

def _check_at_me(
bot: "Bot",
event: Union[GuildMessageEvent, C2CMessageCreateEvent, GroupAtMessageCreateEvent],
event: Union[GuildMessageEvent, QQMessageEvent],
):
if (
isinstance(event, GuildMessageEvent)
Expand Down Expand Up @@ -269,9 +270,7 @@ async def get_authorization_header(self) -> Dict[str, str]:
return headers

async def handle_event(self, event: Event) -> None:
if isinstance(
event, (GuildMessageEvent, C2CMessageCreateEvent, GroupAtMessageCreateEvent)
):
if isinstance(event, (GuildMessageEvent, QQMessageEvent)):
await _check_reply(self, event)
_check_at_me(self, event)
await handle_event(self, event)
Expand Down
67 changes: 24 additions & 43 deletions nonebot/adapters/qq/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,6 @@ class GuildMemberRemoveEvent(GuildMemberEvent):
class MessageEvent(Event):
to_me: bool = False

reply: Optional[GuildMessage] = None
"""
:说明: 消息中提取的回复消息,内容为 ``get_message_of_id`` API 返回结果
:类型: ``Optional[MessageGet]``
"""

@override
def get_type(self) -> str:
return "message"
Expand All @@ -272,6 +265,13 @@ def is_tome(self) -> bool:


class GuildMessageEvent(MessageEvent, GuildMessage):
reply: Optional[GuildMessage] = None
"""
:说明: 消息中提取的回复消息,内容为 ``get_message_of_id`` API 返回结果
:类型: ``Optional[GuildMessage]``
"""

@override
def get_user_id(self) -> str:
return self.author.id
Expand Down Expand Up @@ -309,6 +309,7 @@ class MessageDeleteEvent(NoticeEvent, MessageDelete):
@register_event_class
class AtMessageCreateEvent(GuildMessageEvent):
__type__ = EventType.AT_MESSAGE_CREATE

to_me: bool = True


Expand Down Expand Up @@ -336,10 +337,7 @@ class DirectMessageDeleteEvent(MessageDeleteEvent):
__type__ = EventType.DIRECT_MESSAGE_DELETE


@register_event_class
class C2CMessageCreateEvent(MessageEvent):
__type__ = EventType.C2C_MESSAGE_CREATE

class QQMessageEvent(MessageEvent):
id: str
author: Author
content: str
Expand All @@ -353,56 +351,39 @@ def get_user_id(self) -> str:
def get_session_id(self) -> str:
return self.author.id

@override
def get_event_description(self) -> str:
return escape_tag(
f"Message {self.id} from {self.author.id}: {self.get_message()!r}"
)

@override
def get_message(self) -> Message:
if not hasattr(self, "_message"):
setattr(self, "_message", Message(self.content))
return getattr(self, "_message")

@override
def is_tome(self) -> bool:
return True


@register_event_class
class GroupAtMessageCreateEvent(MessageEvent):
__type__ = EventType.GROUP_AT_MESSAGE_CREATE

id: str
author: Author
group_id: str
content: str
timestamp: str

@override
def get_user_id(self) -> str:
return self.author.id
class C2CMessageCreateEvent(QQMessageEvent):
__type__ = EventType.C2C_MESSAGE_CREATE

@override
def get_session_id(self) -> str:
return self.author.id
to_me: bool = True

@override
def get_event_description(self) -> str:
return escape_tag(
f"Message {self.id} from {self.author.id}: {self.get_message()!r}"
)

@override
def get_message(self) -> Message:
if not hasattr(self, "_message"):
setattr(self, "_message", Message(self.content))
return getattr(self, "_message")

@register_event_class
class GroupAtMessageCreateEvent(QQMessageEvent):
__type__ = EventType.GROUP_AT_MESSAGE_CREATE

group_id: str
to_me: bool = True

@override
def is_tome(self) -> bool:
return True
def get_event_description(self) -> str:
return escape_tag(
f"Message {self.id} from {self.author.id}@[Group:{self.group_id}]: "
f"{self.get_message()!r}"
)


@register_event_class
Expand Down

0 comments on commit 632d3a7

Please sign in to comment.