koishi-plugin-bilibili-notify 1.2.0-alpha.0 → 1.2.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.
@@ -22,12 +22,13 @@ declare class ComRegister {
22
22
  oneBot: Bot<Context>;
23
23
  redBot: Bot<Context>;
24
24
  constructor(ctx: Context, config: ComRegister.Config);
25
- dynamicDetect(ctx: Context, bot: Bot<Context>, guildId: string, uid: string): () => Promise<void>;
26
- liveDetect(ctx: Context, bot: Bot<Context>, guildId: string, roomId: string): () => Promise<string[]>;
25
+ dynamicDetect(ctx: Context, bot: Bot<Context>, uid: string, guildId: Array<string>): () => Promise<void>;
26
+ sendMsg(targets: Array<string>, bot: Bot<Context>, content: any): Promise<void>;
27
+ liveDetect(ctx: Context, bot: Bot<Context>, roomId: string, guildId: Array<string>): () => Promise<void>;
27
28
  subShow(): string;
28
29
  checkIfNeedSub(comNeed: boolean, subType: string, session: Session, data?: any): Promise<boolean>;
29
30
  updateSubNotifier(ctx: Context): void;
30
- getSubFromDatabase(ctx: Context): Promise<string[]>;
31
+ getSubFromDatabase(ctx: Context): Promise<void>;
31
32
  unsubSingle(ctx: Context, id: string, type: number): string;
32
33
  checkIfIsLogin(ctx: Context): Promise<boolean>;
33
34
  }
@@ -35,9 +35,15 @@ class ComRegister {
35
35
  // 拿到各类机器人
36
36
  ctx.bots.forEach(bot => {
37
37
  switch (bot.platform) {
38
- case 'qq': this.qqBot = bot;
39
- case 'qqguild': this.qqguildBot = bot;
40
- case 'onebot': this.oneBot = bot;
38
+ case 'qq':
39
+ this.qqBot = bot;
40
+ break;
41
+ case 'qqguild':
42
+ this.qqguildBot = bot;
43
+ break;
44
+ case 'onebot':
45
+ this.oneBot = bot;
46
+ break;
41
47
  case 'red': this.redBot = bot;
42
48
  }
43
49
  });
@@ -280,12 +286,12 @@ class ComRegister {
280
286
  return subTable;
281
287
  });
282
288
  biliCom
283
- .subcommand('.sub <mid:string> [guildId:string]', '订阅用户动态和直播通知')
289
+ .subcommand('.sub <mid:string> [...guildId:string]', '订阅用户动态和直播通知')
284
290
  .option('live', '-l')
285
291
  .option('dynamic', '-d')
286
292
  .usage('订阅用户动态和直播通知,若需要订阅直播请加上-l,需要订阅动态则加上-d。若没有加任何参数,之后会向你单独询问,尖括号中为必选参数,中括号为可选参数,目标群号若不填,则默认为当前群聊')
