koishi-plugin-bilibili-notify 1.2.2-alpha.0 → 1.2.2-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.
package/lib/biliAPI.d.ts CHANGED
@@ -13,6 +13,7 @@ declare class BiliAPI extends Service {
13
13
  loginData: any;
14
14
  loginNotifier: Notifier;
15
15
  refreshCookieTimer: Function;
16
+ loginInfoIsLoaded: boolean;
16
17
  constructor(ctx: Context);
17
18
  protected start(): void | Promise<void>;
18
19
  getServerUTCTime(): Promise<number>;
@@ -29,6 +30,7 @@ declare class BiliAPI extends Service {
29
30
  createNewClient(): void;
30
31
  getTimeOfUTC8(): number;
31
32
  getCookies(): string;
33
+ getLoginInfoIsLoaded(): boolean;
32
34
  getLoginInfoFromDB(): Promise<{
33
35
  cookies: any;
34
36
  refresh_token: string;
package/lib/biliAPI.js CHANGED
@@ -29,6 +29,7 @@ class BiliAPI extends koishi_1.Service {
29
29
  loginData;
30
30
  loginNotifier;
31
31
  refreshCookieTimer;
32
+ loginInfoIsLoaded = false;
32
33
  constructor(ctx) {
33
34
  super(ctx, 'biliAPI');
34
35
  }
@@ -165,6 +166,9 @@ class BiliAPI extends koishi_1.Service {
165
166
  cookies = JSON.stringify(this.jar.serializeSync().cookies);
166
167
  return cookies;
167
168
  }
169
+ getLoginInfoIsLoaded() {
170
+ return this.loginInfoIsLoaded;
171
+ }
168
172
  async getLoginInfoFromDB() {
169
173
  // 读取数据库获取cookies
170
174
  const data = (await this.ctx.database.get('loginBili', 1))[0];
@@ -229,6 +233,8 @@ class BiliAPI extends koishi_1.Service {
229
233
  });
230
234
  this.jar.setCookieSync(cookie, `http${cookie.secure ? 's' : ''}://${cookie.domain}${cookie.path}`, {});
231
235
  });
236
+ // Login info is loaded
237
+ this.loginInfoIsLoaded = true;
232
238
  // restart plugin check
233
239
  this.checkIfTokenNeedRefresh(refresh_token, csrf);
234
240
  // enable refresh cookies detect
@@ -252,7 +258,7 @@ class BiliAPI extends koishi_1.Service {
252
258
  }).value;
253
259
  // 检查是否需要更新
254
260
  this.checkIfTokenNeedRefresh(refresh_token, csrf);
255
- }, 43200000);
261
+ }, 3600000);
256
262
  }
