-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathelems.py
More file actions
390 lines (287 loc) · 7.42 KB
/
elems.py
File metadata and controls
390 lines (287 loc) · 7.42 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import json
from dataclasses import dataclass, field
import time
from typing import TYPE_CHECKING, Optional
from lagrange.client.events.group import GroupMessage
from lagrange.info.serialize import JsonSerializer
if TYPE_CHECKING:
from .types import Element
@dataclass
class BaseElem(JsonSerializer):
@property
def display(self) -> str:
return ""
@property
def type(self) -> str:
return self.__class__.__name__.lower()
@property
def raw_text(self) -> str:
return ""
@dataclass
class CompatibleText(BaseElem):
"""仅用于兼容性变更,不应作为判断条件"""
@property
def text(self) -> str:
return self.display
@text.setter
def text(self, text: str):
"""ignore"""
@dataclass
class MediaInfo:
name: str
size: int
url: str
id: int = field(repr=False)
md5: bytes = field(repr=False)
qmsg: Optional[bytes] = field(repr=False) # not online image
@dataclass
class Text(BaseElem):
text: str
@property
def display(self) -> str:
return self.text
@property
def raw_text(self) -> str:
return self.text
@dataclass
class Quote(CompatibleText):
seq: int
uin: int
timestamp: int
uid: str = ""
msg: str = ""
@classmethod
def build(cls, ev: GroupMessage) -> "Quote":
return cls(
seq=ev.seq,
uid=ev.uid,
uin=ev.uin,
timestamp=ev.time,
msg=ev.msg,
)
@property
def display(self) -> str:
return f"[quote:{self.msg}]"
@dataclass
class Json(CompatibleText):
raw: bytes
def to_dict(self) -> dict:
return json.loads(self.raw)
@property
def display(self) -> str:
return f"[json:{len(self.raw)}]"
@dataclass
class Service(Json):
id: int
@property
def display(self) -> str:
return f"[service:{self.id}]"
@dataclass
class AtAll(BaseElem):
text: str
@property
def display(self) -> str:
return self.text
@property
def raw_text(self) -> str:
return self.text
@dataclass
class At(BaseElem):
text: str
uin: int
uid: str
@classmethod
def build(cls, ev: GroupMessage) -> "At":
return cls(uin=ev.uin, uid=ev.uid, text=f"@{ev.nickname or ev.uin}")
@property
def display(self) -> str:
return self.text
@property
def raw_text(self) -> str:
return self.text
@dataclass
class Image(CompatibleText, MediaInfo):
width: int
height: int
is_emoji: bool
display_name: str
@property
def raw_text(self) -> str:
return "[图片]"
@property
def display(self) -> str:
return self.display_name
@dataclass
class Video(CompatibleText, MediaInfo):
width: int
height: int
time: int
file_key: str = field(repr=True)
@property
def raw_text(self) -> str:
return "[视频]"
@property
def display(self) -> str:
return f"[video:{self.width}x{self.height},{self.time}s]"
@dataclass
class Audio(CompatibleText, MediaInfo):
time: int
file_key: str = field(repr=True)
@property
def raw_text(self) -> str:
return "[语音]"
@property
def display(self) -> str:
return f"[audio:{self.time}]"
@dataclass
class Raw(CompatibleText):
data: bytes
@property
def display(self) -> str:
return f"[raw:{len(self.data)}]"
@dataclass
class Emoji(CompatibleText):
id: int
@property
def display(self) -> str:
return f"[emoji:{self.id}]"
@dataclass
class Reaction(Emoji):
"""QQ: super emoji"""
# text: str
# show_type: int # size - sm: 33, bg: 37
@dataclass
class Poke(CompatibleText):
id: int
f7: int = 0
f8: int = 0
@property
def display(self) -> str:
return f"[poke:{self.id}]"
@dataclass
class MarketFace(CompatibleText):
name: str
face_id: bytes
tab_id: int
width: int
height: int
@property
def url(self) -> str:
pic_id = self.face_id.hex()
return f"https://i.gtimg.cn/club/item/parcel/item/{pic_id[:2]}/{pic_id}/{self.width}x{self.height}.png"
@property
def display(self) -> str:
return f"[marketface:{self.name}]"
@property
def raw_text(self) -> str:
return "[动画表情]"
@dataclass
class File(CompatibleText):
file_size: int
file_name: str
file_md5: bytes
file_url: Optional[str]
file_id: Optional[str] # only in group
file_uuid: Optional[str] # only in private
file_hash: Optional[str]
@property
def display(self) -> str:
return f"[file:{self.file_name}]"
@property
def raw_text(self) -> str:
return "[文件]"
@classmethod
def _paste_build(
cls,
file_size: int,
file_name: str,
file_md5: bytes,
file_id: Optional[str] = None,
file_uuid: Optional[str] = None,
file_hash: Optional[str] = None,
) -> "File":
return cls(
file_size=file_size,
file_name=file_name,
file_md5=file_md5,
file_url=None,
file_id=file_id,
file_uuid=file_uuid,
file_hash=file_hash,
)
@classmethod
def grp_paste_build(cls, file_size: int, file_name: str, file_md5: bytes, file_id: str) -> "File":
return cls._paste_build(file_size, file_name, file_md5, file_id=file_id)
@classmethod
def pri_paste_build(cls, file_size: int, file_name: str, file_md5: bytes, file_uuid: str, file_hash: str) -> "File":
return cls._paste_build(file_size, file_name, file_md5, file_uuid=file_uuid, file_hash=file_hash)
@dataclass
class GreyTips(BaseElem):
"""
奇怪的整活元素
建议搭配Text使用
冷却3分钟左右?
"""
text: str
@property
def display(self) -> str:
return f"<GreyTips: {self.text}>"
@dataclass
class Markdown(BaseElem):
content: str
@property
def display(self) -> str:
return f"[markdown:{self.content}]"
@property
def raw_text(self) -> str:
return "[Markdown]"
class Permission:
type: int
specify_role_ids: Optional[list[str]]
specify_user_ids: Optional[list[str]]
class RenderData:
label: Optional[str]
visited_label: Optional[str]
style: int
class Action:
type: Optional[int]
permission: Optional[Permission]
data: str
reply: bool
enter: bool
anchor: Optional[int]
unsupport_tips: Optional[str]
click_limit: Optional[int] # deprecated
at_bot_show_channel_list: bool # deprecated
class Button:
id: Optional[str]
render_data: Optional[RenderData]
action: Optional[Action]
class InlineKeyboardRow:
buttons: Optional[list[Button]]
class InlineKeyboard:
rows: list[InlineKeyboardRow]
@dataclass
class Keyboard(BaseElem):
content: Optional[list[InlineKeyboard]]
bot_appid: int
@property
def display(self) -> str:
return f"[keyboard:{self.bot_appid}]"
@dataclass
class ForwardNode(BaseElem):
content: list["Element"]
sender_uin: int
sender_nick: str = ""
sender_avatar_url: str = ""
timestamp: int = field(default_factory=lambda: int(time.time()))
@dataclass
class MulitMsg(BaseElem):
messages: list[ForwardNode]
resid: Optional[str] = None
@property
def display(self) -> str:
return f"[forward:{self.resid}]"
@property
def raw_text(self) -> str:
return "[聊天记录]"