koishi-plugin-bilibili-notify 3.2.0-alpha.7 → 3.2.1-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
@@ -1,4 +1,6 @@
1
- import { type Context, Schema, Service } from "koishi";
1
+ import { type Awaitable, type Context, Schema, Service } from "koishi";
2
+ import CacheableLookup from "cacheable-lookup";
3
+ import { type AxiosInstance } from "axios";
2
4
  import { CookieJar } from "tough-cookie";
3
5
  import type { Notifier } from "@koishijs/plugin-notifier";
4
6
  declare module "koishi" {
@@ -9,7 +11,8 @@ declare module "koishi" {
9
11
  declare class BiliAPI extends Service {
10
12
  static inject: string[];
11
13
  jar: CookieJar;
12
- client: any;
14
+ client: AxiosInstance;
15
+ cacheable: CacheableLookup;
13
16
  apiConfig: BiliAPI.Config;
14
17
  loginData: any;
15
18
  loginNotifier: Notifier;
@@ -17,6 +20,7 @@ declare class BiliAPI extends Service {
17
20
  loginInfoIsLoaded: boolean;
18
21
  constructor(ctx: Context, config: BiliAPI.Config);
19
22
  protected start(): void | Promise<void>;
23
+ protected stop(): Awaitable<void>;
20
24
  getMixinKey: (orig: string) => string;
21
25
  encWbi(params: {
22
26
  [key: string]: string | number | object;
package/lib/biliAPI.js CHANGED
@@ -1,22 +1,20 @@
1
- "use strict";
2
1
  var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
2
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
3
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
4
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
5
  return c > 3 && r && Object.defineProperty(target, key, r), r;
7
6
  };
8
- var __importDefault = (this && this.__importDefault) || function (mod) {
9
- return (mod && mod.__esModule) ? mod : { "default": mod };
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const koishi_1 = require("koishi");
13
- const md5_1 = __importDefault(require("md5"));
14
- const node_crypto_1 = __importDefault(require("node:crypto"));
15
- const axios_1 = __importDefault(require("axios"));
16
- const tough_cookie_1 = require("tough-cookie");
17
- const axios_cookiejar_support_1 = require("axios-cookiejar-support");
18
- const jsdom_1 = require("jsdom");
19
- const utils_1 = require("./utils");
7
+ import { Schema, Service } from "koishi";
8
+ import md5 from "md5";
9
+ import crypto from "node:crypto";
10
+ import http from "node:http";
11
+ import https from "node:https";
12
+ import CacheableLookup from "cacheable-lookup";
13
+ import axios from "axios";
14
+ import { CookieJar, Cookie } from "tough-cookie";
15
+ import { wrapper } from "axios-cookiejar-support";
16
+ import { JSDOM } from "jsdom";
17
+ import { Retry } from "./utils";
20
18
  const mixinKeyEncTab = [
21
19
  46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35, 27, 43, 5, 49,
22
20
  33, 9, 42, 19, 29, 28, 14, 39, 12, 38, 41, 13, 37, 48, 7, 16, 24, 55, 40, 61,
@@ -49,11 +47,11 @@ const GET_RELATION_GROUP_DETAIL = "https://api.bilibili.com/x/relation/tag";
49
47
  // 直播
50
48
  const GET_LIVE_ROOM_INFO_STREAM_KEY = "https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo";
51
49
  const GET_LIVE_ROOMS_INFO = "https://api.live.bilibili.com/room/v1/Room/get_status_info_by_uids";
52
- class BiliAPI extends koishi_1.Service {
50
+ class BiliAPI extends Service {
53
51
  static inject = ["database", "notifier"];
54
52
  jar;
55
- // biome-ignore lint/suspicious/noExplicitAny: <explanation>
56
53
  client;
54
+ cacheable;
57
55
  apiConfig;
58
56
  // biome-ignore lint/suspicious/noExplicitAny: <explanation>
59
57
  loginData;
@@ -70,6 +68,11 @@ class BiliAPI extends koishi_1.Service {
70
68
  // 从数据库加载cookies
71
69
  this.loadCookiesFromDatabase();
72
70
  }
71
+ stop() {
72
+ // 将DNS缓存卸载
73
+ this.cacheable.uninstall(http.globalAgent);
74
+ this.cacheable.uninstall(https.globalAgent);
75
+ }
73
76
  // WBI签名
74
77
  // 对 imgKey 和 subKey 进行字符顺序打乱编码
75
78
  getMixinKey = (orig) => mixinKeyEncTab
@@ -91,7 +94,7 @@ class BiliAPI extends koishi_1.Service {
91
94
  return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
92
95
  })
93
96
  .join("&");
94
- const wbi_sign = (0, md5_1.default)(query + mixin_key); // 计算 w_rid
97
+ const wbi_sign = md5(query + mixin_key); // 计算 w_rid
95
98
  return `${query}&w_rid=${wbi_sign}`;
96
99
  }
97
100
  async getWbi(params) {
@@ -102,8 +105,8 @@ class BiliAPI extends koishi_1.Service {
102
105
  return query;
103
106
  }
104
107
  encrypt(text) {
105
- const iv = node_crypto_1.default.randomBytes(16);
106
- const cipher = node_crypto_1.default.createCipheriv("aes-256-cbc", Buffer.from(this.apiConfig.key), iv);
108
+ const iv = crypto.randomBytes(16);
109
+ const cipher = crypto.createCipheriv("aes-256-cbc", Buffer.from(this.apiConfig.key), iv);
107
110
  const encrypted = Buffer.concat([cipher.update(text), cipher.final()]);
108
111
  return `${iv.toString("hex")}:${encrypted.toString("hex")}`;
109
112
  }
@@ -111,7 +114,7 @@ class BiliAPI extends koishi_1.Service {
111
114
  const textParts = text.split(":");
112
115
  const iv = Buffer.from(textParts.shift(), "hex");
113
116
  const encryptedText = Buffer.from(textParts.join(":"), "hex");
114
- const decipher = node_crypto_1.default.createDecipheriv("aes-256-cbc", Buffer.from(this.apiConfig.key), iv);
117
+ const decipher = crypto.createDecipheriv("aes-256-cbc", Buffer.from(this.apiConfig.key), iv);
115
118
  const decrypted = Buffer.concat([
116
119
  decipher.update(encryptedText),
117
120
  decipher.final(),
@@ -287,8 +290,15 @@ class BiliAPI extends koishi_1.Service {
287
290
  return userAgents[index];
288
291
  }
289
292
  createNewClient() {
290
- this.jar = new tough_cookie_1.CookieJar();
291
- this.client = (0, axios_cookiejar_support_1.wrapper)(axios_1.default.create({
293
+ // 创建DNS缓存
294
+ this.cacheable = new CacheableLookup();
295
+ // 安装到http和https
296
+ this.cacheable.install(http.globalAgent);
297
+ this.cacheable.install(https.globalAgent);
298
+ // 创建cookieJar
299
+ this.jar = new CookieJar();
300
+ // 包装cookieJar
301
+ this.client = wrapper(axios.create({
292
302
  jar: this.jar,
293
303
  headers: {
294
304
  "Content-Type": "application/json",
@@ -404,7 +414,7 @@ class BiliAPI extends koishi_1.Service {
404
414
  sameSite = cookieData.sameSite;
405
415
  }
406
416
  // 创建一个完整的 Cookie 实例
407
- const cookie = new tough_cookie_1.Cookie({
417
+ const cookie = new Cookie({
408
418
  key: cookieData.key,
409
419
  value: cookieData.value,
410
420
  expires: new Date(cookieData.expires),
@@ -417,7 +427,7 @@ class BiliAPI extends koishi_1.Service {
417
427
  this.jar.setCookieSync(cookie, `http${cookie.secure ? "s" : ""}://${cookie.domain}${cookie.path}`, {});
418
428
  }
419
429
  // 对于某些 IP 地址,需要在 Cookie 中提供任意非空的 buvid3 字段
420
- const buvid3Cookie = new tough_cookie_1.Cookie({
430
+ const buvid3Cookie = new Cookie({
421
431
  key: "buvid3",
422
432
  value: "some_non_empty_value", // 设置任意非空值
423
433
  expires, // 设置过期时间
@@ -490,7 +500,7 @@ class BiliAPI extends koishi_1.Service {
490
500
  // 如果请求失败,有可能是404,直接刷新cookie
491
501
  }
492
502
  // 定义Key
493
- const publicKey = await node_crypto_1.default.subtle.importKey("jwk", {
503
+ const publicKey = await crypto.subtle.importKey("jwk", {
494
504
  kty: "RSA",
495
505
  n: "y4HdjgJHBlbaBN04VERG4qNBIFHP6a3GozCl75AihQloSWCXC5HDNgyinEnhaQ_4-gaMud_GF50elYXLlCToR9se9Z8z433U3KjM-3Yx7ptKkmQNAMggQwAVKgq3zYAoidNEWuxpkY_mAitTSRLnsJW-NCTa0bqBFF6Wm1MxgfE",
496
506
  e: "AQAB",
@@ -498,7 +508,7 @@ class BiliAPI extends koishi_1.Service {
498
508
  // 定义获取CorrespondPath方法
499
509
  async function getCorrespondPath(timestamp) {
500
510
  const data = new TextEncoder().encode(`refresh_${timestamp}`);
501
- const encrypted = new Uint8Array(await node_crypto_1.default.subtle.encrypt({ name: "RSA-OAEP" }, publicKey, data));
511
+ const encrypted = new Uint8Array(await crypto.subtle.encrypt({ name: "RSA-OAEP" }, publicKey, data));
502
512
  return encrypted.reduce((str, c) => str + c.toString(16).padStart(2, "0"), "");
503
513
  }
504
514
  // 获取CorrespondPath
@@ -507,7 +517,7 @@ class BiliAPI extends koishi_1.Service {
507
517
  // 获取refresh_csrf
508
518
  const { data: refreshCsrfHtml } = await this.client.get(`https://www.bilibili.com/correspond/1/${correspondPath}`);
509
519
  // 创建一个虚拟的DOM元素
510
- const { document } = new jsdom_1.JSDOM(refreshCsrfHtml).window;
520
+ const { document } = new JSDOM(refreshCsrfHtml).window;
511
521
  // 提取标签name为1-name的内容
512
522
  const targetElement = document.getElementById("1-name");
513
523
  const refresh_csrf = targetElement ? targetElement.textContent : null;
@@ -575,7 +585,7 @@ class BiliAPI extends koishi_1.Service {
575
585
  }
576
586
  }
577
587
  __decorate([
578
- (0, utils_1.Retry)({
588
+ Retry({
579
589
  attempts: 3,
580
590
  onFailure(error, attempts) {
581
591
  this.logger.error(`getTheUserWhoIsLiveStreaming() 第${attempts}次失败: ${error.message}`);
@@ -583,7 +593,7 @@ __decorate([
583
593
  })
584
594
  ], BiliAPI.prototype, "getTheUserWhoIsLiveStreaming", null);
585
595
  __decorate([
586
- (0, utils_1.Retry)({
596
+ Retry({
587
597
  attempts: 3,
588
598
  onFailure(error, attempts) {
589
599
  this.logger.error(`getLiveRoomInfoStreamKey() 第${attempts}次失败: ${error.message}`);
@@ -591,7 +601,7 @@ __decorate([
591
601
  })
592
602
  ], BiliAPI.prototype, "getLiveRoomInfoStreamKey", null);
593
603
  __decorate([
594
- (0, utils_1.Retry)({
604
+ Retry({
595
605
  attempts: 3,
596
606
  onFailure(error, attempts) {
597
607
  this.logger.error(`getLiveRoomInfoByUids() 第${attempts}次失败: ${error.message}`);
@@ -599,7 +609,7 @@ __decorate([
599
609
  })
600
610
  ], BiliAPI.prototype, "getLiveRoomInfoByUids", null);
601
611
  __decorate([
602
- (0, utils_1.Retry)({
612
+ Retry({
603
613
  attempts: 3,
604
614
  onFailure(error, attempts) {
605
615
  this.logger.error(`getServerUTCTime() 第${attempts}次失败: ${error.message}`);
@@ -607,7 +617,7 @@ __decorate([
607
617
  })
608
618
  ], BiliAPI.prototype, "getServerUTCTime", null);
609
619
  __decorate([
610
- (0, utils_1.Retry)({
620
+ Retry({
611
621
  attempts: 3,
612
622
  onFailure(error, attempts) {
613
623
  this.logger.error(`getTimeNow() 第${attempts}次失败: ${error.message}`);
@@ -615,7 +625,7 @@ __decorate([
615
625
  })
616
626
  ], BiliAPI.prototype, "getTimeNow", null);
617
627
  __decorate([
618
- (0, utils_1.Retry)({
628
+ Retry({
619
629
  attempts: 3,
620
630
  onFailure(error, attempts) {
621
631
  this.logger.error(`getAllGroup() 第${attempts}次失败: ${error.message}`);
@@ -623,7 +633,7 @@ __decorate([
623
633
  })
624
634
  ], BiliAPI.prototype, "getAllGroup", null);
625
635
  __decorate([
626
- (0, utils_1.Retry)({
636
+ Retry({
627
637
  attempts: 3,
628
638
  onFailure(error, attempts) {
629
639
  this.logger.error(`removeUserFromGroup() 第${attempts}次失败: ${error.message}`);
@@ -631,7 +641,7 @@ __decorate([
631
641
  })
632
642
  ], BiliAPI.prototype, "removeUserFromGroup", null);
633
643
  __decorate([
634
- (0, utils_1.Retry)({
644
+ Retry({
635
645
  attempts: 3,
636
646
  onFailure(error, attempts) {
637
647
  this.logger.error(`copyUserToGroup() 第${attempts}次失败: ${error.message}`);
@@ -639,7 +649,7 @@ __decorate([
639
649
  })
640
650
  ], BiliAPI.prototype, "copyUserToGroup", null);
641
651
  __decorate([
642
- (0, utils_1.Retry)({
652
+ Retry({
643
653
  attempts: 3,
644
654
  onFailure(error, attempts) {
645
655
  this.logger.error(`getUserSpaceDynamic() 第${attempts}次失败: ${error.message}`);
@@ -647,7 +657,7 @@ __decorate([
647
657
  })
648
658
  ], BiliAPI.prototype, "getUserSpaceDynamic", null);
649
659
  __decorate([
650
- (0, utils_1.Retry)({
660
+ Retry({
651
661
  attempts: 3,
652
662
  onFailure(error, attempts) {
653
663
  this.logger.error(`createGroup() 第${attempts}次失败: ${error.message}`);
@@ -655,7 +665,7 @@ __decorate([
655
665
  })
656
666
  ], BiliAPI.prototype, "createGroup", null);
657
667
  __decorate([
658
- (0, utils_1.Retry)({
668
+ Retry({
659
669
  attempts: 3,
660
670
  onFailure(error, attempts) {
661
671
  this.logger.error(`getAllDynamic() 第${attempts}次失败: ${error.message}`);
@@ -663,7 +673,7 @@ __decorate([
663
673
  })
664
674
  ], BiliAPI.prototype, "getAllDynamic", null);
665
675
  __decorate([
666
- (0, utils_1.Retry)({
676
+ Retry({
667
677
  attempts: 3,
668
678
  onFailure(error, attempts) {
669
679
  this.logger.error(`hasNewDynamic() 第${attempts}次失败: ${error.message}`);
@@ -671,7 +681,7 @@ __decorate([
671
681
  })
672
682
  ], BiliAPI.prototype, "hasNewDynamic", null);
673
683
  __decorate([
674
- (0, utils_1.Retry)({
684
+ Retry({
675
685
  attempts: 3,
676
686
  onFailure(error, attempts) {
677
687
  this.logger.error(`follow() 第${attempts}次失败: ${error.message}`);
@@ -679,7 +689,7 @@ __decorate([
679
689
  })
680
690
  ], BiliAPI.prototype, "follow", null);
681
691
  __decorate([
682
- (0, utils_1.Retry)({
692
+ Retry({
683
693
  attempts: 3,
684
694
  onFailure(error, attempts) {
685
695
  this.logger.error(`getRelationGroupDetail() 第${attempts}次失败: ${error.message}`);
@@ -687,7 +697,7 @@ __decorate([
687
697
  })
688
698
  ], BiliAPI.prototype, "getRelationGroupDetail", null);
689
699
  __decorate([
690
- (0, utils_1.Retry)({
700
+ Retry({
691
701
  attempts: 3,
692
702
  onFailure(error, attempts) {
693
703
  this.logger.error(`getCookieInfo() 第${attempts}次失败: ${error.message}`);
@@ -695,7 +705,7 @@ __decorate([
695
705
  })
696
706
  ], BiliAPI.prototype, "getCookieInfo", null);
697
707
  __decorate([
698
- (0, utils_1.Retry)({
708
+ Retry({
699
709
  attempts: 3,
700
710
  onFailure(error, attempts) {
701
711
  this.logger.error(`getUserInfo() 第${attempts}次失败: ${error.message}`);
@@ -703,7 +713,7 @@ __decorate([
703
713
  })
704
714
  ], BiliAPI.prototype, "getUserInfo", null);
705
715
  __decorate([
706
- (0, utils_1.Retry)({
716
+ Retry({
707
717
  attempts: 3,
708
718
  onFailure(error, attempts) {
709
719
  this.logger.error(`getWbiKeys() 第${attempts}次失败: ${error.message}`);
@@ -711,7 +721,7 @@ __decorate([
711
721
  })
712
722
  ], BiliAPI.prototype, "getWbiKeys", null);
713
723
  __decorate([
714
- (0, utils_1.Retry)({
724
+ Retry({
715
725
  attempts: 3,
716
726
  onFailure(error, attempts) {
717
727
  this.logger.error(`getMyselfInfo() 第${attempts}次失败: ${error.message}`);
@@ -719,7 +729,7 @@ __decorate([
719
729
  })
720
730
  ], BiliAPI.prototype, "getMyselfInfo", null);
721
731
  __decorate([
722
- (0, utils_1.Retry)({
732
+ Retry({
723
733
  attempts: 3,
724
734
  onFailure(error, attempts) {
725
735
  this.logger.error(`getLoginQRCode() 第${attempts}次失败: ${error.message}`);
@@ -727,7 +737,7 @@ __decorate([
727
737
  })
728
738
  ], BiliAPI.prototype, "getLoginQRCode", null);
729
739
  __decorate([
730
- (0, utils_1.Retry)({
740
+ Retry({
731
741
  attempts: 3,
732
742
  onFailure(error, attempts) {
733
743
  this.logger.error(`getLoginStatus() 第${attempts}次失败: ${error.message}`);
@@ -735,7 +745,7 @@ __decorate([
735
745
  })
736
746
  ], BiliAPI.prototype, "getLoginStatus", null);
737
747
  __decorate([
738
- (0, utils_1.Retry)({
748
+ Retry({
739
749
  attempts: 3,
740
750
  onFailure(error, attempts) {
741
751
  this.logger.error(`getLiveRoomInfo() 第${attempts}次失败: ${error.message}`);
@@ -743,7 +753,7 @@ __decorate([
743
753
  })
744
754
  ], BiliAPI.prototype, "getLiveRoomInfo", null);
745
755
  __decorate([
746
- (0, utils_1.Retry)({
756
+ Retry({
747
757
  attempts: 3,
748
758
  onFailure(error, attempts) {
749
759
  this.logger.error(`getMasterInfo() 第${attempts}次失败: ${error.message}`);
@@ -751,11 +761,11 @@ __decorate([
751
761
  })
752
762
  ], BiliAPI.prototype, "getMasterInfo", null);
753
763
  (function (BiliAPI) {
754
- BiliAPI.Config = koishi_1.Schema.object({
755
- userAgent: koishi_1.Schema.string(),
756
- key: koishi_1.Schema.string()
764
+ BiliAPI.Config = Schema.object({
765
+ userAgent: Schema.string(),
766
+ key: Schema.string()
757
767
  .pattern(/^[0-9a-f]{32}$/)
758
768
  .required(),
759
769
  });
760
770
  })(BiliAPI || (BiliAPI = {}));
761
- exports.default = BiliAPI;
771
+ export default BiliAPI;
package/lib/blive.js CHANGED
@@ -1,8 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const koishi_1 = require("koishi");
4
- const blive_message_listener_1 = require("blive-message-listener");
5
- class BLive extends koishi_1.Service {
1
+ import { Service } from "koishi";
2
+ import { startListen, } from "blive-message-listener";
3
+ class BLive extends Service {
6
4
  // 必要服务
7
5
  static inject = ["ba"];
8
6
  // 定义类属性
@@ -24,7 +22,7 @@ class BLive extends koishi_1.Service {
24
22
  // 获取自身信息
25
23
  const mySelfInfo = await this.ctx.ba.getMyselfInfo();
26
24
  // 创建实例并保存到Record中
27
- this.listenerRecord[roomId] = (0, blive_message_listener_1.startListen)(Number.parseInt(roomId), handler, {
25
+ this.listenerRecord[roomId] = startListen(Number.parseInt(roomId), handler, {
28
26
  ws: {
29
27
  headers: {
30
28
  Cookie: cookiesStr,
@@ -32,14 +30,11 @@ class BLive extends koishi_1.Service {
32
30
  uid: mySelfInfo.data.mid,
33
31
  },
34
32
  });
35
- // logger
36
- this.logger.info(`${roomId}直播间监听已开启`);
37
33
  }
38
34
  closeListener(roomId) {
39
35
  // 判断直播间监听器是否关闭
40
36
  if (!this.listenerRecord ||
41
- !this.listenerRecord[roomId] ||
42
- !this.listenerRecord[roomId].closed) {
37
+ !this.listenerRecord[roomId]?.closed) {
43
38
  // 输出logger
44
39
  this.logger.info(`${roomId}直播间弹幕监听器无需关闭`);
45
40
  }
@@ -58,4 +53,4 @@ class BLive extends koishi_1.Service {
58
53
  this.logger.warn(`${roomId}直播间弹幕监听未成功关闭`);
59
54
  }
60
55
  }
61
- exports.default = BLive;
56
+ export default BLive;
@@ -23,7 +23,7 @@ declare class ComRegister {
23
23
  constructor(ctx: Context, config: ComRegister.Config);
24
24
  init(config: ComRegister.Config): Promise<void>;
25
25
  initManager(): void;
26
- getBot(pf: string): Bot<Context, any>;
26
+ getBot(pf: string, selfId?: string): Bot<Context, any>;
27
27
  sendPrivateMsg(content: string): Promise<void>;
28
28
  sendPrivateMsgAndRebootService(): Promise<void>;
29
29
  sendPrivateMsgAndStopService(): Promise<void>;
@@ -65,6 +65,7 @@ declare namespace ComRegister {
65
65
  live: boolean;
66
66
  liveGuardBuy: boolean;
67
67
  atAll: boolean;
68
+ bot: string;
68
69
  }>;
69
70
  platform: string;
70
71
  }>;