257
263
  async checkIfTokenNeedRefresh(refreshToken, csrf, times = 3) {
258
264
  // 定义数据
@@ -24,6 +24,7 @@ declare class ComRegister {
24
24
  redBot: Bot<Context>;
25
25
  telegramBot: Bot<Context>;
26
26
  satoriBot: Bot<Context>;
27
+ chronocatBot: Bot<Context>;
27
28
  constructor(ctx: Context, config: ComRegister.Config);
28
29
  dynamicDetect(ctx: Context, bot: Bot<Context>, uid: string, guildId: Array<string>): () => Promise<void>;
29
30
  sendMsg(targets: Array<string>, bot: Bot<Context>, content: any): Promise<void>;
@@ -31,6 +32,7 @@ declare class ComRegister {
31
32
  subShow(): string;
32
33
  checkIfNeedSub(comNeed: boolean, subType: string, session: Session, data?: any): Promise<boolean>;
33
34
  updateSubNotifier(ctx: Context): void;
35
+ checkIfLoginInfoIsLoaded(ctx: Context): Promise<unknown>;
34
36
  getSubFromDatabase(ctx: Context): Promise<void>;
35
37
  unsubSingle(ctx: Context, id: string, type: number): string;
36
38
  checkIfIsLogin(ctx: Context): Promise<boolean>;
@@ -33,6 +33,8 @@ class ComRegister {
33
33
  telegramBot;
34
34
  // Satori机器人
35
35
  satoriBot;
36
+ // Chronocat机器人
37
+ chronocatBot;
36
38
  constructor(ctx, config) {
37
39
  this.logger = ctx.logger('commandRegister');
38
40
  this.config = config;
@@ -57,6 +59,9 @@ class ComRegister {
57
59
  case 'satori':
58
60
  this.satoriBot = bot;
59
61
  break;
62
+ case 'chronocat':
63
+ this.chronocatBot = bot;
64
+ break;
60
65
  }
61
66
  });
62
67
  // 从数据库获取订阅
@@ -146,11 +151,17 @@ class ComRegister {
146
151
  })
147
152
 
148
153
  testCom
149
- .subcommand('.refresh')
150
- .usage('测试cookie刷新方法')
151
- .example('test refresh')
154
+ .subcommand('.livestop', '发送下播提示语测试')
155
+ .usage('发送下播提示语测试')
156
+ .example('test livestop')
152
157
  .action(async ({ session }) => {
153
- ctx.biliAPI.test_refresh_token()
158
+ const { data } = await ctx.biliAPI.getMasterInfo('194484313')
159
+ console.log(data);
160
+ await session.send(
161
+ <>
162
+ <img width="10px" height="10px" src="https://koishi.chat/logo.png"/>
163
+ </>
164
+ )
154
165
  }) */
155
166
  const biliCom = ctx.command('bili', 'bili-notify插件相关指令', { permissions: ['authority:3'] });
156
167
  biliCom.subcommand('.login', '登录B站之后才可以进行之后的操作')
@@ -311,6 +322,7 @@ class ComRegister {
311
322
  case 'onebot':
312
323
  case 'telegram':
313
324
  case 'satori':
325
+ case 'chronocat':
314
326
  case 'qq':
315
327
  case 'qqguild': break;
316
328
  default: return '暂不支持该平台';
@@ -404,6 +416,10 @@ class ComRegister {
404
416
  okGuild = await checkIfGuildHasJoined(this.satoriBot);
405
417
  break;
406
418
  }
419
+ case 'chronocat': {
420
+ okGuild = await checkIfGuildHasJoined(this.chronocatBot);
421
+ break;
422
+ }
407
423
  default: {
408
424
  // 发送错误提示并返回
409
425
  session.send('您尚未配置任何QQ群相关机器人,不能对QQ群进行操作');
@@ -517,6 +533,9 @@ class ComRegister {
517
533
  case 'satori':
518
534
  bot = this.satoriBot;
519
535
  break;
536
+ case 'chronocat':
537
+ bot = this.chronocatBot;
538
+ break;
520
539
  default: {
521
540
  this.logger.warn(`${uid}非法调用 dynamic 指令,不支持该平台`);
522
541
  return '非法调用';
@@ -563,6 +582,9 @@ class ComRegister {
563
582
  case 'satori':
564
583
  bot = this.satoriBot;
565
584
  break;
585
+ case 'chronocat':
586
+ bot = this.chronocatBot;
587
+ break;
566
588
  default: {
567
589
  this.logger.warn(`${roomId}非法调用 dynamic 指令,不支持该平台`);
568
590
  return `${roomId}非法调用 dynamic 指令`;
@@ -616,6 +638,19 @@ class ComRegister {
616
638
  // pic不存在,说明使用的是page模式
617
639
  await session.send(koishi_1.h.image(buffer, 'image/png'));
618
640
  });
641
+ biliCom
642
+ .subcommand('.bot', '查询当前拥有的机器人信息', { hidden: true })
643
+ .usage('查询当前拥有的机器人信息')
644
+ .example('bili bot 查询当前拥有的机器人信息')
645
+ .action(() => {
646
+ this.logger.info('开始输出BOT信息');
647
+ ctx.bots.forEach(bot => {
648
+ this.logger.info('--------------------------------');
649
+ this.logger.info('平台:' + bot.platform);
650
+ this.logger.info('名称:' + bot.user.name);
651
+ this.logger.info('--------------------------------');
652
+ });
653
+ });
619
654
  }
620
655
  dynamicDetect(ctx, bot, uid, guildId) {
621
656
  let firstSubscription = true;
@@ -740,18 +775,34 @@ class ComRegister {
740
775
  let uData;
741
776
  // 相当于锁的作用,防止上一个循环没处理完
742
777
  let flag = true;
743
- const sendLiveNotifyCard = async (data, uData, liveType) => {
778
+ const sendLiveNotifyCard = async (data, uData, liveType, liveStartMsg, atAll) => {
744
779
  let attempts = 3;
745
780
  for (let i = 0; i < attempts; i++) {
746
781
  try {
747
782
  // 获取直播通知卡片
748
783
  const { pic, buffer } = await ctx.gimg.generateLiveImg(data, uData, liveType);
749
784
  // 推送直播信息
750
- // pic 存在,使用的是render模式
751
- if (pic)
752
- return await this.sendMsg(guildId, bot, pic);
753
- // pic不存在,说明使用的是page模式
754
- await this.sendMsg(guildId, bot, koishi_1.h.image(buffer, 'image/png'));
785
+ if (!liveStartMsg) {
786
+ // pic 存在,使用的是render模式
787
+ if (pic)
788
+ return await this.sendMsg(guildId, bot, pic);
789
+ // pic不存在,说明使用的是page模式
790
+ await this.sendMsg(guildId, bot, koishi_1.h.image(buffer, 'image/png'));
791
+ }
792
+ else if (liveStartMsg && atAll) {
793
+ // pic 存在,使用的是render模式
794
+ if (pic)
795
+ return await this.sendMsg(guildId, bot, pic + (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("at", { type: "all" }), " ", liveStartMsg, " "] }));
796
+ // pic不存在,说明使用的是page模式
797
+ await this.sendMsg(guildId, bot, koishi_1.h.image(buffer, 'image/png' + ' ' + (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("at", { type: "all" }), " ", liveStartMsg] })));
798
+ }
799
+ else {
800
+ // pic 存在,使用的是render模式
801
+ if (pic)
802
+ return await this.sendMsg(guildId, bot, pic + liveStartMsg);
803
+ // pic不存在,说明使用的是page模式
804
+ await this.sendMsg(guildId, bot, koishi_1.h.image(buffer, 'image/png' + ' ' + liveStartMsg));
805
+ }
755
806
  // 成功则跳出循环
756
807
  break;
757
808
  }
@@ -869,16 +920,16 @@ class ComRegister {
869
920
  // 定义开播通知语
870
921
  let liveStartMsg = this.config.customLiveStart
871
922
  .replace('-name', uData.info.uname)
872
- .replace('-time', await ctx.gimg.getTimeDifference(liveTime));
873
- // 发送直播通知卡片
874
- await sendLiveNotifyCard(data, uData, LiveType.StartBroadcasting);
923
+ .replace('-time', await ctx.gimg.getTimeDifference(liveTime))
924
+ .replace('-link', `https://live.bilibili.com/${data.short_id === 0 ? data.room_id : data.short_id}`);
875
925
  // 判断是否需要@全体成员
876
926
  if (this.config.liveStartAtAll) {
877
927
  // 发送@全体成员通知
878
- await this.sendMsg(guildId, bot, (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("at", { type: "all" }), " ", liveStartMsg, " "] }));
928
+ await sendLiveNotifyCard(data, uData, LiveType.StartBroadcasting, liveStartMsg, true);
879
929
  }
880
930
  else {
881
- await this.sendMsg(guildId, bot, liveStartMsg);
931
+ // 发送直播通知卡片
932
+ await sendLiveNotifyCard(data, uData, LiveType.StartBroadcasting, liveStartMsg);
882
933
  }
883
934
  }
884
935
  else { // 还在直播
@@ -966,24 +1017,33 @@ class ComRegister {
966
1017
  // 设置更新后的提示
967
1018
  this.subNotifier = ctx.notifier.create(table);
968
1019
  }
1020
+ async checkIfLoginInfoIsLoaded(ctx) {
1021
+ return new Promise(resolve => {
1022
+ const check = () => {
1023
+ if (!ctx.biliAPI.getLoginInfoIsLoaded()) {
1024
+ ctx.setTimeout(check, 500);
1025
+ }
1026
+ else {
1027
+ resolve('success');
1028
+ }
1029
+ };
1030
+ check();
1031
+ });
1032
+ }
969
1033
  async getSubFromDatabase(ctx) {
970
- this.logger.info('开始执行数据库读取操作');
971
- // 检查登录状态
972
- const isLogin = await this.checkIfIsLogin(ctx);
973
- // log
974
- this.logger.info(`登录状态:${isLogin}`);
1034
+ // 判断登录信息是否已加载完毕
1035
+ await this.checkIfLoginInfoIsLoaded(ctx);
975
1036
  // 如果未登录,则直接返回
976
- if (!(await this.checkIfIsLogin(ctx)))
1037
+ if (!(await this.checkIfIsLogin(ctx))) {
1038
+ // log
1039
+ this.logger.info(`账号未登录,请登录`);
977
1040
  return;
978
- this.logger.info('已登录账号');
1041
+ }
979
1042
  // 已存在订阅管理对象,不再进行订阅操作
980
1043
  if (this.subManager.length !== 0)
981
1044
  return;
982
- this.logger.info('不存在订阅管理对象');
983
1045
  // 从数据库中获取数据
984
1046
  const subData = await ctx.database.get('bilibili', { id: { $gt: 0 } });
985
- this.logger.info('已从数据库获取到数据,数据为:');
986
- this.logger.info(subData);
987
1047
  // 设定订阅数量
988
1048
  this.num = subData.length;
989
1049
  // 如果订阅数量超过三个则数据库被非法修改
@@ -1028,6 +1088,9 @@ class ComRegister {
1028
1088
  case 'satori':
1029
1089
  bot = this.satoriBot;
1030
1090
  break;
1091
+ case 'chronocat':
1092
+ bot = this.chronocatBot;
1093
+ break;
1031
1094
  default: {
1032
1095
  // 本条数据被篡改,删除该条订阅
1033
1096
  ctx.database.remove('bilibili', { id: sub.id });
@@ -1047,8 +1110,6 @@ class ComRegister {
1047
1110
  try {
1048
1111
  // 获取用户信息
1049
1112
  content = await ctx.biliAPI.getUserInfo(sub.uid);
1050
- // log
1051
- this.logger.info(`UID:${sub.uid} 获取到用户信息`);
1052
1113
  // 成功则跳出循环
1053
1114
  break;
1054
1115
  }
@@ -1094,8 +1155,6 @@ class ComRegister {
1094
1155
  this.logger.info(`UID:${sub.uid} 房间号被篡改,自动取消订阅`);
1095
1156
  return;
1096
1157
  }
1097
- // log
1098
- this.logger.info(`UID:${sub.uid} 开始构建订阅对象`);
1099
1158
  // 构建订阅对象
1100
1159
  let subManagerItem = {
1101
1160
  id: sub.id,
@@ -1108,34 +1167,24 @@ class ComRegister {
1108
1167
  liveDispose: null,
1109
1168
  dynamicDispose: null
1110
1169
  };
1111
- // log
1112
- this.logger.info(`UID:${sub.uid} 订阅对象构建成功,开始进行订阅操作`);
1113
1170
  // 判断需要订阅的服务
1114
1171
  if (sub.dynamic) { // 需要订阅动态
1115
1172
  // 开始循环检测
1116
1173
  const dispose = ctx.setInterval(this.dynamicDetect(ctx, bot, sub.uid, targetArr), this.config.dynamicLoopTime * 1000);
1117
1174
  // 保存销毁函数
1118
1175
  subManagerItem.dynamicDispose = dispose;
1119
- // log
1120
- this.logger.info(`UID:${sub.uid} 成功订阅动态`);
1121
1176
  }
1122
1177
  if (sub.live) { // 需要订阅直播
1123
1178
  // 开始循环检测
1124
1179
  const dispose = ctx.setInterval(this.liveDetect(ctx, bot, sub.room_id, targetArr), this.config.liveLoopTime * 1000);
1125
1180
  // 保存销毁函数
1126
1181
  subManagerItem.liveDispose = dispose;
1127
- // log
1128
- this.logger.info(`UID:${sub.uid} 成功订阅直播`);
1129
1182
  }
1130
1183
  // 保存新订阅对象
1131
1184
  this.subManager.push(subManagerItem);
1132
- // log
1133
- this.logger.info(`UID:${sub.uid} 成功保存订阅对象`);
1134
1185
  }
1135
1186
  // 在控制台中显示订阅对象
1136
1187
  this.updateSubNotifier(ctx);
1137
- // log
1138
- this.logger.info(`数据库读取操作已完成`);
1139
1188
  }
1140
1189
  unsubSingle(ctx, id /* UID或RoomId */, type /* 0取消Live订阅,1取消Dynamic订阅 */) {
1141
1190
  let index;
package/lib/index.js CHANGED
@@ -80,8 +80,8 @@ exports.Config = koishi_1.Schema.object({
80
80
  .default(1)
81
81
  .description('设定隔多长时间推送一次直播状态,单位为小时,默认为一小时'),
82
82
  customLiveStart: koishi_1.Schema.string()
83
- .default('-name开播啦')
84
- .description('自定义开播提示语,-name代表UP昵称。例如-name开播啦,会发送为xxxUP开播啦'),
83
+ .default('-name开播啦 -link')
84
+ .description('自定义开播提示语,-name代表UP昵称,-link代表直播间链接(如果使用的是QQ官方机器人,请不要使用)。例如-name开播啦,会发送为xxxUP开播啦'),
85
85
  customLiveEnd: koishi_1.Schema.string()
86
86
  .default('-name下播啦,本次直播了-time')
87
87
  .description('自定义下播提示语,-name代表UP昵称,-time代表开播时长。例如-name下播啦,本次直播了-time,会发送为xxxUP下播啦,直播时长为xx小时xx分钟xx秒'),
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.2-alpha.0",
4
+ "version": "1.2.2-beta.1",
5
5
  "contributors": [
6
6
  "Akokko <admin@akokko.com>"
7
7
  ],
package/readme.md CHANGED
@@ -116,6 +116,8 @@
116
116
  - ver 1.2.0 添加屏蔽转发动态功能,添加发送动态卡片时附带文本信息和动态链接功能,支持订阅哔哩哔哩番剧出差
117
117
  - ver 1.2.1 现已支持Satori平台(实验性)
118
118
  - ver 1.2.2-alpha.0 bug测试
119
+ - ver 1.2.2-beta.0 修复重启koishi后,提示没有任何订阅的bug,新增对chronocat的支持(实验性)
120
+ - ver 1.2.2-beta.1 现已支持直播开播发送链接(实验性)
119
121
 
120
122
  ## 交流群
121
123
  801338523 使用问题或bug都可以在群里提出