OneBotConnecter 0.3.5__tar.gz → 0.3.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: OneBotConnecter
3
- Version: 0.3.5
3
+ Version: 0.3.7
4
4
  Summary: 基于websocket(服务器正向)连接的onebot11通用python接口
5
5
  Author-email: Sugar51243 <1733682365@qq.com>
6
6
  License: MIT License
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "OneBotConnecter"
7
- version = "0.3.05"
7
+ version = "0.3.07"
8
8
  authors = [
9
9
  { name="Sugar51243", email="1733682365@qq.com" },
10
10
  ]
@@ -1,9 +1,14 @@
1
1
  #import
2
+ try:
3
+ from OneBotConnecter.logger import print
4
+ except:
5
+ print("File [logger.py] missing")
6
+ raise Exception("File [logger.py] missing")
2
7
  try:
3
8
  from OneBotConnecter.MessageType import Message, ReplyMessage, AtMessage, MessageChain
4
9
  except:
5
10
  print("File [OneBotMessageType.py] missing")
6
- raise Exception()
11
+ raise Exception("File [OneBotMessageType.py] missing")
7
12
  from typing import Literal
8
13
  import os
9
14
  moduleList = ["traceback", "asyncio", "json", "websockets"]
@@ -50,8 +55,11 @@ class OneBot:
50
55
  else: print("[W]: Main file location input is None")
51
56
  #调试模式
52
57
  self.testMode = testMode
58
+ print("初始化完成", needPrint=False)
59
+
53
60
  #建立连接 (WS正向) == #初次连接
54
61
  async def run(self, on_message: __module__ = _on_message, sleep_time: int = 1):
62
+ print("正在建立初次连接...", needPrint=False)
55
63
  #直到连接成功为止,持续尝试连接
56
64
  while self.bot == None:
57
65
  try: self.bot = await websockets.connect(self._uri)
@@ -73,6 +81,7 @@ class OneBot:
73
81
  if self.localtion != None: print(f"机器人根目录地址: {self.localtion}")
74
82
  print(f"开始监听机器人信息推送\n")
75
83
  #持续从接口收取信息
84
+ counter = 0
76
85
  while True:
77
86
  #连接失败 => 直到连接成功为止,持续尝试重连
78
87
  if self.bot == None:
@@ -84,7 +93,7 @@ class OneBot:
84
93
  await asyncio.sleep(5)
85
94
  #连接正常
86
95
  if self.bot != None:
87
- if self.testMode: print("Next Loop")
96
+ print(f"下一轮信息收集[{counter}]", needPrint=self.testMode)
88
97
  #从接口收取信息,并进行信息处理
89
98
  task = asyncio.create_task(self._receive_messages(on_message))
90
99
  try:
@@ -93,6 +102,8 @@ class OneBot:
93
102
  #可以不作处理
94
103
  except Exception: pass
95
104
  await asyncio.sleep(sleep_time)
105
+ print(f"此轮结束[{counter}]", needPrint=self.testMode)
106
+ counter+=1
96
107
 
97
108
  #收到信息时
98
109
  async def _receive_messages(self, callback: __module__):
@@ -102,40 +113,46 @@ class OneBot:
102
113
  if self.get_message:
103
114
  message = await self.bot.recv()
104
115
  message = json.loads(message)
116
+ print(f"获取信息:\n{message}", needPrint=self.testMode)
105
117
  #处理 => 缓存
106
118
  try:
107
119
  #识别是否为心跳信息
108
120
  if message["post_type"] != "meta_event" and self.bot != None:
109
121
  self.message_list.append(message) #放入缓存,排队处理
110
- elif self.testMode: print(f"{message}\n") #调试模式下打印所有信息
111
- except:
122
+ print(f"非心跳信息,已放入缓存", needPrint=self.testMode)
123
+ except Exception as e:
112
124
  #报错处理,这里可能是来自post_type的Key_Exception
113
125
  # 所以打印处理,方便进一步人工识别和修改
114
- print(f"{message}\n")
126
+ tb = e.__traceback__
127
+ formatted_tb = ''.join(traceback.format_tb(tb))
128
+ print(f"信息报错:\n{formatted_tb}", needPrint=self.testMode)
115
129
  #处理 => 信息
116
- if self.testMode: print(f"待处理信息列表数量为: {len(self.message_list)}\n")
130
+ print(f"待处理信息列表数量为: {len(self.message_list)}", needPrint=self.testMode)
117
131
  #一口气清空缓存
118
132
  while len(self.message_list) > 0:
119
133
  try:
120
134
  message = self.message_list.pop(0)
121
- if self.testMode: print(f"{message}\n")
135
+ print(f"正在处理: \n{message}", needPrint=self.testMode)
122
136
  await callback(self, message)
123
137
  except Exception as e:
124
- traceback.print_exc()
125
- print("")
126
- if self.testMode: print(f"信息列表处理完毕\n")
127
- if not self.get_message: self.get_message = True
138
+ tb = e.__traceback__
139
+ formatted_tb = ''.join(traceback.format_tb(tb))
140
+ print(f"处理信息报错:\n{formatted_tb}", needPrint=self.testMode)
141
+ print(f"信息列表处理完毕", needPrint=self.testMode)
142
+ if not self.get_message:
143
+ self.get_message = True
144
+ print(f"当前信息收集已中断,已强制恢复", needPrint=self.testMode)
128
145
  #连接失败
129
146
  except websockets.exceptions.ConnectionClosed:
130
- print("与机器人连接已断开\n")
147
+ print("与机器人连接已断开")
131
148
  self.bot = None
132
149
  #异步报错
