-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
utils.py
72 lines (54 loc) · 2.07 KB
/
utils.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
from typing import Literal
from pydantic import BaseModel, create_model
from nonebot.adapters.onebot.v11 import Message
from nonebot.adapters.onebot.v11.event import (
Sender,
GroupMessageEvent,
PrivateMessageEvent,
)
def escape_text(s: str, *, escape_comma: bool = True) -> str:
s = s.replace("&", "&").replace("[", "[").replace("]", "]")
if escape_comma:
s = s.replace(",", ",")
return s
class CommonFields(BaseModel):
time: int = 1000000
self_id: int = 1
post_type: str = "message"
sub_type: str = "normal"
message_id: int = 1
message: Message = Message("test")
original_message: Message = Message("test")
raw_message: str = "test"
font: int = 0
to_me: bool = False
class GroupMessageEventFields(BaseModel):
user_id: int = 1145141919
message_type: Literal["group"] = "group"
group_id: int = 10000
sender: Sender = Sender(card="", nickname="test", role="member")
class PrivateMessageEventFields(BaseModel):
sub_type: str = "friend"
user_id: int = 1145141919
message_type: Literal["private"] = "private"
sender: Sender = Sender(nickname="test")
def fake_event(event_cls, upgrade_cls: BaseModel, **field):
_Fake = create_model("_Fake", __base__=event_cls)
class FakeEvent(_Fake):
__fields__ = {
**CommonFields.__fields__,
**upgrade_cls.__fields__,
}
class Config:
extra = "forbid"
return FakeEvent(**field)
def group_message_event(**field) -> GroupMessageEvent:
if "message" in field:
field.update({"original_message": field["message"]})
field.update({"raw_message": str(field["message"])})
return fake_event(GroupMessageEvent, GroupMessageEventFields, **field) # type: ignore
def private_message_event(**field) -> PrivateMessageEvent:
if "message" in field:
field.update({"original_message": field["message"]})
field.update({"raw_message": str(field["message"])})
return fake_event(PrivateMessageEvent, PrivateMessageEventFields, **field) # type: ignore