koishi-plugin-bilibili-notify 1.0.3 → 1.0.4-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/0.html ADDED
File without changes
package/lib/biliAPI.d.ts CHANGED
@@ -14,6 +14,7 @@ declare class BiliAPI extends Service {
14
14
  loginNotifier: Notifier;
15
15
  constructor(ctx: Context);
16
16
  protected start(): void | Promise<void>;
17
+ getServerUTCTime(): Promise<number>;
17
18
  getTimeNow(): Promise<any>;
18
19
  getUserSpaceDynamic(mid: string): Promise<any>;
19
20
  getCookieInfo(refreshToken: string): Promise<any>;
@@ -23,7 +24,6 @@ declare class BiliAPI extends Service {
23
24
  getLoginStatus(qrcodeKey: string): Promise<any>;
24
25
  getLiveRoomInfo(roomId: string): Promise<any>;
25
26
  getMasterInfo(mid: string): Promise<any>;
26
- getUTCPlus8Time(): number;
27
27
  disposeNotifier(): void;
28
28
  createNewClient(): void;
29
29
  getCookies(): string;
package/lib/biliAPI.js CHANGED
@@ -18,6 +18,7 @@ const GET_LOGIN_STATUS = 'https://passport.bilibili.com/x/passport-login/web/qrc
18
18
  const GET_LIVE_ROOM_INFO = 'https://api.live.bilibili.com/room/v1/Room/get_info';
19
19
  const GET_MASTER_INFO = 'https://api.live.bilibili.com/live_user/v1/Master/info';
20
20
  const GET_TIME_NOW = 'https://api.bilibili.com/x/report/click/now';
21
+ const GET_SERVER_UTC_TIME = 'https://interface.bilibili.com/serverdate.js';
21
22
  class BiliAPI extends koishi_1.Service {
22
23
  static inject = ['database', 'wbi', 'notifier'];
23
24
  jar;
@@ -35,6 +36,23 @@ class BiliAPI extends koishi_1.Service {
35
36
  this.loadCookiesFromDatabase();
36
37
  this.logger.info('BiliAPI已被注册到Context中');
37
38
  }
39
+ async getServerUTCTime() {
40
+ try {
41
+ const { data } = await this.client.get(GET_SERVER_UTC_TIME);
42
+ const regex = /Date\.UTC\((.*?)\)/;
43
+ const match = data.match(regex);
44
+ if (match) {
45
+ const timestamp = new Function(`return Date.UTC(${match[1]})`)();
46
+ return timestamp / 1000;
47
+ }
48
+ else {
49
+ throw new Error('Failed to parse server time');
50
+ }
51
+ }
52
+ catch (e) {
53
+ throw new Error('网络异常,本次请求失败!');
54
+ }
55
+ }
38
56
  async getTimeNow() {
39
57
  try {
40
58
  const { data } = await this.client.get(GET_TIME_NOW);
@@ -118,12 +136,6 @@ class BiliAPI extends koishi_1.Service {
118
136
  throw new Error('网络异常,本次请求失败!');
119
137
  }
120
138
  }
121
- getUTCPlus8Time() {
122
- // 获取当前时间的UTC+8时间对象
123
- const currentTimeInUtc8 = new Date().toLocaleString('en-US', { timeZone: 'Asia/Shanghai' });
124
- // 将时间转换为Unix时间戳(单位:秒)
125
- return Math.floor(new Date(currentTimeInUtc8).getTime() / 1000);
126
- }
127
139
  disposeNotifier() { this.loginNotifier && this.loginNotifier.dispose(); }
128
140
  createNewClient() {
129
141
  this.jar = new tough_cookie_1.CookieJar();
@@ -17,7 +17,7 @@ declare class ComRegister {
17
17
  qqBot: Bot<Context>;
18
18
  qqguildBot: Bot<Context>;
19
19
  constructor(ctx: Context, config: ComRegister.Config);
20
- dynamicDetect(ctx: Context, bot: Bot<Context>, guildId: string, uid: string): () => Promise<void>;
20
+ dynamicDetect(ctx: Context, bot: Bot<Context>, guildId: string, uid: string): () => Promise<void | string[]>;
21
21
  liveDetect(ctx: Context, bot: Bot<Context>, guildId: string, roomId: string): () => Promise<void>;
22
22
  checkIfNeedSub(comNeed: boolean, subType: string, session: Session, data?: any): Promise<boolean>;
23
23
  getSubFromDatabase(ctx: Context): Promise<void>;
@@ -31,91 +31,80 @@ class ComRegister {
31
31
  this.qqguildBot = ctx.bots[ctx.bots.findIndex(bot => bot.platform === 'qqguild')];
32
32
  // 从数据库获取订阅
33
33
  this.getSubFromDatabase(ctx);
34
- /* ctx.command('test', { hidden: true, permissions: ['authority:5'] })
34
+ ctx.command('test', { hidden: true, /* permissions: ['authority:5'] */ })
35
35
  .subcommand('.cookies')
36
36
  .usage('测试指令,用于测试从数据库读取cookies')
37
37
  .action(async () => {
38
- await ctx.biliAPI.loadCookiesFromDatabase()
39
- })
40
-
38
+ await ctx.biliAPI.loadCookiesFromDatabase();
39
+ });
41
40
  ctx.command('test')
42
41
  .subcommand('.my')
43
42
  .usage('测试指令,用于测试获取自己信息')
44
43
  .example('test.my')
45
44
  .action(async () => {
46
- const content = await ctx.biliAPI.getMyselfInfo()
47
- console.log(content);
48
- })
49
-
45
+ const content = await ctx.biliAPI.getMyselfInfo();
46
+ console.log(content);
47
+ });
50
48
  ctx.command('test')
51
49
  .subcommand('.user <mid:string>')
52
50
  .usage('测试指令,用于测试获取用户信息')
53
51
  .example('test.user 用户UID')
54
52
  .action(async (_, mid) => {
55
- const content = await ctx.biliAPI.getUserInfo(mid)
56
- console.log(content);
57
- })
58
-
53
+ const content = await ctx.biliAPI.getUserInfo(mid);
54
+ console.log(content);
55
+ });
59
56
  ctx.command('test')