133
150
  except RuntimeError: pass
134
151
  #其他奇怪报错
135
152
  except:
136
- if self.testMode:
137
- traceback.print_exc()
138
- print("")
153
+ tb = e.__traceback__
154
+ formatted_tb = ''.join(traceback.format_tb(tb))
155
+ print(f"连接报错:\n{formatted_tb}", needPrint=self.testMode)
139
156
 
140
157
  #为信息发送构造数据包
141
158
  def _createDataPack(self, action: str, params: dict):
@@ -148,11 +165,12 @@ class OneBot:
148
165
  async def _sendToServer(self, action: str, params: dict):
149
166
  #构造数据包
150
167
  datapack = self._createDataPack(action, params)
151
- if self.testMode:print(f"数据包发送: {datapack}\n")
152
168
  #发送
153
169
  await self.bot.send(datapack)
170
+ print(f"数据包发送: {datapack}", needPrint=self.testMode)
154
171
  #收集处理结果
155
172
  message = None
173
+ print(f"中断信息收集", needPrint=self.testMode)
156
174
  self.get_message = False
157
175
  while True:
158
176
  #因为处理可能会有延迟,需要识别从接口收取的信息
@@ -160,41 +178,46 @@ class OneBot:
160
178
  #从接口收取信息
161
179
  callback = await self.bot.recv()
162
180
  message = json.loads(callback)
181
+ print(f"数据包发送后收取: \n{message}", needPrint=self.testMode)
163
182
  #识别是否为正常信息
164
183
  try:
165
184
  #正常信息 => 缓存
166
185
  try:
167
186
  if message["post_type"] != "meta_event" and self.bot != None:
168
187
  self.message_list.append(message)
169
- if self.testMode: print("正在处理其他信息,缓存信息")
188
+ print("正在处理其他信息,缓存信息", needPrint=self.testMode)
170
189
  #其他信息 => 识别
171
190
  except:
172
191
  if message == {}:
173
- if self.testMode: print("空包识别")
192
+ print("空包识别", needPrint=self.testMode)
174
193
  break
175
194
  try:
176
- if self.testMode: print("retcode识别")
195
+ print("retcode识别", needPrint=self.testMode)
177
196
  retcode = message["retcode"]
178
197
  break
179
198
  except:
180
- if self.testMode:
181
- print("其他信息识别:")
182
- print(f"{message}\n")
199
+ print(f"其他信息识别", needPrint=self.testMode)
183
200
  #非常规信息 => 强行返回
184
201
  except:
185
- if self.testMode:
186
- print("非常规信息识别:")
187
- print(f"{message}\n")
202
+ tb = e.__traceback__
203
+ formatted_tb = ''.join(traceback.format_tb(tb))
204
+ print(f"报错识别: \n{formatted_tb}", needPrint=self.testMode)
188
205
  break
189
206
  #异步报错
190
207
  except RuntimeError: pass
191
208
  #处理失败
192
- except Exception as e: print(e)
209
+ except Exception as e:
210
+ tb = e.__traceback__
211
+ formatted_tb = ''.join(traceback.format_tb(tb))
212
+ print(f"处理失败: \n{formatted_tb}", needPrint=self.testMode)
213
+ break
193
214
  #继续收取
215
+ print(f"未识别返回值,继续收取", needPrint=self.testMode)
194
216
  await asyncio.sleep(1)
195
217
  self.get_message = True
218
+ print(f"恢复信息收集", needPrint=self.testMode)
196
219
  #识别完毕,返回
197
- if self.testMode: print(f"数据包返回: {message}\n")
220
+ print(f"数据包返回: {message}", needPrint=self.testMode)
198
221
  return message
199
222
  #调试模式开关
200
223
  async def test(self, testMode: bool = False):
@@ -0,0 +1,24 @@
1
+ #引用库
2
+ import time, os
3
+ import builtins as __builtin__
4
+
5
+ #系统时间 => 日志文件名字
6
+ current_time = time.strftime("%Y-%m-%d_%H_%M_%S", time.localtime())
7
+ loger_time = time.strftime("%Y-%m-%d", time.localtime()) #每日轮换文件
8
+
9
+
10
+ #日志写入
11
+ def print(data: str = "", needPrint = True):
12
+ #每日轮换文件
13
+ today = time.strftime("%Y-%m-%d", time.localtime())
14
+ global current_time
15
+ global loger_time
16
+ if today != loger_time:
17
+ loger_time = time.strftime("%Y-%m-%d", time.localtime())
18
+ #后台打印
19
+ if needPrint: __builtin__.print(data)
20
+ #文件写入
21
+ if not os.path.isdir("log"): os.makedirs("log")
22
+ file = open(f'log/OneBotConnecter_{current_time}.log', 'a', encoding="utf-8")
23
+ file.write(f"{data}\n")
24
+ file.close()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: OneBotConnecter
3
- Version: 0.3.5
3
+ Version: 0.3.7
4
4
  Summary: 基于websocket(服务器正向)连接的onebot11通用python接口
5
5
  Author-email: Sugar51243 <1733682365@qq.com>
6
6
  License: MIT License
@@ -4,6 +4,7 @@ pyproject.toml
4
4
  src/OneBotConnecter/MessageType.py
5
5
  src/OneBotConnecter/OneBot.py
6
6
  src/OneBotConnecter/__init__.py
7
+ src/OneBotConnecter/logger.py
7
8
  src/OneBotConnecter.egg-info/PKG-INFO
8
9
  src/OneBotConnecter.egg-info/SOURCES.txt
9
10
  src/OneBotConnecter.egg-info/dependency_links.txt
File without changes