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