euler-onebot 0.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- euler_onebot-0.0.2.dist-info/METADATA +158 -0
- euler_onebot-0.0.2.dist-info/RECORD +19 -0
- euler_onebot-0.0.2.dist-info/WHEEL +5 -0
- euler_onebot-0.0.2.dist-info/licenses/LICENSE +674 -0
- euler_onebot-0.0.2.dist-info/top_level.txt +1 -0
- euleronebot/config.py +79 -0
- euleronebot/hyperogger.py +171 -0
- euleronebot/onebot/__init__.py +80 -0
- euleronebot/onebot/api.py +291 -0
- euleronebot/onebot/api_data.py +277 -0
- euleronebot/onebot/connector.py +117 -0
- euleronebot/onebot/events.py +215 -0
- euleronebot/onebot/models.py +11 -0
- euleronebot/onebot/segments.py +159 -0
- euleronebot/protocol.py +528 -0
- euleronebot/utils/__init__.py +28 -0
- euleronebot/utils/infomgr.py +105 -0
- euleronebot/utils/transformer.py +224 -0
- euleronebot/versions.py +2 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
def rgb(r: int, g: int, b: int) -> tuple[int, int, int]:
|
|
2
|
+
return r, g, b
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def color_txt(text: str, color: tuple[int, int, int]) -> str:
|
|
6
|
+
r = color[0]
|
|
7
|
+
g = color[1]
|
|
8
|
+
b = color[2]
|
|
9
|
+
return f"\x1b[38;2;{r};{g};{b}m{text}\x1b[0m"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class NerdICONs:
|
|
13
|
+
def __init__(self, enable: bool):
|
|
14
|
+
self.enable = enable
|
|
15
|
+
|
|
16
|
+
def __getattribute__(self, item) -> str:
|
|
17
|
+
if super().__getattribute__("enable"):
|
|
18
|
+
return str(super().__getattribute__(item))
|
|
19
|
+
else:
|
|
20
|
+
return " "
|
|
21
|
+
|
|
22
|
+
nf_fa_circle_info = " \uf05a"
|
|
23
|
+
nf_cod_bracket_error = " \uebe6"
|
|
24
|
+
nf_cod_error = " \uea87"
|
|
25
|
+
nf_fa_warn = " \uf071"
|
|
26
|
+
nf_cod_debug_alt = " \ueb91"
|
|
27
|
+
nf_cod_debug_breakpoint_log = " \ueaab"
|
|
28
|
+
nf_weather_time_4 = " \ue385"
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import os.path
|
|
2
|
+
|
|
3
|
+
from pydantic import BaseModel
|
|
4
|
+
from typing import Dict, Literal, Union, Self
|
|
5
|
+
import random
|
|
6
|
+
|
|
7
|
+
from lagrange.client.message import elems
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class MsgInfo(BaseModel):
|
|
11
|
+
scene_type: Literal["group", "user"]
|
|
12
|
+
scene_id: int
|
|
13
|
+
uin: int = 0
|
|
14
|
+
uid: str = ""
|
|
15
|
+
timestamp: int = 0
|
|
16
|
+
raw_msg: list[elems.BaseElem] = []
|
|
17
|
+
seq: int
|
|
18
|
+
text: str = ""
|
|
19
|
+
|
|
20
|
+
def __eq__(self, other: Self) -> bool:
|
|
21
|
+
return (
|
|
22
|
+
other.scene_type == self.scene_type and
|
|
23
|
+
other.scene_id == self.scene_id and
|
|
24
|
+
other.seq == self.seq
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class MsgIDPool(BaseModel):
|
|
29
|
+
pool: Dict[str, MsgInfo] = {}
|
|
30
|
+
|
|
31
|
+
def _gen_id(self) -> int:
|
|
32
|
+
x = 0
|
|
33
|
+
while not x or str(x) in list(self.pool.keys()):
|
|
34
|
+
x = random.randint(1 << 15, (1 << 18) - 1)
|
|
35
|
+
return x
|
|
36
|
+
|
|
37
|
+
def add(self, info: MsgInfo) -> int:
|
|
38
|
+
nid = self._gen_id()
|
|
39
|
+
self.pool[str(nid)] = info
|
|
40
|
+
return nid
|
|
41
|
+
|
|
42
|
+
def fetch(self, nid: int) -> MsgInfo:
|
|
43
|
+
if str(nid) in list(self.pool.keys()):
|
|
44
|
+
return self.pool[str(nid)]
|
|
45
|
+
else:
|
|
46
|
+
raise KeyError(f"message_id = {nid} 没有对应的消息")
|
|
47
|
+
|
|
48
|
+
def search(self, info: MsgInfo) -> int:
|
|
49
|
+
if info in list(self.pool.values()):
|
|
50
|
+
index = list(self.pool.values()).index(info)
|
|
51
|
+
else:
|
|
52
|
+
return 0
|
|
53
|
+
return int(list(self.pool.keys())[index])
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class UIDPool(BaseModel):
|
|
57
|
+
pool: Dict[str, int] = {}
|
|
58
|
+
|
|
59
|
+
def add(self, uid: str, uin: int) -> int:
|
|
60
|
+
self.pool[uid] = uin
|
|
61
|
+
return uin
|
|
62
|
+
|
|
63
|
+
def add_fake(self, uid: str) -> int:
|
|
64
|
+
x = 0
|
|
65
|
+
while not x or str(x) + "0145" in list(self.pool.keys()):
|
|
66
|
+
x = random.randint(1 << 15, (1 << 16) - 1)
|
|
67
|
+
self.add(uid, int(str(x) + "0145"))
|
|
68
|
+
return int(str(x) + "0145")
|
|
69
|
+
|
|
70
|
+
def from_uid(self, uid: str) -> int:
|
|
71
|
+
if uid in list(self.pool.keys()):
|
|
72
|
+
return self.pool[uid]
|
|
73
|
+
else:
|
|
74
|
+
raise ValueError(f"未缓存 uid = {uid}")
|
|
75
|
+
|
|
76
|
+
def from_uin(self, uin: int) -> str:
|
|
77
|
+
if uin in list(self.pool.values()):
|
|
78
|
+
index = list(self.pool.values()).index(uin)
|
|
79
|
+
return list(self.pool.keys())[index]
|
|
80
|
+
else:
|
|
81
|
+
raise ValueError(f"未缓存 uin = {uin}")
|
|
82
|
+
|
|
83
|
+
def is_exist(self, uid_or_uin: Union[int, str]) -> bool:
|
|
84
|
+
if isinstance(uid_or_uin, str) and uid_or_uin in list(self.pool.keys()):
|
|
85
|
+
if str(self.from_uid(uid_or_uin)).endswith("0145"):
|
|
86
|
+
return False
|
|
87
|
+
return uid_or_uin in list(self.pool.keys()) or uid_or_uin in list(self.pool.values())
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
class InfoManager(BaseModel):
|
|
91
|
+
msgid_mgr: MsgIDPool = MsgIDPool()
|
|
92
|
+
uid_mgr: UIDPool = UIDPool()
|
|
93
|
+
|
|
94
|
+
async def save(self) -> None:
|
|
95
|
+
with open("cache.json", "w", encoding="utf-8") as f:
|
|
96
|
+
f.write(self.model_dump_json(indent=2))
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
if os.path.exists("cache.json"):
|
|
100
|
+
with open("cache.json", "r", encoding="utf-8") as f:
|
|
101
|
+
info_mgr = InfoManager.model_validate_json(f.read())
|
|
102
|
+
else:
|
|
103
|
+
info_mgr = InfoManager()
|
|
104
|
+
with open("cache.json", "w", encoding="utf-8") as f:
|
|
105
|
+
f.write(info_mgr.model_dump_json(indent=2))
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import io
|
|
3
|
+
import base64
|
|
4
|
+
import httpx
|
|
5
|
+
from typing import Union
|
|
6
|
+
from urllib.parse import urlparse, unquote
|
|
7
|
+
|
|
8
|
+
from lagrange import Client
|
|
9
|
+
from lagrange.client.message import elems
|
|
10
|
+
from lagrange.client.events.group import GroupMessage
|
|
11
|
+
from lagrange.client.events.friend import FriendMessage
|
|
12
|
+
from lagrange.client.message.types import Element as LgrElement
|
|
13
|
+
|
|
14
|
+
from ..onebot import segments as seg
|
|
15
|
+
from .infomgr import MsgInfo, info_mgr
|
|
16
|
+
from ..onebot.models import TargetInfo
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
async def to_onebot_msg(
|
|
20
|
+
lgrc: Client,
|
|
21
|
+
event: Union[GroupMessage, FriendMessage] = None,
|
|
22
|
+
msg: MsgInfo = None
|
|
23
|
+
) -> list[seg.BaseSegment]:
|
|
24
|
+
new: list[seg.BaseSegment] = []
|
|
25
|
+
info_renewed = False
|
|
26
|
+
if event:
|
|
27
|
+
msgc = event.msg_chain
|
|
28
|
+
elif msg:
|
|
29
|
+
msgc = msg
|
|
30
|
+
else:
|
|
31
|
+
return [] # 特喵的泥啥也不给我
|
|
32
|
+
for i in msgc:
|
|
33
|
+
if isinstance(i, elems.Text):
|
|
34
|
+
new.append(seg.Text(data=seg.TextData(text=i.text)))
|
|
35
|
+
elif isinstance(i, elems.Quote):
|
|
36
|
+
if event and isinstance(event, GroupMessage):
|
|
37
|
+
info = MsgInfo(
|
|
38
|
+
scene_type="group",
|
|
39
|
+
scene_id=event.grp_id,
|
|
40
|
+
uin=event.uin,
|
|
41
|
+
uid=event.uid,
|
|
42
|
+
timestamp=event.time,
|
|
43
|
+
raw_msg=event.msg_chain,
|
|
44
|
+
seq=event.seq
|
|
45
|
+
)
|
|
46
|
+
if msgid := info_mgr.msgid_mgr.search(info):
|
|
47
|
+
pass
|
|
48
|
+
else:
|
|
49
|
+
msgid = info_mgr.msgid_mgr.add(info)
|
|
50
|
+
info_renewed = True
|
|
51
|
+
else:
|
|
52
|
+
msgid = info_mgr.msgid_mgr.search(msg)
|
|
53
|
+
new.append(seg.Reply(data=seg.ReplyData(id=str(msgid))))
|
|
54
|
+
|
|
55
|
+
elif isinstance(i, elems.AtAll):
|
|
56
|
+
new.append(seg.At(data=seg.AtData(qq="all")))
|
|
57
|
+
elif isinstance(i, elems.At):
|
|
58
|
+
if not info_mgr.uid_mgr.is_exist(i.uid):
|
|
59
|
+
info_mgr.uid_mgr.add(i.uid, i.uin)
|
|
60
|
+
info_renewed = True
|
|
61
|
+
new.append(seg.At(data=seg.AtData(qq=str(i.uin))))
|
|
62
|
+
elif isinstance(i, elems.Image):
|
|
63
|
+
new.append(seg.Image(data=seg.ImageData(file=i.url, url=i.url, summary=i.text, is_emoji=i.is_emoji)))
|
|
64
|
+
elif isinstance(i, elems.Video):
|
|
65
|
+
new.append(seg.Video(data=seg.VideoData(file=i.url, url=i.url)))
|
|
66
|
+
elif isinstance(i, elems.Audio):
|
|
67
|
+
new.append(seg.Record(data=seg.RecordData(file=i.url, url=i.url)))
|
|
68
|
+
elif isinstance(i, elems.Emoji):
|
|
69
|
+
new.append(seg.Face(data=seg.FaceData(id=str(i.id))))
|
|
70
|
+
elif isinstance(i, elems.Reaction):
|
|
71
|
+
pass
|
|
72
|
+
elif isinstance(i, elems.Poke):
|
|
73
|
+
new.append(seg.Poke(data=seg.PokeData(id=str(i.id), type="")))
|
|
74
|
+
elif isinstance(i, elems.MarketFace):
|
|
75
|
+
new.append(
|
|
76
|
+
seg.MarketFace(data=seg.MarketFaceData(face_id=str(i.face_id), tab_id=str(i.tab_id), name=i.name)))
|
|
77
|
+
elif isinstance(i, elems.File):
|
|
78
|
+
pass # TODO: As an event
|
|
79
|
+
elif isinstance(i, elems.MulitMsg):
|
|
80
|
+
new.append(
|
|
81
|
+
seg.Forward(
|
|
82
|
+
data=seg.ForwardData(
|
|
83
|
+
id=str(i.resid),
|
|
84
|
+
content=[
|
|
85
|
+
seg.Node(data=seg.NodeData(content=[], nickname=x.sender_nick, user_id=str(x.sender_uin)))
|
|
86
|
+
for x in i.messages
|
|
87
|
+
]
|
|
88
|
+
)
|
|
89
|
+
)
|
|
90
|
+
) # TODO: Real content
|
|
91
|
+
else:
|
|
92
|
+
continue
|
|
93
|
+
if info_renewed:
|
|
94
|
+
asyncio.create_task(info_mgr.save())
|
|
95
|
+
return new
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
async def to_lagrange_msg(msg: list[seg.BaseSegment], lgrc: Client, target: TargetInfo) -> list[LgrElement]:
|
|
99
|
+
new: list[LgrElement] = []
|
|
100
|
+
for i in msg:
|
|
101
|
+
if isinstance(i, seg.Text):
|
|
102
|
+
new.append(elems.Text(text=i.data.text))
|
|
103
|
+
elif isinstance(i, seg.At):
|
|
104
|
+
if i.data.qq == "all":
|
|
105
|
+
new.append(elems.AtAll(text="@全体成员"))
|
|
106
|
+
else:
|
|
107
|
+
try:
|
|
108
|
+
qq = int(i.data.qq)
|
|
109
|
+
except TypeError:
|
|
110
|
+
continue
|
|
111
|
+
uid = info_mgr.uid_mgr.from_uin(qq)
|
|
112
|
+
info = await lgrc.get_user_info(uid)
|
|
113
|
+
new.append(elems.At(text=f"@{info.name}", uin=qq, uid=uid))
|
|
114
|
+
elif isinstance(i, seg.Reply):
|
|
115
|
+
msgid = int(i.data.id)
|
|
116
|
+
msg_info = info_mgr.msgid_mgr.fetch(msgid)
|
|
117
|
+
new.append(elems.Quote(seq=msg_info.seq, uin=msg_info.uin, timestamp=msg_info.timestamp, uid=msg_info.uid,
|
|
118
|
+
msg=msg_info.text))
|
|
119
|
+
elif isinstance(i, seg.Face):
|
|
120
|
+
faceid = int(i.data.id)
|
|
121
|
+
new.append(elems.Emoji(id=faceid))
|
|
122
|
+
elif isinstance(i, seg.Poke):
|
|
123
|
+
new.append(elems.Poke(id=114514))
|
|
124
|
+
elif isinstance(i, seg.MarketFace):
|
|
125
|
+
new.append(elems.MarketFace(face_id=i.data.face_id.encode(), name=i.data.name, tab_id=int(i.data.tab_id),
|
|
126
|
+
width=512, height=512))
|
|
127
|
+
elif isinstance(i, seg.Node):
|
|
128
|
+
new.append(elems.ForwardNode(content=await to_lagrange_msg(i.data.content, lgrc, target), # type: ignore
|
|
129
|
+
sender_uin=int(i.data.user_id), sender_nick=i.data.nickname))
|
|
130
|
+
elif isinstance(i, seg.Forward):
|
|
131
|
+
if i.data.content:
|
|
132
|
+
new.append(elems.MulitMsg(messages=await to_lagrange_msg(i.data.content, lgrc, target))) # type: ignore
|
|
133
|
+
elif i.data.id:
|
|
134
|
+
new.append(elems.MulitMsg(resid=i.data.id))
|
|
135
|
+
else:
|
|
136
|
+
continue
|
|
137
|
+
elif isinstance(i, seg.Image):
|
|
138
|
+
url = urlparse(i.data.file)
|
|
139
|
+
scheme = url.scheme
|
|
140
|
+
path = unquote(url.path)
|
|
141
|
+
if scheme in ["http", "https"]:
|
|
142
|
+
async with httpx.AsyncClient() as cli:
|
|
143
|
+
retried = 0
|
|
144
|
+
while retried < 3:
|
|
145
|
+
response = await cli.get(url.geturl())
|
|
146
|
+
if response.status_code != 200:
|
|
147
|
+
retried += 1
|
|
148
|
+
continue
|
|
149
|
+
else:
|
|
150
|
+
break
|
|
151
|
+
if retried == 3:
|
|
152
|
+
continue
|
|
153
|
+
if target.target == "group":
|
|
154
|
+
img = await lgrc.upload_grp_image(grp_id=target.id, image=io.BytesIO(response.content))
|
|
155
|
+
else:
|
|
156
|
+
img = await lgrc.upload_friend_image(
|
|
157
|
+
uid=info_mgr.uid_mgr.from_uin(target.id),
|
|
158
|
+
is_emoji=i.data.is_emoji,
|
|
159
|
+
image=io.BytesIO(response.content)
|
|
160
|
+
)
|
|
161
|
+
elif scheme == "file":
|
|
162
|
+
with open(path, "rb") as f:
|
|
163
|
+
if target.target == "group":
|
|
164
|
+
img = await lgrc.upload_grp_image(grp_id=target.id, image=f)
|
|
165
|
+
else:
|
|
166
|
+
img = await lgrc.upload_friend_image(
|
|
167
|
+
uid=info_mgr.uid_mgr.from_uin(target.id),
|
|
168
|
+
is_emoji=i.data.is_emoji,
|
|
169
|
+
image=f
|
|
170
|
+
)
|
|
171
|
+
elif scheme == "base64":
|
|
172
|
+
data = i.data.file.removeprefix("base64://")
|
|
173
|
+
img = base64.b64decode(data)
|
|
174
|
+
if target.target == "group":
|
|
175
|
+
img = await lgrc.upload_grp_image(grp_id=target.id, image=io.BytesIO(img))
|
|
176
|
+
else:
|
|
177
|
+
img = await lgrc.upload_friend_image(
|
|
178
|
+
uid=info_mgr.uid_mgr.from_uin(target.id),
|
|
179
|
+
is_emoji=i.data.is_emoji,
|
|
180
|
+
image=io.BytesIO(img)
|
|
181
|
+
)
|
|
182
|
+
else:
|
|
183
|
+
continue
|
|
184
|
+
new.append(img)
|
|
185
|
+
elif isinstance(i, seg.Record):
|
|
186
|
+
url = urlparse(i.data.file)
|
|
187
|
+
scheme = url.scheme
|
|
188
|
+
path = unquote(url.path)
|
|
189
|
+
if scheme in ["http", "https"]:
|
|
190
|
+
async with httpx.AsyncClient() as cli:
|
|
191
|
+
response = await cli.get(url) # type: ignore
|
|
192
|
+
if target.target == "group":
|
|
193
|
+
voice = await lgrc.upload_grp_audio(grp_id=target.id, voice=io.BytesIO(response.content))
|
|
194
|
+
else:
|
|
195
|
+
voice = await lgrc.upload_friend_audio(
|
|
196
|
+
uid=info_mgr.uid_mgr.from_uin(target.id),
|
|
197
|
+
voice=io.BytesIO(response.content)
|
|
198
|
+
)
|
|
199
|
+
elif scheme == "file":
|
|
200
|
+
with open(path, "rb") as f:
|
|
201
|
+
if target.target == "group":
|
|
202
|
+
voice = await lgrc.upload_grp_audio(grp_id=target.id, voice=f)
|
|
203
|
+
else:
|
|
204
|
+
voice = await lgrc.upload_friend_audio(
|
|
205
|
+
uid=info_mgr.uid_mgr.from_uin(target.id),
|
|
206
|
+
voice=f
|
|
207
|
+
)
|
|
208
|
+
elif scheme == "base64":
|
|
209
|
+
data = i.data.file.removeprefix("base64://")
|
|
210
|
+
voice = base64.b64decode(data)
|
|
211
|
+
if target.target == "group":
|
|
212
|
+
voice = await lgrc.upload_grp_audio(grp_id=target.id, voice=io.BytesIO(voice))
|
|
213
|
+
else:
|
|
214
|
+
voice = await lgrc.upload_friend_audio(
|
|
215
|
+
uid=info_mgr.uid_mgr.from_uin(target.id),
|
|
216
|
+
voice=io.BytesIO(voice)
|
|
217
|
+
)
|
|
218
|
+
else:
|
|
219
|
+
continue
|
|
220
|
+
new.append(voice)
|
|
221
|
+
else:
|
|
222
|
+
continue
|
|
223
|
+
|
|
224
|
+
return new
|
euleronebot/versions.py
ADDED