hyper-bot 0.78__tar.gz → 0.78.7__tar.gz
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.
- hyper-bot-0.78.7/Hyper/Adapters/Kritor.py +114 -0
- hyper-bot-0.78.7/Hyper/Adapters/LagrangePy.py +113 -0
- {hyper-bot-0.78 → hyper-bot-0.78.7}/Hyper/Adapters/OneBot.py +45 -38
- {hyper-bot-0.78 → hyper-bot-0.78.7}/Hyper/Adapters/OneBotLib/Manager.py +33 -20
- hyper-bot-0.78.7/Hyper/Adapters/OneBotLib/Res.py +101 -0
- {hyper-bot-0.78 → hyper-bot-0.78.7}/Hyper/Adapters/Satori.py +3 -1
- {hyper-bot-0.78 → hyper-bot-0.78.7}/Hyper/Configurator.py +8 -0
- {hyper-bot-0.78 → hyper-bot-0.78.7}/Hyper/Events.py +46 -7
- hyper-bot-0.78.7/Hyper/Listener.py +23 -0
- {hyper-bot-0.78 → hyper-bot-0.78.7}/Hyper/Logger.py +19 -0
- {hyper-bot-0.78 → hyper-bot-0.78.7}/Hyper/Manager.py +6 -0
- {hyper-bot-0.78 → hyper-bot-0.78.7}/Hyper/Network.py +42 -32
- {hyper-bot-0.78 → hyper-bot-0.78.7}/Hyper/Segments.py +40 -41
- hyper-bot-0.78.7/Hyper/Utils/APIRsp.py +201 -0
- hyper-bot-0.78.7/Hyper/Utils/ArkSignHelper.py +175 -0
- {hyper-bot-0.78 → hyper-bot-0.78.7}/Hyper/Utils/Logic.py +54 -2
- hyper-bot-0.78.7/Hyper/Utils/Screens.py +72 -0
- hyper-bot-0.78.7/Hyper/__init__.py +80 -0
- hyper-bot-0.78.7/LICENSE +674 -0
- {hyper-bot-0.78 → hyper-bot-0.78.7}/PKG-INFO +1 -1
- {hyper-bot-0.78 → hyper-bot-0.78.7}/hyper_bot.egg-info/PKG-INFO +1 -1
- {hyper-bot-0.78 → hyper-bot-0.78.7}/hyper_bot.egg-info/SOURCES.txt +5 -0
- hyper-bot-0.78/Hyper/Adapters/OneBotLib/Res.py +0 -111
- hyper-bot-0.78/Hyper/Listener.py +0 -18
- hyper-bot-0.78/Hyper/__init__.py +0 -1
- hyper-bot-0.78/LICENSE +0 -547
- {hyper-bot-0.78 → hyper-bot-0.78.7}/Hyper/Utils/Errors.py +0 -0
- {hyper-bot-0.78 → hyper-bot-0.78.7}/Hyper/Utils/TypeExt.py +0 -0
- {hyper-bot-0.78 → hyper-bot-0.78.7}/hyper_bot.egg-info/dependency_links.txt +0 -0
- {hyper-bot-0.78 → hyper-bot-0.78.7}/hyper_bot.egg-info/top_level.txt +0 -0
- {hyper-bot-0.78 → hyper-bot-0.78.7}/setup.cfg +0 -0
- {hyper-bot-0.78 → hyper-bot-0.78.7}/setup.py +0 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
from typing import Union
|
|
3
|
+
import os
|
|
4
|
+
import threading
|
|
5
|
+
import time
|
|
6
|
+
|
|
7
|
+
from Hyper.Adapters.OneBot import Actions as OneBotActions
|
|
8
|
+
from Hyper.Events import Event, HyperNotify, HyperListenerStartNotify
|
|
9
|
+
from Hyper.Network import KritorConnection
|
|
10
|
+
from Hyper.Utils import Errors
|
|
11
|
+
from Hyper import Configurator, Logger
|
|
12
|
+
|
|
13
|
+
config = Configurator.cm.get_cfg()
|
|
14
|
+
logger = Logger.Logger()
|
|
15
|
+
logger.set_level(config.log_level)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class Actions(OneBotActions):
|
|
19
|
+
def __init__(self, cnt: KritorConnection):
|
|
20
|
+
self.connection = cnt
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
async def tester(message_data: Union[Event, HyperNotify], actions: Actions) -> None:
|
|
24
|
+
...
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def __handler(data: Union[dict, HyperNotify], actions: Actions) -> None:
|
|
28
|
+
print(data)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
handler: callable = tester
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def reg(func: callable):
|
|
35
|
+
global handler
|
|
36
|
+
handler = func
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
connection: KritorConnection
|
|
40
|
+
listener_ran = False
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def run():
|
|
44
|
+
async def runner():
|
|
45
|
+
global connection, listener_ran
|
|
46
|
+
try:
|
|
47
|
+
if handler is tester:
|
|
48
|
+
raise Errors.ListenerNotRegisteredError("No handler registered")
|
|
49
|
+
# connection = websocket.WebSocket()
|
|
50
|
+
# if isinstance(config.connection, Configurator.WSConnectionC):
|
|
51
|
+
# connection = Network.WebsocketConnection(f"ws://{config.connection.host}:{config.connection.port}")
|
|
52
|
+
# elif isinstance(config.connection, Configurator.HTTPConnectionC):
|
|
53
|
+
# connection = Network.HTTPConnection(
|
|
54
|
+
# url=f"http://{config.connection.host}:{config.connection.port}",
|
|
55
|
+
# listener_url=f"http://{config.connection.listener_host}:{config.connection.listener_port}"
|
|
56
|
+
# )
|
|
57
|
+
connection = KritorConnection(
|
|
58
|
+
host=config.connection.host,
|
|
59
|
+
port=config.connection.port,
|
|
60
|
+
)
|
|
61
|
+
retried = 0
|
|
62
|
+
while True:
|
|
63
|
+
try:
|
|
64
|
+
connection.connect()
|
|
65
|
+
except ConnectionRefusedError or TimeoutError:
|
|
66
|
+
if retried >= config.connection.retries:
|
|
67
|
+
logger.log(f"重试次数达到最大值({config.connection.retries}),退出",
|
|
68
|
+
level=Logger.levels.CRITICAL)
|
|
69
|
+
break
|
|
70
|
+
|
|
71
|
+
logger.log(f"连接建立失败,3秒后重试({retried}/{config.connection.retries})",
|
|
72
|
+
level=Logger.levels.WARNING)
|
|
73
|
+
retried += 1
|
|
74
|
+
time.sleep(3)
|
|
75
|
+
continue
|
|
76
|
+
logger.log("成功建立连接", level=Logger.levels.INFO)
|
|
77
|
+
retried = 0
|
|
78
|
+
actions = Actions(connection)
|
|
79
|
+
data = HyperListenerStartNotify(
|
|
80
|
+
time_now=int(time.time()),
|
|
81
|
+
notify_type="listener_start",
|
|
82
|
+
connection=connection
|
|
83
|
+
)
|
|
84
|
+
threading.Thread(target=lambda: __handler(data, actions)).start()
|
|
85
|
+
task = []
|
|
86
|
+
while True:
|
|
87
|
+
try:
|
|
88
|
+
if not task:
|
|
89
|
+
task.append(asyncio.create_task(connection.recv()))
|
|
90
|
+
else:
|
|
91
|
+
await asyncio.sleep(1)
|
|
92
|
+
except ConnectionResetError:
|
|
93
|
+
logger.log("连接断开", level=Logger.levels.ERROR)
|
|
94
|
+
break
|
|
95
|
+
# threading.Thread(target=lambda: asyncio.run(__handler(data, actions))).start()
|
|
96
|
+
# threading.Thread(target=lambda: __handler(data, actions)).start()
|
|
97
|
+
# asyncio.create_task(__handler(data, actions))
|
|
98
|
+
except KeyboardInterrupt:
|
|
99
|
+
logger.log("正在退出(Ctrl+C)", level=Logger.levels.WARNING)
|
|
100
|
+
try:
|
|
101
|
+
connection.close()
|
|
102
|
+
except:
|
|
103
|
+
pass
|
|
104
|
+
os._exit(0)
|
|
105
|
+
|
|
106
|
+
asyncio.get_event_loop().run_until_complete(runner())
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def stop() -> None:
|
|
110
|
+
try:
|
|
111
|
+
connection.close()
|
|
112
|
+
except:
|
|
113
|
+
pass
|
|
114
|
+
logger.log("停止运行监听器", level=Logger.levels.WARNING)
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
from Hyper.Adapters.LagrangeLib.Res import get_msg_id, uc, get_msg_info
|
|
2
|
+
from Hyper.Adapters.OneBot import *
|
|
3
|
+
from Hyper.Adapters.LagrangeLib.LagrangeClient import lgr, event_queue
|
|
4
|
+
from Hyper.Utils.TypeExt import ObjectedJson
|
|
5
|
+
import Hyper
|
|
6
|
+
|
|
7
|
+
from lagrange.client.client import Client as LgrCli
|
|
8
|
+
from lagrange.client.events.service import ClientOnline
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Actions(Actions):
|
|
12
|
+
def __init__(self, cli: LgrCli):
|
|
13
|
+
self.client = cli
|
|
14
|
+
|
|
15
|
+
@Logger.AutoLogAsync.register(Logger.AutoLog.templates().send, logger)
|
|
16
|
+
async def send(self, message: Manager.Message, group_id: int = None, user_id: int = None) -> Manager.Ret:
|
|
17
|
+
chain = await message.get(group_id, user_id)
|
|
18
|
+
try:
|
|
19
|
+
if group_id:
|
|
20
|
+
seq = await self.client.send_grp_msg(chain, group_id)
|
|
21
|
+
elif user_id:
|
|
22
|
+
seq = await self.client.send_friend_msg(chain, uc.to_uid(user_id))
|
|
23
|
+
else:
|
|
24
|
+
raise Exception()
|
|
25
|
+
msg_id = get_msg_id(seq, self.client.uin)
|
|
26
|
+
data = {"status": "ok", "retcode": 0, "data": {"message_id": msg_id}}
|
|
27
|
+
except Exception as e:
|
|
28
|
+
data = {"status": "failed", "retcode": 1400, "data": None}
|
|
29
|
+
|
|
30
|
+
return Manager.Ret(data, ObjectedJson)
|
|
31
|
+
|
|
32
|
+
async def get_version_info(self) -> Manager.Ret:
|
|
33
|
+
return Manager.Ret(
|
|
34
|
+
{
|
|
35
|
+
"status": "ok",
|
|
36
|
+
"retcode": 0,
|
|
37
|
+
"data": {
|
|
38
|
+
"app_name": "LagrangePython with HypeR",
|
|
39
|
+
"app_version": Hyper.HYPER_BOT_VERSION,
|
|
40
|
+
"protocol_version": "None"
|
|
41
|
+
}
|
|
42
|
+
}, ObjectedJson
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
@Logger.AutoLogAsync.register(Logger.AutoLog.templates().recall, logger)
|
|
46
|
+
async def del_message(self, message_id: int) -> None:
|
|
47
|
+
seq, uin, gid = get_msg_info(message_id)
|
|
48
|
+
if gid != 0:
|
|
49
|
+
await self.client.recall_grp_msg(gid, seq)
|
|
50
|
+
else:
|
|
51
|
+
pass
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def __handler(data: dict, actions: Actions) -> None:
|
|
55
|
+
if isinstance(data, dict):
|
|
56
|
+
if data.get("echo") is not None:
|
|
57
|
+
reports.put(data.get("echo"), data)
|
|
58
|
+
elif data.get("post_type") == "meta_event" or data.get("user_id") == data.get("self_id"):
|
|
59
|
+
pass
|
|
60
|
+
else:
|
|
61
|
+
asyncio.run(handler(Events.em.new(data), actions))
|
|
62
|
+
else:
|
|
63
|
+
asyncio.run(handler(data, actions))
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def reg(func: callable):
|
|
67
|
+
global handler
|
|
68
|
+
handler = func
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
listener_ran = False
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def run():
|
|
75
|
+
def start_listener() -> None:
|
|
76
|
+
global listener_ran
|
|
77
|
+
listener_ran = True
|
|
78
|
+
try:
|
|
79
|
+
if handler is tester:
|
|
80
|
+
raise Errors.ListenerNotRegisteredError("No handler registered")
|
|
81
|
+
while True:
|
|
82
|
+
logger.log("Lagrange 已在运行", level=Logger.levels.INFO)
|
|
83
|
+
retried = 0
|
|
84
|
+
actions = Actions(lgr.client)
|
|
85
|
+
data = HyperListenerStartNotify(
|
|
86
|
+
time_now=int(time.time()),
|
|
87
|
+
notify_type="listener_start_lgr_py"
|
|
88
|
+
)
|
|
89
|
+
threading.Thread(target=lambda: __handler(data, actions)).start()
|
|
90
|
+
while True:
|
|
91
|
+
data = event_queue.get()
|
|
92
|
+
threading.Thread(target=lambda: __handler(data, actions)).start()
|
|
93
|
+
except KeyboardInterrupt:
|
|
94
|
+
logger.log("正在退出(Ctrl+C)", level=Logger.levels.WARNING)
|
|
95
|
+
try:
|
|
96
|
+
connection.close()
|
|
97
|
+
except:
|
|
98
|
+
pass
|
|
99
|
+
os._exit(0)
|
|
100
|
+
|
|
101
|
+
async def run_listener(_1, _2) -> None:
|
|
102
|
+
threading.Thread(target=start_listener).start()
|
|
103
|
+
|
|
104
|
+
lgr.subscribe(ClientOnline, run_listener)
|
|
105
|
+
lgr.launch()
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def stop() -> None:
|
|
109
|
+
try:
|
|
110
|
+
connection.close()
|
|
111
|
+
except:
|
|
112
|
+
pass
|
|
113
|
+
logger.log("停止运行监听器", level=Logger.levels.WARNING)
|
|
@@ -3,16 +3,17 @@ import threading
|
|
|
3
3
|
import time
|
|
4
4
|
import asyncio
|
|
5
5
|
import os
|
|
6
|
-
from typing import Union
|
|
7
6
|
|
|
8
7
|
from Hyper import Network, Events
|
|
9
8
|
from Hyper.Utils import Errors, Logic
|
|
9
|
+
from Hyper.Utils.APIRsp import *
|
|
10
10
|
from Hyper.Manager import reports
|
|
11
11
|
from Hyper.Events import *
|
|
12
12
|
|
|
13
13
|
config = Configurator.cm.get_cfg()
|
|
14
14
|
logger = Logger.Logger()
|
|
15
15
|
logger.set_level(config.log_level)
|
|
16
|
+
listener_ran = False
|
|
16
17
|
|
|
17
18
|
|
|
18
19
|
class Actions:
|
|
@@ -37,7 +38,9 @@ class Actions:
|
|
|
37
38
|
self.custom = CustomAction(self.connection)
|
|
38
39
|
|
|
39
40
|
@Logger.AutoLogAsync.register(Logger.AutoLog.templates().send, logger)
|
|
40
|
-
async def send(
|
|
41
|
+
async def send(
|
|
42
|
+
self, message: Manager.Message, group_id: int = None, user_id: int = None
|
|
43
|
+
) -> Manager.Ret[MsgSendRsp]:
|
|
41
44
|
if group_id is not None:
|
|
42
45
|
packet = Manager.Packet(
|
|
43
46
|
"send_msg",
|
|
@@ -53,7 +56,7 @@ class Actions:
|
|
|
53
56
|
else:
|
|
54
57
|
raise Errors.ArgsInvalidError("'send' API requires 'group_id' or 'user_id' but none of them are provided.")
|
|
55
58
|
packet.send_to(self.connection)
|
|
56
|
-
return Manager.Ret.fetch(packet.echo)
|
|
59
|
+
return Manager.Ret.fetch(packet.echo, MsgSendRsp)
|
|
57
60
|
|
|
58
61
|
@Logger.AutoLogAsync.register(Logger.AutoLog.templates().recall, logger)
|
|
59
62
|
async def del_message(self, message_id: int) -> None:
|
|
@@ -80,33 +83,33 @@ class Actions:
|
|
|
80
83
|
).send_to(self.connection)
|
|
81
84
|
|
|
82
85
|
@Logic.Cacher().cache_async
|
|
83
|
-
async def get_login_info(self) -> Manager.Ret:
|
|
86
|
+
async def get_login_info(self) -> Manager.Ret[GetLoginInfoRsp]:
|
|
84
87
|
packet = Manager.Packet("get_login_info")
|
|
85
88
|
packet.send_to(self.connection)
|
|
86
|
-
return Manager.Ret.fetch(packet.echo)
|
|
89
|
+
return Manager.Ret.fetch(packet.echo, GetLoginInfoRsp)
|
|
87
90
|
|
|
88
91
|
@Logic.Cacher().cache_async
|
|
89
|
-
async def get_version_info(self) -> Manager.Ret:
|
|
92
|
+
async def get_version_info(self) -> Manager.Ret[GetVerInfoRsp]:
|
|
90
93
|
packet = Manager.Packet("get_version_info")
|
|
91
94
|
packet.send_to(self.connection)
|
|
92
|
-
return Manager.Ret.fetch(packet.echo)
|
|
95
|
+
return Manager.Ret.fetch(packet.echo, GetVerInfoRsp)
|
|
93
96
|
|
|
94
|
-
async def send_forward_msg(self, message: Manager.Message) -> Manager.Ret:
|
|
97
|
+
async def send_forward_msg(self, message: Manager.Message) -> Manager.Ret[SendForwardRsp]:
|
|
95
98
|
packet = Manager.Packet(
|
|
96
99
|
"send_forward_msg",
|
|
97
100
|
messages=await message.get()
|
|
98
101
|
)
|
|
99
102
|
packet.send_to(self.connection)
|
|
100
|
-
return Manager.Ret.fetch(packet.echo)
|
|
103
|
+
return Manager.Ret.fetch(packet.echo, SendForwardRsp)
|
|
101
104
|
|
|
102
|
-
async def send_group_forward_msg(self, group_id: int, message: Manager.Message) -> Manager.Ret:
|
|
105
|
+
async def send_group_forward_msg(self, group_id: int, message: Manager.Message) -> Manager.Ret[SendGrpForwardRsp]:
|
|
103
106
|
packet = Manager.Packet(
|
|
104
107
|
"send_group_forward_msg",
|
|
105
108
|
group_id=group_id,
|
|
106
109
|
messages=await message.get()
|
|
107
110
|
)
|
|
108
111
|
packet.send_to(self.connection)
|
|
109
|
-
return Manager.Ret.fetch(packet.echo)
|
|
112
|
+
return Manager.Ret.fetch(packet.echo, SendForwardRsp)
|
|
110
113
|
|
|
111
114
|
@Logger.AutoLogAsync.register(Logger.AutoLog.templates().set_req, logger)
|
|
112
115
|
async def set_group_add_request(self, flag: str, sub_type: str, approve: bool, reason: str = "Refused") -> None:
|
|
@@ -119,17 +122,17 @@ class Actions:
|
|
|
119
122
|
).send_to(self.connection)
|
|
120
123
|
|
|
121
124
|
@Logic.Cacher().cache_async
|
|
122
|
-
async def get_stranger_info(self, user_id: int) -> Manager.Ret:
|
|
125
|
+
async def get_stranger_info(self, user_id: int) -> Manager.Ret[GetStrInfoRsp]:
|
|
123
126
|
packet = Manager.Packet(
|
|
124
127
|
"get_stranger_info",
|
|
125
128
|
user_id=user_id,
|
|
126
129
|
no_cache=True,
|
|
127
130
|
)
|
|
128
131
|
packet.send_to(self.connection)
|
|
129
|
-
return Manager.Ret.fetch(packet.echo)
|
|
132
|
+
return Manager.Ret.fetch(packet.echo, GetStrInfoRsp)
|
|
130
133
|
|
|
131
134
|
@Logic.Cacher().cache_async
|
|
132
|
-
async def get_group_member_info(self, group_id: int, user_id: int) -> Manager.Ret:
|
|
135
|
+
async def get_group_member_info(self, group_id: int, user_id: int) -> Manager.Ret[GetGrpMemInfoRsp]:
|
|
133
136
|
packet = Manager.Packet(
|
|
134
137
|
"get_group_member_info",
|
|
135
138
|
group_id=group_id,
|
|
@@ -137,17 +140,17 @@ class Actions:
|
|
|
137
140
|
no_cache=True
|
|
138
141
|
)
|
|
139
142
|
packet.send_to(self.connection)
|
|
140
|
-
return Manager.Ret.fetch(packet.echo)
|
|
143
|
+
return Manager.Ret.fetch(packet.echo, GetGrpMemInfoRsp)
|
|
141
144
|
|
|
142
145
|
@Logic.Cacher().cache_async
|
|
143
|
-
async def get_group_info(self, group_id: int) -> Manager.Ret:
|
|
146
|
+
async def get_group_info(self, group_id: int) -> Manager.Ret[GetGrpInfoRsp]:
|
|
144
147
|
packet = Manager.Packet(
|
|
145
148
|
"get_group_info",
|
|
146
149
|
group_id=group_id,
|
|
147
150
|
no_cache=True
|
|
148
151
|
)
|
|
149
152
|
packet.send_to(self.connection)
|
|
150
|
-
return Manager.Ret.fetch(packet.echo)
|
|
153
|
+
return Manager.Ret.fetch(packet.echo, GetGrpInfoRsp)
|
|
151
154
|
|
|
152
155
|
async def get_status(self) -> Manager.Ret:
|
|
153
156
|
packet = Manager.Packet("get_status")
|
|
@@ -169,36 +172,29 @@ class Actions:
|
|
|
169
172
|
special_title=title,
|
|
170
173
|
).send_to(self.connection)
|
|
171
174
|
|
|
172
|
-
async def get_msg(self, msg_id: int) -> Manager.Ret:
|
|
175
|
+
async def get_msg(self, msg_id: int) -> Manager.Ret[GetMsgRsp]:
|
|
173
176
|
packet = Manager.Packet(
|
|
174
177
|
"get_msg",
|
|
175
178
|
message_id=msg_id
|
|
176
179
|
)
|
|
177
180
|
packet.send_to(self.connection)
|
|
178
|
-
return Manager.Ret.fetch(packet.echo)
|
|
181
|
+
return Manager.Ret.fetch(packet.echo, GetMsgRsp)
|
|
179
182
|
|
|
180
183
|
|
|
181
|
-
async def tester(message_data: Event, actions: Actions) -> None:
|
|
184
|
+
async def tester(message_data: Union[Event, HyperNotify], actions: Actions) -> None:
|
|
182
185
|
...
|
|
183
186
|
|
|
184
187
|
|
|
185
|
-
def __handler(data: dict, actions: Actions) -> None:
|
|
186
|
-
if data
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
188
|
+
def __handler(data: Union[dict, HyperNotify], actions: Actions) -> None:
|
|
189
|
+
if isinstance(data, dict):
|
|
190
|
+
if data.get("echo") is not None:
|
|
191
|
+
reports.put(data.get("echo"), data)
|
|
192
|
+
elif data.get("post_type") == "meta_event" or data.get("user_id") == data.get("self_id"):
|
|
193
|
+
pass
|
|
194
|
+
else:
|
|
195
|
+
asyncio.run(handler(Events.em.new(data), actions))
|
|
190
196
|
else:
|
|
191
|
-
|
|
192
|
-
asyncio.run(handler(Events.em.new(data), actions))
|
|
193
|
-
# timed = 0
|
|
194
|
-
#
|
|
195
|
-
# while not task.done():
|
|
196
|
-
# time.sleep(0.1)
|
|
197
|
-
# timed += 0.1
|
|
198
|
-
# if timed >= 30:
|
|
199
|
-
# task.cancel()
|
|
200
|
-
# logger.log(f"处理{task.get_name()}超时", level=Logger.levels.ERROR)
|
|
201
|
-
# break
|
|
197
|
+
asyncio.run(handler(data, actions))
|
|
202
198
|
|
|
203
199
|
|
|
204
200
|
handler: callable = tester
|
|
@@ -213,7 +209,8 @@ connection: Union[Network.WebsocketConnection, Network.HTTPConnection]
|
|
|
213
209
|
|
|
214
210
|
|
|
215
211
|
def run():
|
|
216
|
-
global connection
|
|
212
|
+
global connection, listener_ran
|
|
213
|
+
listener_ran = True
|
|
217
214
|
try:
|
|
218
215
|
if handler is tester:
|
|
219
216
|
raise Errors.ListenerNotRegisteredError("No handler registered")
|
|
@@ -239,9 +236,18 @@ def run():
|
|
|
239
236
|
retried += 1
|
|
240
237
|
time.sleep(3)
|
|
241
238
|
continue
|
|
242
|
-
logger.log("成功建立连接", level=Logger.levels.INFO)
|
|
243
239
|
retried = 0
|
|
240
|
+
logger.log(
|
|
241
|
+
f"成功在 {connection.url} 建立连接",
|
|
242
|
+
level=Logger.levels.INFO
|
|
243
|
+
)
|
|
244
244
|
actions = Actions(connection)
|
|
245
|
+
data = HyperListenerStartNotify(
|
|
246
|
+
time_now=int(time.time()),
|
|
247
|
+
notify_type="listener_start",
|
|
248
|
+
connection=connection
|
|
249
|
+
)
|
|
250
|
+
threading.Thread(target=lambda: __handler(data, actions)).start()
|
|
245
251
|
while True:
|
|
246
252
|
try:
|
|
247
253
|
data = connection.recv()
|
|
@@ -253,6 +259,7 @@ def run():
|
|
|
253
259
|
continue
|
|
254
260
|
# threading.Thread(target=lambda: asyncio.run(__handler(data, actions))).start()
|
|
255
261
|
threading.Thread(target=lambda: __handler(data, actions)).start()
|
|
262
|
+
# asyncio.create_task(__handler(data, actions))
|
|
256
263
|
except KeyboardInterrupt:
|
|
257
264
|
logger.log("正在退出(Ctrl+C)", level=Logger.levels.WARNING)
|
|
258
265
|
try:
|
|
@@ -1,16 +1,24 @@
|
|
|
1
|
-
import Hyper.Utils.TypeExt
|
|
2
1
|
from Hyper import Configurator, Logger, Network, Segments
|
|
3
2
|
from Hyper.Utils import Logic
|
|
3
|
+
from Hyper.Utils.TypeExt import ObjectedJson
|
|
4
4
|
|
|
5
|
-
from typing import Union
|
|
6
|
-
import queue
|
|
5
|
+
from typing import Union, Generic, TypeVar
|
|
7
6
|
import random
|
|
8
7
|
import json
|
|
9
8
|
|
|
10
|
-
reports = queue.Queue()
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
# reports = queue.Queue()
|
|
10
|
+
reports = Logic.KeyQueue()
|
|
11
|
+
|
|
12
|
+
config: Configurator.Config
|
|
13
|
+
logger: Logger.Logger
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def init() -> None:
|
|
17
|
+
global config, logger
|
|
18
|
+
config = Configurator.cm.get_cfg()
|
|
19
|
+
logger = Logger.Logger()
|
|
20
|
+
logger.set_level(config.log_level)
|
|
21
|
+
|
|
14
22
|
|
|
15
23
|
servicing = []
|
|
16
24
|
|
|
@@ -111,21 +119,26 @@ class Message:
|
|
|
111
119
|
return self
|
|
112
120
|
|
|
113
121
|
|
|
114
|
-
|
|
115
|
-
|
|
122
|
+
T = TypeVar("T")
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
class Ret(Generic[T]):
|
|
126
|
+
def __init__(self, json_data: dict, serializer):
|
|
127
|
+
self.raw = json_data.copy()
|
|
116
128
|
self.status = json_data["status"]
|
|
117
129
|
self.ret_code = json_data["retcode"]
|
|
118
|
-
self.data =
|
|
130
|
+
self.data: T = serializer(json_data.get("data"))
|
|
119
131
|
self.echo = json_data.get("echo")
|
|
120
132
|
|
|
121
133
|
@classmethod
|
|
122
|
-
def fetch(cls, echo: str) -> "Ret":
|
|
123
|
-
old = None
|
|
124
|
-
while True:
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
134
|
+
def fetch(cls, echo: str, serializer=ObjectedJson) -> "Ret":
|
|
135
|
+
# old = None
|
|
136
|
+
# while True:
|
|
137
|
+
# content = reports.get()
|
|
138
|
+
# if old is not None:
|
|
139
|
+
# reports.put(old)
|
|
140
|
+
# if content["echo"] == echo:
|
|
141
|
+
# return cls(content)
|
|
142
|
+
# else:
|
|
143
|
+
# old = content
|
|
144
|
+
return cls(reports.get(echo), serializer)
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
from abc import ABC
|
|
2
|
+
|
|
3
|
+
message_types = {}
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class SegmentBase(ABC):
|
|
7
|
+
def __init__(self, *args, **kwargs):
|
|
8
|
+
var = self.__var
|
|
9
|
+
anns = self.__anns
|
|
10
|
+
arg = {}
|
|
11
|
+
if len(args) > 0:
|
|
12
|
+
for i in args:
|
|
13
|
+
arg[list(anns.keys())[list(args).index(i)]] = i
|
|
14
|
+
|
|
15
|
+
if len(kwargs) > 0:
|
|
16
|
+
for i in kwargs:
|
|
17
|
+
try:
|
|
18
|
+
arg[i] = anns[i](kwargs[i])
|
|
19
|
+
except TypeError:
|
|
20
|
+
arg[i] = kwargs[i]
|
|
21
|
+
new_arg = arg.copy()
|
|
22
|
+
|
|
23
|
+
if len(anns) > len(arg):
|
|
24
|
+
for i in anns.keys():
|
|
25
|
+
if i not in arg.keys():
|
|
26
|
+
if i not in var.keys():
|
|
27
|
+
new_arg[i] = None
|
|
28
|
+
continue
|
|
29
|
+
if not isinstance(var[i], anns[i]):
|
|
30
|
+
new_arg[i] = anns[i](var[i])
|
|
31
|
+
else:
|
|
32
|
+
new_arg[i] = var[i]
|
|
33
|
+
|
|
34
|
+
for i in new_arg:
|
|
35
|
+
setattr(self, i, new_arg[i])
|
|
36
|
+
|
|
37
|
+
def __init_subclass__(cls, **kwargs):
|
|
38
|
+
sg_type = kwargs.get("sg_type") or kwargs.get("st")
|
|
39
|
+
summary_tmp = kwargs.get("summary_tmp") or kwargs.get("su")
|
|
40
|
+
|
|
41
|
+
if sg_type is summary_tmp is None:
|
|
42
|
+
return
|
|
43
|
+
|
|
44
|
+
cls.__sg_type = sg_type
|
|
45
|
+
cls.__var = dict(vars(cls))
|
|
46
|
+
cls.__anns: dict = cls.__var.get("__annotations__", False) or dict()
|
|
47
|
+
|
|
48
|
+
def to_str(self) -> str:
|
|
49
|
+
text = summary_tmp
|
|
50
|
+
if text is None:
|
|
51
|
+
text = "[]"
|
|
52
|
+
if "<" not in text and ">" not in text:
|
|
53
|
+
return text
|
|
54
|
+
|
|
55
|
+
for i in cls.__anns:
|
|
56
|
+
if f"<{i}>" in summary_tmp:
|
|
57
|
+
try:
|
|
58
|
+
v = self.__getattribute__(i)
|
|
59
|
+
except AttributeError:
|
|
60
|
+
v = None
|
|
61
|
+
text = text.replace(f"<{i}>", str(v))
|
|
62
|
+
|
|
63
|
+
return text
|
|
64
|
+
|
|
65
|
+
cls.__str__ = to_str if cls().__str__() == "__not_set__" else cls.__str__
|
|
66
|
+
|
|
67
|
+
message_types[sg_type] = {
|
|
68
|
+
"type": cls,
|
|
69
|
+
"args": list(cls.__anns.keys())
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return cls
|
|
73
|
+
|
|
74
|
+
def to_json(self) -> dict:
|
|
75
|
+
base = {"type": self.__sg_type, "data": {}}
|
|
76
|
+
for i in self.__anns:
|
|
77
|
+
if i.startswith("__") or getattr(self, i) is None:
|
|
78
|
+
continue
|
|
79
|
+
if not isinstance(getattr(self, i), self.__anns[i]):
|
|
80
|
+
base["data"][i] = self.__anns[i](getattr(self, i))
|
|
81
|
+
else:
|
|
82
|
+
base["data"][i] = getattr(self, i)
|
|
83
|
+
# try:
|
|
84
|
+
# base["data"][i] = anns[i](getattr(self, i))
|
|
85
|
+
# except TypeError:
|
|
86
|
+
# base["data"][i] = getattr(self, i)
|
|
87
|
+
return base
|
|
88
|
+
|
|
89
|
+
def __str__(self) -> str: return "__not_set__"
|
|
90
|
+
|
|
91
|
+
def __eq__(self, other) -> bool:
|
|
92
|
+
if type(self) is type(other) and self.to_json() == other.to_json():
|
|
93
|
+
return True
|
|
94
|
+
else:
|
|
95
|
+
return False
|
|
96
|
+
|
|
97
|
+
def __ne__(self, other) -> bool:
|
|
98
|
+
if type(self) is type(other) and self.to_json() == other.to_json():
|
|
99
|
+
return False
|
|
100
|
+
else:
|
|
101
|
+
return True
|
|
@@ -47,11 +47,13 @@ def reg(func: callable):
|
|
|
47
47
|
handler = func
|
|
48
48
|
|
|
49
49
|
|
|
50
|
+
listener_ran = False
|
|
50
51
|
connection: Union[Network.WebsocketConnection, Network.SatoriConnection]
|
|
51
52
|
|
|
52
53
|
|
|
53
54
|
def run():
|
|
54
|
-
global connection
|
|
55
|
+
global connection, listener_ran
|
|
56
|
+
listener_ran = True
|
|
55
57
|
try:
|
|
56
58
|
if handler is tester:
|
|
57
59
|
raise Errors.ListenerNotRegisteredError("No handler registered")
|
|
@@ -48,6 +48,7 @@ class Config:
|
|
|
48
48
|
silents: list[int] = None,
|
|
49
49
|
connection: typing.Union[WSConnectionC, HTTPConnectionC] = None,
|
|
50
50
|
log_level: str = "INFO",
|
|
51
|
+
uin: int = 0,
|
|
51
52
|
others: dict = None
|
|
52
53
|
):
|
|
53
54
|
self.inited = False
|
|
@@ -60,6 +61,7 @@ class Config:
|
|
|
60
61
|
self.silents: list[int] = silents or list
|
|
61
62
|
self.connection = connection
|
|
62
63
|
self.log_level = log_level
|
|
64
|
+
self.uin = uin
|
|
63
65
|
self.others = others or dict()
|
|
64
66
|
self.inited = True
|
|
65
67
|
|
|
@@ -86,6 +88,7 @@ class Config:
|
|
|
86
88
|
config_json["Connection"]["retries"]
|
|
87
89
|
)
|
|
88
90
|
self.log_level = config_json["Log_level"]
|
|
91
|
+
self.uin = config_json["uin"]
|
|
89
92
|
self.others = config_json["Others"]
|
|
90
93
|
|
|
91
94
|
self.inited = True
|
|
@@ -116,3 +119,8 @@ class ConfigManager:
|
|
|
116
119
|
|
|
117
120
|
|
|
118
121
|
cm: ConfigManager
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def init(cfg: Config):
|
|
125
|
+
global cm
|
|
126
|
+
cm = ConfigManager(cfg)
|