60
57
  .subcommand('.time')
61
58
  .usage('测试时间接口')
62
59
  .example('test.time')
63
60
  .action(async () => {
64
- const content = await ctx.biliAPI.getTimeNow()
65
- console.log(content);
66
- })
67
-
61
+ const content = await ctx.biliAPI.getTimeNow();
62
+ return content;
63
+ });
68
64
  ctx.command('test')
69
65
  .subcommand('.exist')
70
66
  .usage('测试写法')
71
67
  .example('test.exist')
72
68
  .action(async () => {
73
- let num = 1;
74
- console.log(num && `Hello World`);
75
- })
76
-
69
+ let num = 1;
70
+ console.log(num && `Hello World`);
71
+ });
77
72
  ctx.command('test')
78
73
  .subcommand('.gimg <uid:string> <index:number>')
79
74
  .usage('测试图片生成')
80
75
  .example('test.gimg')
81
- .action(async (_, uid, index) => {
82
- // 获取用户空间动态数据
83
- const { data } = await ctx.biliAPI.getUserSpaceDynamic(uid)
84
- const [pic] = await ctx.gimg.generateDynamicImg(data.items[index])
85
- return pic
86
- })
87
-
76
+ .action(async ({ session }, uid, index) => {
77
+ // 获取用户空间动态数据
78
+ const { data } = await ctx.biliAPI.getUserSpaceDynamic(uid);
79
+ // 获取动态推送图片
80
+ const { pic, buffer } = await ctx.gimg.generateDynamicImg(data.items[index]);
81
+ // 如果pic存在,则直接返回pic
82
+ if (pic)
83
+ return pic;
84
+ // pic不存在,说明使用的是page模式
85
+ await session.send(koishi_1.h.image(buffer, 'image/png'));
86
+ });
88
87
  ctx.command('test')
89
88
  .subcommand('.group')
90
89
  .usage('查看session groupId')
91
90
  .example('test group')
92
91
  .action(({ session }) => {
93
- console.log(session.event.channel);
94
- })
95
-
92
+ console.log(session.event.channel);
93
+ });
96
94
  ctx.command('test')
97
95
  .subcommand('.session')
98
96
  .usage('查看seesion')
99
97
  .example('test session')
100
98
  .action(({ session }) => {
101
- console.log(session);
102
- })
103
-
104
- // ctx.command('test')
105
- // .subcommand('.dynamic <uid:string>')
106
- // .usage('测试动态监测')
107
- // .example('test dynamic uid')
108
- // .action(({ session }, uid) => {
109
- // ctx.setInterval(this.test_dynamicDetect(ctx, session, uid), 30000)
110
- // })
111
-
99
+ console.log(session);
100
+ });
112
101
  ctx.command('test')
113
102
  .subcommand('.utc')
114
103
  .usage('获取当前UTC+8 Unix时间戳')
115
104
  .example('test utc')
116
- .action(() => {
117
- console.log(ctx.biliAPI.getUTCPlus8Time());
118
- }) */
105
+ .action(async ({ session }) => {
106
+ session.send((await ctx.biliAPI.getServerUTCTime()).toString());
107
+ });
119
108
  ctx.command('bili', 'bili-notify插件相关指令', { permissions: ['authority:3'] })
