koishi-plugin-bilibili-notify 1.1.2 → 1.2.0-alpha.1

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.
package/lib/biliAPI.d.ts CHANGED
@@ -26,11 +26,15 @@ declare class BiliAPI extends Service {
26
26
  getLoginStatus(qrcodeKey: string): Promise<any>;
27
27
  getLiveRoomInfo(roomId: string): Promise<any>;
28
28
  getMasterInfo(mid: string): Promise<any>;
29
- enableRefreshCookiesDetect(refreshToken: string, csrf?: string): void;
30
29
  disposeNotifier(): void;
31
30
  createNewClient(): void;
32
31
  getCookies(): string;
32
+ getLoginInfoFromDB(): Promise<{
33
+ cookies: any;
34
+ refresh_token: string;
35
+ }>;
33
36
  loadCookiesFromDatabase(): Promise<void>;
37
+ enableRefreshCookiesDetect(): void;
34
38
  checkIfTokenNeedRefresh(refreshToken: string, csrf: string, times?: number): Promise<void>;
35
39
  }
36
40
  export default BiliAPI;
package/lib/biliAPI.js CHANGED
@@ -180,26 +180,6 @@ class BiliAPI extends koishi_1.Service {
180
180
  throw new Error('网络异常,本次请求失败!');
181
181
  }
182
182
  }
183
- enableRefreshCookiesDetect(refreshToken, csrf) {
184
- // 获取cookies
185
- const cookies = JSON.parse(this.getCookies());
186
- // 获取csrf
187
- if (!csrf) {
188
- cookies.find(cookie => {
189
- // 获取key为bili_jct的值
190
- if (cookie.key === 'bili_jct') {
191
- csrf = cookie.value;
192
- return true;
193
- }
194
- });
195
- }
196
- // 判断之前是否启动检测
197
- this.refreshCookieTimer && this.refreshCookieTimer();
198
- // Open scheduled tasks and check if token need refresh
199
- this.refreshCookieTimer = this.ctx.setInterval(() => {
200
- this.checkIfTokenNeedRefresh(refreshToken, csrf);
201
- }, 43200000);
202
- }
203
183
  disposeNotifier() { this.loginNotifier && this.loginNotifier.dispose(); }
