forked from XDzzzzzZyq/Guluton_test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecvContext.py
188 lines (153 loc) · 7.02 KB
/
RecvContext.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# Generated by QuickType
# Reference to OneBot v11
# https://github.com/botuniverse/onebot-11/blob/master/event/message.md
from dataclasses import dataclass
from typing import Optional, Any, TypeVar, Type, cast
T = TypeVar("T")
def from_int(x: Any) -> int:
assert isinstance(x, int) and not isinstance(x, bool)
return x
def from_none(x: Any) -> Any:
assert x is None
return x
def from_union(fs, x):
for f in fs:
try:
return f(x)
except:
pass
assert False
def from_str(x: Any) -> str:
assert isinstance(x, str)
return x
def to_class(c: Type[T], x: Any) -> dict:
assert isinstance(x, c)
return cast(Any, x).to_dict()
@dataclass
class Sender:
'''消息发送者信息'''
age: Optional[int] = None
'''年龄'''
nickname: Optional[str] = None
'''昵称'''
sex: Optional[str] = None
'''性别,male 或 female 或 unknown'''
user_id: Optional[int] = None
'''发送者 QQ 号'''
area: Optional[str] = None
'''(仅群聊)地区'''
card: Optional[str] = None
'''(仅群聊)群名片/备注'''
level: Optional[str] = None
'''(仅群聊)成员等级'''
role: Optional[str] = None
'''(仅群聊)角色,owner 或 admin 或 member'''
title: Optional[str] = None
'''(仅群聊)专属头衔'''
@staticmethod
def from_dict(obj: Any) -> 'Sender':
assert isinstance(obj, dict)
age = from_union([from_int, from_none], obj.get("age"))
nickname = from_union([from_str, from_none], obj.get("nickname"))
sex = from_union([from_str, from_none], obj.get("sex"))
user_id = from_union([from_int, from_none], obj.get("user_id"))
area = from_union([from_str, from_none], obj.get("area"))
card = from_union([from_str, from_none], obj.get("card"))
level = from_union([from_str, from_none], obj.get("level"))
role = from_union([from_str, from_none], obj.get("role"))
title = from_union([from_str, from_none], obj.get("title"))
return Sender(age, nickname, sex, user_id, area, card, level, role, title)
def to_dict(self) -> dict:
result: dict = {}
result["age"] = from_union([from_int, from_none], self.age)
result["nickname"] = from_union([from_str, from_none], self.nickname)
result["sex"] = from_union([from_str, from_none], self.sex)
result["user_id"] = from_union([from_int, from_none], self.user_id)
result["area"] = from_union([from_str, from_none], self.area)
result["card"] = from_union([from_str, from_none], self.card)
result["level"] = from_union([from_str, from_none], self.level)
result["role"] = from_union([from_str, from_none], self.role)
result["title"] = from_union([from_str, from_none], self.title)
return result
@dataclass
class RecvContext:
anonymous: None
'''(仅群聊)匿名信息,如果不是匿名消息则为 null'''
post_type: Optional[str] = None
'''上报类型
message:消息事件
notice:通知事件
request:请求事件
meta_event:元事件'''
message_type: Optional[str] = None
'''消息类型'''
time: Optional[int] = None
'''事件发生的时间戳'''
self_id: Optional[int] = None
'''收到事件的机器人 QQ 号'''
sub_type: Optional[str] = None
'''消息子类型
对于私聊,如果是好友则是 friend, 如果是群临时会话则是 group
对于群聊,正常消息是 normal, 匿名消息是 anonymous, 系统提示(如「管理员已禁止群内匿名聊天」)是 notice'''
message_id: Optional[int] = None
''' 消息 ID'''
user_id: Optional[int] = None
'''发送者 QQ 号'''
target_id: Optional[int] = None
message: Optional[str] = None
'''消息内容'''
raw_message: Optional[str] = None
'''原始消息内容'''
font: Optional[int] = None
'''字体'''
sender: Optional[Sender] = None
'''发送人信息'''
group_id: Optional[int] = None
'''(仅群聊)群号'''
message_seq: Optional[int] = None
@staticmethod
def from_dict(obj: Any) -> 'RecvContext':
assert isinstance(obj, dict)
anonymous = from_none(obj.get("anonymous"))
post_type = from_union([from_str, from_none], obj.get("post_type"))
message_type = from_union([from_str, from_none], obj.get("message_type"))
time = from_union([from_int, from_none], obj.get("time"))
self_id = from_union([from_int, from_none], obj.get("self_id"))
sub_type = from_union([from_str, from_none], obj.get("sub_type"))
message_id = from_union([from_int, from_none], obj.get("message_id"))
user_id = from_union([from_int, from_none], obj.get("user_id"))
target_id = from_union([from_int, from_none], obj.get("target_id"))
message = from_union([from_str, from_none], obj.get("message"))
raw_message = from_union([from_str, from_none], obj.get("raw_message"))
font = from_union([from_int, from_none], obj.get("font"))
sender = from_union([Sender.from_dict, from_none], obj.get("sender"))
group_id = from_union([from_int, from_none], obj.get("group_id"))
message_seq = from_union([from_int, from_none], obj.get("message_seq"))
return RecvContext(anonymous, post_type, message_type, time, self_id, sub_type, message_id, user_id, target_id, message, raw_message, font, sender, group_id, message_seq)
def to_dict(self) -> dict:
result: dict = {}
result["anonymous"] = from_none(self.anonymous)
result["post_type"] = from_union([from_str, from_none], self.post_type)
result["message_type"] = from_union([from_str, from_none], self.message_type)
result["time"] = from_union([from_int, from_none], self.time)
result["self_id"] = from_union([from_int, from_none], self.self_id)
result["sub_type"] = from_union([from_str, from_none], self.sub_type)
result["message_id"] = from_union([from_int, from_none], self.message_id)
result["user_id"] = from_union([from_int, from_none], self.user_id)
result["target_id"] = from_union([from_int, from_none], self.target_id)
result["message"] = from_union([from_str, from_none], self.message)
result["raw_message"] = from_union([from_str, from_none], self.raw_message)
result["font"] = from_union([from_int, from_none], self.font)
result["sender"] = from_union([lambda x: to_class(Sender, x), from_none], self.sender)
result["group_id"] = from_union([from_int, from_none], self.group_id)
result["message_seq"] = from_union([from_int, from_none], self.message_seq)
return result
def recv_context_from_dict(s: Any) -> RecvContext:
return RecvContext.from_dict(s)
def recv_context_to_dict(x: RecvContext) -> Any:
return to_class(RecvContext, x)
from json import dumps as jsonify, loads as from_json
def fromjson(json):
return recv_context_from_dict(from_json(json))
def asjson(x:RecvContext):
jsonify(recv_context_to_dict(x))