120
109
  .subcommand('.login', '登录B站之后才可以进行之后的操作')
121
110
  .usage('使用二维码登录,登录B站之后才可以进行之后的操作')
@@ -494,28 +483,31 @@ class ComRegister {
494
483
  }
495
484
  return;
496
485
  }
497
- let liveTime = (new Date(data.live_time).getTime()) / 1000;
498
- const string = await ctx.gimg.generateLiveImg(data, userData, data.live_status !== 1 ?
486
+ const { pic, buffer } = await ctx.gimg.generateLiveImg(data, userData, data.live_status !== 1 ?
499
487
  LiveType.NotLiveBroadcast :
500
488
  LiveType.LiveBroadcast);
501
- session.send(string);
489
+ // pic 存在,使用的是render模式
490
+ if (pic)
491
+ return pic;
492
+ // pic不存在,说明使用的是page模式
493
+ await session.send(koishi_1.h.image(buffer, 'image/png'));
502
494
  });
503
495
  }
504
496
  dynamicDetect(ctx, bot, guildId, uid) {
505
497
  let firstSubscription = true;
506
498
  let timePoint;
507
499
  // Test code
508
- // let timer = 0
500
+ let timer = 0;
509
501
  return async () => {
510
502
  // Test code
511
- /* console.log('timer:' + timer++);
503
+ console.log('timer:' + timer++);
512
504
  console.log('firstSubscription:' + firstSubscription);
513
505
  console.log(`timePoint: ${timePoint}`);
514
- console.log(`timePoint: ${ctx.gimg.unixTimestampToString(timePoint)}`); */
506
+ console.log(`timePoint: ${ctx.gimg.unixTimestampToString(timePoint)}`);
515
507
  // 第一次订阅判断
516
508
  if (firstSubscription) {
517
509
  // 设置第一次的时间点
518
- timePoint = ctx.biliAPI.getUTCPlus8Time();
510
+ timePoint = await ctx.biliAPI.getServerUTCTime();
519
511
  // 设置第一次为false
520
512
  firstSubscription = false;
521
513
  return;
@@ -544,13 +536,14 @@ class ComRegister {
544
536
  }
545
537
  // 获取数据内容
546
538
  const items = content.data.items;
547
- // 发送请求 默认只查看前五条数据
539
+ // 发送请求 默认只查看配置文件规定的数据
548
540
  for (let num = this.config.dynamicCheckNumber - 1; num >= 0; num--) {
549
541
  // 没有动态内容则直接跳过
550
542
  if (!items[num])
551
543
  continue;
552
544
  // Test code
553
- // console.log(`items[${num}].modules.module_author.pub_ts: ${ctx.gimg.unixTimestampToString(items[num].modules.module_author.pub_ts)}`);
545
+ console.log(`items[${num}].modules.module_author.pub_ts: ${items[num].modules.module_author.pub_ts}`);
546
+ console.log(`items[${num}].modules.module_author.pub_ts: ${ctx.gimg.unixTimestampToString(items[num].modules.module_author.pub_ts)}`);
554
547
  // 寻找发布时间比时间点时间更晚的动态
555
548
  if (items[num].modules.module_author.pub_ts > timePoint) {
556
549
  // 如果这是遍历的最后一条,将时间点设置为这条动态的发布时间
@@ -572,9 +565,15 @@ class ComRegister {
572
565
  let attempts = 3;
573
566
  for (let i = 0; i < attempts; i++) {
574
567
  try {
575
- const [pic] = await ctx.gimg.generateDynamicImg(items[num]);
576
- await bot.sendMessage(guildId, pic);
577
- break; // 如果成功,那么跳出循环
568
+ // 获取动态推送图片
569
+ const { pic, buffer } = await ctx.gimg.generateDynamicImg(items[num]);
570
+ // 如果pic存在,则直接返回pic
571
+ if (pic)
572
+ return await bot.sendMessage(guildId, pic);
573
+ // pic不存在,说明使用的是page模式
574
+ await bot.sendMessage(guildId, koishi_1.h.image(buffer, 'image/png'));
575
+ // 如果成功,那么跳出循环
576
+ break;
578
577
  }
579
578
  catch (e) {
580
579
  if (i === attempts - 1) { // 如果已经尝试了三次,那么抛出错误
@@ -594,6 +593,16 @@ class ComRegister {
594
593
  let uData;
595
594
  // 相当于锁的作用,防止上一个循环没处理完
596
595
  let flag = true;
596
+ async function sendLiveNotifyCard(data, uData, liveType) {
597
+ // 获取直播通知卡片
598
+ const { pic, buffer } = await ctx.gimg.generateLiveImg(data, uData, liveType);
599
+ // 推送直播信息
600
+ // pic 存在,使用的是render模式
601
+ if (pic)
602
+ return bot.sendMessage(guildId, pic);
603
+ // pic不存在,说明使用的是page模式
604
+ await bot.sendMessage(guildId, koishi_1.h.image(buffer, 'image/png'));
605
+ }
597
606
  return async () => {
598
607
  try {
599
608
  // console.log('start before' + ' ' + flag);
@@ -640,10 +649,8 @@ class ComRegister {
640
649
  if (data.live_status === 1) { // 当前正在直播
641
650
  // 设置开播时间
642
651
  liveTime = data.live_time;
643
- // 推送直播信息
644
- await bot.sendMessage(guildId, await ctx
645
- .gimg
646
- .generateLiveImg(data, uData, LiveType.LiveBroadcast));
652
+ // 发送直播通知卡片
653
+ sendLiveNotifyCard(data, uData, LiveType.LiveBroadcast);
647
654
  // 改变开播状态
648
655
  open = true;
649
656
  } // 未开播,直接返回
@@ -659,7 +666,7 @@ class ComRegister {
659
666
  // 下播了将定时器清零
660
667
  timer = 0;
661
668
  // 发送下播通知
662
- bot.sendMessage(guildId, `${uData.info.uname}下播啦,本次直播了${ctx.gimg.getTimeDifference(liveTime)}`);
669
+ bot.sendMessage(guildId, `${uData.info.uname}下播啦,本次直播了${await ctx.gimg.getTimeDifference(liveTime)}`);
663
670
  }
664
671
  // 未进循环,还未开播,继续循环
665
672
  break;
@@ -681,8 +688,8 @@ class ComRegister {
681
688
  }
682
689
  // 主播信息不会变,开播时刷新一次即可
683
690
  uData = userData;
684
- // 发送直播通知
685
- await bot.sendMessage(guildId, await ctx.gimg.generateLiveImg(data, uData, LiveType.StartBroadcasting));
691
+ // 发送直播通知卡片
692
+ sendLiveNotifyCard(data, uData, LiveType.StartBroadcasting);
686
693
  }
687
694
  else { // 还在直播
688
695
  if (this.config.pushTime > 0) {
@@ -691,10 +698,8 @@ class ComRegister {
691
698
  if (timer >= (6 * 60 * this.config.pushTime)) { // 到时间推送直播消息
692
699
  // 到时间重新计时
693
700
  timer = 0;
694
- // 发送状态信息
695
- bot.sendMessage(guildId, await ctx
696
- .gimg
697
- .generateLiveImg(data, uData, LiveType.LiveBroadcast));
701
+ // 发送直播通知卡片
702
+ sendLiveNotifyCard(data, uData, LiveType.LiveBroadcast);
698
703
  }
699
704
  }
700
705
  // 否则继续循环
@@ -1,3 +1,4 @@
1
+ /// <reference types="node" />
1
2
  import { Context, Schema, Service } from "koishi";
2
3
  declare module 'koishi' {
3
4
  interface Context {
@@ -9,14 +10,29 @@ declare class GenerateImg extends Service {
9
10
  config: GenerateImg.Config;
10
11
  constructor(ctx: Context, config: GenerateImg.Config);
11
12
  protected start(): void | Promise<void>;
12
- generateLiveImg(data: any, userData: any, liveStatus: number): Promise<string>;
13
- generateDynamicImg(data: any): Promise<string[]>;
14
- getLiveStatus(time: string, liveStatus: number): [string, string, boolean];
15
- getTimeDifference(dateString: string): string;
13
+ generateLiveImg(data: any, userData: any, liveStatus: number): Promise<{
14
+ buffer: Buffer;
15
+ pic?: undefined;
16
+ } | {
17
+ pic: string;
18
+ buffer?: undefined;
19
+ }>;
20
+ generateDynamicImg(data: any): Promise<{
21
+ buffer: Buffer;
22
+ link: string;
23
+ pic?: undefined;
24
+ } | {
25
+ pic: string;
26
+ link: string;
27
+ buffer?: undefined;
28
+ }>;
29
+ getLiveStatus(time: string, liveStatus: number): Promise<[string, string, boolean]>;
30
+ getTimeDifference(dateString: string): Promise<string>;
16
31
  unixTimestampToString(timestamp: number): string;
17
32
  }
18
33
  declare namespace GenerateImg {
19
34
  interface Config {
35
+ renderType: number;
20
36
  cardColorStart: string;
21
37
  cardColorEnd: string;
22
38
  font: string;