koishi-plugin-bilibili-notify 3.0.0-alpha.1 → 3.0.0-alpha.2

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.
@@ -18,14 +18,25 @@ var LiveType;
18
18
  LiveType[LiveType["StopBroadcast"] = 3] = "StopBroadcast";
19
19
  })(LiveType || (LiveType = {}));
20
20
  class ComRegister {
21
+ // 必须服务
21
22
  static inject = ['ba', 'gi', 'database', 'bl', 'sm'];
23
+ // 定义数组:QQ相关bot
22
24
  qqRelatedBotList = ['qq', 'onebot', 'red', 'satori', 'chronocat'];
25
+ // logger
23
26
  logger;
27
+ // config
24
28
  config;
29
+ // 登录定时器
25
30
  loginTimer;
31
+ // 订阅数量
26
32
  num = 0;
33
+ // 重启次数
27
34
  rebootCount = 0;
35
+ // 订阅通知
28
36
  subNotifier;
37
+ // Context
38
+ ctx;
39
+ // 订阅管理器
29
40
  subManager = [];
30
41
  // 检查登录数据库是否有数据
31
42
  loginDBData;
@@ -37,8 +48,10 @@ class ComRegister {
37
48
  sendMsgFunc;
38
49
  // 构造函数
39
50
  constructor(ctx, config) {
51
+ // 将ctx赋值给类属性
52
+ this.ctx = ctx;
40
53
  // 初始化
41
- this.init(ctx, config);
54
+ this.init(config);
42
55
  // 注册指令
43
56
  const statusCom = ctx.command('status', '插件状态相关指令', { permissions: ['authority:5'] });
44
57
  statusCom.subcommand('.dyn', '查看动态监测运行状态')
@@ -151,7 +164,7 @@ class ComRegister {
151
164
  // 销毁定时器
152
165
  this.loginTimer();
153
166
  // 订阅之前的订阅
154
- await this.loadSubFromDatabase(ctx);
167
+ await this.loadSubFromDatabase();
155
168
  // 清除控制台通知
156
169
  ctx.ba.disposeNotifier();
157
170
  // 发送成功登录推送
@@ -189,17 +202,14 @@ class ComRegister {
189
202
  // 取消单个订阅
190
203
  if (options.live || options.dynamic) {
191
204
  if (options.live)
192
- await session.send(this.unsubSingle(ctx, sub.roomId, 0)); /* 0为取消订阅Live */
205
+ await session.send(this.unsubSingle(sub.roomId, 0)); /* 0为取消订阅Live */
193
206
  if (options.dynamic)
194
- await session.send(this.unsubSingle(ctx, sub.uid, 1)); /* 1为取消订阅Dynamic */
207
+ await session.send(this.unsubSingle(sub.uid, 1)); /* 1为取消订阅Dynamic */
195
208
  // 将存在flag设置为true
196
209
  exist = true;
197
210
  // 结束循环
198
211
  return;
199
212
  }
200
- // 取消全部订阅 执行dispose方法,销毁定时器
201
- if (sub.live)
202
- this.subManager[i].liveDispose();
203
213
  // 从数据库中删除订阅
204
214
  await ctx.database.remove('bilibili', { uid: this.subManager[i].uid });
205
215
  // 将该订阅对象从订阅管理对象中移除
@@ -222,7 +232,7 @@ class ComRegister {
222
232
  // 发送成功通知
223
233
  await session.send('已取消订阅该用户');
224
234
  // 更新控制台提示
225
- this.updateSubNotifier(ctx);
235
+ this.updateSubNotifier();
226
236
  // 将存在flag设置为true
227
237
  exist = true;
228
238
  }
@@ -254,7 +264,7 @@ class ComRegister {
254
264
  return '直播订阅已达上限,请取消部分直播订阅后再进行订阅';
255
265
  }
256
266
  // 检查是否登录
257
- if (!(await this.checkIfIsLogin(ctx))) {
267
+ if (!(await this.checkIfIsLogin())) {
258
268
  // 未登录直接返回
259
269
  return '请使用指令bili login登录后再进行订阅操作';
260
270
  }
@@ -262,7 +272,7 @@ class ComRegister {
262
272
  if (!mid)
263
273
  return '请输入用户uid';
264
274
  // 订阅对象
265
- const subUserData = await this.subUserInBili(ctx, mid);
275
+ const subUserData = await this.subUserInBili(mid);
266
276
  // 判断是否订阅对象存在
267
277
  if (!subUserData.flag)
268
278
  return '订阅对象失败,请稍后重试!';
@@ -278,6 +288,7 @@ class ComRegister {
278
288
  channelId: group,
279
289
  dynamic: true,
280
290
  live: true,
291
+ liveDanmaku: false,
281
292
  atAll: options.atAll
282
293
  });
283
294
  });
@@ -297,7 +308,7 @@ class ComRegister {
297
308
  for (const [index, { channelIdArr, platform }] of target.entries()) {
298
309
  if (channelIdArr.length > 0) { // 输入了推送群号或频道号
299
310
  // 拿到对应的bot
300
- const bot = this.getBot(ctx, platform);
311
+ const bot = this.getBot(platform);
301
312
  // 判断是否配置了对应平台的机器人
302
313
  if (!ctx.bots.some(bot => bot.platform === platform)) {
303
314
  // 发送提示消息
@@ -337,7 +348,7 @@ class ComRegister {
337
348
  // 判断targetArr是否为空
338
349
  if (target.length === 0) {
339
350
  // 为空则默认为当前环境
340
- target = [{ channelIdArr: [{ channelId: session.event.channel.id, dynamic: true, live: true, atAll: options.atAll ? options.atAll : false }], platform: session.event.platform }];
351
+ target = [{ channelIdArr: [{ channelId: session.event.channel.id, dynamic: true, live: true, liveDanmaku: false, atAll: options.atAll ? options.atAll : false }], platform: session.event.platform }];
341
352
  // 没有满足条件的群组或频道
342
353
  await session.send('没有满足条件的群组或频道,默认订阅到当前聊天环境');
343
354
  }
@@ -348,7 +359,7 @@ class ComRegister {
348
359
  }
349
360
  else {
350
361
  // 未填写群号或频道号,默认为当前环境
351
- target = [{ channelIdArr: [{ channelId: session.event.channel.id, dynamic: true, live: true, atAll: options.atAll ? options.atAll : false }], platform: session.event.platform }];
362
+ target = [{ channelIdArr: [{ channelId: session.event.channel.id, dynamic: true, live: true, liveDanmaku: false, atAll: options.atAll ? options.atAll : false }], platform: session.event.platform }];
352
363
  // 发送提示消息
353
364
  await session.send('没有填写群号或频道号,默认订阅到当前聊天环境');
354
365
  }
@@ -356,7 +367,7 @@ class ComRegister {
356
367
  }
357
368
  else {
358
369
  // 用户直接订阅,将当前环境赋值给target
359
- target = [{ channelIdArr: [{ channelId: session.event.channel.id, dynamic: true, live: true, atAll: options.atAll ? options.atAll : false }], platform: session.event.platform }];
370
+ target = [{ channelIdArr: [{ channelId: session.event.channel.id, dynamic: true, live: true, liveDanmaku: false, atAll: options.atAll ? options.atAll : false }], platform: session.event.platform }];
360
371
  }
361
372
  }
362
373
  // 定义外围变量
@@ -426,12 +437,10 @@ class ComRegister {
426
437
  this.logger.error('bili sub指令 getMasterInfo() 发生了错误,错误为:' + e.message);
427
438
  return '订阅出错啦,请重试';
428
439
  }
429
- // 定义live销毁函数
430
- let liveDispose;
431
440
  // 订阅直播
432
441
  if (liveMsg) {
433
442
  // 连接到服务器
434
- this.liveDetectWithListener(ctx, roomId, target);
443
+ this.liveDetectWithListener(roomId, target);
435
444
  // 发送订阅消息通知
436
445
  await session.send(`订阅${userData.info.uname}直播通知`);
437
446
  }
@@ -439,7 +448,7 @@ class ComRegister {
439
448
  if (dynamicMsg) {
440
449
  // 判断是否开启动态监测
441
450
  if (!this.dynamicDispose) {
442
- this.enableDynamicDetect(ctx);
451
+ this.enableDynamicDetect();
443
452
  }
444
453
  // 发送订阅消息通知
445
454
  await session.send(`订阅${userData.info.uname}动态通知`);
@@ -465,10 +474,9 @@ class ComRegister {
465
474
  platform: session.event.platform,
466
475
  live: liveMsg,
467
476
  dynamic: dynamicMsg,
468
- liveDispose
469
477
  });
470
478
  // 新增订阅展示到控制台
471
- this.updateSubNotifier(ctx);
479
+ this.updateSubNotifier();
472
480
  });
473
481
  biliCom
474
482
  .subcommand('.status <roomId:string>', '查询主播当前直播状态', { hidden: true })
@@ -524,25 +532,19 @@ class ComRegister {
524
532
  await session.send('已发送消息,如未收到则说明您的机器人不支持发送私聊消息或您的信息填写有误');
525
533
  });
526
534
  }
527
- async init(ctx, config) {
535
+ async init(config) {
528
536
  // 设置logger
529
- this.logger = ctx.logger('cr');
537
+ this.logger = this.ctx.logger('cr');
530
538
  // 将config设置给类属性
531
539
  this.config = config;
532
540
  // 拿到私人机器人实例
533
- this.privateBot = ctx.bots.find(bot => bot.platform === config.master.platform);
541
+ this.privateBot = this.ctx.bots.find(bot => bot.platform === config.master.platform);
534
542
  if (!this.privateBot) {
535
- ctx.notifier.create({
543
+ this.ctx.notifier.create({
536
544
  content: '您未配置私人机器人,将无法向您推送机器人状态!'
537
545
  });
538
546
  this.logger.error('您未配置私人机器人,将无法向您推送机器人状态!');
539
547
  }
540
- // 检查登录数据库是否有数据
541
- this.loginDBData = (await ctx.database.get('loginBili', 1, ['dynamic_group_id']))[0];
542
- // 从配置获取订阅
543
- config.sub && await this.loadSubFromConfig(ctx, config.sub);
544
- // 从数据库获取订阅
545
- await this.loadSubFromDatabase(ctx);
546
548
  // 判断消息发送方式
547
549
  if (config.automaticResend) {
548
550
  this.sendMsgFunc = async (bot, channelId, content) => {
@@ -553,7 +555,7 @@ class ComRegister {
553
555
  // 发送消息
554
556
  await bot.sendMessage(channelId, content);
555
557
  // 防止消息发送速度过快被忽略
556
- await ctx.sleep(500);
558
+ await this.ctx.sleep(500);
557
559
  // 成功发送消息,跳出循环
558
560
  break;
559
561
  }
@@ -579,23 +581,29 @@ class ComRegister {
579
581
  }
580
582
  };
581
583
  }
584
+ // 检查登录数据库是否有数据
585
+ this.loginDBData = (await this.ctx.database.get('loginBili', 1, ['dynamic_group_id']))[0];
586
+ // 从配置获取订阅
587
+ config.sub && await this.loadSubFromConfig(config.sub);
588
+ // 从数据库获取订阅
589
+ await this.loadSubFromDatabase();
582
590
  // 检查是否需要动态监测
583
- this.checkIfDynamicDetectIsNeeded(ctx);
591
+ this.checkIfDynamicDetectIsNeeded();
584
592
  // 在控制台中显示订阅对象
585
- this.updateSubNotifier(ctx);
593
+ this.updateSubNotifier();
586
594
  }
587
595
  splitMultiPlatformStr(str) {
588
596
  return str.split(';').map(cv => cv.split('.')).map(([idStr, platform]) => {
589
597
  const channelIdArr = idStr.split(',').map(id => {
590
598
  const atAll = /@$/.test(id); // 使用正则表达式检查 id 是否以 @ 结尾
591
599
  const channelId = atAll ? id.slice(0, -1) : id; // 去除末尾的 @
592
- return { channelId, dynamic: true, live: true, atAll };
600
+ return { channelId, dynamic: true, live: true, liveDanmaku: false, atAll };
593
601
  });
594
602
  return { channelIdArr, platform };
595
603
  });
596
604
  }
597
- getBot(ctx, pf) {
598
- return ctx.bots.find(bot => bot.platform === pf);
605
+ getBot(pf) {
606
+ return this.ctx.bots.find(bot => bot.platform === pf);
599
607
  }
600
608
  async sendPrivateMsg(content) {
601
609
  if (this.config.master.enable) {
@@ -609,7 +617,7 @@ class ComRegister {
609
617
  }
610
618
  }
611
619
  }
612
- async sendPrivateMsgAndRebootService(ctx) {
620
+ async sendPrivateMsgAndRebootService() {
613
621
  // 判断重启次数是否超过三次
614
622
  if (this.rebootCount >= 3) {
615
623
  // logger
@@ -617,7 +625,7 @@ class ComRegister {
617
625
  // 重启失败,发送消息
618
626
  await this.sendPrivateMsg('已重启插件三次,请检查机器人状态后使用指令 sys start 启动插件');
619
627
  // 关闭插件
620
- await ctx.sm.disposePlugin();
628
+ await this.ctx.sm.disposePlugin();
621
629
  // 结束
622
630
  return;
623
631
  }
@@ -626,7 +634,7 @@ class ComRegister {
626
634
  // logger
627
635
  this.logger.info('插件出现未知错误,正在重启插件');
628
636
  // 重启插件
629
- const flag = await ctx.sm.restartPlugin();
637
+ const flag = await this.ctx.sm.restartPlugin();
630
638
  // 判断是否重启成功
631
639
  if (flag) {
632
640
  this.logger.info('重启插件成功');
@@ -637,23 +645,23 @@ class ComRegister {
637
645
  // 重启失败,发送消息
638
646
  await this.sendPrivateMsg('重启插件失败,请检查机器人状态后使用指令 sys start 启动插件');
639
647
  // 关闭插件
640
- await ctx.sm.disposePlugin();
648
+ await this.ctx.sm.disposePlugin();
641
649
  }
642
650
  }
643
- async sendPrivateMsgAndStopService(ctx) {
651
+ async sendPrivateMsgAndStopService() {
644
652
  // 发送消息
645
653
  await this.sendPrivateMsg('插件发生未知错误,请检查机器人状态后使用指令 sys start 启动插件');
646
654
  // logger
647
655
  this.logger.error('插件发生未知错误,请检查机器人状态后使用指令 sys start 启动插件');
648
656
  // 关闭插件
649
- await ctx.sm.disposePlugin();
657
+ await this.ctx.sm.disposePlugin();
650
658
  // 结束
651
659
  return;
652
660
  }
653
- async sendMsg(ctx, targets, content, live) {
661
+ async sendMsg(targets, content, live) {
654
662
  for (const target of targets) {
655
663
  // 获取机器人实例
656
- const bot = this.getBot(ctx, target.platform);
664
+ const bot = this.getBot(target.platform);
657
665
  // 定义需要发送的数组
658
666
  let sendArr = [];
659
667
  // 判断是否需要推送所有机器人加入的群
@@ -664,6 +672,7 @@ class ComRegister {
664
672
  channelId: guild.id,
665
673
  dynamic: target.channelIdArr[0].dynamic,
666
674
  live: target.channelIdArr[0].live,
675
+ liveDanmaku: target.channelIdArr[0].liveDanmaku,
667
676
  atAll: target.channelIdArr[0].atAll
668
677
  });
669
678
  }
@@ -695,7 +704,7 @@ class ComRegister {
695
704
  }
696
705
  }
697
706
  }
698
- dynamicDetect(ctx) {
707
+ dynamicDetect() {
699
708
  // 检测初始化变量
700
709
  let detectSetup = true;
701
710
  // 更新基线
@@ -715,7 +724,7 @@ class ComRegister {
715
724
  // 检测启动初始化
716
725
  if (detectSetup) {
717
726
  // 获取动态信息
718
- const data = await ctx.ba.getAllDynamic();
727
+ const data = await this.ctx.ba.getAllDynamic();
719
728
  // 判断获取动态信息是否成功
720
729
  if (data.code !== 0)
721
730
  return;
@@ -731,13 +740,13 @@ class ComRegister {
731
740
  let content;
732
741
  try {
733
742
  // 查询是否有新动态
734
- const data = await ctx.ba.hasNewDynamic(updateBaseline);
743
+ const data = await this.ctx.ba.hasNewDynamic(updateBaseline);
735
744
  updateNum = data.data.update_num;
736
745
  // 没有新动态或获取动态信息失败直接返回
737
746
  if (updateNum <= 0 || data.code !== 0)
738
747
  return;
739
748
  // 获取动态内容
740
- content = await ctx.ba.getAllDynamic(updateBaseline);
749
+ content = await this.ctx.ba.getAllDynamic(updateBaseline);
741
750
  }
742
751
  catch (e) {
743
752
  return this.logger.error('dynamicDetect getUserSpaceDynamic() 发生了错误,错误为:' + e.message);
@@ -751,7 +760,7 @@ class ComRegister {
751
760
  // 发送私聊消息
752
761
  await this.sendPrivateMsg('账号未登录,插件已停止工作,请登录后,输入指令 sys start 启动插件');
753
762
  // 停止服务
754
- await ctx.sm.disposePlugin();
763
+ await this.ctx.sm.disposePlugin();
755
764
  // 结束循环
756
765
  break;
757
766
  }
@@ -761,7 +770,7 @@ class ComRegister {
761
770
  // 发送私聊消息
762
771
  await this.sendPrivateMsg('账号被风控,插件已停止工作,请确认风控解除后,输入指令 sys start 启动插件');
763
772
  // 停止服务
764
- await ctx.sm.disposePlugin();
773
+ await this.ctx.sm.disposePlugin();
765
774
  // 结束循环
766
775
  break;
767
776
  }
@@ -821,7 +830,7 @@ class ComRegister {
821
830
  // 获取动态推送图片
822
831
  try {
823
832
  // 渲染图片
824
- const { pic: gimgPic, buffer: gimgBuffer } = await ctx.gi.generateDynamicImg(items[num]);
833
+ const { pic: gimgPic, buffer: gimgBuffer } = await this.ctx.gi.generateDynamicImg(items[num]);
825
834
  // 赋值
826
835
  pic = gimgPic;
827
836
  buffer = gimgBuffer;
@@ -835,13 +844,13 @@ class ComRegister {
835
844
  if (e.message === '出现关键词,屏蔽该动态') {
836
845
  // 如果需要发送才发送
837
846
  if (this.config.filter.notify) {
838
- await this.sendMsg(ctx, sub.target, `${upName}发布了一条含有屏蔽关键字的动态`);
847
+ await this.sendMsg(sub.target, `${upName}发布了一条含有屏蔽关键字的动态`);
839
848
  }
840
849
  return;
841
850
  }
842
851
  if (e.message === '已屏蔽转发动态') {
843
852
  if (this.config.filter.notify) {
844
- await this.sendMsg(ctx, sub.target, `${upName}发布了一条转发动态,已屏蔽`);
853
+ await this.sendMsg(sub.target, `${upName}发布了一条转发动态,已屏蔽`);
845
854
  }
846
855
  return;
847
856
  }
@@ -849,7 +858,7 @@ class ComRegister {
849
858
  if (i === attempts - 1) {
850
859
  this.logger.error('dynamicDetect generateDynamicImg() 推送卡片发送失败,原因:' + e.message);
851
860
  // 发送私聊消息并重启服务
852
- return await this.sendPrivateMsgAndStopService(ctx);
861
+ return await this.sendPrivateMsgAndStopService();
853
862
  }
854
863
  }
855
864
  }
@@ -859,12 +868,12 @@ class ComRegister {
859
868
  if (pic) {
860
869
  this.logger.info('推送动态中,使用render模式');
861
870
  // pic存在,使用的是render模式
862
- await this.sendMsg(ctx, sub.target, pic + (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: dUrl }));
871
+ await this.sendMsg(sub.target, pic + (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: dUrl }));
863
872
  }
864
873
  else if (buffer) {
865
874
  this.logger.info('推送动态中,使用page模式');
866
875
  // pic不存在,说明使用的是page模式
867
- await this.sendMsg(ctx, sub.target, (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [koishi_1.h.image(buffer, 'image/png'), dUrl] }));
876
+ await this.sendMsg(sub.target, (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [koishi_1.h.image(buffer, 'image/png'), dUrl] }));
868
877
  }
869
878
  else {
870
879
  this.logger.info(items[num].modules.module_author.name + '发布了一条动态,但是推送失败');
@@ -878,7 +887,7 @@ class ComRegister {
878
887
  }
879
888
  };
880
889
  }
881
- debug_dynamicDetect(ctx) {
890
+ debug_dynamicDetect() {
882
891
  // 检测初始化变量
883
892
  let detectSetup = true;
884
893
  // 更新基线
@@ -899,7 +908,7 @@ class ComRegister {
899
908
  // 检测启动初始化
900
909
  if (detectSetup) {
901
910
  // 获取动态信息
902
- const data = await ctx.ba.getAllDynamic();
911
+ const data = await this.ctx.ba.getAllDynamic();
903
912
  // 判断获取动态信息是否成功
904
913
  if (data.code !== 0)
905
914
  return;
@@ -916,7 +925,7 @@ class ComRegister {
916
925
  let content;
917
926
  try {
918
927
  // 查询是否有新动态
919
- const data = await ctx.ba.hasNewDynamic(updateBaseline);
928
+ const data = await this.ctx.ba.hasNewDynamic(updateBaseline);
920
929
  updateNum = data.data.update_num;
921
930
  console.log(`获取是否有新动态:`);
922
931
  console.log(data);
@@ -924,7 +933,7 @@ class ComRegister {
924
933
  if (updateNum <= 0 || data.code !== 0)
925
934
  return;
926
935
  // 获取动态内容
927
- content = await ctx.ba.getAllDynamic(updateBaseline);
936
+ content = await this.ctx.ba.getAllDynamic(updateBaseline);
928
937
  console.log('获取动态内容:');
929
938
  console.log(content.data.items[0]);
930
939
  }
@@ -940,7 +949,7 @@ class ComRegister {
940
949
  // 发送私聊消息
941
950
  await this.sendPrivateMsg('账号未登录,插件已停止工作,请登录后,输入指令 sys start 启动插件');
942
951
  // 停止服务
943
- await ctx.sm.disposePlugin();
952
+ await this.ctx.sm.disposePlugin();
944
953
  // 结束循环
945
954
  break;
946
955
  }
@@ -950,7 +959,7 @@ class ComRegister {
950
959
  // 发送私聊消息
951
960
  await this.sendPrivateMsg('账号被风控,插件已停止工作,请确认风控解除后,输入指令 sys start 启动插件');
952
961
  // 停止服务
953
- await ctx.sm.disposePlugin();
962
+ await this.ctx.sm.disposePlugin();
954
963
  // 结束循环
955
964
  break;
956
965
  }
@@ -1018,7 +1027,7 @@ class ComRegister {
1018
1027
  // 获取动态推送图片
1019
1028
  try {
1020
1029
  // 渲染图片
1021
- const { pic: gimgPic, buffer: gimgBuffer } = await ctx.gi.generateDynamicImg(items[num]);
1030
+ const { pic: gimgPic, buffer: gimgBuffer } = await this.ctx.gi.generateDynamicImg(items[num]);
1022
1031
  // 赋值
1023
1032
  pic = gimgPic;
1024
1033
  buffer = gimgBuffer;
@@ -1032,13 +1041,13 @@ class ComRegister {
1032
1041
  if (e.message === '出现关键词,屏蔽该动态') {
1033
1042
  // 如果需要发送才发送
1034
1043
  if (this.config.filter.notify) {
1035
- await this.sendMsg(ctx, sub.target, `${upName}发布了一条含有屏蔽关键字的动态`);
1044
+ await this.sendMsg(sub.target, `${upName}发布了一条含有屏蔽关键字的动态`);
1036
1045
  }
1037
1046
  return;
1038
1047
  }
1039
1048
  if (e.message === '已屏蔽转发动态') {
1040
1049
  if (this.config.filter.notify) {
1041
- await this.sendMsg(ctx, sub.target, `${upName}发布了一条转发动态,已屏蔽`);
1050
+ await this.sendMsg(sub.target, `${upName}发布了一条转发动态,已屏蔽`);
1042
1051
  }
1043
1052
  return;
1044
1053
  }
@@ -1046,7 +1055,7 @@ class ComRegister {
1046
1055
  if (i === attempts - 1) {
1047
1056
  this.logger.error('dynamicDetect generateDynamicImg() 推送卡片发送失败,原因:' + e.message);
1048
1057
  // 发送私聊消息并重启服务
1049
- return await this.sendPrivateMsgAndStopService(ctx);
1058
+ return await this.sendPrivateMsgAndStopService();
1050
1059
  }
1051
1060
  }
1052
1061
  }
@@ -1056,12 +1065,12 @@ class ComRegister {
1056
1065
  if (pic) {
1057
1066
  this.logger.info('推送动态中,使用render模式');
1058
1067
  // pic存在,使用的是render模式
1059
- await this.sendMsg(ctx, sub.target, pic + (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: dUrl }));
1068
+ await this.sendMsg(sub.target, pic + (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: dUrl }));
1060
1069
  }
1061
1070
  else if (buffer) {
1062
1071
  this.logger.info('推送动态中,使用page模式');
1063
1072
  // pic不存在,说明使用的是page模式
1064
- await this.sendMsg(ctx, sub.target, (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [koishi_1.h.image(buffer, 'image/png'), dUrl] }));
1073
+ await this.sendMsg(sub.target, (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [koishi_1.h.image(buffer, 'image/png'), dUrl] }));
1065
1074
  }
1066
1075
  else {
1067
1076
  this.logger.info(items[num].modules.module_author.name + '发布了一条动态,但是推送失败');
@@ -1076,7 +1085,7 @@ class ComRegister {
1076
1085
  };
1077
1086
  }
1078
1087
  // 定义发送直播通知卡片方法
1079
- async sendLiveNotifyCard(ctx, info, liveType, liveNotifyMsg) {
1088
+ async sendLiveNotifyCard(info, liveType, liveNotifyMsg) {
1080
1089
  // 定义变量
1081
1090
  let pic;
1082
1091
  let buffer;
@@ -1085,7 +1094,7 @@ class ComRegister {
1085
1094
  for (let i = 0; i < attempts; i++) {
1086
1095
  try {
1087
1096
  // 获取直播通知卡片
1088
- const { pic: picv, buffer: bufferv } = await ctx.gi.generateLiveImg(info.data, info.username, info.userface, liveType);
1097
+ const { pic: picv, buffer: bufferv } = await this.ctx.gi.generateLiveImg(info.data, info.username, info.userface, liveType);
1089
1098
  // 赋值
1090
1099
  pic = picv;
1091
1100
  buffer = bufferv;
@@ -1096,7 +1105,7 @@ class ComRegister {
1096
1105
  if (i === attempts - 1) { // 已尝试三次
1097
1106
  this.logger.error('liveDetect generateLiveImg() 推送卡片生成失败,原因:' + e.message);
1098
1107
  // 发送私聊消息并重启服务
1099
- return await this.sendPrivateMsgAndStopService(ctx);
1108
+ return await this.sendPrivateMsgAndStopService();
1100
1109
  }
1101
1110
  }
1102
1111
  }
@@ -1105,33 +1114,33 @@ class ComRegister {
1105
1114
  if (pic) {
1106
1115
  // 只有在开播时才艾特全体成员
1107
1116
  if (liveType === LiveType.StartBroadcasting) {
1108
- return await this.sendMsg(ctx, info.target, pic + (liveNotifyMsg ?? ''), true);
1117
+ return await this.sendMsg(info.target, pic + (liveNotifyMsg ?? ''), true);
1109
1118
  }
1110
1119
  // 正常不需要艾特全体成员
1111
- return await this.sendMsg(ctx, info.target, pic + (liveNotifyMsg ?? ''));
1120
+ return await this.sendMsg(info.target, pic + (liveNotifyMsg ?? ''));
1112
1121
  }
1113
1122
  // pic不存在,说明使用的是page模式
1114
1123
  const msg = (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [koishi_1.h.image(buffer, 'image/png'), liveNotifyMsg || ''] });
1115
1124
  // 只有在开播时才艾特全体成员
1116
1125
  if (liveType === LiveType.StartBroadcasting) {
1117
- return await this.sendMsg(ctx, info.target, msg, true);
1126
+ return await this.sendMsg(info.target, msg, true);
1118
1127
  }
1119
1128
  // 正常不需要艾特全体成员
1120
- return await this.sendMsg(ctx, info.target, msg);
1129
+ return await this.sendMsg(info.target, msg);
1121
1130
  }
1122
1131
  // 定义获取主播信息方法
1123
- async useMasterInfo(ctx, uid) {
1124
- const { data: { info } } = await ctx.ba.getMasterInfo(uid);
1125
- return { username: info.name, userface: info.face };
1132
+ async useMasterInfo(uid) {
1133
+ const { data } = await this.ctx.ba.getMasterInfo(uid);
1134
+ return { username: data.info.uname, userface: data.info.face, roomId: data.room_id };
1126
1135
  }
1127
- async useLiveRoomInfo(ctx, roomId) {
1136
+ async useLiveRoomInfo(roomId) {
1128
1137
  // 发送请求获取直播间信息
1129
1138
  let content;
1130
1139
  const attempts = 3;
1131
1140
  for (let i = 0; i < attempts; i++) {
1132
1141
  try {
1133
1142
  // 发送请求获取room信息
1134
- content = await ctx.ba.getLiveRoomInfo(roomId);
1143
+ content = await this.ctx.ba.getLiveRoomInfo(roomId);
1135
1144
  // 成功则跳出循环
1136
1145
  break;
1137
1146
  }
@@ -1139,164 +1148,13 @@ class ComRegister {
1139
1148
  this.logger.error('liveDetect getLiveRoomInfo 发生了错误,错误为:' + e.message);
1140
1149
  if (i === attempts - 1) { // 已尝试三次
1141
1150
  // 发送私聊消息并重启服务
1142
- return await this.sendPrivateMsgAndStopService(ctx);
1151
+ return await this.sendPrivateMsgAndStopService();
1143
1152
  }
1144
1153
  }
1145
1154
  }
1146
1155
  return content.data;
1147
1156
  }
1148
- /* liveDetect(
1149
- ctx: Context,
1150
- roomId: string,
1151
- target: Target
1152
- ) {
1153
- let firstSubscription: boolean = true;
1154
- let timer: number = 0;
1155
- let open: boolean = false;
1156
- let liveTime: string;
1157
- let username: string
1158
- let userface: string
1159
- // 相当于锁的作用,防止上一个循环没处理完
1160
- let flag: boolean = true
1161
-
1162
- return async () => {
1163
- // 如果flag为false则说明前面的代码还未执行完,则直接返回
1164
- if (!flag) return
1165
- flag = false
1166
- // 无论是否执行成功都要释放锁
1167
- try {
1168
- // 发送请求检测直播状态
1169
- let content: any
1170
- const attempts = 3
1171
- for (let i = 0; i < attempts; i++) {
1172
- try {
1173
- // 发送请求获取room信息
1174
- content = await ctx.ba.getLiveRoomInfo(roomId)
1175
- // 成功则跳出循环
1176
- break
1177
- } catch (e) {
1178
- this.logger.error('liveDetect getLiveRoomInfo 发生了错误,错误为:' + e.message)
1179
- if (i === attempts - 1) { // 已尝试三次
1180
- // 发送私聊消息并重启服务
1181
- return await this.sendPrivateMsgAndStopService(ctx)
1182
- }
1183
- }
1184
- }
1185
- const { data } = content
1186
- // 判断是否是第一次订阅
1187
- if (firstSubscription) {
1188
- firstSubscription = false
1189
- // 获取主播信息
1190
- const attempts = 3
1191
- for (let i = 0; i < attempts; i++) {
1192
- try {
1193
- // 发送请求获取主播信息
1194
- await useMasterInfo(data.uid)
1195
- // 成功则跳出循环
1196
- break
1197
- } catch (e) {
1198
- this.logger.error('liveDetect getMasterInfo() 发生了错误,错误为:' + e.message)
1199
- if (i === attempts - 1) { // 已尝试三次
1200
- // 发送私聊消息并重启服务
1201
- return await this.sendPrivateMsgAndStopService(ctx)
1202
- }
1203
- }
1204
- }
1205
- // 判断直播状态
1206
- if (data.live_status === 1) { // 当前正在直播
1207
- // 设置开播时间
1208
- liveTime = data.live_time
1209
- // 设置直播中消息
1210
- const liveMsg = this.config.customLive ? this.config.customLive
1211
- .replace('-name', username)
1212
- .replace('-time', await ctx.gi.getTimeDifference(liveTime))
1213
- .replace('-link', `https://live.bilibili.com/${data.short_id === 0 ? data.room_id : data.short_id}`) : null
1214
- // 发送直播通知卡片
1215
- if (this.config.restartPush) sendLiveNotifyCard(data, LiveType.LiveBroadcast, liveMsg)
1216
- // 改变开播状态
1217
- open = true
1218
- } // 未开播,直接返回
1219
- return
1220
- }
1221
- // 检查直播状态
1222
- switch (data.live_status) {
1223
- case 0:
1224
- case 2: { // 状态 0 和 2 说明未开播
1225
- if (open) { // 之前开播,现在下播了
1226
- // 更改直播状态
1227
- open = false
1228
- // 下播了将定时器清零
1229
- timer = 0
1230
- // 定义下播通知消息
1231
- const liveEndMsg = this.config.customLiveEnd ? this.config.customLiveEnd
1232
- .replace('-name', username)
1233
- .replace('-time', await ctx.gi.getTimeDifference(liveTime)) : null
1234
- // 更改直播时长
1235
- data.live_time = liveTime
1236
- // 推送下播通知
1237
- await sendLiveNotifyCard(data, LiveType.StopBroadcast, liveEndMsg)
1238
- }
1239
- // 未进循环,还未开播,继续循环
1240
- break
1241
- }
1242
- case 1: {
1243
- if (!open) { // 之前未开播,现在开播了
1244
- // 更改直播状态
1245
- open = true
1246
- // 设置开播时间
1247
- liveTime = data.live_time
1248
- // 获取主播信息
1249
- const attempts = 3
1250
- for (let i = 0; i < attempts; i++) {
1251
- try {
1252
- // 主播信息不会变,开播时刷新一次即可
1253
- // 发送请求获取主播信息
1254
- await useMasterInfo(data.uid)
1255
- // 成功则跳出循环
1256
- break
1257
- } catch (e) {
1258
- this.logger.error('liveDetect open getMasterInfo() 发生了错误,错误为:' + e.message)
1259
- if (i === attempts - 1) { // 已尝试三次
1260
- // 发送私聊消息并重启服务
1261
- return await this.sendPrivateMsgAndStopService(ctx)
1262
- }
1263
- }
1264
- }
1265
- // 定义开播通知语
1266
- const liveStartMsg = this.config.customLiveStart ? this.config.customLiveStart
1267
- .replace('-name', username)
1268
- .replace('-time', await ctx.gi.getTimeDifference(liveTime))
1269
- .replace('-link', `https://live.bilibili.com/${data.short_id === 0 ? data.room_id : data.short_id}`) : null
1270
- // 发送消息
1271
- await sendLiveNotifyCard(data, LiveType.StartBroadcasting, liveStartMsg)
1272
- } else { // 还在直播
1273
- if (this.config.pushTime > 0) {
1274
- timer++
1275
- // 开始记录时间
1276
- if (timer >= (6 * 60 * this.config.pushTime)) { // 到时间推送直播消息
1277
- // 到时间重新计时
1278
- timer = 0
1279
- // 定义直播中通知消息
1280
- const liveMsg = this.config.customLive ? this.config.customLive
1281
- .replace('-name', username)
1282
- .replace('-time', await ctx.gi.getTimeDifference(liveTime))
1283
- .replace('-link', `https://live.bilibili.com/${data.short_id === 0 ? data.room_id : data.short_id}`) : null
1284
- // 发送直播通知卡片
1285
- sendLiveNotifyCard(data, LiveType.LiveBroadcast, liveMsg)
1286
- }
1287
- }
1288
- // 否则继续循环
1289
- }
1290
- }
1291
- }
1292
- }
1293
- finally {
1294
- // 执行完方法体不论如何都把flag设置为true
1295
- flag = true
1296
- }
1297
- }
1298
- } */
1299
- async liveDetectWithAPI(ctx) {
1157
+ async liveDetectWithAPI() {
1300
1158
  // 定义变量:第一次订阅
1301
1159
  let firstSubscription = true;
1302
1160
  // 定义变量:timer计时器
@@ -1320,7 +1178,7 @@ class ComRegister {
1320
1178
  for (let i = 0; i < attempts; i++) {
1321
1179
  try {
1322
1180
  // 发送请求获取room信息
1323
- content = await ctx.ba.getLiveRoomInfo(roomId);
1181
+ content = await this.ctx.ba.getLiveRoomInfo(roomId);
1324
1182
  // 成功则跳出循环
1325
1183
  break;
1326
1184
  }
@@ -1328,7 +1186,7 @@ class ComRegister {
1328
1186
  this.logger.error('liveDetect getLiveRoomInfo 发生了错误,错误为:' + e.message);
1329
1187
  if (i === attempts - 1) { // 已尝试三次
1330
1188
  // 发送私聊消息并重启服务
1331
- return await this.sendPrivateMsgAndStopService(ctx);
1189
+ return await this.sendPrivateMsgAndStopService();
1332
1190
  }
1333
1191
  }
1334
1192
  }
@@ -1343,7 +1201,7 @@ class ComRegister {
1343
1201
  flag = false;
1344
1202
  try {
1345
1203
  // 获取正在直播对象
1346
- const liveUsers = await ctx.ba.getTheUserWhoIsLiveStreaming();
1204
+ const liveUsers = await this.ctx.ba.getTheUserWhoIsLiveStreaming();
1347
1205
  // 判断是否是第一次订阅
1348
1206
  if (firstSubscription) {
1349
1207
  // 将第一次订阅置为false
@@ -1361,11 +1219,11 @@ class ComRegister {
1361
1219
  // 设置直播中消息
1362
1220
  const liveMsg = this.config.customLive ? this.config.customLive
1363
1221
  .replace('-name', item.uname)
1364
- .replace('-time', await ctx.gi.getTimeDifference(liveRecord[item.mid].liveTime))
1222
+ .replace('-time', await this.ctx.gi.getTimeDifference(liveRecord[item.mid].liveTime))
1365
1223
  .replace('-link', `https://live.bilibili.com/${data.short_id === 0 ? data.room_id : data.short_id}`) : null;
1366
1224
  // 发送直播通知卡片
1367
1225
  if (this.config.restartPush)
1368
- this.sendLiveNotifyCard(ctx, {
1226
+ this.sendLiveNotifyCard({
1369
1227
  username: item.uname,
1370
1228
  userface: item.face,
1371
1229
  target: liveRecord[item.mid].target,
@@ -1418,6 +1276,7 @@ class ComRegister {
1418
1276
  delete liveRecord[subUID];
1419
1277
  }
1420
1278
  }
1279
+ // 数量没有差异,则不进行其他操作
1421
1280
  // 遍历liveUsers
1422
1281
  liveUsers.items.forEach(async (item) => {
1423
1282
  // 判断是否有正在直播的订阅对象
@@ -1432,16 +1291,15 @@ class ComRegister {
1432
1291
  // 定义开播通知语
1433
1292
  const liveStartMsg = this.config.customLiveStart ? this.config.customLiveStart
1434
1293
  .replace('-name', item.uname)
1435
- .replace('-time', await ctx.gi.getTimeDifference(liveRecord[item.mid].liveTime))
1294
+ .replace('-time', await this.ctx.gi.getTimeDifference(liveRecord[item.mid].liveTime))
1436
1295
  .replace('-link', `https://live.bilibili.com/${data.short_id === 0 ? data.room_id : data.short_id}`) : null;
1437
1296
  // 发送直播通知卡片
1438
- if (this.config.restartPush)
1439
- this.sendLiveNotifyCard(ctx, {
1440
- username: item.uname,
1441
- userface: item.face,
1442
- target: liveRecord[item.mid].target,
1443
- data
1444
- }, LiveType.LiveBroadcast, liveStartMsg);
1297
+ await this.sendLiveNotifyCard({
1298
+ username: item.uname,
1299
+ userface: item.face,
1300
+ target: liveRecord[item.mid].target,
1301
+ data
1302
+ }, LiveType.LiveBroadcast, liveStartMsg);
1445
1303
  // 改变开播状态
1446
1304
  liveRecord[item.mid].liveStatus = 1;
1447
1305
  // 结束
@@ -1457,10 +1315,10 @@ class ComRegister {
1457
1315
  // 定义直播中通知消息
1458
1316
  const liveMsg = this.config.customLive ? this.config.customLive
1459
1317
  .replace('-name', item.uname)
1460
- .replace('-time', await ctx.gi.getTimeDifference(liveRecord[item.mid].liveTime))
1318
+ .replace('-time', await this.ctx.gi.getTimeDifference(liveRecord[item.mid].liveTime))
1461
1319
  .replace('-link', `https://live.bilibili.com/${data.short_id === 0 ? data.room_id : data.short_id}`) : null;
1462
1320
  // 发送直播通知卡片
1463
- this.sendLiveNotifyCard(ctx, {
1321
+ this.sendLiveNotifyCard({
1464
1322
  username: item.uname,
1465
1323
  userface: item.face,
1466
1324
  target: liveRecord[item.mid].target,
@@ -1472,6 +1330,27 @@ class ComRegister {
1472
1330
  }
1473
1331
  }
1474
1332
  });
1333
+ // 找出liveRecord中liveStatus为1但liveUsers中没有的元素
1334
+ const extraInLiveRecord = currentLiveRecordKeys.filter(key => !liveUsers.items.some(item => item.mid === Number(key)));
1335
+ // 遍历 extraInLiveRecord
1336
+ for (const subUID of extraInLiveRecord) { // 下播的主播
1337
+ // 获取主播信息
1338
+ const masterInfo = await this.useMasterInfo(subUID);
1339
+ // 获取直播间消息
1340
+ const liveRoomInfo = await this.useLiveRoomInfo(masterInfo.roomId.toString());
1341
+ // 定义下播播通知语
1342
+ const liveEndMsg = this.config.customLiveEnd ? this.config.customLiveEnd
1343
+ .replace('-name', masterInfo.username)
1344
+ .replace('-time', await this.ctx.gi.getTimeDifference(liveRecord[subUID].liveTime))
1345
+ .replace('-link', `https://live.bilibili.com/${liveRoomInfo.short_id === 0 ? liveRoomInfo.room_id : liveRoomInfo.short_id}`) : null;
1346
+ // 发送下播通知
1347
+ this.sendLiveNotifyCard({
1348
+ username: masterInfo.username,
1349
+ userface: masterInfo.userface,
1350
+ target: liveRecord[subUID].target,
1351
+ data: liveRoomInfo
1352
+ }, LiveType.StopBroadcast, liveEndMsg);
1353
+ }
1475
1354
  }
1476
1355
  finally {
1477
1356
  // 执行完方法体不论如何都把flag设置为true
@@ -1479,7 +1358,7 @@ class ComRegister {
1479
1358
  }
1480
1359
  };
1481
1360
  }
1482
- liveDetectWithListener(ctx, roomId, target) {
1361
+ async liveDetectWithListener(roomId, target) {
1483
1362
  // 定义开播时间
1484
1363
  let liveTime;
1485
1364
  // 定义定时推送定时器
@@ -1487,35 +1366,48 @@ class ComRegister {
1487
1366
  // 定义弹幕存放数组
1488
1367
  const currentLiveDanmakuArr = [];
1489
1368
  const temporaryLiveDanmakuArr = [];
1369
+ // 处理target
1370
+ // 找到频道/群组对应的
1371
+ const newTarget = target.map(channel => {
1372
+ const liveDanmakuArr = channel.channelIdArr.filter(channelId => channelId.liveDanmaku);
1373
+ return {
1374
+ channelIdArr: liveDanmakuArr,
1375
+ platform: channel.platform
1376
+ };
1377
+ });
1490
1378
  // 定义定时推送函数
1491
- const pushAtTime = async () => {
1379
+ const pushAtTimeFunc = async () => {
1492
1380
  // 获取直播间信息
1493
- const liveRoomInfo = await this.useLiveRoomInfo(ctx, roomId);
1381
+ const liveRoomInfo = await this.useLiveRoomInfo(roomId);
1494
1382
  // 获取主播信息
1495
- const masterInfo = await this.useMasterInfo(ctx, liveRoomInfo.uid);
1383
+ const masterInfo = await this.useMasterInfo(liveRoomInfo.uid);
1496
1384
  // 定义直播中通知消息
1497
1385
  const liveMsg = this.config.customLive ? this.config.customLive
1498
1386
  .replace('-name', masterInfo.username)
1499
- .replace('-time', await ctx.gi.getTimeDifference(liveTime))
1387
+ .replace('-time', await this.ctx.gi.getTimeDifference(liveTime))
1500
1388
  .replace('-link', `https://live.bilibili.com/${liveRoomInfo.short_id === 0 ? liveRoomInfo.room_id : liveRoomInfo.short_id}`) : null;
1501
1389
  // 发送直播通知卡片
1502
- await this.sendLiveNotifyCard(liveRoomInfo, {
1390
+ await this.sendLiveNotifyCard({
1503
1391
  username: masterInfo.username,
1504
1392
  userface: masterInfo.userface,
1505
- target,
1393
+ target: newTarget,
1506
1394
  data: liveRoomInfo
1507
1395
  }, LiveType.LiveBroadcast, liveMsg);
1508
1396
  };
1509
- // 定义10秒推送函数
1510
- const pushOnceEveryTenS = () => {
1397
+ // 定义弹幕推送函数
1398
+ const danmakuPushFunc = () => {
1511
1399
  // 判断数组是否有内容
1512
1400
  if (temporaryLiveDanmakuArr.length > 0) {
1513
1401
  // 发送消息
1514
- this.sendMsg(ctx, target, temporaryLiveDanmakuArr.join('\n'));
1402
+ this.sendMsg(newTarget, temporaryLiveDanmakuArr.join('\n'));
1515
1403
  // 将临时消息数组清空
1516
1404
  temporaryLiveDanmakuArr.length = 0;
1517
1405
  }
1518
1406
  };
1407
+ // 获取直播间信息
1408
+ const liveRoomInfo = await this.useLiveRoomInfo(roomId);
1409
+ // 获取主播信息
1410
+ const masterInfo = await this.useMasterInfo(liveRoomInfo.uid);
1519
1411
  // 构建消息处理函数
1520
1412
  const handler = {
1521
1413
  onOpen: () => {
@@ -1526,62 +1418,89 @@ class ComRegister {
1526
1418
  },
1527
1419
  onIncomeDanmu: ({ body }) => {
1528
1420
  // 处理消息,只需要UP主名字和消息内容
1529
- const content = `${body.user.uname}:${body.content}`;
1421
+ const content = `【${masterInfo.username}的直播间】${body.user.uname}:${body.content}`;
1530
1422
  // 保存消息到数组
1531
- currentLiveDanmakuArr.push(content);
1423
+ currentLiveDanmakuArr.push(body.content);
1532
1424
  temporaryLiveDanmakuArr.push(content);
1533
1425
  },
1534
- onIncomeSuperChat: (msg) => {
1535
- console.log(msg.id, msg.body);
1426
+ onIncomeSuperChat: ({ body }) => {
1427
+ // 处理SC消息
1428
+ const content = `【${masterInfo.username}的直播间】${body.user.uname}发送了一条SC:${body.content}`;
1429
+ // 保存消息到数组
1430
+ currentLiveDanmakuArr.push(body.content);
1431
+ temporaryLiveDanmakuArr.push(content);
1432
+ },
1433
+ onGuardBuy: ({ body }) => {
1434
+ const content = `【${masterInfo.username}的直播间】${body.user.uname}加入了大航海(${body.gift_name})`;
1435
+ // 保存消息到数组
1436
+ currentLiveDanmakuArr.push(content);
1437
+ temporaryLiveDanmakuArr.push(content);
1536
1438
  },
1537
1439
  onLiveStart: async () => {
1538
1440
  // 获取直播间信息
1539
- const liveRoomInfo = await this.useLiveRoomInfo(ctx, roomId);
1441
+ const liveRoomInfo = await this.useLiveRoomInfo(roomId);
1540
1442
  // 获取主播信息
1541
- const masterInfo = await this.useMasterInfo(ctx, liveRoomInfo.uid);
1443
+ const masterInfo = await this.useMasterInfo(liveRoomInfo.uid);
1542
1444
  // 定义下播通知消息
1543
1445
  const liveEndMsg = this.config.customLiveEnd ? this.config.customLiveEnd
1544
1446
  .replace('-name', masterInfo.username)
1545
- .replace('-time', await ctx.gi.getTimeDifference(liveTime)) : null;
1447
+ .replace('-time', await this.ctx.gi.getTimeDifference(liveTime)) : null;
1546
1448
  // 更改直播时长
1547
1449
  liveRoomInfo.live_time = liveTime;
1548
1450
  // 推送下播通知
1549
- await this.sendLiveNotifyCard(liveRoomInfo, {
1451
+ await this.sendLiveNotifyCard({
1550
1452
  username: masterInfo.username,
1551
1453
  userface: masterInfo.userface,
1552
- target,
1454
+ target: newTarget,
1553
1455
  data: liveRoomInfo
1554
1456
  }, LiveType.StopBroadcast, liveEndMsg);
1555
- // 关闭定时器
1556
- pushAtTimeTimer();
1557
- // 定时器变量置空
1558
- pushAtTimeTimer = null;
1559
1457
  },
1560
1458
  onLiveEnd: async () => {
1561
1459
  // 获取直播间消息
1562
- const liveRoomInfo = await this.useLiveRoomInfo(ctx, roomId);
1460
+ const liveRoomInfo = await this.useLiveRoomInfo(roomId);
1563
1461
  // 获取主播信息
1564
- const masterInfo = await this.useMasterInfo(ctx, liveRoomInfo.uid);
1565
- // 设置开播时间
1566
- liveTime = liveRoomInfo.live_time;
1567
- // 开启推送定时器
1568
- pushAtTimeTimer = ctx.setInterval(pushAtTime, 3600 * 1000);
1569
- // 定义开播通知语
1570
- const liveStartMsg = this.config.customLiveStart ? this.config.customLiveStart
1462
+ const masterInfo = await this.useMasterInfo(liveRoomInfo.uid);
1463
+ // 关闭定时推送定时器
1464
+ pushAtTimeTimer();
1465
+ // 将推送定时器变量置空
1466
+ pushAtTimeTimer = null;
1467
+ // 定义下播播通知语
1468
+ const liveEndMsg = this.config.customLiveEnd ? this.config.customLiveEnd
1571
1469
  .replace('-name', masterInfo.username)
1572
- .replace('-time', await ctx.gi.getTimeDifference(liveTime))
1470
+ .replace('-time', await this.ctx.gi.getTimeDifference(liveTime))
1573
1471
  .replace('-link', `https://live.bilibili.com/${liveRoomInfo.short_id === 0 ? liveRoomInfo.room_id : liveRoomInfo.short_id}`) : null;
1574
1472
  // 推送通知卡片
1575
- await this.sendLiveNotifyCard(ctx, {
1473
+ await this.sendLiveNotifyCard({
1576
1474
  username: masterInfo.username,
1577
1475
  userface: masterInfo.userface,
1578
- target,
1476
+ target: newTarget,
1579
1477
  data: liveRoomInfo
1580
- }, LiveType.StartBroadcasting, liveStartMsg);
1478
+ }, LiveType.StartBroadcasting, liveEndMsg);
1581
1479
  }
1582
1480
  };
1583
1481
  // 启动直播间弹幕监测
1584
- ctx.bl.startLiveRoomListener(roomId, handler, pushOnceEveryTenS);
1482
+ this.ctx.bl.startLiveRoomListener(roomId, handler, danmakuPushFunc);
1483
+ // 判断直播状态
1484
+ if (liveRoomInfo.live_status === 1) {
1485
+ // 设置开播时间
1486
+ liveTime = liveRoomInfo.live_time;
1487
+ // 定义直播中通知消息
1488
+ const liveMsg = this.config.customLive ? this.config.customLive
1489
+ .replace('-name', masterInfo.username)
1490
+ .replace('-time', await this.ctx.gi.getTimeDifference(liveTime))
1491
+ .replace('-link', `https://live.bilibili.com/${liveRoomInfo.short_id === 0 ? liveRoomInfo.room_id : liveRoomInfo.short_id}`) : null;
1492
+ // 发送直播通知卡片
1493
+ if (this.config.restartPush) {
1494
+ await this.sendLiveNotifyCard({
1495
+ username: masterInfo.username,
1496
+ userface: masterInfo.userface,
1497
+ target: newTarget,
1498
+ data: liveRoomInfo
1499
+ }, LiveType.LiveBroadcast, liveMsg);
1500
+ }
1501
+ // 正在直播,开启定时器
1502
+ pushAtTimeTimer = this.ctx.setInterval(pushAtTimeFunc, this.config.pushTime * 1000 * 60 * 60);
1503
+ }
1585
1504
  }
1586
1505
  subShow() {
1587
1506
  // 在控制台中显示订阅对象
@@ -1621,7 +1540,7 @@ class ComRegister {
1621
1540
  // 只订阅动态
1622
1541
  return [false, true];
1623
1542
  }
1624
- updateSubNotifier(ctx) {
1543
+ updateSubNotifier() {
1625
1544
  // 更新控制台提示
1626
1545
  if (this.subNotifier)
1627
1546
  this.subNotifier.dispose();
@@ -1640,13 +1559,13 @@ class ComRegister {
1640
1559
  table = (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("p", { children: "\u5F53\u524D\u8BA2\u9605\u5BF9\u8C61\uFF1A" }), (0, jsx_runtime_1.jsx)("ul", { children: subTableArray.map(str => ((0, jsx_runtime_1.jsx)("li", { children: str }))) })] });
1641
1560
  }
1642
1561
  // 设置更新后的提示
1643
- this.subNotifier = ctx.notifier.create(table);
1562
+ this.subNotifier = this.ctx.notifier.create(table);
1644
1563
  }
1645
- async checkIfLoginInfoIsLoaded(ctx) {
1564
+ async checkIfLoginInfoIsLoaded() {
1646
1565
  return new Promise(resolve => {
1647
1566
  const check = () => {
1648
- if (!ctx.ba.getLoginInfoIsLoaded()) {
1649
- ctx.setTimeout(check, 500);
1567
+ if (!this.ctx.ba.getLoginInfoIsLoaded()) {
1568
+ this.ctx.setTimeout(check, 500);
1650
1569
  }
1651
1570
  else {
1652
1571
  resolve('success');
@@ -1655,17 +1574,17 @@ class ComRegister {
1655
1574
  check();
1656
1575
  });
1657
1576
  }
1658
- async subUserInBili(ctx, mid) {
1577
+ async subUserInBili(mid) {
1659
1578
  // 获取关注分组信息
1660
1579
  const checkGroupIsReady = async () => {
1661
1580
  // 判断是否有数据
1662
1581
  if (this.loginDBData.dynamic_group_id === '' || this.loginDBData.dynamic_group_id === null) {
1663
1582
  // 没有数据,没有创建分组,尝试创建分组
1664
- const createGroupData = await ctx.ba.createGroup("订阅");
1583
+ const createGroupData = await this.ctx.ba.createGroup("订阅");
1665
1584
  // 如果分组已创建,则获取分组id
1666
1585
  if (createGroupData.code === 22106) {
1667
1586
  // 分组已存在,拿到之前的分组id
1668
- const allGroupData = await ctx.ba.getAllGroup();
1587
+ const allGroupData = await this.ctx.ba.getAllGroup();
1669
1588
  // 遍历所有分组
1670
1589
  for (const group of allGroupData.data) {
1671
1590
  // 找到订阅分组
@@ -1683,7 +1602,7 @@ class ComRegister {
1683
1602
  return false;
1684
1603
  }
1685
1604
  // 创建成功,保存到数据库
1686
- ctx.database.set('loginBili', 1, { dynamic_group_id: this.loginDBData.dynamic_group_id });
1605
+ this.ctx.database.set('loginBili', 1, { dynamic_group_id: this.loginDBData.dynamic_group_id });
1687
1606
  // 创建成功
1688
1607
  return true;
1689
1608
  }
@@ -1697,7 +1616,7 @@ class ComRegister {
1697
1616
  return { flag: false, msg: '创建分组失败,请尝试重启插件' };
1698
1617
  }
1699
1618
  // 获取分组明细
1700
- const relationGroupDetailData = await ctx.ba.getRelationGroupDetail(this.loginDBData.dynamic_group_id);
1619
+ const relationGroupDetailData = await this.ctx.ba.getRelationGroupDetail(this.loginDBData.dynamic_group_id);
1701
1620
  // 判断分组信息是否获取成功
1702
1621
  if (relationGroupDetailData.code !== 0) {
1703
1622
  if (relationGroupDetailData.code === 22104) {
@@ -1722,7 +1641,7 @@ class ComRegister {
1722
1641
  }
1723
1642
  });
1724
1643
  // 订阅对象
1725
- const subUserData = await ctx.ba.follow(mid);
1644
+ const subUserData = await this.ctx.ba.follow(mid);
1726
1645
  // 判断是否订阅成功
1727
1646
  switch (subUserData.code) {
1728
1647
  case -101: return { flag: false, msg: '账号未登录,请使用指令bili login登录后再进行订阅操作' };
@@ -1735,7 +1654,7 @@ class ComRegister {
1735
1654
  case 22014: // 已关注订阅对象 无需再次关注
1736
1655
  case 0: { // 执行订阅成功
1737
1656
  // 把订阅对象添加到分组中
1738
- const copyUserToGroupData = await ctx.ba.copyUserToGroup(mid, this.loginDBData.dynamic_group_id);
1657
+ const copyUserToGroupData = await this.ctx.ba.copyUserToGroup(mid, this.loginDBData.dynamic_group_id);
1739
1658
  // 判断是否添加成功
1740
1659
  if (copyUserToGroupData.code !== 0) {
1741
1660
  // 添加失败
@@ -1746,12 +1665,10 @@ class ComRegister {
1746
1665
  // 订阅成功
1747
1666
  return { flag: true, msg: '用户订阅成功' };
1748
1667
  }
1749
- async loadSubFromConfig(ctx, subs) {
1668
+ async loadSubFromConfig(subs) {
1750
1669
  for (const sub of subs) {
1751
1670
  // 定义Data
1752
1671
  let data;
1753
- // 定义直播销毁函数
1754
- let liveDispose;
1755
1672
  // 判断是否需要订阅直播
1756
1673
  if (sub.live) {
1757
1674
  // 获取用户信息
@@ -1761,7 +1678,7 @@ class ComRegister {
1761
1678
  for (let i = 0; i < attempts; i++) {
1762
1679
  try {
1763
1680
  // 获取用户信息
1764
- content = await ctx.ba.getUserInfo(sub.uid);
1681
+ content = await this.ctx.ba.getUserInfo(sub.uid);
1765
1682
  // 成功则跳出循环
1766
1683
  break;
1767
1684
  }
@@ -1769,7 +1686,7 @@ class ComRegister {
1769
1686
  this.logger.error('getSubFromDatabase() getUserInfo() 发生了错误,错误为:' + e.message);
1770
1687
  if (i === attempts - 1) { // 已尝试三次
1771
1688
  // 发送私聊消息并重启服务
1772
- return await this.sendPrivateMsgAndStopService(ctx);
1689
+ return await this.sendPrivateMsgAndStopService();
1773
1690
  }
1774
1691
  }
1775
1692
  }
@@ -1785,11 +1702,11 @@ class ComRegister {
1785
1702
  // 判断是否订阅直播
1786
1703
  if (sub.live) {
1787
1704
  // 订阅直播
1788
- this.liveDetectWithListener(ctx, data.live_room.roomid, sub.target);
1705
+ this.liveDetectWithListener(data.live_room.roomid, sub.target);
1789
1706
  }
1790
1707
  }
1791
1708
  // 在B站中订阅该对象
1792
- const subInfo = await this.subUserInBili(ctx, sub.uid);
1709
+ const subInfo = await this.subUserInBili(sub.uid);
1793
1710
  // 判断订阅是否成功
1794
1711
  if (!subInfo.flag)
1795
1712
  this.logger.warn(subInfo.msg);
@@ -1801,16 +1718,15 @@ class ComRegister {
1801
1718
  target: sub.target,
1802
1719
  platform: '',
1803
1720
  live: sub.live,
1804
- dynamic: sub.dynamic,
1805
- liveDispose
1721
+ dynamic: sub.dynamic
1806
1722
  });
1807
1723
  }
1808
1724
  }
1809
- async loadSubFromDatabase(ctx) {
1725
+ async loadSubFromDatabase() {
1810
1726
  // 判断登录信息是否已加载完毕
1811
- await this.checkIfLoginInfoIsLoaded(ctx);
1727
+ await this.checkIfLoginInfoIsLoaded();
1812
1728
  // 如果未登录,则直接返回
1813
- if (!(await this.checkIfIsLogin(ctx))) {
1729
+ if (!(await this.checkIfIsLogin())) {
1814
1730
  // log
1815
1731
  this.logger.info(`账号未登录,请登录`);
1816
1732
  return;
@@ -1819,7 +1735,7 @@ class ComRegister {
1819
1735
  if (this.subManager.length !== 0)
1820
1736
  return;
1821
1737
  // 从数据库中获取数据
1822
- const subData = await ctx.database.get('bilibili', { id: { $gt: 0 } });
1738
+ const subData = await this.ctx.database.get('bilibili', { id: { $gt: 0 } });
1823
1739
  // 定义变量:订阅直播数
1824
1740
  let liveSubNum = 0;
1825
1741
  // 循环遍历
@@ -1827,14 +1743,14 @@ class ComRegister {
1827
1743
  // 判断是否存在没有任何订阅的数据
1828
1744
  if (!sub.dynamic && !sub.live) { // 存在未订阅任何项目的数据
1829
1745
  // 删除该条数据
1830
- ctx.database.remove('bilibili', { id: sub.id });
1746
+ this.ctx.database.remove('bilibili', { id: sub.id });
1831
1747
  // log
1832
1748
  this.logger.warn(`UID:${sub.uid} 该条数据没有任何订阅数据,自动取消订阅`);
1833
1749
  // 跳过下面的步骤
1834
1750
  continue;
1835
1751
  }
1836
1752
  // 判断用户是否在B站中订阅了
1837
- const subUserData = await this.subUserInBili(ctx, sub.uid);
1753
+ const subUserData = await this.subUserInBili(sub.uid);
1838
1754
  // 判断是否订阅
1839
1755
  if (!subUserData.flag) {
1840
1756
  // log
@@ -1842,7 +1758,7 @@ class ComRegister {
1842
1758
  // 发送私聊消息
1843
1759
  await this.sendPrivateMsg(`UID:${sub.uid} ${subUserData.msg},自动取消订阅`);
1844
1760
  // 删除该条数据
1845
- await ctx.database.remove('bilibili', { id: sub.id });
1761
+ await this.ctx.database.remove('bilibili', { id: sub.id });
1846
1762
  // 跳过下面的步骤
1847
1763
  continue;
1848
1764
  }
@@ -1855,7 +1771,7 @@ class ComRegister {
1855
1771
  for (let i = 0; i < attempts; i++) {
1856
1772
  try {
1857
1773
  // 获取用户信息
1858
- content = await ctx.ba.getUserInfo(sub.uid);
1774
+ content = await this.ctx.ba.getUserInfo(sub.uid);
1859
1775
  // 成功则跳出循环
1860
1776
  break;
1861
1777
  }
@@ -1863,7 +1779,7 @@ class ComRegister {
1863
1779
  this.logger.error('getSubFromDatabase() getUserInfo() 发生了错误,错误为:' + e.message);
1864
1780
  if (i === attempts - 1) { // 已尝试三次
1865
1781
  // 发送私聊消息并重启服务
1866
- return await this.sendPrivateMsgAndStopService(ctx);
1782
+ return await this.sendPrivateMsgAndStopService();
1867
1783
  }
1868
1784
  }
1869
1785
  }
@@ -1872,7 +1788,7 @@ class ComRegister {
1872
1788
  // 定义函数删除数据和发送提示
1873
1789
  const deleteSub = async () => {
1874
1790
  // 从数据库删除该条数据
1875
- await ctx.database.remove('bilibili', { id: sub.id });
1791
+ await this.ctx.database.remove('bilibili', { id: sub.id });
1876
1792
  // 给用户发送提示
1877
1793
  await this.sendPrivateMsg(`UID:${sub.uid} 数据库内容被篡改,已取消对该UP主的订阅`);
1878
1794
  };
@@ -1933,28 +1849,28 @@ class ComRegister {
1933
1849
  // 直播订阅数+1
1934
1850
  liveSubNum++;
1935
1851
  // 订阅直播,开始循环检测
1936
- this.liveDetectWithListener(ctx, sub.room_id, target);
1852
+ this.liveDetectWithListener(sub.room_id, target);
1937
1853
  }
1938
1854
  }
1939
1855
  // 保存新订阅对象
1940
1856
  this.subManager.push(subManagerItem);
1941
1857
  }
1942
1858
  }
1943
- checkIfDynamicDetectIsNeeded(ctx) {
1859
+ checkIfDynamicDetectIsNeeded() {
1944
1860
  // 检查是否有订阅对象需要动态监测
1945
1861
  if (this.subManager.some(sub => sub.dynamic))
1946
- this.enableDynamicDetect(ctx);
1862
+ this.enableDynamicDetect();
1947
1863
  }
1948
- enableDynamicDetect(ctx) {
1864
+ enableDynamicDetect() {
1949
1865
  // 开始动态监测
1950
1866
  if (this.config.dynamicDebugMode) {
1951
- this.dynamicDispose = ctx.setInterval(this.debug_dynamicDetect(ctx), this.config.dynamicLoopTime * 1000);
1867
+ this.dynamicDispose = this.ctx.setInterval(this.debug_dynamicDetect(), this.config.dynamicLoopTime * 1000);
1952
1868
  }
1953
1869
  else {
1954
- this.dynamicDispose = ctx.setInterval(this.dynamicDetect(ctx), this.config.dynamicLoopTime * 1000);
1870
+ this.dynamicDispose = this.ctx.setInterval(this.dynamicDetect(), this.config.dynamicLoopTime * 1000);
1955
1871
  }
1956
1872
  }
1957
- unsubSingle(ctx, id /* UID或RoomId */, type /* 0取消Live订阅,1取消Dynamic订阅 */) {
1873
+ unsubSingle(id /* UID或RoomId */, type /* 0取消Live订阅,1取消Dynamic订阅 */) {
1958
1874
  // 定义返回消息
1959
1875
  let msg;
1960
1876
  // 定义方法:检查是否没有任何订阅
@@ -1964,7 +1880,7 @@ class ComRegister {
1964
1880
  // 从管理对象中移除
1965
1881
  this.subManager.splice(index, 1);
1966
1882
  // 从数据库中删除
1967
- ctx.database.remove('bilibili', [this.subManager[index].id]);
1883
+ this.ctx.database.remove('bilibili', [this.subManager[index].id]);
1968
1884
  // num--
1969
1885
  this.num--;
1970
1886
  // 判断是否还存在订阅了动态的对象,不存在则停止动态监测
@@ -1983,9 +1899,6 @@ class ComRegister {
1983
1899
  return msg;
1984
1900
  }
1985
1901
  // 取消订阅
1986
- if (sub.live)
1987
- sub.liveDispose();
1988
- sub.liveDispose = null;
1989
1902
  sub.live = false;
1990
1903
  // 如果没有对这个UP的任何订阅,则移除
1991
1904
  if (checkIfNoSubExist(sub)) {
@@ -1994,7 +1907,7 @@ class ComRegister {
1994
1907
  return '已取消订阅该用户';
1995
1908
  }
1996
1909
  // 更新数据库
1997
- ctx.database.upsert('bilibili', [{
1910
+ this.ctx.database.upsert('bilibili', [{
1998
1911
  id: +`${sub.id}`,
1999
1912
  live: 0
2000
1913
  }]);
@@ -2021,7 +1934,7 @@ class ComRegister {
2021
1934
  return '已取消订阅该用户';
2022
1935
  }
2023
1936
  // 更新数据库
2024
- ctx.database.upsert('bilibili', [{
1937
+ this.ctx.database.upsert('bilibili', [{
2025
1938
  id: sub.id,
2026
1939
  dynamic: 0
2027
1940
  }]);
@@ -2031,7 +1944,7 @@ class ComRegister {
2031
1944
  }
2032
1945
  finally {
2033
1946
  // 执行完该方法后,保证执行一次updateSubNotifier()
2034
- this.updateSubNotifier(ctx);
1947
+ this.updateSubNotifier();
2035
1948
  }
2036
1949
  }
2037
1950
  checkIfUserIsTheLastOneWhoSubDyn() {
@@ -2041,15 +1954,12 @@ class ComRegister {
2041
1954
  this.dynamicDispose = null;
2042
1955
  }
2043
1956
  }
2044
- unsubAll(ctx, uid) {
1957
+ unsubAll(uid) {
2045
1958
  this.subManager.filter(sub => sub.uid === uid).map(async (sub, i) => {
2046
- // 取消全部订阅 执行dispose方法,销毁定时器
2047
- if (sub.live)
2048
- await this.subManager[i].liveDispose();
2049
1959
  // 判断是否还存在订阅了动态的对象,不存在则停止动态监测
2050
1960
  this.checkIfUserIsTheLastOneWhoSubDyn();
2051
1961
  // 从数据库中删除订阅
2052
- await ctx.database.remove('bilibili', { uid: this.subManager[i].uid });
1962
+ await this.ctx.database.remove('bilibili', { uid: this.subManager[i].uid });
2053
1963
  // 将该订阅对象从订阅管理对象中移除
2054
1964
  this.subManager.splice(i, 1);
2055
1965
  // id--
@@ -2057,13 +1967,13 @@ class ComRegister {
2057
1967
  // 发送成功通知
2058
1968
  this.sendPrivateMsg(`UID:${uid},已取消订阅该用户`);
2059
1969
  // 更新控制台提示
2060
- this.updateSubNotifier(ctx);
1970
+ this.updateSubNotifier();
2061
1971
  });
2062
1972
  }
2063
- async checkIfIsLogin(ctx) {
2064
- if ((await ctx.database.get('loginBili', 1)).length !== 0) { // 数据库中有数据
1973
+ async checkIfIsLogin() {
1974
+ if ((await this.ctx.database.get('loginBili', 1)).length !== 0) { // 数据库中有数据
2065
1975
  // 检查cookie中是否有值
2066
- if (ctx.ba.getCookies() !== '[]') { // 有值说明已登录
1976
+ if (this.ctx.ba.getCookies() !== '[]') { // 有值说明已登录
2067
1977
  return true;
2068
1978
  }
2069
1979
  }
@@ -2081,6 +1991,7 @@ class ComRegister {
2081
1991
  channelId: koishi_1.Schema.string().description('频道/群组号'),
2082
1992
  dynamic: koishi_1.Schema.boolean().description('该频道/群组是否推送动态信息'),
2083
1993
  live: koishi_1.Schema.boolean().description('该频道/群组是否推送直播通知'),
1994
+ liveDanmaku: koishi_1.Schema.boolean().description('该频道/群组是否推送弹幕消息'),
2084
1995
  atAll: koishi_1.Schema.boolean().description('推送开播通知时是否艾特全体成员')
2085
1996
  })).description('频道/群组信息'),
2086
1997
  platform: koishi_1.Schema.string().description('推送平台')
@@ -2094,7 +2005,6 @@ class ComRegister {
2094
2005
  }),
2095
2006
  unlockSubLimits: koishi_1.Schema.boolean().required(),
2096
2007
  automaticResend: koishi_1.Schema.boolean().required(),
2097
- changeMasterInfoApi: koishi_1.Schema.boolean().required(),
2098
2008
  restartPush: koishi_1.Schema.boolean().required(),
2099
2009
  pushTime: koishi_1.Schema.number().required(),
2100
2010
  liveLoopTime: koishi_1.Schema.number().default(10),