koishi-plugin-bilibili-notify 1.0.0-alpha.1 → 1.0.0-beta.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.
@@ -23,6 +23,7 @@ declare class ComRegister {
23
23
  getSubFromDatabase(ctx: Context): Promise<void>;
24
24
  unsubSingle(ctx: Context, id: string, type: number): string;
25
25
  checkIfIsLogin(ctx: Context): Promise<boolean>;
26
+ test_dynamicDetect(ctx: Context, session: Session, uid: string): () => Promise<void>;
26
27
  }
27
28
  declare namespace ComRegister {
28
29
  interface Config {
@@ -72,7 +72,7 @@ class ComRegister {
72
72
  .subcommand('.gimg <uid:string> <index:number>')
73
73
  .usage('测试图片生成')
74
74
  .example('test.gimg')
75
- .action(async ({ session }, uid, index) => {
75
+ .action(async (_, uid, index) => {
76
76
  // 获取用户空间动态数据
77
77
  const { data } = await ctx.biliAPI.getUserSpaceDynamic(uid);
78
78
  const [pic] = await ctx.gimg.generateDynamicImg(data.items[index]);
@@ -92,6 +92,13 @@ class ComRegister {
92
92
  .action(({ session }) => {
93
93
  console.log(session);
94
94
  });
95
+ ctx.command('test')
96
+ .subcommand('.dynamic <uid:string>')
97
+ .usage('测试动态监测')
98
+ .example('test dynamic uid')
99
+ .action(({ session }, uid) => {
100
+ ctx.setInterval(this.test_dynamicDetect(ctx, session, uid), 30000);
101
+ });
95
102
  ctx.command('bili', 'bili-notify插件相关指令', { permissions: ['authority:3'] })
96
103
  .subcommand('.login', '登录B站之后才可以进行之后的操作')
97
104
  .usage('使用二维码登录,登录B站之后才可以进行之后的操作')
@@ -282,6 +289,10 @@ class ComRegister {
282
289
  liveMsg = await this.checkIfNeedSub(options.live, '直播', session, data);
283
290
  // 判断是否需要订阅动态
284
291
  dynamicMsg = await this.checkIfNeedSub(options.dynamic, '动态', session);
292
+ // 判断是否未订阅任何消息
293
+ if (!liveMsg && !dynamicMsg) {
294
+ return '您未订阅该UP的任何消息';
295
+ }
285
296
  // 判断是哪个平台
286
297
  let platform;
287
298
  if (!guildId) { // 没有输入群号,默认当前聊天环境
@@ -314,7 +325,7 @@ class ComRegister {
314
325
  // 保存到数据库中
315
326
  const sub = await ctx.database.create('bilibili', {
316
327
  uid: mid,
317
- room_id: data.live_room.roomid.toString(),
328
+ room_id: data.live_room?.roomid.toString(),
318
329
  dynamic: dynamicMsg ? 1 : 0,
319
330
  video: 1,
320
331
  live: liveMsg ? 1 : 0,
@@ -330,7 +341,7 @@ class ComRegister {
330
341
  id: sub.id,
331
342
  uid: mid,
332
343
  targetId: guildId,
333
- roomId: data.live_room.roomid.toString(),
344
+ roomId: data.live_room?.roomid.toString(),
334
345
  live: liveMsg,
335
346
  dynamic: dynamicMsg,
336
347
  liveDispose: null,
@@ -474,11 +485,18 @@ class ComRegister {
474
485
  dynamicDetect(ctx, bot, guildId, uid) {
475
486
  let firstSubscription = true;
476
487
  let timePoint;
488
+ // Test code
489
+ // let timer = 0
477
490
  return async () => {
491
+ // Test code
492
+ /* console.log('timer:' + timer++);
493
+ console.log('firstSubscription:' + firstSubscription);
494
+ console.log(`timePoint: ${timePoint}`);
495
+ console.log(`timePoint: ${ctx.gimg.unixTimestampToString(timePoint)}`); */
478
496
  // 第一次订阅判断
479
497
  if (firstSubscription) {
480
498
  // 设置第一次的时间点
481
- timePoint = Date.now();
499
+ timePoint = Math.floor(Date.now() / 1000);
482
500
  // 设置第一次为false
483
501
  firstSubscription = false;
484
502
  return;
@@ -512,6 +530,8 @@ class ComRegister {
512
530
  // 没有动态内容则直接跳过
513
531
  if (!items[num])
514
532
  continue;
533
+ // Test code
534
+ // console.log(`items[${num}].modules.module_author.pub_ts: ${ctx.gimg.unixTimestampToString(items[num].modules.module_author.pub_ts)}`);
515
535
  // 寻找发布时间比时间点时间更晚的动态
516
536
  if (items[num].modules.module_author.pub_ts > timePoint) {
517
537
  // 如果这是遍历的最后一条,将时间点设置为这条动态的发布时间
@@ -526,8 +546,19 @@ class ComRegister {
526
546
  case 1: timePoint = items[num].modules.module_author.pub_ts;
527
547
  }
528
548
  // 推送该条动态
529
- const [pic] = await ctx.gimg.generateDynamicImg(items[num]);
530
- await bot.sendMessage(guildId, pic);
549
+ let attempts = 3;
550
+ for (let i = 0; i < attempts; i++) {
551
+ try {
552
+ const [pic] = await ctx.gimg.generateDynamicImg(items[num]);
553
+ await bot.sendMessage(guildId, pic);
554
+ break; // 如果成功,那么跳出循环
555
+ }
556
+ catch (e) {
557
+ if (i === attempts - 1) { // 如果已经尝试了三次,那么抛出错误
558
+ throw e;
559
+ }
560
+ }
561
+ }
531
562
  }
532
563
  }
533
564
  };
@@ -651,7 +682,8 @@ class ComRegister {
651
682
  }
652
683
  async checkIfNeedSub(comNeed, subType, session, data) {
653
684
  if (comNeed) {
654
- if (subType === '直播' && data.live_room.roomStatus === 0) {
685
+ if (subType === '直播' && !data.live_room) {
686
+ await session.send('该用户未开通直播间,无法订阅直播');
655
687
  return false;
656
688
  }
657
689
  return true;
@@ -668,8 +700,8 @@ class ComRegister {
668
700
  switch (input) {
669
701
  case 'y': { // 需要订阅直播
670
702
  // 如果用户没有开通直播间则无法订阅
671
- if (subType === '直播' && data.live_room.roomStatus === 0) {
672
- await session.send('该用户没有开通直播间,无法订阅直播');
703
+ if (subType === '直播' && !data.live_room) {
704
+ await session.send('该用户未开通直播间,无法订阅直播');
673
705
  return false;
674
706
  }
675
707
  // 开启直播订阅
@@ -801,12 +833,82 @@ class ComRegister {
801
833
  }
802
834
  return false;
803
835
  }
836
+ test_dynamicDetect(ctx, session, uid) {
837
+ let firstSubscription = true;
838
+ let timePoint;
839
+ // Test code
840
+ let timer = 0;
841
+ return async () => {
842
+ // Test code
843
+ console.log('timer:' + timer++);
844
+ console.log('firstSubscription:' + firstSubscription);
845
+ console.log(`timePoint: ${timePoint}`);
846
+ console.log(`timePoint: ${ctx.gimg.unixTimestampToString(timePoint)}`);
847
+ // 第一次订阅判断
848
+ if (firstSubscription) {
849
+ // 设置第一次的时间点
850
+ timePoint = Math.floor(Date.now() / 1000);
851
+ // 设置第一次为false
852
+ firstSubscription = false;
853
+ return;
854
+ }
855
+ // 获取用户空间动态数据
856
+ let content;
857
+ try {
858
+ content = await ctx.biliAPI.getUserSpaceDynamic(uid);
859
+ }
860
+ catch (e) {
861
+ return this.logger.error('dynamicDetect getUserSpaceDynamic() 网络请求失败');
862
+ }
863
+ // 判断是否出现其他问题
864
+ if (content.code !== 0) {
865
+ switch (content.code) {
866
+ case -101: { // 账号未登录
867
+ await session.send('账号未登录,请登录后重新订阅动态');
868
+ }
869
+ default: { // 未知错误
870
+ await session.send('未知错误,请重新订阅动态');
871
+ }
872
+ }
873
+ // 取消订阅
874
+ this.unsubSingle(ctx, uid, 1); /* 1为取消动态订阅 */
875
+ return;
876
+ }
877
+ // 获取数据内容
878
+ const items = content.data.items;
879
+ // 发送请求 只查看前五条数据
880
+ for (let num = 4; num >= 0; num--) {
881
+ // 没有动态内容则直接跳过
882
+ if (!items[num])
883
+ continue;
884
+ // Test code
885
+ console.log(`items[${num}].modules.module_author.pub_ts: ${ctx.gimg.unixTimestampToString(items[num].modules.module_author.pub_ts)}`);
886
+ // 寻找发布时间比时间点时间更晚的动态
887
+ if (items[num].modules.module_author.pub_ts > timePoint) {
888
+ // 如果这是遍历的最后一条,将时间点设置为这条动态的发布时间
889
+ /* if (num === 1) timePoint = items[num].modules.module_author.pub_ts
890
+ if (num === 0) {
891
+ timePoint = items[num].modules.module_author.pub_ts
892
+ } */
893
+ switch (num) {
894
+ // 如果是置顶动态,则跳过
895
+ case 0: if (items[num].modules.module_tag)
896
+ continue;
897
+ case 1: timePoint = items[num].modules.module_author.pub_ts;
898
+ }
899
+ // 推送该条动态
900
+ const [pic] = await ctx.gimg.generateDynamicImg(items[num]);
901
+ await session.send(pic);
902
+ }
903
+ }
904
+ };
905
+ }
804
906
  }
805
907
  (function (ComRegister) {
806
908
  ComRegister.Config = koishi_1.Schema.object({
807
909
  pushTime: koishi_1.Schema.number().required(),
808
- liveLoopTime: koishi_1.Schema.number().default(5),
809
- dynamicLoopTime: koishi_1.Schema.number().default(60)
910
+ liveLoopTime: koishi_1.Schema.number().default(10),
911
+ dynamicLoopTime: koishi_1.Schema.number().default(30)
810
912
  });
811
913
  })(ComRegister || (ComRegister = {}));
812
914
  exports.default = ComRegister;
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.0.0-alpha.1",
4
+ "version": "1.0.0-beta.1",
5
5
  "contributors": [
6
6
  "Akokko <admin@akokko.com>"
7
7
  ],