hyper-bot 0.7__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.
@@ -0,0 +1,278 @@
1
+ import json
2
+ import queue
3
+ import threading
4
+ import time
5
+ import asyncio
6
+ import os
7
+ from typing import Union
8
+
9
+ from Hyper import Configurator, Errors, Logger, Logic, Manager, Network, Events
10
+ from Hyper.Events import *
11
+
12
+ reports = queue.Queue()
13
+ config = Configurator.cm.get_cfg()
14
+ logger = Logger.Logger()
15
+ logger.set_level(config.log_level)
16
+
17
+
18
+ class Actions:
19
+ def __init__(self, cnt: Union[Network.WebsocketConnection, Network.HTTPConnection]):
20
+ self.connection = cnt
21
+
22
+ class CustomAction:
23
+ def __init__(self, cnt_i: Union[Network.WebsocketConnection, Network.HTTPConnection]):
24
+ self.connection = cnt_i
25
+
26
+ def __getattr__(self, item) -> callable:
27
+ def wrapper(no_return: bool, **kwargs) -> Manager.Ret | None:
28
+ if no_return:
29
+ Manager.Packet(
30
+ str(item),
31
+ **kwargs
32
+ ).send_to(self.connection)
33
+ return None
34
+ else:
35
+ packet = Manager.Packet(
36
+ str(item),
37
+ **kwargs
38
+ )
39
+ packet.send_to(self.connection)
40
+ return get_ret(packet.echo)
41
+
42
+ return wrapper
43
+
44
+ self.custom = CustomAction(self.connection)
45
+
46
+ @Logger.AutoLogAsync.register(Logger.AutoLog.templates().send, logger)
47
+ async def send(self, message: Manager.Message, group_id: int = None, user_id: int = None) -> Manager.Ret:
48
+ if group_id is not None:
49
+ packet = Manager.Packet(
50
+ "send_msg",
51
+ group_id=group_id,
52
+ message=await message.get()
53
+ )
54
+ elif user_id is not None:
55
+ packet = Manager.Packet(
56
+ "send_msg",
57
+ user_id=user_id,
58
+ message=await message.get()
59
+ )
60
+ else:
61
+ raise Errors.ArgsInvalidError("'send' API requires 'group_id' or 'user_id' but none of them are provided.")
62
+ packet.send_to(self.connection)
63
+ return get_ret(packet.echo)
64
+
65
+ @Logger.AutoLogAsync.register(Logger.AutoLog.templates().recall, logger)
66
+ async def del_message(self, message_id: int) -> None:
67
+ Manager.Packet(
68
+ "delete_msg",
69
+ message_id=message_id,
70
+ ).send_to(self.connection)
71
+
72
+ @Logger.AutoLogAsync.register(Logger.AutoLog.templates().kick, logger)
73
+ async def set_group_kick(self, group_id: int, user_id: int) -> None:
74
+ Manager.Packet(
75
+ "set_group_kick",
76
+ group_id=group_id,
77
+ user_id=user_id,
78
+ ).send_to(self.connection)
79
+
80
+ @Logger.AutoLogAsync.register(Logger.AutoLog.templates().mute, logger)
81
+ async def set_group_ban(self, group_id: int, user_id: int, duration: int = 60) -> None:
82
+ Manager.Packet(
83
+ "set_group_ban",
84
+ group_id=group_id,
85
+ user_id=user_id,
86
+ duration=duration,
87
+ ).send_to(self.connection)
88
+
89
+ @Logic.Cacher().cache_async
90
+ async def get_login_info(self) -> Manager.Ret:
91
+ packet = Manager.Packet("get_login_info")
92
+ packet.send_to(self.connection)
93
+ return get_ret(packet.echo)
94
+
95
+ @Logic.Cacher().cache_async
96
+ async def get_version_info(self) -> Manager.Ret:
97
+ packet = Manager.Packet("get_version_info")
98
+ packet.send_to(self.connection)
99
+ return get_ret(packet.echo)
100
+
101
+ async def send_forward_msg(self, message: Manager.Message) -> Manager.Ret:
102
+ packet = Manager.Packet(
103
+ "send_forward_msg",
104
+ messages=await message.get()
105
+ )
106
+ packet.send_to(self.connection)
107
+ return get_ret(packet.echo)
108
+
109
+ async def send_group_forward_msg(self, group_id: int, message: Manager.Message) -> Manager.Ret:
110
+ packet = Manager.Packet(
111
+ "send_group_forward_msg",
112
+ group_id=group_id,
113
+ messages=await message.get()
114
+ )
115
+ packet.send_to(self.connection)
116
+ return get_ret(packet.echo)
117
+
118
+ @Logger.AutoLogAsync.register(Logger.AutoLog.templates().set_req, logger)
119
+ async def set_group_add_request(self, flag: str, sub_type: str, approve: bool, reason: str = "Refused") -> None:
120
+ Manager.Packet(
121
+ "set_group_add_request",
122
+ flag=flag,
123
+ sub_type=sub_type,
124
+ approve=approve,
125
+ reason=reason
126
+ ).send_to(self.connection)
127
+
128
+ @Logic.Cacher().cache_async
129
+ async def get_stranger_info(self, user_id: int) -> Manager.Ret:
130
+ packet = Manager.Packet(
131
+ "get_stranger_info",
132
+ user_id=user_id,
133
+ no_cache=True,
134
+ )
135
+ packet.send_to(self.connection)
136
+ return get_ret(packet.echo)
137
+
138
+ @Logic.Cacher().cache_async
139
+ async def get_group_member_info(self, group_id: int, user_id: int) -> Manager.Ret:
140
+ packet = Manager.Packet(
141
+ "get_group_member_info",
142
+ group_id=group_id,
143
+ user_id=user_id,
144
+ no_cache=True
145
+ )
146
+ packet.send_to(self.connection)
147
+ return get_ret(packet.echo)
148
+
149
+ @Logic.Cacher().cache_async
150
+ async def get_group_info(self, group_id: int) -> Manager.Ret:
151
+ packet = Manager.Packet(
152
+ "get_group_info",
153
+ group_id=group_id,
154
+ no_cache=True
155
+ )
156
+ packet.send_to(self.connection)
157
+ return get_ret(packet.echo)
158
+
159
+ async def get_status(self) -> Manager.Ret:
160
+ packet = Manager.Packet("get_status")
161
+ packet.send_to(self.connection)
162
+ return get_ret(packet.echo)
163
+
164
+ @Logger.AutoLogAsync.register(Logger.AutoLog.templates().set_ess, logger)
165
+ async def set_essence_msg(self, message_id: int) -> None:
166
+ Manager.Packet(
167
+ "set_essence_msg",
168
+ message_id=message_id
169
+ ).send_to(self.connection)
170
+
171
+ async def set_group_special_title(self, group_id: int, user_id: int, title: str) -> None:
172
+ Manager.Packet(
173
+ "set_group_special_title",
174
+ group_id=group_id,
175
+ user_id=user_id,
176
+ special_title=title,
177
+ ).send_to(self.connection)
178
+
179
+ async def get_msg(self, msg_id: int) -> Manager.Ret:
180
+ packet = Manager.Packet(
181
+ "get_msg",
182
+ message_id=msg_id
183
+ )
184
+ packet.send_to(self.connection)
185
+ return get_ret(packet.echo)
186
+
187
+
188
+ async def tester(message_data: Event, actions: Actions) -> None:
189
+ ...
190
+
191
+
192
+ async def __handler(data: dict, actions: Actions) -> None:
193
+ if data.get("echo") is not None:
194
+ reports.put(Manager.Ret(data))
195
+ elif data.get("post_type") == "meta_event" or data.get("user_id") == data.get("self_id"):
196
+ pass
197
+ else:
198
+ task = asyncio.create_task(handler(Events.em.new(data), actions))
199
+ timed = 0
200
+
201
+ while not task.done():
202
+ await asyncio.sleep(0.1)
203
+ timed += 0.1
204
+ if timed >= 30:
205
+ task.cancel()
206
+ logger.log(f"处理{task.get_name()}超时", level=Logger.levels.ERROR)
207
+ break
208
+
209
+
210
+ handler: callable = tester
211
+ connection: callable = tester
212
+
213
+
214
+ def reg(func: callable):
215
+ global handler
216
+ handler = func
217
+
218
+
219
+
220
+
221
+ def get_ret(echo: str) -> Manager.Ret:
222
+ old = None
223
+ while True:
224
+ content: Manager.Ret = reports.get()
225
+ if old is not None:
226
+ reports.put(old)
227
+ if content.echo == echo:
228
+ return content
229
+ else:
230
+ old = content
231
+
232
+
233
+ def run():
234
+ global connection
235
+ try:
236
+ if handler is tester:
237
+ raise Errors.ListenerNotRegisteredError("No handler registered")
238
+ # connection = websocket.WebSocket()
239
+ if isinstance(config.connection, Configurator.WSConnectionC):
240
+ connection = Network.WebsocketConnection(f"ws://{config.connection.host}:{config.connection.port}")
241
+ elif isinstance(config.connection, Configurator.HTTPConnectionC):
242
+ connection = Network.HTTPConnection(
243
+ url=f"http://{config.connection.host}:{config.connection.port}",
244
+ listener_url=f"http://{config.connection.listener_host}:{config.connection.listener_port}"
245
+ )
246
+ retried = 0
247
+ while True:
248
+ try:
249
+ connection.connect()
250
+ except ConnectionRefusedError or TimeoutError:
251
+ if retried >= config.connection.retries:
252
+ logger.log(f"重试次数达到最大值({config.connection.retries}),退出", level=Logger.levels.CRITICAL)
253
+ break
254
+
255
+ logger.log(f"连接建立失败,3秒后重试({retried}/{config.connection.retries})",
256
+ level=Logger.levels.WARNING)
257
+ retried += 1
258
+ time.sleep(3)
259
+ continue
260
+ logger.log("成功建立连接", level=Logger.levels.INFO)
261
+ retried = 0
262
+ actions = Actions(connection)
263
+ while True:
264
+ try:
265
+ data = connection.recv()
266
+ except ConnectionResetError:
267
+ logger.log("连接断开", level=Logger.levels.ERROR)
268
+ break
269
+ except json.decoder.JSONDecodeError:
270
+ logger.log("收到错误的JSON内容", level=Logger.levels.ERROR)
271
+ threading.Thread(target=lambda: asyncio.run(__handler(data, actions))).start()
272
+ except KeyboardInterrupt:
273
+ logger.log("正在退出(Ctrl+C)", level=Logger.levels.WARNING)
274
+ try:
275
+ connection.close()
276
+ except:
277
+ pass
278
+ os._exit(0)
@@ -0,0 +1,93 @@
1
+ from Hyper import Logic, Configurator, Logger, Network
2
+ from Hyper.Logger import levels
3
+ from Hyper.Segments import *
4
+
5
+ from typing import Union
6
+ import inspect
7
+ import random
8
+
9
+ config = Configurator.cm.get_cfg()
10
+ logger = Logger.Logger()
11
+ logger.set_level(config.log_level)
12
+
13
+ servicing = []
14
+
15
+
16
+ class Packet:
17
+ def __init__(self, endpoint: str, **kwargs):
18
+ self.endpoint = endpoint
19
+ self.paras = kwargs
20
+ self.echo = f"{endpoint}_{random.randint(1000, 9999)}"
21
+
22
+ def send_to(self, connection: Union[Network.WebsocketConnection, Network.HTTPConnection]) -> None:
23
+ if isinstance(connection, Network.WebsocketConnection):
24
+ payload = {
25
+ "action": self.endpoint,
26
+ "params": self.paras,
27
+ "echo": self.echo,
28
+ }
29
+ connection.send(json.dumps(payload))
30
+
31
+ elif isinstance(connection, Network.HTTPConnection):
32
+ payload = self.paras
33
+ connection.send(self.endpoint, payload, self.echo)
34
+
35
+
36
+ class Message:
37
+ def __init__(self, contents: list = None):
38
+ if contents is None:
39
+ contents = []
40
+ self.contents = contents
41
+
42
+ def add(self, content) -> None:
43
+ self.contents.append(content)
44
+
45
+ async def get(self) -> list:
46
+ ret = []
47
+ for i in self.contents:
48
+ ret.append(i.to_json())
49
+ return ret
50
+
51
+ def get_sync(self) -> list:
52
+ ret = []
53
+ for i in self.contents:
54
+ ret.append(i.to_json())
55
+ return ret
56
+
57
+ def __len__(self) -> int:
58
+ return len(self.contents)
59
+
60
+ def __getitem__(self, index: int):
61
+ return self.contents[index]
62
+
63
+ def __setitem__(self, index: int, content) -> None:
64
+ self.contents[index] = content
65
+
66
+ def __delitem__(self, index: int) -> None:
67
+ del self.contents[index]
68
+
69
+ def __iter__(self):
70
+ for i in self.contents:
71
+ yield i
72
+
73
+ def __str__(self) -> str:
74
+ return "".join([str(content) for content in self.contents])
75
+
76
+ def __repr__(self) -> str:
77
+ return str(self.contents)
78
+
79
+ def __add__(self, new):
80
+ self.contents += new.contents
81
+ return self
82
+
83
+ def __iadd__(self, new):
84
+ self.contents += new.contents
85
+ return self
86
+
87
+
88
+ class Ret:
89
+ def __init__(self, json_data: dict):
90
+ self.status = json_data["status"]
91
+ self.ret_code = json_data["retcode"]
92
+ self.data = json_data.get("data")
93
+ self.echo = json_data.get("echo")
Hyper/Configurator.py ADDED
@@ -0,0 +1,83 @@
1
+ import typing
2
+
3
+ from Hyper import Logic
4
+
5
+
6
+ class WSConnectionC:
7
+ def __init__(self, host: str, port: int, retries: int = 0):
8
+ self.host: str = host
9
+ self.port: int = port
10
+ self.retries: int = retries
11
+
12
+
13
+ class HTTPConnectionC:
14
+ def __init__(self, host: str, port: int, listener_host: str, listener_port: int, retries: int = 0):
15
+ self.host: str = host
16
+ self.port: int = port
17
+ self.listener_host: str = listener_host
18
+ self.listener_port: int = listener_port
19
+ self.retries: int = retries
20
+
21
+
22
+ class Config:
23
+ def __init__(
24
+ self,
25
+ file: str = None,
26
+ protocol: str = "OneBot",
27
+ owner: list[int] = None,
28
+ black_list: list[int] = None,
29
+ silents: list[int] = None,
30
+ connection: typing.Union[WSConnectionC, HTTPConnectionC] = None,
31
+ log_level: str = "INFO",
32
+ others: dict = None
33
+ ):
34
+ self.inited = False
35
+ if file is not None:
36
+ self.file = file
37
+ else:
38
+ self.protocol: str = protocol
39
+ self.owner: list[int] = owner or list()
40
+ self.black_list: list[int] = black_list or list()
41
+ self.silents: list[int] = silents or list
42
+ self.connection = connection
43
+ self.log_level = log_level
44
+ self.others = others or dict()
45
+ self.inited = True
46
+
47
+ def load_from_file(self):
48
+ config_json = Logic.FileManager.read_as_json(self.file)
49
+
50
+ self.protocol: str = config_json["protocol"]
51
+ self.owner: list[int] = config_json["owner"]
52
+ self.black_list: list[int] = config_json["black_list"]
53
+ self.silents: list[int] = config_json["silents"]
54
+ if config_json["Connection"]["mode"] == "FWS":
55
+ self.connection = WSConnectionC(
56
+ config_json["Connection"]["host"],
57
+ config_json["Connection"]["port"],
58
+ config_json["Connection"]["retries"]
59
+ )
60
+ elif config_json["Connection"]["mode"] == "HTTP":
61
+ self.connection = HTTPConnectionC(
62
+ config_json["Connection"]["host"],
63
+ config_json["Connection"]["port"],
64
+ config_json["Connection"]["listener_host"],
65
+ config_json["Connection"]["listener_port"],
66
+ config_json["Connection"]["retries"]
67
+ )
68
+ self.log_level = config_json["Log_level"]
69
+ self.others = config_json["Others"]
70
+
71
+ self.inited = True
72
+ return self
73
+
74
+
75
+ class ConfigManager:
76
+ def __init__(self, config: Config):
77
+ self.config: Config = config
78
+
79
+ def get_cfg(self) -> Config:
80
+ return self.config
81
+
82
+
83
+ cm: ConfigManager
Hyper/DataBase.py ADDED
@@ -0,0 +1,81 @@
1
+ import json
2
+ import uuid
3
+
4
+
5
+ class Dataset:
6
+ def __init__(self, path: str = "./data.json", index: str = "./index.json"):
7
+ self.path = path
8
+ self.index = index
9
+ self.data = {}
10
+ self.indexes = {}
11
+
12
+ def load(self):
13
+ with open(self.path, "r") as f:
14
+ self.data = json.load(f)
15
+ with open(self.index, "r") as f:
16
+ self.indexes = json.load(f)
17
+
18
+ def save(self):
19
+ with open(self.path, "w") as f:
20
+ json.dump(self.data, f, indent=2)
21
+ with open(self.index, "w") as f:
22
+ json.dump(self.indexes, f, indent=2)
23
+
24
+ def set(self, id, key, value):
25
+ self.data[id][key] = value
26
+ self.save()
27
+
28
+ def dict_set(self, id, item: dict):
29
+ self.data[id] = item
30
+ self.save()
31
+
32
+ def create(self):
33
+ id = str(uuid.uuid4())
34
+ self.data[id] = dict()
35
+ self.save()
36
+ return id
37
+
38
+ def delete(self, id: str):
39
+ del self.data[id]
40
+ self.save()
41
+
42
+ def get(self, item: dict = None, id: str = None, index=True):
43
+ if item is not None:
44
+ itme_id = self.queue(item, index)
45
+ if not itme_id:
46
+ itme_id = self.create()
47
+ self.dict_set(itme_id, item)
48
+ return self.data[itme_id]
49
+ elif id is not None:
50
+ return self.data[id]
51
+ return False
52
+
53
+ def queue(self, key: dict, index=True):
54
+ if index:
55
+ try:
56
+ return self.indexes[str(key)]
57
+ except KeyError or TypeError:
58
+ pass
59
+ for k, v in key.items():
60
+ for i in self.data:
61
+ try:
62
+ if self.data[i][k] == v:
63
+ if index:
64
+ self.indexes[str(key)] = i
65
+ return i
66
+ except KeyError:
67
+ pass
68
+ return False
69
+
70
+ def exist(self, item: dict = None, id: str = None, index=True):
71
+ if item:
72
+ return True if self.get(item, index=index) else False
73
+ elif id:
74
+ return True if self.get({"_id": id}, index=index) else False
75
+ else:
76
+ return None
77
+
78
+ def clear(self):
79
+ self.data = {}
80
+ self.indexes = {}
81
+ self.save()
Hyper/Errors.py ADDED
@@ -0,0 +1,18 @@
1
+ class ButtonRowFulledError(Exception):
2
+ def __init__(self, message: str = None):
3
+ super().__init__(message)
4
+
5
+
6
+ class NotSupportError(NotImplementedError):
7
+ def __init__(self, message: str = None):
8
+ super().__init__(message)
9
+
10
+
11
+ class ListenerNotRegisteredError(Exception):
12
+ def __init__(self, message: str = None):
13
+ super().__init__(message)
14
+
15
+
16
+ class ArgsInvalidError(Exception):
17
+ def __init__(self, message: str = None):
18
+ super().__init__(message)