204
184
  createNewClient() {
205
185
  this.jar = new tough_cookie_1.CookieJar();
@@ -218,7 +198,7 @@ class BiliAPI extends koishi_1.Service {
218
198
  cookies = JSON.stringify(this.jar.serializeSync().cookies);
219
199
  return cookies;
220
200
  }
221
- async loadCookiesFromDatabase() {
201
+ async getLoginInfoFromDB() {
222
202
  // 读取数据库获取cookies
223
203
  const data = (await this.ctx.database.get('loginBili', 1))[0];
224
204
  // 判断是否登录
@@ -228,7 +208,11 @@ class BiliAPI extends koishi_1.Service {
228
208
  type: 'warning',
229
209
  content: '您尚未登录,将无法使用插件提供的指令'
230
210
  });
231
- return;
211
+ // 返回空值
212
+ return {
213
+ cookies: null,
214
+ refresh_token: null
215
+ };
232
216
  }
233
217
  // 定义解密信息
234
218
  let decryptedCookies;
@@ -247,6 +231,18 @@ class BiliAPI extends koishi_1.Service {
247
231
  }
248
232
  // 解析从数据库读到的cookies
249
233
  const cookies = JSON.parse(decryptedCookies);
234
+ // 返回值
235
+ return {
236
+ cookies,
237
+ refresh_token: decryptedRefreshToken
238
+ };
239
+ }
240
+ async loadCookiesFromDatabase() {
241
+ // Get login info from db
242
+ const { cookies, refresh_token } = await this.getLoginInfoFromDB();
243
+ // 判断是否有值
244
+ if (!cookies || !refresh_token)
245
+ return;
250
246
  // 定义CSRF Token
251
247
  let csrf;
252
248
  cookies.forEach(cookieData => {
@@ -267,11 +263,31 @@ class BiliAPI extends koishi_1.Service {
267
263
  this.jar.setCookieSync(cookie, `http${cookie.secure ? 's' : ''}://${cookie.domain}${cookie.path}`, {});
268
264
  });
269
265
  // restart plugin check
270
- this.checkIfTokenNeedRefresh(decryptedRefreshToken, csrf);
266
+ this.checkIfTokenNeedRefresh(refresh_token, csrf);
271
267
  // enable refresh cookies detect
272
- this.enableRefreshCookiesDetect(decryptedRefreshToken, csrf);
268
+ this.enableRefreshCookiesDetect();
269
+ }
270
+ enableRefreshCookiesDetect() {
271
+ // 判断之前是否启动检测
272
+ this.refreshCookieTimer && this.refreshCookieTimer();
273
+ // Open scheduled tasks and check if token need refresh
274
+ this.refreshCookieTimer = this.ctx.setInterval(async () => {
275
+ // 从数据库获取登录信息
276
+ const { cookies, refresh_token } = await this.getLoginInfoFromDB();
277
+ // 判断是否有值
278
+ if (!cookies || !refresh_token)
279
+ return;
280
+ // 获取csrf
281
+ const csrf = cookies.find(cookie => {
282
+ // 判断key是否为bili_jct
283
+ if (cookie.key === 'bili_jct')
284
+ return true;
285
+ }).value;
286
+ // 检查是否需要更新
287
+ this.checkIfTokenNeedRefresh(refresh_token, csrf);
288
+ }, 43200000);
273
289
  }
274
- async checkIfTokenNeedRefresh(refreshToken, csrf, times = 0) {
290
+ async checkIfTokenNeedRefresh(refreshToken, csrf, times = 3) {
275
291
  // 定义数据
276
292
  let data;
277
293
  // 定义方法
@@ -295,11 +311,11 @@ class BiliAPI extends koishi_1.Service {
295
311
  }
296
312
  catch (e) {
297
313
  // 发送三次仍网络错误则给管理员发送错误信息
298
- if (times > 3)
314
+ if (times < 1)
299
315
  return;
300
316
  // 等待3秒再次尝试
301
317
  this.ctx.setTimeout(() => {
302
- this.checkIfTokenNeedRefresh(refreshToken, csrf, times + 1);
318
+ this.checkIfTokenNeedRefresh(refreshToken, csrf, times - 1);
303
319
  }, 3000);
304
320
  return;
305
321
  }
@@ -361,11 +377,10 @@ class BiliAPI extends koishi_1.Service {
361
377
  bili_refresh_token: encryptedRefreshToken
362
378
  }]);
363
379
  // Get new csrf from cookies
364
- let newCsrf;
365
- this.jar.serializeSync().cookies.forEach(cookie => {
380
+ let newCsrf = this.jar.serializeSync().cookies.find(cookie => {
366
381
  if (cookie.key === 'bili_jct')
367
- newCsrf = cookie.value;
368
- });
382
+ return true;
383
+ }).value;
369
384
  // Accept update
370
385
  const { data: aceeptData } = await this.client.post('https://passport.bilibili.com/x/passport-login/web/confirm/refresh', {
371
386
  csrf: newCsrf,
@@ -212,7 +212,7 @@ class ComRegister {
212
212
  // bili show
213
213
  await session.execute('bili show');
214
214
  // 开启cookies刷新检测
215
- ctx.biliAPI.enableRefreshCookiesDetect(loginContent.data.refresh_token);
215
+ ctx.biliAPI.enableRefreshCookiesDetect();
216
216
  return;
217
217
  }
218
218
  }
@@ -777,11 +777,11 @@ class ComRegister {
777
777
  // 主播信息不会变,开播时刷新一次即可
778
778
  uData = userData;
779
779
  // 发送直播通知卡片
780
- sendLiveNotifyCard(data, uData, LiveType.StartBroadcasting);
780
+ await sendLiveNotifyCard(data, uData, LiveType.StartBroadcasting);
781
781
  // 判断是否需要@全体成员
782
782
  if (this.config.liveStartAtAll) {
783
783
  // 发送@全体成员通知
784
- bot.sendMessage(guildId, (0, jsx_runtime_1.jsx)("at", { type: "all" }));
784
+ await bot.sendMessage(guildId, (0, jsx_runtime_1.jsx)("at", { type: "all" }));
785
785
  }
786
786
  }
787
787
  else { // 还在直播
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-bilibili-notify",
3
3
  "description": "Koishi bilibili notify plugin",
4
- "version": "1.1.2",
4
+ "version": "1.2.0-alpha.1",
5
5
  "contributors": [
6
6
  "Akokko <admin@akokko.com>"
7
7
  ],
package/readme.md CHANGED
@@ -96,6 +96,7 @@
96
96
  - ver 1.1.0 移除了直播艾特全体成员选项实验性的标志,优化了直播房间号的获取逻辑,移除了部分测试代码
97
97
  - ver 1.1.1 新增依赖axios
98
98
  - ver 1.1.2 修复了对red协议支持的一个bug
99
+ - ver 1.2.0-alpha.0 对自动更新登录信息的功能做了提升,修复了一些bug
99
100
 
100
101
  ## 感谢
101
102