1
0
forked from bot/app

擴展event字段

This commit is contained in:
2024-08-20 20:30:50 +08:00
parent 287ab63091
commit eb7c8300fa
8 changed files with 74 additions and 125 deletions

View File

@ -8,32 +8,57 @@ Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
@File : event.py
@Software: PyCharm
"""
from typing import Any
from typing import Any, Optional
from liteyuki.comm.storage import shared_memory
class Event:
def __init__(self, type: str, data: dict[str, Any], bot_id: str, session_id: str, session_type: str, receive_channel: str = "event_to_nonebot"):
class MessageEvent:
def __init__(
self,
bot_id: str,
message: list[dict[str, Any]] | str,
message_type: str,
raw_message: str,
session_id: str,
session_type: str,
receive_channel: str,
data: Optional[dict[str, Any]] = None,
):
"""
事件
轻雪抽象消息事件
Args:
type: 类型
data: 数据
bot_id: 机器人ID
session_id: 会话ID
session_type: 会话类型
receive_channel: 接收频道
message: 消息,消息段数组[{type: str, data: dict[str, Any]}]
raw_message: 原始消息(通常为纯文本的格式)
message_type: 消息类型(private, group, other)
session_id: 会话ID(私聊通常为用户ID群聊通常为群ID)
session_type: 会话类型(private, group)
receive_channel: 接收频道(用于回复消息)
data: 附加数据
"""
self.type = type
if data is None:
data = {}
self.message_type = message_type
self.data = data
self.bot_id = bot_id
self.message = message
self.raw_message = raw_message
self.session_id = session_id
self.session_type = session_type
self.receive_channel = receive_channel
def __str__(self):
return f"Event(type={self.type}, data={self.data}, bot_id={self.bot_id}, session_id={self.session_id}, session_type={self.session_type})"
return (f"Event(message_type={self.message_type}, data={self.data}, bot_id={self.bot_id}, "
f"session_id={self.session_id}, session_type={self.session_type})")
def reply(self, message: str | dict[str, Any]):
"""
@ -42,8 +67,10 @@ class Event:
message:
Returns:
"""
to_nonebot_event = Event(
type=self.session_type,
reply_event = MessageEvent(
message_type=self.session_type,
message=message,
raw_message="",
data={
"message": message
},
@ -52,5 +79,4 @@ class Event:
session_type=self.session_type,
receive_channel="_"
)
print(to_nonebot_event)
shared_memory.publish(self.receive_channel, to_nonebot_event)
shared_memory.publish(self.receive_channel, reply_event)