287
- .example('bili sub 1194210119 目标QQ群号(暂不支持) -l -d 订阅UID为1194210119的UP主的动态和直播')
288
- .action(async ({ session, options }, mid, guildId) => {
293
+ .example('bili sub 1194210119 目标QQ群号(实验性) -l -d 订阅UID为1194210119的UP主的动态和直播')
294
+ .action(async ({ session, options }, mid, ...guildId) => {
289
295
  this.logger.info('调用bili.sub指令');
290
296
  // 检查是否登录
291
297
  if (!(await this.checkIfIsLogin(ctx))) {
@@ -332,6 +338,60 @@ class ComRegister {
332
338
  }
333
339
  return msg;
334
340
  }
341
+ // 设置目标ID
342
+ let targetId;
343
+ // 判断是否输入了QQ群号
344
+ if (guildId.length > 0) { // 输入了QQ群号
345
+ // 定义方法
346
+ const checkIfGuildHasJoined = async (bot) => {
347
+ // 获取机器人加入的群组
348
+ const guildList = await bot.getGuildList();
349
+ // 定义满足条件的群组数组
350
+ const targetArr = [];
351
+ // 判断群号是否符合条件
352
+ for (let guild in guildId) {
353
+ if (guildList.data.some(cv => cv.id === guild)) { // 机器人加入了该群
354
+ // 保存到数组
355
+ targetArr.push(guild);
356
+ // 继续下一个循环
357
+ continue;
358
+ }
359
+ // 不满足条件发送错误提示
360
+ session.send(`您的机器未加入${guild},无法对该群进行推送`);
361
+ }
362
+ // 返回数组
363
+ return targetArr;
364
+ };
365
+ // 定义可用的群组数组
366
+ let okGuild;
367
+ // 判断是否有群机器人相关Bot
368
+ switch (session.event.platform) {
369
+ case 'qq': {
370
+ okGuild = await checkIfGuildHasJoined(this.qqBot);
371
+ break;
372
+ }
373
+ case 'onebot': {
374
+ okGuild = await checkIfGuildHasJoined(this.oneBot);
375
+ break;
376
+ }
377
+ case 'red': {
378
+ okGuild = await checkIfGuildHasJoined(this.redBot);
379
+ break;
380
+ }
381
+ default: {
382
+ // 发送错误提示并返回
383
+ session.send('您尚未配置任何QQ群相关机器人,不能对QQ群进行操作');
384
+ // 直接返回
385
+ return;
386
+ }
387
+ }
388
+ // 将群号用,进行分割
389
+ targetId = okGuild.join(' ');
390
+ }
391
+ else { // 没有输入QQ群号
392
+ // 为当前群聊环境进行推送
393
+ targetId = session.event.channel.id;
394
+ }
335
395
  // 获取data
336
396
  const { data } = content;
337
397
  // 判断是否需要订阅直播
@@ -342,23 +402,6 @@ class ComRegister {
342
402
  if (!liveMsg && !dynamicMsg) {
343
403
  return '您未订阅该UP的任何消息';
344
404
  }
345
- // 设置频道号
346
- if (!guildId) { // 没有输入频道号,默认当前聊天环境
347
- switch (session.event.platform) {
348
- case 'red':
349
- case 'onebot':
350
- case 'qqguild':
351
- guildId = session.event.channel.id;
352
- break;
353
- case 'qq':
354
- guildId = session.event.guild.id;
355
- break;
356
- default: return '暂不支持该平台';
357
- }
358
- }
359
- else {
360
- return '暂不支持群号发送';
361
- }
362
405
  // 获取直播房间号
363
406
  let roomId = data.live_room?.roomid.toString();
364
407
  // 保存到数据库中
@@ -368,7 +411,7 @@ class ComRegister {
368
411
  dynamic: dynamicMsg ? 1 : 0,
369
412
  video: 1,
370
413
  live: liveMsg ? 1 : 0,
371
- targetId: guildId,
414
+ targetId,
372
415
  platform: session.event.platform,
373
416
  time: new Date()
374
417
  });
@@ -379,7 +422,7 @@ class ComRegister {
379
422
  this.subManager.push({
380
423
  id: sub.id,
381
424
  uid: mid,
382
- targetId: guildId,
425
+ targetId,
383
426
  roomId,
384
427
  live: liveMsg,
385
428
  dynamic: dynamicMsg,
@@ -397,13 +440,13 @@ class ComRegister {
397
440
  }
398
441
  // 需要订阅直播
399
442
  if (liveMsg) {
400
- await session.execute(`bili live ${roomId} ${guildId} -b ${session.event.platform}`);
443
+ await session.execute(`bili live ${roomId} ${targetId.split(',').join(' ')} -b ${session.event.platform}`);
401
444
  // 发送订阅消息通知
402
445
  await session.send(`订阅${userData.info.uname}直播通知`);
403
446
  }
404
447
  // 需要订阅动态
405
448
  if (dynamicMsg) {
406
- await session.execute(`bili dynamic ${mid} ${guildId} -b ${session.event.platform}`);
449
+ await session.execute(`bili dynamic ${mid} ${targetId.split(',').join(' ')} -b ${session.event.platform}`);
407
450
  // 发送订阅消息通知
408
451
  await session.send(`订阅${userData.info.uname}动态通知`);
409
452
  }
@@ -411,11 +454,11 @@ class ComRegister {
411
454
  this.updateSubNotifier(ctx);
412
455
  });
413
456
  biliCom
414
- .subcommand('.dynamic <uid:string> <guildId:string>', '订阅用户动态推送', { hidden: true })
457
+ .subcommand('.dynamic <uid:string> <...guildId:string>', '订阅用户动态推送', { hidden: true })
415
458
  .option('bot', '-b <type:string>')
416
459
  .usage('订阅用户动态推送')
417
460
  .example('bili dynamic 1194210119 订阅UID为1194210119的动态')
418
- .action(async ({ session, options }, uid, guildId) => {
461
+ .action(async ({ session, options }, uid, ...guildId) => {
419
462
  this.logger.info('调用bili.dynamic指令');
420
463
  // 如果uid为空则返回
421
464
  if (!uid)
@@ -449,16 +492,16 @@ class ComRegister {
449
492
  default: return '非法调用';
450
493
  }
451
494
  // 开始循环检测
452
- const dispose = ctx.setInterval(this.dynamicDetect(ctx, bot, guildId, uid), config.dynamicLoopTime * 1000);
495
+ const dispose = ctx.setInterval(this.dynamicDetect(ctx, bot, uid, guildId), config.dynamicLoopTime * 1000);
453
496
  // 将销毁函数保存到订阅管理对象
454
497
  this.subManager[index].dynamicDispose = dispose;
455
498
  });
456
499
  biliCom
457
- .subcommand('.live <roomId:string> <guildId:string>', '订阅主播开播通知', { hidden: true })
500
+ .subcommand('.live <roomId:string> <...guildId:string>', '订阅主播开播通知', { hidden: true })
458
501
  .option('bot', '-b <type:string>')
459
502
  .usage('订阅主播开播通知')
460
503
  .example('bili live 26316137 订阅房间号为26316137的直播间')
461
- .action(async ({ options }, roomId, guildId) => {
504
+ .action(async ({ options }, roomId, ...guildId) => {
462
505
  this.logger.info('调用bili.live指令');
463
506
  // 如果room_id为空则返回
464
507
  if (!roomId)
@@ -490,7 +533,7 @@ class ComRegister {
490
533
  default: return '非法调用';
491
534
  }
492
535
  // 开始循环检测
493
- const dispose = ctx.setInterval(this.liveDetect(ctx, bot, guildId, roomId), config.liveLoopTime * 1000);
536
+ const dispose = ctx.setInterval(this.liveDetect(ctx, bot, roomId, guildId), config.liveLoopTime * 1000);
494
537
  // 保存销毁函数
495
538
  this.subManager[index].liveDispose = dispose;
496
539
  });
@@ -538,7 +581,7 @@ class ComRegister {
538
581
  await session.send(koishi_1.h.image(buffer, 'image/png'));
539
582
  });
540
583
  }
541
- dynamicDetect(ctx, bot, guildId, uid) {
584
+ dynamicDetect(ctx, bot, uid, guildId) {
542
585
  let firstSubscription = true;
543
586
  let timePoint;
544
587
  return async () => {
@@ -562,10 +605,10 @@ class ComRegister {
562
605
  if (content.code !== 0) {
563
606
  switch (content.code) {
564
607
  case -101: { // 账号未登录
565
- bot.sendMessage(guildId, '账号未登录,请登录后重新订阅动态');
608
+ await this.sendMsg(guildId, bot, '账号未登录,请登录后重新订阅动态');
566
609
  }
567
610
  default: { // 未知错误
568
- bot.sendMessage(guildId, '未知错误,请重新订阅动态');
611
+ await this.sendMsg(guildId, bot, '未知错误,请重新订阅动态');
569
612
  }
570
613
  }
571
614
  // 取消订阅
@@ -600,18 +643,18 @@ class ComRegister {
600
643
  if (e.message === '直播开播动态,不做处理')
601
644
  break;
602
645
  if (e.message === '出现关键词,屏蔽该动态') {
603
- await bot.sendMessage(guildId, `UID:${uid} 发布了一条含有屏蔽关键字的动态`);
646
+ await this.sendMsg(guildId, bot, `UID:${uid} 发布了一条含有屏蔽关键字的动态`);
604
647
  break;
605
648
  }
606
649
  }
607
650
  // 如果pic存在,则直接返回pic
608
651
  if (pic) {
609
652
  // pic存在,使用的是render模式
610
- await bot.sendMessage(guildId, pic);
653
+ await this.sendMsg(guildId, bot, pic);
611
654
  }
612
655
  else {
613
656
  // pic不存在,说明使用的是page模式
614
- await bot.sendMessage(guildId, koishi_1.h.image(buffer, 'image/png'));
657
+ await this.sendMsg(guildId, bot, koishi_1.h.image(buffer, 'image/png'));
615
658
  }
616
659
  // 如果成功,那么跳出循环
617
660
  break;
@@ -636,7 +679,13 @@ class ComRegister {
636
679
  }
637
680
  };
638
681
  }
639
- liveDetect(ctx, bot, guildId, roomId) {
682
+ async sendMsg(targets, bot, content) {
683
+ // 循环给每个群组发送
684
+ for (let guildId of targets) {
685
+ bot.sendMessage(guildId, content);
686
+ }
687
+ }
688
+ liveDetect(ctx, bot, roomId, guildId) {
640
689
  let firstSubscription = true;
641
690
  let timer = 0;
642
691
  let open = false;
@@ -653,16 +702,16 @@ class ComRegister {
653
702
  // 推送直播信息
654
703
  // pic 存在,使用的是render模式
655
704
  if (pic)
656
- return bot.sendMessage(guildId, pic);
705
+ return await this.sendMsg(guildId, bot, pic);
657
706
  // pic不存在,说明使用的是page模式
658
- await bot.sendMessage(guildId, koishi_1.h.image(buffer, 'image/png'));
707
+ await this.sendMsg(guildId, bot, koishi_1.h.image(buffer, 'image/png'));
659
708
  // 成功则跳出循环
660
709
  break;
661
710
  }
662
711
  catch (e) {
663
712
  this.logger.error('liveDetect generateLiveImg() 推送卡片发送失败');
664
713
  if (i === attempts - 1) { // 已尝试三次
665
- return bot.sendMessage(guildId, '插件可能出现某些未知错误,请尝试重启插件,如果仍然会发生该错误,请向作者反馈');
714
+ return this.sendMsg(guildId, bot, '插件可能出现某些未知错误,请尝试重启插件,如果仍然会发生该错误,请向作者反馈');
666
715
  }
667
716
  }
668
717
  }
@@ -686,22 +735,12 @@ class ComRegister {
686
735
  catch (e) {
687
736
  this.logger.error('liveDetect getLiveRoomInfo 网络请求失败');
688
737
  if (i === attempts - 1) { // 已尝试三次
689
- return bot.sendMessage(guildId, '你的网络可能出现了某些问题,请检查后重启插件');
738
+ return await this.sendMsg(guildId, bot, '你的网络可能出现了某些问题,请检查后重启插件');
690
739
  }
691
740
  }
692
741
  }
693
742
  const { data } = content;
694
- // B站出问题了
695
- /* if (content.code !== 0) {
696
- if (content.msg === '未找到该房间') {
697
- await bot.sendMessage(guildId, '未找到该房间,请检查房间号后重新订阅')
698
- } else {
699
- await bot.sendMessage(guildId, '未知错误,请呼叫管理员检查问题后重新订阅')
700
- }
701
- // dispose
702
- this.unsubSingle(ctx, roomId, 0) // 0为取消Live订阅
703
- return
704
- } */
743
+ // 判断是否是第一次订阅
705
744
  if (firstSubscription) {
706
745
  firstSubscription = false;
707
746
  // 获取主播信息
@@ -718,7 +757,7 @@ class ComRegister {
718
757
  catch (e) {
719
758
  this.logger.error('liveDetect getMasterInfo() 本次网络请求失败');
720
759
  if (i === attempts - 1) { // 已尝试三次
721
- return bot.sendMessage(guildId, '你的网络可能出现了某些问题,请检查后重启插件');
760
+ return await this.sendMsg(guildId, bot, '你的网络可能出现了某些问题,请检查后重启插件');
722
761
  }
723
762
  }
724
763
  }
@@ -745,7 +784,7 @@ class ComRegister {
745
784
  // 下播了将定时器清零
746
785
  timer = 0;
747
786
  // 发送下播通知
748
- await bot.sendMessage(guildId, `${uData.info.uname}下播啦,本次直播了${await ctx.gimg.getTimeDifference(liveTime)}`);
787
+ await this.sendMsg(guildId, bot, `${uData.info.uname}下播啦,本次直播了${await ctx.gimg.getTimeDifference(liveTime)}`);
749
788
  }
750
789
  // 未进循环,还未开播,继续循环
751
790
  break;
@@ -770,18 +809,18 @@ class ComRegister {
770
809
  catch (e) {
771
810
  this.logger.error('liveDetect open getMasterInfo() 网络请求错误');
772
811
  if (i === attempts - 1) { // 已尝试三次
773
- return bot.sendMessage(guildId, '你的网络可能出现了某些问题,请检查后重启插件');
812
+ return this.sendMsg(guildId, bot, '你的网络可能出现了某些问题,请检查后重启插件');
774
813
  }
775
814
  }
776
815
  }
777
816
  // 主播信息不会变,开播时刷新一次即可
778
817
  uData = userData;
779
818
  // 发送直播通知卡片
780
- sendLiveNotifyCard(data, uData, LiveType.StartBroadcasting);
819
+ await sendLiveNotifyCard(data, uData, LiveType.StartBroadcasting);
781
820
  // 判断是否需要@全体成员
782
821
  if (this.config.liveStartAtAll) {
783
822
  // 发送@全体成员通知
784
- bot.sendMessage(guildId, (0, jsx_runtime_1.jsx)("at", { type: "all" }));
823
+ await this.sendMsg(guildId, bot, (0, jsx_runtime_1.jsx)("at", { type: "all" }));
785
824
  }
786
825
  }
787
826
  else { // 还在直播
@@ -889,10 +928,10 @@ class ComRegister {
889
928
  });
890
929
  return;
891
930
  }
892
- // 定义Bot
893
- let bot;
894
931
  // 循环遍历
895
932
  for (const sub of subData) {
933
+ // 定义Bot
934
+ let bot;
896
935
  // 判断是否存在没有任何订阅的数据
897
936
  if (!sub.dynamic && !sub.live) { // 存在未订阅任何项目的数据
898
937
  // 删除该条数据
@@ -921,6 +960,8 @@ class ComRegister {
921
960
  continue;
922
961
  }
923
962
  }
963
+ // 获取推送目标数组
964
+ const targetArr = sub.targetId.split(' ');
924
965
  // 判断数据库是否被篡改
925
966
  // 获取用户信息
926
967
  let content;
@@ -934,7 +975,7 @@ class ComRegister {
934
975
  catch (e) {
935
976
  this.logger.error('getSubFromDatabase() getUserInfo() 本次网络请求失败');
936
977
  if (i === attempts - 1) { // 已尝试三次
937
- return bot.sendMessage(sub.targetId, '你的网络可能出现了某些问题,请检查后重启插件');
978
+ return await this.sendMsg(targetArr, bot, '你的网络可能出现了某些问题,请检查后重启插件');
938
979
  }
939
980
  }
940
981
  }
@@ -945,26 +986,27 @@ class ComRegister {
945
986
  // 从数据库删除该条数据
946
987
  await ctx.database.remove('bilibili', { id: sub.id });
947
988
  // 给用户发送提示
948
- bot.sendMessage(sub.targetId, `UID:${sub.uid} 数据库内容被篡改,已取消对该UP主的订阅`);
989
+ this.sendMsg(targetArr, bot, `UID:${sub.uid} 数据库内容被篡改,已取消对该UP主的订阅`);
949
990
  };
950
991
  // 判断是否有其他问题
951
992
  if (content.code !== 0) {
952
993
  switch (content.code) {
953
994
  case -352:
954
- case -403:
955
- bot.sendMessage(sub.targetId, '你的登录信息已过期,请重新登录Bilibili');
995
+ case -403: {
996
+ this.sendMsg(targetArr, bot, '你的登录信息已过期,请重新登录Bilibili');
956
997
  return;
998
+ }
957
999
  case -400:
958
1000
  case -404:
959
1001
  default:
960
- deleteSub();
1002
+ await deleteSub();
961
1003
  return;
962
1004
  }
963
1005
  }
964
1006
  // 检测房间号是否被篡改
965
1007
  if (sub.live && (!data.live_room || data.live_room.roomid.toString() !== sub.room_id)) {
966
1008
  // 房间号被篡改,删除该订阅
967
- deleteSub();
1009
+ await deleteSub();
968
1010
  return;
969
1011
  }
970
1012
  // 构建订阅对象
@@ -981,13 +1023,13 @@ class ComRegister {
981
1023
  // 判断需要订阅的服务
982
1024
  if (sub.dynamic) { // 需要订阅动态
983
1025
  // 开始循环检测
984
- const dispose = ctx.setInterval(this.dynamicDetect(ctx, bot, sub.targetId, sub.uid), this.config.dynamicLoopTime * 1000);
1026
+ const dispose = ctx.setInterval(this.dynamicDetect(ctx, bot, sub.uid, targetArr), this.config.dynamicLoopTime * 1000);
985
1027
  // 保存销毁函数
986
1028
  subManagerItem.dynamicDispose = dispose;
987
1029
  }
988
1030
  if (sub.live) { // 需要订阅动态
989
1031
  // 开始循环检测
990
- const dispose = ctx.setInterval(this.liveDetect(ctx, bot, sub.targetId, sub.room_id), this.config.liveLoopTime * 1000);
1032
+ const dispose = ctx.setInterval(this.liveDetect(ctx, bot, sub.room_id, targetArr), this.config.liveLoopTime * 1000);
991
1033
  // 保存销毁函数
992
1034
  subManagerItem.liveDispose = dispose;
993
1035
  }
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.2.0-alpha.0",
4
+ "version": "1.2.0-alpha.2",
5
5
  "contributors": [
6
6
  "Akokko <admin@akokko.com>"
7
7
  ],
package/readme.md CHANGED
@@ -32,8 +32,10 @@
32
32
 
33
33
  订阅UP主:订阅你想要推送的UP主
34
34
 
35
- - 使用指令 `bili sub <uid>` 订阅需要订阅的UP主
36
- - 参数说明:`uid` 为必填参数,为 `up主` 的 `uid`
35
+ - 使用指令 `bili sub <uid> [Q群号]` 订阅需要订阅的UP主
36
+ - 参数说明:
37
+ - `uid` 为必填参数,为 `up主` 的 `uid`
38
+ - `Q群号` 为可选参数,可以添加多个
37
39
  - 选项说明:`bili sub <uid>` 有两个选项:-l 和 -d
38
40
  - `-l` 为订阅UP主直播间,包括直播开播通知,定时推送直播内容,下播通知
39
41
  - `-d` 为订阅UP主动态推送,目前实现推送的动态类型有:普通图文动态,转发动态,直播预约动态
@@ -42,8 +44,10 @@
42
44
  - `bili sub 1194210119 ` 订阅UID为1194210119的UP主
43
45
  - `bili sub 1194210119 -d` 订阅UID为1194210119的UP主动态推送
44
46
  - `bili sub 1194210119 -ld` 订阅UID为1194210119的UP主动态推送和直播间
47
+ - `bili sub 1194210119 1234567 2345678` 订阅UID为1194210119的UP主,向Q群号为1234567和2345678两个群进行推送
45
48
  - Tips:
46
49
  - 除非使用指令 `bili sub 1194210119 -ld` ,没有加选项或只加了一个选项的指令都会再次询问是否需要订阅另一项。例如:使用指令 `bili sub 1194210119 -d` 机器人会询问是否需要订阅直播间
50
+ - `[Q群号]` 这个可选参数仅支持Q群
47
51
 
48
52
  取消订阅UP主:取消订阅不需要推送的UP主
49
53
 
@@ -97,6 +101,8 @@
97
101
  - ver 1.1.1 新增依赖axios
98
102
  - ver 1.1.2 修复了对red协议支持的一个bug
99
103
  - ver 1.2.0-alpha.0 对自动更新登录信息的功能做了提升,修复了一些bug
104
+ - ver 1.2.0-alpha.1 对推送进行了改进:在开启直播开播艾特全体成员的情况下,发送图片后才会艾特全体成员
105
+ - ver 1.2.0-alpha.2 支持QQ群多群推送(实验性),修复了一些bug
100
106
 
101
107
  ## 感谢
102
108