11"""CQHTTP 适配器事件。"""
22# pyright: reportIncompatibleVariableOverride=false
33
4- from typing import TYPE_CHECKING , Any , Literal , get_args , get_origin
4+ from typing import TYPE_CHECKING , Any , Literal , get_origin
55from typing_extensions import override
66
77from pydantic import BaseModel , ConfigDict , Field
88from pydantic .fields import FieldInfo
9+ from typing_inspection import typing_objects
10+ from typing_inspection .introspection import get_literal_values
911
1012from alicebot .event import Event
1113from alicebot .event import MessageEvent as BaseMessageEvent
@@ -61,9 +63,9 @@ def _get_literal_field(field: FieldInfo | None) -> str | None:
6163 if field is None :
6264 return None
6365 annotation = field .annotation
64- if annotation is None or get_origin (annotation ) is not Literal :
66+ if annotation is None or not typing_objects . is_literal ( get_origin (annotation )) :
6567 return None
66- literal_values = get_args ( annotation )
68+ literal_values = list ( get_literal_values ( annotation ) )
6769 if len (literal_values ) != 1 :
6870 return None
6971 return literal_values [0 ]
@@ -72,7 +74,6 @@ def _get_literal_field(field: FieldInfo | None) -> str | None:
7274class CQHTTPEvent (Event ["CQHTTPAdapter" ]):
7375 """CQHTTP 事件基类"""
7476
75- __event__ = ""
7677 type : str | None = Field (alias = "post_type" )
7778 time : int
7879 self_id : int
@@ -103,7 +104,6 @@ def get_event_type(cls) -> tuple[str | None, str | None, str | None]:
103104class MessageEvent (CQHTTPEvent , BaseMessageEvent ["CQHTTPAdapter" ]):
104105 """消息事件"""
105106
106- __event__ = "message"
107107 post_type : Literal ["message" ]
108108 message_type : Literal ["private" , "group" ]
109109 sub_type : str
@@ -144,7 +144,6 @@ async def reply(
144144class PrivateMessageEvent (MessageEvent ):
145145 """私聊消息"""
146146
147- __event__ = "message.private"
148147 message_type : Literal ["private" ]
149148 sub_type : Literal ["friend" , "group" , "other" ]
150149
@@ -160,7 +159,6 @@ async def reply(
160159class GroupMessageEvent (MessageEvent ):
161160 """群消息"""
162161
163- __event__ = "message.group"
164162 message_type : Literal ["group" ]
165163 sub_type : Literal ["normal" , "anonymous" , "notice" ]
166164 group_id : int
@@ -178,15 +176,13 @@ async def reply(
178176class NoticeEvent (CQHTTPEvent ):
179177 """通知事件"""
180178
181- __event__ = "notice"
182179 post_type : Literal ["notice" ]
183180 notice_type : str
184181
185182
186183class GroupUploadNoticeEvent (NoticeEvent ):
187184 """群文件上传"""
188185
189- __event__ = "notice.group_upload"
190186 notice_type : Literal ["group_upload" ]
191187 user_id : int
192188 group_id : int
@@ -196,7 +192,6 @@ class GroupUploadNoticeEvent(NoticeEvent):
196192class GroupAdminNoticeEvent (NoticeEvent ):
197193 """群管理员变动"""
198194
199- __event__ = "notice.group_admin"
200195 notice_type : Literal ["group_admin" ]
201196 sub_type : Literal ["set" , "unset" ]
202197 user_id : int
@@ -206,7 +201,6 @@ class GroupAdminNoticeEvent(NoticeEvent):
206201class GroupDecreaseNoticeEvent (NoticeEvent ):
207202 """群成员减少"""
208203
209- __event__ = "notice.group_decrease"
210204 notice_type : Literal ["group_decrease" ]
211205 sub_type : Literal ["leave" , "kick" , "kick_me" ]
212206 group_id : int
@@ -217,7 +211,6 @@ class GroupDecreaseNoticeEvent(NoticeEvent):
217211class GroupIncreaseNoticeEvent (NoticeEvent ):
218212 """群成员增加"""
219213
220- __event__ = "notice.group_increase"
221214 notice_type : Literal ["group_increase" ]
222215 sub_type : Literal ["approve" , "invite" ]
223216 group_id : int
@@ -228,7 +221,6 @@ class GroupIncreaseNoticeEvent(NoticeEvent):
228221class GroupBanNoticeEvent (NoticeEvent ):
229222 """群禁言"""
230223
231- __event__ = "notice.group_ban"
232224 notice_type : Literal ["group_ban" ]
233225 sub_type : Literal ["ban" , "lift_ban" ]
234226 group_id : int
@@ -240,15 +232,13 @@ class GroupBanNoticeEvent(NoticeEvent):
240232class FriendAddNoticeEvent (NoticeEvent ):
241233 """好友添加"""
242234
243- __event__ = "notice.friend_add"
244235 notice_type : Literal ["friend_add" ]
245236 user_id : int
246237
247238
248239class GroupRecallNoticeEvent (NoticeEvent ):
249240 """群消息撤回"""
250241
251- __event__ = "notice.group_recall"
252242 notice_type : Literal ["group_recall" ]
253243 group_id : int
254244 operator_id : int
@@ -259,7 +249,6 @@ class GroupRecallNoticeEvent(NoticeEvent):
259249class FriendRecallNoticeEvent (NoticeEvent ):
260250 """好友消息撤回"""
261251
262- __event__ = "notice.friend_recall"
263252 notice_type : Literal ["friend_recall" ]
264253 user_id : int
265254 message_id : int
@@ -268,7 +257,6 @@ class FriendRecallNoticeEvent(NoticeEvent):
268257class NotifyEvent (NoticeEvent ):
269258 """提醒事件"""
270259
271- __event__ = "notice.notify"
272260 notice_type : Literal ["notify" ]
273261 sub_type : str
274262 user_id : int
@@ -277,7 +265,6 @@ class NotifyEvent(NoticeEvent):
277265class PokeNotifyEvent (NotifyEvent ):
278266 """戳一戳"""
279267
280- __event__ = "notice.notify.poke"
281268 sub_type : Literal ["poke" ]
282269 target_id : int
283270 group_id : int | None = None
@@ -286,7 +273,6 @@ class PokeNotifyEvent(NotifyEvent):
286273class GroupLuckyKingNotifyEvent (NotifyEvent ):
287274 """群红包运气王"""
288275
289- __event__ = "notice.notify.lucky_king"
290276 sub_type : Literal ["lucky_king" ]
291277 group_id : int
292278 target_id : int
@@ -295,7 +281,6 @@ class GroupLuckyKingNotifyEvent(NotifyEvent):
295281class GroupHonorNotifyEvent (NotifyEvent ):
296282 """群成员荣誉变更"""
297283
298- __event__ = "notice.notify.honor"
299284 sub_type : Literal ["honor" ]
300285 group_id : int
301286 honor_type : Literal ["talkative" , "performer" , "emotion" ]
@@ -304,7 +289,6 @@ class GroupHonorNotifyEvent(NotifyEvent):
304289class RequestEvent (CQHTTPEvent ):
305290 """请求事件"""
306291
307- __event__ = "request"
308292 post_type : Literal ["request" ]
309293 request_type : str
310294
@@ -328,7 +312,6 @@ async def refuse(self) -> dict[str, Any]:
328312class FriendRequestEvent (RequestEvent ):
329313 """加好友请求"""
330314
331- __event__ = "request.friend"
332315 request_type : Literal ["friend" ]
333316 user_id : int
334317 comment : str
@@ -356,7 +339,6 @@ async def refuse(self) -> dict[str, Any]:
356339class GroupRequestEvent (RequestEvent ):
357340 """加群请求 / 邀请"""
358341
359- __event__ = "request.group"
360342 request_type : Literal ["group" ]
361343 sub_type : Literal ["add" , "invite" ]
362344 group_id : int
@@ -388,23 +370,20 @@ async def refuse(self, reason: str = "") -> dict[str, Any]:
388370class MetaEvent (CQHTTPEvent ):
389371 """元事件"""
390372
391- __event__ = "meta_event"
392373 post_type : Literal ["meta_event" ]
393374 meta_event_type : str
394375
395376
396377class LifecycleMetaEvent (MetaEvent ):
397378 """生命周期"""
398379
399- __event__ = "meta_event.lifecycle"
400380 meta_event_type : Literal ["lifecycle" ]
401381 sub_type : Literal ["enable" , "disable" , "connect" ]
402382
403383
404384class HeartbeatMetaEvent (MetaEvent ):
405385 """心跳"""
406386
407- __event__ = "meta_event.heartbeat"
408387 meta_event_type : Literal ["heartbeat" ]
409388 status : Status
410389 interval : int
0 commit comments