koishi-plugin-bilibili-notify 1.0.0-beta.0 → 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站之后才可以进行之后的操作')
@@ -539,8 +546,19 @@ class ComRegister {
539
546
  case 1: timePoint = items[num].modules.module_author.pub_ts;
540
547
  }
541
548
  // 推送该条动态
542
- const [pic] = await ctx.gimg.generateDynamicImg(items[num]);
543
- 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
+ }
544
562
  }
545
563
  }
546
564
  };
@@ -815,6 +833,76 @@ class ComRegister {
815
833
  }
816
834
  return false;
817
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
+ }
818
906
  }
819
907
  (function (ComRegister) {
820
908
  ComRegister.Config = koishi_1.Schema.object({
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-beta.0",
4
+ "version": "1.0.0-beta.1",
5
5
  "contributors": [
6
6
  "Akokko <admin@akokko.com>"
7
7
  ],