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