koishi-plugin-bilibili-notify 1.0.2 → 1.0.4-alpha.0

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
+ console.log(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 () => {
106
+ console.log(await ctx.biliAPI.getServerUTCTime());
107
+ });
119
108
  ctx.command('bili', 'bili-notify插件相关指令', { permissions: ['authority:3'] })
120
109
  .subcommand('.login', '登录B站之后才可以进行之后的操作')
121
110
  .usage('使用二维码登录,登录B站之后才可以进行之后的操作')
@@ -494,11 +483,14 @@ 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) {
@@ -515,7 +507,7 @@ class ComRegister {
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;
@@ -572,9 +564,15 @@ class ComRegister {
572
564
  let attempts = 3;
573
565
  for (let i = 0; i < attempts; i++) {
574
566
  try {
575
- const [pic] = await ctx.gimg.generateDynamicImg(items[num]);
576
- await bot.sendMessage(guildId, pic);
577
- break; // 如果成功,那么跳出循环
567
+ // 获取动态推送图片
568
+ const { pic, buffer } = await ctx.gimg.generateDynamicImg(items[num]);
569
+ // 如果pic存在,则直接返回pic
570
+ if (pic)
571
+ return await bot.sendMessage(guildId, pic);
572
+ // pic不存在,说明使用的是page模式
573
+ await bot.sendMessage(guildId, koishi_1.h.image(buffer, 'image/png'));
574
+ // 如果成功,那么跳出循环
575
+ break;
578
576
  }
579
577
  catch (e) {
580
578
  if (i === attempts - 1) { // 如果已经尝试了三次,那么抛出错误
@@ -594,6 +592,16 @@ class ComRegister {
594
592
  let uData;
595
593
  // 相当于锁的作用,防止上一个循环没处理完
596
594
  let flag = true;
595
+ async function sendLiveNotifyCard(data, uData, liveType) {
596
+ // 获取直播通知卡片
597
+ const { pic, buffer } = await ctx.gimg.generateLiveImg(data, uData, liveType);
598
+ // 推送直播信息
599
+ // pic 存在,使用的是render模式
600
+ if (pic)
601
+ return bot.sendMessage(guildId, pic);
602
+ // pic不存在,说明使用的是page模式
603
+ await bot.sendMessage(guildId, koishi_1.h.image(buffer, 'image/png'));
604
+ }
597
605
  return async () => {
598
606
  try {
599
607
  // console.log('start before' + ' ' + flag);
@@ -640,10 +648,8 @@ class ComRegister {
640
648
  if (data.live_status === 1) { // 当前正在直播
641
649
  // 设置开播时间
642
650
  liveTime = data.live_time;
643
- // 推送直播信息
644
- await bot.sendMessage(guildId, await ctx
645
- .gimg
646
- .generateLiveImg(data, uData, LiveType.LiveBroadcast));
651
+ // 发送直播通知卡片
652
+ sendLiveNotifyCard(data, uData, LiveType.LiveBroadcast);
647
653
  // 改变开播状态
648
654
  open = true;
649
655
  } // 未开播,直接返回
@@ -659,7 +665,7 @@ class ComRegister {
659
665
  // 下播了将定时器清零
660
666
  timer = 0;
661
667
  // 发送下播通知
662
- bot.sendMessage(guildId, `${uData.info.uname}下播啦,本次直播了${ctx.gimg.getTimeDifference(liveTime)}`);
668
+ bot.sendMessage(guildId, `${uData.info.uname}下播啦,本次直播了${await ctx.gimg.getTimeDifference(liveTime)}`);
663
669
  }
664
670
  // 未进循环,还未开播,继续循环
665
671
  break;
@@ -681,8 +687,8 @@ class ComRegister {
681
687
  }
682
688
  // 主播信息不会变,开播时刷新一次即可
683
689
  uData = userData;
684
- // 发送直播通知
685
- await bot.sendMessage(guildId, await ctx.gimg.generateLiveImg(data, uData, LiveType.StartBroadcasting));
690
+ // 发送直播通知卡片
691
+ sendLiveNotifyCard(data, uData, LiveType.StartBroadcasting);
686
692
  }
687
693
  else { // 还在直播
688
694
  if (this.config.pushTime > 0) {
@@ -691,10 +697,8 @@ class ComRegister {
691
697
  if (timer >= (6 * 60 * this.config.pushTime)) { // 到时间推送直播消息
692
698
  // 到时间重新计时
693
699
  timer = 0;
694
- // 发送状态信息
695
- bot.sendMessage(guildId, await ctx
696
- .gimg
697
- .generateLiveImg(data, uData, LiveType.LiveBroadcast));
700
+ // 发送直播通知卡片
701
+ sendLiveNotifyCard(data, uData, LiveType.LiveBroadcast);
698
702
  }
699
703
  }
700
704
  // 否则继续循环
@@ -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,16 +10,32 @@ 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;
38
+ font: string;
22
39
  }
23
40
  const Config: Schema<Config>;
24
41
  }