koishi-plugin-bilibili-notify 3.0.0-alpha.0 → 3.0.0-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/blive.d.ts +2 -3
- package/lib/blive.js +7 -14
- package/lib/comRegister.d.ts +24 -26
- package/lib/comRegister.js +373 -257
- package/lib/generateImg.js +2 -2
- package/lib/index.d.ts +2 -1
- package/lib/index.js +8 -1
- package/package.json +1 -1
- package/readme.md +1 -0
package/lib/comRegister.js
CHANGED
|
@@ -18,25 +18,14 @@ var LiveType;
|
|
|
18
18
|
LiveType[LiveType["StopBroadcast"] = 3] = "StopBroadcast";
|
|
19
19
|
})(LiveType || (LiveType = {}));
|
|
20
20
|
class ComRegister {
|
|
21
|
-
// 必须服务
|
|
22
21
|
static inject = ['ba', 'gi', 'database', 'bl', 'sm'];
|
|
23
|
-
// 定义数组:QQ相关bot
|
|
24
22
|
qqRelatedBotList = ['qq', 'onebot', 'red', 'satori', 'chronocat'];
|
|
25
|
-
// logger
|
|
26
23
|
logger;
|
|
27
|
-
// config
|
|
28
24
|
config;
|
|
29
|
-
// 登录定时器
|
|
30
25
|
loginTimer;
|
|
31
|
-
// 订阅数量
|
|
32
26
|
num = 0;
|
|
33
|
-
// 重启次数
|
|
34
27
|
rebootCount = 0;
|
|
35
|
-
// 订阅通知
|
|
36
28
|
subNotifier;
|
|
37
|
-
// Context
|
|
38
|
-
ctx;
|
|
39
|
-
// 订阅管理器
|
|
40
29
|
subManager = [];
|
|
41
30
|
// 检查登录数据库是否有数据
|
|
42
31
|
loginDBData;
|
|
@@ -48,10 +37,8 @@ class ComRegister {
|
|
|
48
37
|
sendMsgFunc;
|
|
49
38
|
// 构造函数
|
|
50
39
|
constructor(ctx, config) {
|
|
51
|
-
// 将ctx赋值给类属性
|
|
52
|
-
this.ctx = ctx;
|
|
53
40
|
// 初始化
|
|
54
|
-
this.init(config);
|
|
41
|
+
this.init(ctx, config);
|
|
55
42
|
// 注册指令
|
|
56
43
|
const statusCom = ctx.command('status', '插件状态相关指令', { permissions: ['authority:5'] });
|
|
57
44
|
statusCom.subcommand('.dyn', '查看动态监测运行状态')
|
|
@@ -164,7 +151,7 @@ class ComRegister {
|
|
|
164
151
|
// 销毁定时器
|
|
165
152
|
this.loginTimer();
|
|
166
153
|
// 订阅之前的订阅
|
|
167
|
-
await this.loadSubFromDatabase();
|
|
154
|
+
await this.loadSubFromDatabase(ctx);
|
|
168
155
|
// 清除控制台通知
|
|
169
156
|
ctx.ba.disposeNotifier();
|
|
170
157
|
// 发送成功登录推送
|
|
@@ -202,14 +189,17 @@ class ComRegister {
|
|
|
202
189
|
// 取消单个订阅
|
|
203
190
|
if (options.live || options.dynamic) {
|
|
204
191
|
if (options.live)
|
|
205
|
-
await session.send(this.unsubSingle(sub.roomId, 0)); /* 0为取消订阅Live */
|
|
192
|
+
await session.send(this.unsubSingle(ctx, sub.roomId, 0)); /* 0为取消订阅Live */
|
|
206
193
|
if (options.dynamic)
|
|
207
|
-
await session.send(this.unsubSingle(sub.uid, 1)); /* 1为取消订阅Dynamic */
|
|
194
|
+
await session.send(this.unsubSingle(ctx, sub.uid, 1)); /* 1为取消订阅Dynamic */
|
|
208
195
|
// 将存在flag设置为true
|
|
209
196
|
exist = true;
|
|
210
197
|
// 结束循环
|
|
211
198
|
return;
|
|
212
199
|
}
|
|
200
|
+
// 取消全部订阅 执行dispose方法,销毁定时器
|
|
201
|
+
if (sub.live)
|
|
202
|
+
this.subManager[i].liveDispose();
|
|
213
203
|
// 从数据库中删除订阅
|
|
214
204
|
await ctx.database.remove('bilibili', { uid: this.subManager[i].uid });
|
|
215
205
|
// 将该订阅对象从订阅管理对象中移除
|
|
@@ -232,7 +222,7 @@ class ComRegister {
|
|
|
232
222
|
// 发送成功通知
|
|
233
223
|
await session.send('已取消订阅该用户');
|
|
234
224
|
// 更新控制台提示
|
|
235
|
-
this.updateSubNotifier();
|
|
225
|
+
this.updateSubNotifier(ctx);
|
|
236
226
|
// 将存在flag设置为true
|
|
237
227
|
exist = true;
|
|
238
228
|
}
|
|
@@ -264,7 +254,7 @@ class ComRegister {
|
|
|
264
254
|
return '直播订阅已达上限,请取消部分直播订阅后再进行订阅';
|
|
265
255
|
}
|
|
266
256
|
// 检查是否登录
|
|
267
|
-
if (!(await this.checkIfIsLogin())) {
|
|
257
|
+
if (!(await this.checkIfIsLogin(ctx))) {
|
|
268
258
|
// 未登录直接返回
|
|
269
259
|
return '请使用指令bili login登录后再进行订阅操作';
|
|
270
260
|
}
|
|
@@ -272,7 +262,7 @@ class ComRegister {
|
|
|
272
262
|
if (!mid)
|
|
273
263
|
return '请输入用户uid';
|
|
274
264
|
// 订阅对象
|
|
275
|
-
const subUserData = await this.subUserInBili(mid);
|
|
265
|
+
const subUserData = await this.subUserInBili(ctx, mid);
|
|
276
266
|
// 判断是否订阅对象存在
|
|
277
267
|
if (!subUserData.flag)
|
|
278
268
|
return '订阅对象失败,请稍后重试!';
|
|
@@ -288,7 +278,6 @@ class ComRegister {
|
|
|
288
278
|
channelId: group,
|
|
289
279
|
dynamic: true,
|
|
290
280
|
live: true,
|
|
291
|
-
liveDanmaku: false,
|
|
292
281
|
atAll: options.atAll
|
|
293
282
|
});
|
|
294
283
|
});
|
|
@@ -308,7 +297,7 @@ class ComRegister {
|
|
|
308
297
|
for (const [index, { channelIdArr, platform }] of target.entries()) {
|
|
309
298
|
if (channelIdArr.length > 0) { // 输入了推送群号或频道号
|
|
310
299
|
// 拿到对应的bot
|
|
311
|
-
const bot = this.getBot(platform);
|
|
300
|
+
const bot = this.getBot(ctx, platform);
|
|
312
301
|
// 判断是否配置了对应平台的机器人
|
|
313
302
|
if (!ctx.bots.some(bot => bot.platform === platform)) {
|
|
314
303
|
// 发送提示消息
|
|
@@ -348,7 +337,7 @@ class ComRegister {
|
|
|
348
337
|
// 判断targetArr是否为空
|
|
349
338
|
if (target.length === 0) {
|
|
350
339
|
// 为空则默认为当前环境
|
|
351
|
-
target = [{ channelIdArr: [{ channelId: session.event.channel.id, dynamic: true, live: true,
|
|
340
|
+
target = [{ channelIdArr: [{ channelId: session.event.channel.id, dynamic: true, live: true, atAll: options.atAll ? options.atAll : false }], platform: session.event.platform }];
|
|
352
341
|
// 没有满足条件的群组或频道
|
|
353
342
|
await session.send('没有满足条件的群组或频道,默认订阅到当前聊天环境');
|
|
354
343
|
}
|
|
@@ -359,7 +348,7 @@ class ComRegister {
|
|
|
359
348
|
}
|
|
360
349
|
else {
|
|
361
350
|
// 未填写群号或频道号,默认为当前环境
|
|
362
|
-
target = [{ channelIdArr: [{ channelId: session.event.channel.id, dynamic: true, live: true,
|
|
351
|
+
target = [{ channelIdArr: [{ channelId: session.event.channel.id, dynamic: true, live: true, atAll: options.atAll ? options.atAll : false }], platform: session.event.platform }];
|
|
363
352
|
// 发送提示消息
|
|
364
353
|
await session.send('没有填写群号或频道号,默认订阅到当前聊天环境');
|
|
365
354
|
}
|
|
@@ -367,7 +356,7 @@ class ComRegister {
|
|
|
367
356
|
}
|
|
368
357
|
else {
|
|
369
358
|
// 用户直接订阅,将当前环境赋值给target
|
|
370
|
-
target = [{ channelIdArr: [{ channelId: session.event.channel.id, dynamic: true, live: true,
|
|
359
|
+
target = [{ channelIdArr: [{ channelId: session.event.channel.id, dynamic: true, live: true, atAll: options.atAll ? options.atAll : false }], platform: session.event.platform }];
|
|
371
360
|
}
|
|
372
361
|
}
|
|
373
362
|
// 定义外围变量
|
|
@@ -437,10 +426,12 @@ class ComRegister {
|
|
|
437
426
|
this.logger.error('bili sub指令 getMasterInfo() 发生了错误,错误为:' + e.message);
|
|
438
427
|
return '订阅出错啦,请重试';
|
|
439
428
|
}
|
|
429
|
+
// 定义live销毁函数
|
|
430
|
+
let liveDispose;
|
|
440
431
|
// 订阅直播
|
|
441
432
|
if (liveMsg) {
|
|
442
433
|
// 连接到服务器
|
|
443
|
-
this.liveDetectWithListener(roomId, target);
|
|
434
|
+
this.liveDetectWithListener(ctx, roomId, target);
|
|
444
435
|
// 发送订阅消息通知
|
|
445
436
|
await session.send(`订阅${userData.info.uname}直播通知`);
|
|
446
437
|
}
|
|
@@ -448,7 +439,7 @@ class ComRegister {
|
|
|
448
439
|
if (dynamicMsg) {
|
|
449
440
|
// 判断是否开启动态监测
|
|
450
441
|
if (!this.dynamicDispose) {
|
|
451
|
-
this.enableDynamicDetect();
|
|
442
|
+
this.enableDynamicDetect(ctx);
|
|
452
443
|
}
|
|
453
444
|
// 发送订阅消息通知
|
|
454
445
|
await session.send(`订阅${userData.info.uname}动态通知`);
|
|
@@ -474,9 +465,10 @@ class ComRegister {
|
|
|
474
465
|
platform: session.event.platform,
|
|
475
466
|
live: liveMsg,
|
|
476
467
|
dynamic: dynamicMsg,
|
|
468
|
+
liveDispose
|
|
477
469
|
});
|
|
478
470
|
// 新增订阅展示到控制台
|
|
479
|
-
this.updateSubNotifier();
|
|
471
|
+
this.updateSubNotifier(ctx);
|
|
480
472
|
});
|
|
481
473
|
biliCom
|
|
482
474
|
.subcommand('.status <roomId:string>', '查询主播当前直播状态', { hidden: true })
|
|
@@ -532,19 +524,25 @@ class ComRegister {
|
|
|
532
524
|
await session.send('已发送消息,如未收到则说明您的机器人不支持发送私聊消息或您的信息填写有误');
|
|
533
525
|
});
|
|
534
526
|
}
|
|
535
|
-
async init(config) {
|
|
527
|
+
async init(ctx, config) {
|
|
536
528
|
// 设置logger
|
|
537
|
-
this.logger =
|
|
529
|
+
this.logger = ctx.logger('cr');
|
|
538
530
|
// 将config设置给类属性
|
|
539
531
|
this.config = config;
|
|
540
532
|
// 拿到私人机器人实例
|
|
541
|
-
this.privateBot =
|
|
533
|
+
this.privateBot = ctx.bots.find(bot => bot.platform === config.master.platform);
|
|
542
534
|
if (!this.privateBot) {
|
|
543
|
-
|
|
535
|
+
ctx.notifier.create({
|
|
544
536
|
content: '您未配置私人机器人,将无法向您推送机器人状态!'
|
|
545
537
|
});
|
|
546
538
|
this.logger.error('您未配置私人机器人,将无法向您推送机器人状态!');
|
|
547
539
|
}
|
|
540
|
+
// 检查登录数据库是否有数据
|
|
541
|
+
this.loginDBData = (await ctx.database.get('loginBili', 1, ['dynamic_group_id']))[0];
|
|
542
|
+
// 从配置获取订阅
|
|
543
|
+
config.sub && await this.loadSubFromConfig(ctx, config.sub);
|
|
544
|
+
// 从数据库获取订阅
|
|
545
|
+
await this.loadSubFromDatabase(ctx);
|
|
548
546
|
// 判断消息发送方式
|
|
549
547
|
if (config.automaticResend) {
|
|
550
548
|
this.sendMsgFunc = async (bot, channelId, content) => {
|
|
@@ -555,7 +553,7 @@ class ComRegister {
|
|
|
555
553
|
// 发送消息
|
|
556
554
|
await bot.sendMessage(channelId, content);
|
|
557
555
|
// 防止消息发送速度过快被忽略
|
|
558
|
-
await
|
|
556
|
+
await ctx.sleep(500);
|
|
559
557
|
// 成功发送消息,跳出循环
|
|
560
558
|
break;
|
|
561
559
|
}
|
|
@@ -581,29 +579,23 @@ class ComRegister {
|
|
|
581
579
|
}
|
|
582
580
|
};
|
|
583
581
|
}
|
|
584
|
-
// 检查登录数据库是否有数据
|
|
585
|
-
this.loginDBData = (await this.ctx.database.get('loginBili', 1, ['dynamic_group_id']))[0];
|
|
586
|
-
// 从配置获取订阅
|
|
587
|
-
config.sub && await this.loadSubFromConfig(config.sub);
|
|
588
|
-
// 从数据库获取订阅
|
|
589
|
-
await this.loadSubFromDatabase();
|
|
590
582
|
// 检查是否需要动态监测
|
|
591
|
-
this.checkIfDynamicDetectIsNeeded();
|
|
583
|
+
this.checkIfDynamicDetectIsNeeded(ctx);
|
|
592
584
|
// 在控制台中显示订阅对象
|
|
593
|
-
this.updateSubNotifier();
|
|
585
|
+
this.updateSubNotifier(ctx);
|
|
594
586
|
}
|
|
595
587
|
splitMultiPlatformStr(str) {
|
|
596
588
|
return str.split(';').map(cv => cv.split('.')).map(([idStr, platform]) => {
|
|
597
589
|
const channelIdArr = idStr.split(',').map(id => {
|
|
598
590
|
const atAll = /@$/.test(id); // 使用正则表达式检查 id 是否以 @ 结尾
|
|
599
591
|
const channelId = atAll ? id.slice(0, -1) : id; // 去除末尾的 @
|
|
600
|
-
return { channelId, dynamic: true, live: true,
|
|
592
|
+
return { channelId, dynamic: true, live: true, atAll };
|
|
601
593
|
});
|
|
602
594
|
return { channelIdArr, platform };
|
|
603
595
|
});
|
|
604
596
|
}
|
|
605
|
-
getBot(pf) {
|
|
606
|
-
return
|
|
597
|
+
getBot(ctx, pf) {
|
|
598
|
+
return ctx.bots.find(bot => bot.platform === pf);
|
|
607
599
|
}
|
|
608
600
|
async sendPrivateMsg(content) {
|
|
609
601
|
if (this.config.master.enable) {
|
|
@@ -617,7 +609,7 @@ class ComRegister {
|
|
|
617
609
|
}
|
|
618
610
|
}
|
|
619
611
|
}
|
|
620
|
-
async sendPrivateMsgAndRebootService() {
|
|
612
|
+
async sendPrivateMsgAndRebootService(ctx) {
|
|
621
613
|
// 判断重启次数是否超过三次
|
|
622
614
|
if (this.rebootCount >= 3) {
|
|
623
615
|
// logger
|
|
@@ -625,7 +617,7 @@ class ComRegister {
|
|
|
625
617
|
// 重启失败,发送消息
|
|
626
618
|
await this.sendPrivateMsg('已重启插件三次,请检查机器人状态后使用指令 sys start 启动插件');
|
|
627
619
|
// 关闭插件
|
|
628
|
-
await
|
|
620
|
+
await ctx.sm.disposePlugin();
|
|
629
621
|
// 结束
|
|
630
622
|
return;
|
|
631
623
|
}
|
|
@@ -634,7 +626,7 @@ class ComRegister {
|
|
|
634
626
|
// logger
|
|
635
627
|
this.logger.info('插件出现未知错误,正在重启插件');
|
|
636
628
|
// 重启插件
|
|
637
|
-
const flag = await
|
|
629
|
+
const flag = await ctx.sm.restartPlugin();
|
|
638
630
|
// 判断是否重启成功
|
|
639
631
|
if (flag) {
|
|
640
632
|
this.logger.info('重启插件成功');
|
|
@@ -645,23 +637,23 @@ class ComRegister {
|
|
|
645
637
|
// 重启失败,发送消息
|
|
646
638
|
await this.sendPrivateMsg('重启插件失败,请检查机器人状态后使用指令 sys start 启动插件');
|
|
647
639
|
// 关闭插件
|
|
648
|
-
await
|
|
640
|
+
await ctx.sm.disposePlugin();
|
|
649
641
|
}
|
|
650
642
|
}
|
|
651
|
-
async sendPrivateMsgAndStopService() {
|
|
643
|
+
async sendPrivateMsgAndStopService(ctx) {
|
|
652
644
|
// 发送消息
|
|
653
645
|
await this.sendPrivateMsg('插件发生未知错误,请检查机器人状态后使用指令 sys start 启动插件');
|
|
654
646
|
// logger
|
|
655
647
|
this.logger.error('插件发生未知错误,请检查机器人状态后使用指令 sys start 启动插件');
|
|
656
648
|
// 关闭插件
|
|
657
|
-
await
|
|
649
|
+
await ctx.sm.disposePlugin();
|
|
658
650
|
// 结束
|
|
659
651
|
return;
|
|
660
652
|
}
|
|
661
|
-
async sendMsg(targets, content, live) {
|
|
653
|
+
async sendMsg(ctx, targets, content, live) {
|
|
662
654
|
for (const target of targets) {
|
|
663
655
|
// 获取机器人实例
|
|
664
|
-
const bot = this.getBot(target.platform);
|
|
656
|
+
const bot = this.getBot(ctx, target.platform);
|
|
665
657
|
// 定义需要发送的数组
|
|
666
658
|
let sendArr = [];
|
|
667
659
|
// 判断是否需要推送所有机器人加入的群
|
|
@@ -672,7 +664,6 @@ class ComRegister {
|
|
|
672
664
|
channelId: guild.id,
|
|
673
665
|
dynamic: target.channelIdArr[0].dynamic,
|
|
674
666
|
live: target.channelIdArr[0].live,
|
|
675
|
-
liveDanmaku: target.channelIdArr[0].liveDanmaku,
|
|
676
667
|
atAll: target.channelIdArr[0].atAll
|
|
677
668
|
});
|
|
678
669
|
}
|
|
@@ -704,9 +695,13 @@ class ComRegister {
|
|
|
704
695
|
}
|
|
705
696
|
}
|
|
706
697
|
}
|
|
707
|
-
dynamicDetect() {
|
|
698
|
+
dynamicDetect(ctx) {
|
|
699
|
+
// 检测初始化变量
|
|
708
700
|
let detectSetup = true;
|
|
701
|
+
// 更新基线
|
|
709
702
|
let updateBaseline;
|
|
703
|
+
// 第一条动态的动态ID
|
|
704
|
+
let dynamicIdStr;
|
|
710
705
|
// 相当于锁的作用,防止上一个循环没处理完
|
|
711
706
|
let flag = true;
|
|
712
707
|
// 返回一个闭包函数
|
|
@@ -720,7 +715,7 @@ class ComRegister {
|
|
|
720
715
|
// 检测启动初始化
|
|
721
716
|
if (detectSetup) {
|
|
722
717
|
// 获取动态信息
|
|
723
|
-
const data = await
|
|
718
|
+
const data = await ctx.ba.getAllDynamic();
|
|
724
719
|
// 判断获取动态信息是否成功
|
|
725
720
|
if (data.code !== 0)
|
|
726
721
|
return;
|
|
@@ -736,13 +731,13 @@ class ComRegister {
|
|
|
736
731
|
let content;
|
|
737
732
|
try {
|
|
738
733
|
// 查询是否有新动态
|
|
739
|
-
const data = await
|
|
734
|
+
const data = await ctx.ba.hasNewDynamic(updateBaseline);
|
|
740
735
|
updateNum = data.data.update_num;
|
|
741
736
|
// 没有新动态或获取动态信息失败直接返回
|
|
742
737
|
if (updateNum <= 0 || data.code !== 0)
|
|
743
738
|
return;
|
|
744
739
|
// 获取动态内容
|
|
745
|
-
content = await
|
|
740
|
+
content = await ctx.ba.getAllDynamic(updateBaseline);
|
|
746
741
|
}
|
|
747
742
|
catch (e) {
|
|
748
743
|
return this.logger.error('dynamicDetect getUserSpaceDynamic() 发生了错误,错误为:' + e.message);
|
|
@@ -756,7 +751,7 @@ class ComRegister {
|
|
|
756
751
|
// 发送私聊消息
|
|
757
752
|
await this.sendPrivateMsg('账号未登录,插件已停止工作,请登录后,输入指令 sys start 启动插件');
|
|
758
753
|
// 停止服务
|
|
759
|
-
await
|
|
754
|
+
await ctx.sm.disposePlugin();
|
|
760
755
|
// 结束循环
|
|
761
756
|
break;
|
|
762
757
|
}
|
|
@@ -766,7 +761,7 @@ class ComRegister {
|
|
|
766
761
|
// 发送私聊消息
|
|
767
762
|
await this.sendPrivateMsg('账号被风控,插件已停止工作,请确认风控解除后,输入指令 sys start 启动插件');
|
|
768
763
|
// 停止服务
|
|
769
|
-
await
|
|
764
|
+
await ctx.sm.disposePlugin();
|
|
770
765
|
// 结束循环
|
|
771
766
|
break;
|
|
772
767
|
}
|
|
@@ -803,21 +798,30 @@ class ComRegister {
|
|
|
803
798
|
// 寻找关注的UP主的动态
|
|
804
799
|
this.subManager.forEach(async (sub) => {
|
|
805
800
|
// 判断是否是订阅的UP主
|
|
806
|
-
if (sub.uid == upUID) {
|
|
807
|
-
//
|
|
801
|
+
if (sub.dynamic && sub.uid == upUID) { // 订阅该UP主,推送该动态
|
|
802
|
+
// 判断更新动态是否为1条
|
|
803
|
+
if (updateNum === 1) {
|
|
804
|
+
// 判断dynamicIdStr是否有值,是否与当前动态ID一致
|
|
805
|
+
if (dynamicIdStr && dynamicIdStr === items[num].id_str) {
|
|
806
|
+
// 重复动态,不再推送,直接返回
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
// 存储该动态ID
|
|
810
|
+
dynamicIdStr = items[num].id_str;
|
|
811
|
+
}
|
|
808
812
|
// 定义变量
|
|
809
813
|
let pic;
|
|
810
814
|
let buffer;
|
|
811
815
|
// 从动态数据中取出UP主名称和动态ID
|
|
812
|
-
const upName =
|
|
813
|
-
const dynamicId =
|
|
816
|
+
const upName = items[num].modules.module_author.name;
|
|
817
|
+
const dynamicId = items[num].id_str;
|
|
814
818
|
// 推送该条动态
|
|
815
819
|
const attempts = 3;
|
|
816
820
|
for (let i = 0; i < attempts; i++) {
|
|
817
821
|
// 获取动态推送图片
|
|
818
822
|
try {
|
|
819
823
|
// 渲染图片
|
|
820
|
-
const { pic: gimgPic, buffer: gimgBuffer } = await
|
|
824
|
+
const { pic: gimgPic, buffer: gimgBuffer } = await ctx.gi.generateDynamicImg(items[num]);
|
|
821
825
|
// 赋值
|
|
822
826
|
pic = gimgPic;
|
|
823
827
|
buffer = gimgBuffer;
|
|
@@ -831,13 +835,13 @@ class ComRegister {
|
|
|
831
835
|
if (e.message === '出现关键词,屏蔽该动态') {
|
|
832
836
|
// 如果需要发送才发送
|
|
833
837
|
if (this.config.filter.notify) {
|
|
834
|
-
await this.sendMsg(sub.target, `${upName}发布了一条含有屏蔽关键字的动态`);
|
|
838
|
+
await this.sendMsg(ctx, sub.target, `${upName}发布了一条含有屏蔽关键字的动态`);
|
|
835
839
|
}
|
|
836
840
|
return;
|
|
837
841
|
}
|
|
838
842
|
if (e.message === '已屏蔽转发动态') {
|
|
839
843
|
if (this.config.filter.notify) {
|
|
840
|
-
await this.sendMsg(sub.target, `${upName}发布了一条转发动态,已屏蔽`);
|
|
844
|
+
await this.sendMsg(ctx, sub.target, `${upName}发布了一条转发动态,已屏蔽`);
|
|
841
845
|
}
|
|
842
846
|
return;
|
|
843
847
|
}
|
|
@@ -845,7 +849,7 @@ class ComRegister {
|
|
|
845
849
|
if (i === attempts - 1) {
|
|
846
850
|
this.logger.error('dynamicDetect generateDynamicImg() 推送卡片发送失败,原因:' + e.message);
|
|
847
851
|
// 发送私聊消息并重启服务
|
|
848
|
-
return await this.sendPrivateMsgAndStopService();
|
|
852
|
+
return await this.sendPrivateMsgAndStopService(ctx);
|
|
849
853
|
}
|
|
850
854
|
}
|
|
851
855
|
}
|
|
@@ -855,12 +859,12 @@ class ComRegister {
|
|
|
855
859
|
if (pic) {
|
|
856
860
|
this.logger.info('推送动态中,使用render模式');
|
|
857
861
|
// pic存在,使用的是render模式
|
|
858
|
-
await this.sendMsg(sub.target, pic + (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: dUrl }));
|
|
862
|
+
await this.sendMsg(ctx, sub.target, pic + (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: dUrl }));
|
|
859
863
|
}
|
|
860
864
|
else if (buffer) {
|
|
861
865
|
this.logger.info('推送动态中,使用page模式');
|
|
862
866
|
// pic不存在,说明使用的是page模式
|
|
863
|
-
await this.sendMsg(sub.target, (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [koishi_1.h.image(buffer, 'image/png'), dUrl] }));
|
|
867
|
+
await this.sendMsg(ctx, sub.target, (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [koishi_1.h.image(buffer, 'image/png'), dUrl] }));
|
|
864
868
|
}
|
|
865
869
|
else {
|
|
866
870
|
this.logger.info(items[num].modules.module_author.name + '发布了一条动态,但是推送失败');
|
|
@@ -874,9 +878,13 @@ class ComRegister {
|
|
|
874
878
|
}
|
|
875
879
|
};
|
|
876
880
|
}
|
|
877
|
-
debug_dynamicDetect() {
|
|
881
|
+
debug_dynamicDetect(ctx) {
|
|
882
|
+
// 检测初始化变量
|
|
878
883
|
let detectSetup = true;
|
|
884
|
+
// 更新基线
|
|
879
885
|
let updateBaseline;
|
|
886
|
+
// 第一条动态的动态ID
|
|
887
|
+
let dynamicIdStr;
|
|
880
888
|
// 相当于锁的作用,防止上一个循环没处理完
|
|
881
889
|
let flag = true;
|
|
882
890
|
// 返回一个闭包函数
|
|
@@ -891,7 +899,7 @@ class ComRegister {
|
|
|
891
899
|
// 检测启动初始化
|
|
892
900
|
if (detectSetup) {
|
|
893
901
|
// 获取动态信息
|
|
894
|
-
const data = await
|
|
902
|
+
const data = await ctx.ba.getAllDynamic();
|
|
895
903
|
// 判断获取动态信息是否成功
|
|
896
904
|
if (data.code !== 0)
|
|
897
905
|
return;
|
|
@@ -908,7 +916,7 @@ class ComRegister {
|
|
|
908
916
|
let content;
|
|
909
917
|
try {
|
|
910
918
|
// 查询是否有新动态
|
|
911
|
-
const data = await
|
|
919
|
+
const data = await ctx.ba.hasNewDynamic(updateBaseline);
|
|
912
920
|
updateNum = data.data.update_num;
|
|
913
921
|
console.log(`获取是否有新动态:`);
|
|
914
922
|
console.log(data);
|
|
@@ -916,7 +924,7 @@ class ComRegister {
|
|
|
916
924
|
if (updateNum <= 0 || data.code !== 0)
|
|
917
925
|
return;
|
|
918
926
|
// 获取动态内容
|
|
919
|
-
content = await
|
|
927
|
+
content = await ctx.ba.getAllDynamic(updateBaseline);
|
|
920
928
|
console.log('获取动态内容:');
|
|
921
929
|
console.log(content.data.items[0]);
|
|
922
930
|
}
|
|
@@ -932,7 +940,7 @@ class ComRegister {
|
|
|
932
940
|
// 发送私聊消息
|
|
933
941
|
await this.sendPrivateMsg('账号未登录,插件已停止工作,请登录后,输入指令 sys start 启动插件');
|
|
934
942
|
// 停止服务
|
|
935
|
-
await
|
|
943
|
+
await ctx.sm.disposePlugin();
|
|
936
944
|
// 结束循环
|
|
937
945
|
break;
|
|
938
946
|
}
|
|
@@ -942,7 +950,7 @@ class ComRegister {
|
|
|
942
950
|
// 发送私聊消息
|
|
943
951
|
await this.sendPrivateMsg('账号被风控,插件已停止工作,请确认风控解除后,输入指令 sys start 启动插件');
|
|
944
952
|
// 停止服务
|
|
945
|
-
await
|
|
953
|
+
await ctx.sm.disposePlugin();
|
|
946
954
|
// 结束循环
|
|
947
955
|
break;
|
|
948
956
|
}
|
|
@@ -986,14 +994,23 @@ class ComRegister {
|
|
|
986
994
|
this.subManager.forEach(async (sub) => {
|
|
987
995
|
console.log(`当前订阅UP主:${sub.uid}`);
|
|
988
996
|
// 判断是否是订阅的UP主
|
|
989
|
-
if (sub.uid == upUID) {
|
|
990
|
-
//
|
|
997
|
+
if (sub.dynamic && sub.uid == upUID) { // 订阅该UP主,推送该动态
|
|
998
|
+
// 判断更新动态是否为1条
|
|
999
|
+
if (updateNum === 1) {
|
|
1000
|
+
// 判断dynamicIdStr是否有值,是否与当前动态ID一致
|
|
1001
|
+
if (dynamicIdStr && dynamicIdStr === items[num].id_str) {
|
|
1002
|
+
// 重复动态,不再推送,直接返回
|
|
1003
|
+
return;
|
|
1004
|
+
}
|
|
1005
|
+
// 存储该动态ID
|
|
1006
|
+
dynamicIdStr = items[num].id_str;
|
|
1007
|
+
}
|
|
991
1008
|
// 定义变量
|
|
992
1009
|
let pic;
|
|
993
1010
|
let buffer;
|
|
994
1011
|
// 从动态数据中取出UP主名称和动态ID
|
|
995
|
-
const upName =
|
|
996
|
-
const dynamicId =
|
|
1012
|
+
const upName = items[num].modules.module_author.name;
|
|
1013
|
+
const dynamicId = items[num].id_str;
|
|
997
1014
|
console.log(`UP主名称:${upName},动态ID:${dynamicId}`);
|
|
998
1015
|
// 推送该条动态
|
|
999
1016
|
const attempts = 3;
|
|
@@ -1001,7 +1018,7 @@ class ComRegister {
|
|
|
1001
1018
|
// 获取动态推送图片
|
|
1002
1019
|
try {
|
|
1003
1020
|
// 渲染图片
|
|
1004
|
-
const { pic: gimgPic, buffer: gimgBuffer } = await
|
|
1021
|
+
const { pic: gimgPic, buffer: gimgBuffer } = await ctx.gi.generateDynamicImg(items[num]);
|
|
1005
1022
|
// 赋值
|
|
1006
1023
|
pic = gimgPic;
|
|
1007
1024
|
buffer = gimgBuffer;
|
|
@@ -1015,13 +1032,13 @@ class ComRegister {
|
|
|
1015
1032
|
if (e.message === '出现关键词,屏蔽该动态') {
|
|
1016
1033
|
// 如果需要发送才发送
|
|
1017
1034
|
if (this.config.filter.notify) {
|
|
1018
|
-
await this.sendMsg(sub.target, `${upName}发布了一条含有屏蔽关键字的动态`);
|
|
1035
|
+
await this.sendMsg(ctx, sub.target, `${upName}发布了一条含有屏蔽关键字的动态`);
|
|
1019
1036
|
}
|
|
1020
1037
|
return;
|
|
1021
1038
|
}
|
|
1022
1039
|
if (e.message === '已屏蔽转发动态') {
|
|
1023
1040
|
if (this.config.filter.notify) {
|
|
1024
|
-
await this.sendMsg(sub.target, `${upName}发布了一条转发动态,已屏蔽`);
|
|
1041
|
+
await this.sendMsg(ctx, sub.target, `${upName}发布了一条转发动态,已屏蔽`);
|
|
1025
1042
|
}
|
|
1026
1043
|
return;
|
|
1027
1044
|
}
|
|
@@ -1029,7 +1046,7 @@ class ComRegister {
|
|
|
1029
1046
|
if (i === attempts - 1) {
|
|
1030
1047
|
this.logger.error('dynamicDetect generateDynamicImg() 推送卡片发送失败,原因:' + e.message);
|
|
1031
1048
|
// 发送私聊消息并重启服务
|
|
1032
|
-
return await this.sendPrivateMsgAndStopService();
|
|
1049
|
+
return await this.sendPrivateMsgAndStopService(ctx);
|
|
1033
1050
|
}
|
|
1034
1051
|
}
|
|
1035
1052
|
}
|
|
@@ -1039,12 +1056,12 @@ class ComRegister {
|
|
|
1039
1056
|
if (pic) {
|
|
1040
1057
|
this.logger.info('推送动态中,使用render模式');
|
|
1041
1058
|
// pic存在,使用的是render模式
|
|
1042
|
-
await this.sendMsg(sub.target, pic + (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: dUrl }));
|
|
1059
|
+
await this.sendMsg(ctx, sub.target, pic + (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: dUrl }));
|
|
1043
1060
|
}
|
|
1044
1061
|
else if (buffer) {
|
|
1045
1062
|
this.logger.info('推送动态中,使用page模式');
|
|
1046
1063
|
// pic不存在,说明使用的是page模式
|
|
1047
|
-
await this.sendMsg(sub.target, (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [koishi_1.h.image(buffer, 'image/png'), dUrl] }));
|
|
1064
|
+
await this.sendMsg(ctx, sub.target, (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [koishi_1.h.image(buffer, 'image/png'), dUrl] }));
|
|
1048
1065
|
}
|
|
1049
1066
|
else {
|
|
1050
1067
|
this.logger.info(items[num].modules.module_author.name + '发布了一条动态,但是推送失败');
|
|
@@ -1059,7 +1076,7 @@ class ComRegister {
|
|
|
1059
1076
|
};
|
|
1060
1077
|
}
|
|
1061
1078
|
// 定义发送直播通知卡片方法
|
|
1062
|
-
async sendLiveNotifyCard(info, liveType, liveNotifyMsg) {
|
|
1079
|
+
async sendLiveNotifyCard(ctx, info, liveType, liveNotifyMsg) {
|
|
1063
1080
|
// 定义变量
|
|
1064
1081
|
let pic;
|
|
1065
1082
|
let buffer;
|
|
@@ -1068,7 +1085,7 @@ class ComRegister {
|
|
|
1068
1085
|
for (let i = 0; i < attempts; i++) {
|
|
1069
1086
|
try {
|
|
1070
1087
|
// 获取直播通知卡片
|
|
1071
|
-
const { pic: picv, buffer: bufferv } = await
|
|
1088
|
+
const { pic: picv, buffer: bufferv } = await ctx.gi.generateLiveImg(info.data, info.username, info.userface, liveType);
|
|
1072
1089
|
// 赋值
|
|
1073
1090
|
pic = picv;
|
|
1074
1091
|
buffer = bufferv;
|
|
@@ -1079,7 +1096,7 @@ class ComRegister {
|
|
|
1079
1096
|
if (i === attempts - 1) { // 已尝试三次
|
|
1080
1097
|
this.logger.error('liveDetect generateLiveImg() 推送卡片生成失败,原因:' + e.message);
|
|
1081
1098
|
// 发送私聊消息并重启服务
|
|
1082
|
-
return await this.sendPrivateMsgAndStopService();
|
|
1099
|
+
return await this.sendPrivateMsgAndStopService(ctx);
|
|
1083
1100
|
}
|
|
1084
1101
|
}
|
|
1085
1102
|
}
|
|
@@ -1088,33 +1105,33 @@ class ComRegister {
|
|
|
1088
1105
|
if (pic) {
|
|
1089
1106
|
// 只有在开播时才艾特全体成员
|
|
1090
1107
|
if (liveType === LiveType.StartBroadcasting) {
|
|
1091
|
-
return await this.sendMsg(info.target, pic + (liveNotifyMsg ?? ''), true);
|
|
1108
|
+
return await this.sendMsg(ctx, info.target, pic + (liveNotifyMsg ?? ''), true);
|
|
1092
1109
|
}
|
|
1093
1110
|
// 正常不需要艾特全体成员
|
|
1094
|
-
return await this.sendMsg(info.target, pic + (liveNotifyMsg ?? ''));
|
|
1111
|
+
return await this.sendMsg(ctx, info.target, pic + (liveNotifyMsg ?? ''));
|
|
1095
1112
|
}
|
|
1096
1113
|
// pic不存在,说明使用的是page模式
|
|
1097
1114
|
const msg = (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [koishi_1.h.image(buffer, 'image/png'), liveNotifyMsg || ''] });
|
|
1098
1115
|
// 只有在开播时才艾特全体成员
|
|
1099
1116
|
if (liveType === LiveType.StartBroadcasting) {
|
|
1100
|
-
return await this.sendMsg(info.target, msg, true);
|
|
1117
|
+
return await this.sendMsg(ctx, info.target, msg, true);
|
|
1101
1118
|
}
|
|
1102
1119
|
// 正常不需要艾特全体成员
|
|
1103
|
-
return await this.sendMsg(info.target, msg);
|
|
1120
|
+
return await this.sendMsg(ctx, info.target, msg);
|
|
1104
1121
|
}
|
|
1105
1122
|
// 定义获取主播信息方法
|
|
1106
|
-
async useMasterInfo(uid) {
|
|
1107
|
-
const { data } = await
|
|
1108
|
-
return { username:
|
|
1123
|
+
async useMasterInfo(ctx, uid) {
|
|
1124
|
+
const { data: { info } } = await ctx.ba.getMasterInfo(uid);
|
|
1125
|
+
return { username: info.name, userface: info.face };
|
|
1109
1126
|
}
|
|
1110
|
-
async useLiveRoomInfo(roomId) {
|
|
1127
|
+
async useLiveRoomInfo(ctx, roomId) {
|
|
1111
1128
|
// 发送请求获取直播间信息
|
|
1112
1129
|
let content;
|
|
1113
1130
|
const attempts = 3;
|
|
1114
1131
|
for (let i = 0; i < attempts; i++) {
|
|
1115
1132
|
try {
|
|
1116
1133
|
// 发送请求获取room信息
|
|
1117
|
-
content = await
|
|
1134
|
+
content = await ctx.ba.getLiveRoomInfo(roomId);
|
|
1118
1135
|
// 成功则跳出循环
|
|
1119
1136
|
break;
|
|
1120
1137
|
}
|
|
@@ -1122,13 +1139,164 @@ class ComRegister {
|
|
|
1122
1139
|
this.logger.error('liveDetect getLiveRoomInfo 发生了错误,错误为:' + e.message);
|
|
1123
1140
|
if (i === attempts - 1) { // 已尝试三次
|
|
1124
1141
|
// 发送私聊消息并重启服务
|
|
1125
|
-
return await this.sendPrivateMsgAndStopService();
|
|
1142
|
+
return await this.sendPrivateMsgAndStopService(ctx);
|
|
1126
1143
|
}
|
|
1127
1144
|
}
|
|
1128
1145
|
}
|
|
1129
1146
|
return content.data;
|
|
1130
1147
|
}
|
|
1131
|
-
|
|
1148
|
+
/* liveDetect(
|
|
1149
|
+
ctx: Context,
|
|
1150
|
+
roomId: string,
|
|
1151
|
+
target: Target
|
|
1152
|
+
) {
|
|
1153
|
+
let firstSubscription: boolean = true;
|
|
1154
|
+
let timer: number = 0;
|
|
1155
|
+
let open: boolean = false;
|
|
1156
|
+
let liveTime: string;
|
|
1157
|
+
let username: string
|
|
1158
|
+
let userface: string
|
|
1159
|
+
// 相当于锁的作用,防止上一个循环没处理完
|
|
1160
|
+
let flag: boolean = true
|
|
1161
|
+
|
|
1162
|
+
return async () => {
|
|
1163
|
+
// 如果flag为false则说明前面的代码还未执行完,则直接返回
|
|
1164
|
+
if (!flag) return
|
|
1165
|
+
flag = false
|
|
1166
|
+
// 无论是否执行成功都要释放锁
|
|
1167
|
+
try {
|
|
1168
|
+
// 发送请求检测直播状态
|
|
1169
|
+
let content: any
|
|
1170
|
+
const attempts = 3
|
|
1171
|
+
for (let i = 0; i < attempts; i++) {
|
|
1172
|
+
try {
|
|
1173
|
+
// 发送请求获取room信息
|
|
1174
|
+
content = await ctx.ba.getLiveRoomInfo(roomId)
|
|
1175
|
+
// 成功则跳出循环
|
|
1176
|
+
break
|
|
1177
|
+
} catch (e) {
|
|
1178
|
+
this.logger.error('liveDetect getLiveRoomInfo 发生了错误,错误为:' + e.message)
|
|
1179
|
+
if (i === attempts - 1) { // 已尝试三次
|
|
1180
|
+
// 发送私聊消息并重启服务
|
|
1181
|
+
return await this.sendPrivateMsgAndStopService(ctx)
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
const { data } = content
|
|
1186
|
+
// 判断是否是第一次订阅
|
|
1187
|
+
if (firstSubscription) {
|
|
1188
|
+
firstSubscription = false
|
|
1189
|
+
// 获取主播信息
|
|
1190
|
+
const attempts = 3
|
|
1191
|
+
for (let i = 0; i < attempts; i++) {
|
|
1192
|
+
try {
|
|
1193
|
+
// 发送请求获取主播信息
|
|
1194
|
+
await useMasterInfo(data.uid)
|
|
1195
|
+
// 成功则跳出循环
|
|
1196
|
+
break
|
|
1197
|
+
} catch (e) {
|
|
1198
|
+
this.logger.error('liveDetect getMasterInfo() 发生了错误,错误为:' + e.message)
|
|
1199
|
+
if (i === attempts - 1) { // 已尝试三次
|
|
1200
|
+
// 发送私聊消息并重启服务
|
|
1201
|
+
return await this.sendPrivateMsgAndStopService(ctx)
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
// 判断直播状态
|
|
1206
|
+
if (data.live_status === 1) { // 当前正在直播
|
|
1207
|
+
// 设置开播时间
|
|
1208
|
+
liveTime = data.live_time
|
|
1209
|
+
// 设置直播中消息
|
|
1210
|
+
const liveMsg = this.config.customLive ? this.config.customLive
|
|
1211
|
+
.replace('-name', username)
|
|
1212
|
+
.replace('-time', await ctx.gi.getTimeDifference(liveTime))
|
|
1213
|
+
.replace('-link', `https://live.bilibili.com/${data.short_id === 0 ? data.room_id : data.short_id}`) : null
|
|
1214
|
+
// 发送直播通知卡片
|
|
1215
|
+
if (this.config.restartPush) sendLiveNotifyCard(data, LiveType.LiveBroadcast, liveMsg)
|
|
1216
|
+
// 改变开播状态
|
|
1217
|
+
open = true
|
|
1218
|
+
} // 未开播,直接返回
|
|
1219
|
+
return
|
|
1220
|
+
}
|
|
1221
|
+
// 检查直播状态
|
|
1222
|
+
switch (data.live_status) {
|
|
1223
|
+
case 0:
|
|
1224
|
+
case 2: { // 状态 0 和 2 说明未开播
|
|
1225
|
+
if (open) { // 之前开播,现在下播了
|
|
1226
|
+
// 更改直播状态
|
|
1227
|
+
open = false
|
|
1228
|
+
// 下播了将定时器清零
|
|
1229
|
+
timer = 0
|
|
1230
|
+
// 定义下播通知消息
|
|
1231
|
+
const liveEndMsg = this.config.customLiveEnd ? this.config.customLiveEnd
|
|
1232
|
+
.replace('-name', username)
|
|
1233
|
+
.replace('-time', await ctx.gi.getTimeDifference(liveTime)) : null
|
|
1234
|
+
// 更改直播时长
|
|
1235
|
+
data.live_time = liveTime
|
|
1236
|
+
// 推送下播通知
|
|
1237
|
+
await sendLiveNotifyCard(data, LiveType.StopBroadcast, liveEndMsg)
|
|
1238
|
+
}
|
|
1239
|
+
// 未进循环,还未开播,继续循环
|
|
1240
|
+
break
|
|
1241
|
+
}
|
|
1242
|
+
case 1: {
|
|
1243
|
+
if (!open) { // 之前未开播,现在开播了
|
|
1244
|
+
// 更改直播状态
|
|
1245
|
+
open = true
|
|
1246
|
+
// 设置开播时间
|
|
1247
|
+
liveTime = data.live_time
|
|
1248
|
+
// 获取主播信息
|
|
1249
|
+
const attempts = 3
|
|
1250
|
+
for (let i = 0; i < attempts; i++) {
|
|
1251
|
+
try {
|
|
1252
|
+
// 主播信息不会变,开播时刷新一次即可
|
|
1253
|
+
// 发送请求获取主播信息
|
|
1254
|
+
await useMasterInfo(data.uid)
|
|
1255
|
+
// 成功则跳出循环
|
|
1256
|
+
break
|
|
1257
|
+
} catch (e) {
|
|
1258
|
+
this.logger.error('liveDetect open getMasterInfo() 发生了错误,错误为:' + e.message)
|
|
1259
|
+
if (i === attempts - 1) { // 已尝试三次
|
|
1260
|
+
// 发送私聊消息并重启服务
|
|
1261
|
+
return await this.sendPrivateMsgAndStopService(ctx)
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
}
|
|
1265
|
+
// 定义开播通知语
|
|
1266
|
+
const liveStartMsg = this.config.customLiveStart ? this.config.customLiveStart
|
|
1267
|
+
.replace('-name', username)
|
|
1268
|
+
.replace('-time', await ctx.gi.getTimeDifference(liveTime))
|
|
1269
|
+
.replace('-link', `https://live.bilibili.com/${data.short_id === 0 ? data.room_id : data.short_id}`) : null
|
|
1270
|
+
// 发送消息
|
|
1271
|
+
await sendLiveNotifyCard(data, LiveType.StartBroadcasting, liveStartMsg)
|
|
1272
|
+
} else { // 还在直播
|
|
1273
|
+
if (this.config.pushTime > 0) {
|
|
1274
|
+
timer++
|
|
1275
|
+
// 开始记录时间
|
|
1276
|
+
if (timer >= (6 * 60 * this.config.pushTime)) { // 到时间推送直播消息
|
|
1277
|
+
// 到时间重新计时
|
|
1278
|
+
timer = 0
|
|
1279
|
+
// 定义直播中通知消息
|
|
1280
|
+
const liveMsg = this.config.customLive ? this.config.customLive
|
|
1281
|
+
.replace('-name', username)
|
|
1282
|
+
.replace('-time', await ctx.gi.getTimeDifference(liveTime))
|
|
1283
|
+
.replace('-link', `https://live.bilibili.com/${data.short_id === 0 ? data.room_id : data.short_id}`) : null
|
|
1284
|
+
// 发送直播通知卡片
|
|
1285
|
+
sendLiveNotifyCard(data, LiveType.LiveBroadcast, liveMsg)
|
|
1286
|
+
}
|
|
1287
|
+
}
|
|
1288
|
+
// 否则继续循环
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
}
|
|
1293
|
+
finally {
|
|
1294
|
+
// 执行完方法体不论如何都把flag设置为true
|
|
1295
|
+
flag = true
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
} */
|
|
1299
|
+
async liveDetectWithAPI(ctx) {
|
|
1132
1300
|
// 定义变量:第一次订阅
|
|
1133
1301
|
let firstSubscription = true;
|
|
1134
1302
|
// 定义变量:timer计时器
|
|
@@ -1152,7 +1320,7 @@ class ComRegister {
|
|
|
1152
1320
|
for (let i = 0; i < attempts; i++) {
|
|
1153
1321
|
try {
|
|
1154
1322
|
// 发送请求获取room信息
|
|
1155
|
-
content = await
|
|
1323
|
+
content = await ctx.ba.getLiveRoomInfo(roomId);
|
|
1156
1324
|
// 成功则跳出循环
|
|
1157
1325
|
break;
|
|
1158
1326
|
}
|
|
@@ -1160,7 +1328,7 @@ class ComRegister {
|
|
|
1160
1328
|
this.logger.error('liveDetect getLiveRoomInfo 发生了错误,错误为:' + e.message);
|
|
1161
1329
|
if (i === attempts - 1) { // 已尝试三次
|
|
1162
1330
|
// 发送私聊消息并重启服务
|
|
1163
|
-
return await this.sendPrivateMsgAndStopService();
|
|
1331
|
+
return await this.sendPrivateMsgAndStopService(ctx);
|
|
1164
1332
|
}
|
|
1165
1333
|
}
|
|
1166
1334
|
}
|
|
@@ -1175,7 +1343,7 @@ class ComRegister {
|
|
|
1175
1343
|
flag = false;
|
|
1176
1344
|
try {
|
|
1177
1345
|
// 获取正在直播对象
|
|
1178
|
-
const liveUsers = await
|
|
1346
|
+
const liveUsers = await ctx.ba.getTheUserWhoIsLiveStreaming();
|
|
1179
1347
|
// 判断是否是第一次订阅
|
|
1180
1348
|
if (firstSubscription) {
|
|
1181
1349
|
// 将第一次订阅置为false
|
|
@@ -1193,11 +1361,11 @@ class ComRegister {
|
|
|
1193
1361
|
// 设置直播中消息
|
|
1194
1362
|
const liveMsg = this.config.customLive ? this.config.customLive
|
|
1195
1363
|
.replace('-name', item.uname)
|
|
1196
|
-
.replace('-time', await
|
|
1364
|
+
.replace('-time', await ctx.gi.getTimeDifference(liveRecord[item.mid].liveTime))
|
|
1197
1365
|
.replace('-link', `https://live.bilibili.com/${data.short_id === 0 ? data.room_id : data.short_id}`) : null;
|
|
1198
1366
|
// 发送直播通知卡片
|
|
1199
1367
|
if (this.config.restartPush)
|
|
1200
|
-
this.sendLiveNotifyCard({
|
|
1368
|
+
this.sendLiveNotifyCard(ctx, {
|
|
1201
1369
|
username: item.uname,
|
|
1202
1370
|
userface: item.face,
|
|
1203
1371
|
target: liveRecord[item.mid].target,
|
|
@@ -1250,7 +1418,6 @@ class ComRegister {
|
|
|
1250
1418
|
delete liveRecord[subUID];
|
|
1251
1419
|
}
|
|
1252
1420
|
}
|
|
1253
|
-
// 数量没有差异,则不进行其他操作
|
|
1254
1421
|
// 遍历liveUsers
|
|
1255
1422
|
liveUsers.items.forEach(async (item) => {
|
|
1256
1423
|
// 判断是否有正在直播的订阅对象
|
|
@@ -1265,15 +1432,16 @@ class ComRegister {
|
|
|
1265
1432
|
// 定义开播通知语
|
|
1266
1433
|
const liveStartMsg = this.config.customLiveStart ? this.config.customLiveStart
|
|
1267
1434
|
.replace('-name', item.uname)
|
|
1268
|
-
.replace('-time', await
|
|
1435
|
+
.replace('-time', await ctx.gi.getTimeDifference(liveRecord[item.mid].liveTime))
|
|
1269
1436
|
.replace('-link', `https://live.bilibili.com/${data.short_id === 0 ? data.room_id : data.short_id}`) : null;
|
|
1270
1437
|
// 发送直播通知卡片
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1438
|
+
if (this.config.restartPush)
|
|
1439
|
+
this.sendLiveNotifyCard(ctx, {
|
|
1440
|
+
username: item.uname,
|
|
1441
|
+
userface: item.face,
|
|
1442
|
+
target: liveRecord[item.mid].target,
|
|
1443
|
+
data
|
|
1444
|
+
}, LiveType.LiveBroadcast, liveStartMsg);
|
|
1277
1445
|
// 改变开播状态
|
|
1278
1446
|
liveRecord[item.mid].liveStatus = 1;
|
|
1279
1447
|
// 结束
|
|
@@ -1289,10 +1457,10 @@ class ComRegister {
|
|
|
1289
1457
|
// 定义直播中通知消息
|
|
1290
1458
|
const liveMsg = this.config.customLive ? this.config.customLive
|
|
1291
1459
|
.replace('-name', item.uname)
|
|
1292
|
-
.replace('-time', await
|
|
1460
|
+
.replace('-time', await ctx.gi.getTimeDifference(liveRecord[item.mid].liveTime))
|
|
1293
1461
|
.replace('-link', `https://live.bilibili.com/${data.short_id === 0 ? data.room_id : data.short_id}`) : null;
|
|
1294
1462
|
// 发送直播通知卡片
|
|
1295
|
-
this.sendLiveNotifyCard({
|
|
1463
|
+
this.sendLiveNotifyCard(ctx, {
|
|
1296
1464
|
username: item.uname,
|
|
1297
1465
|
userface: item.face,
|
|
1298
1466
|
target: liveRecord[item.mid].target,
|
|
@@ -1304,27 +1472,6 @@ class ComRegister {
|
|
|
1304
1472
|
}
|
|
1305
1473
|
}
|
|
1306
1474
|
});
|
|
1307
|
-
// 找出liveRecord中liveStatus为1但liveUsers中没有的元素
|
|
1308
|
-
const extraInLiveRecord = currentLiveRecordKeys.filter(key => !liveUsers.items.some(item => item.mid === Number(key)));
|
|
1309
|
-
// 遍历 extraInLiveRecord
|
|
1310
|
-
for (const subUID of extraInLiveRecord) { // 下播的主播
|
|
1311
|
-
// 获取主播信息
|
|
1312
|
-
const masterInfo = await this.useMasterInfo(subUID);
|
|
1313
|
-
// 获取直播间消息
|
|
1314
|
-
const liveRoomInfo = await this.useLiveRoomInfo(masterInfo.roomId.toString());
|
|
1315
|
-
// 定义下播播通知语
|
|
1316
|
-
const liveEndMsg = this.config.customLiveEnd ? this.config.customLiveEnd
|
|
1317
|
-
.replace('-name', masterInfo.username)
|
|
1318
|
-
.replace('-time', await this.ctx.gi.getTimeDifference(liveRecord[subUID].liveTime))
|
|
1319
|
-
.replace('-link', `https://live.bilibili.com/${liveRoomInfo.short_id === 0 ? liveRoomInfo.room_id : liveRoomInfo.short_id}`) : null;
|
|
1320
|
-
// 发送下播通知
|
|
1321
|
-
this.sendLiveNotifyCard({
|
|
1322
|
-
username: masterInfo.username,
|
|
1323
|
-
userface: masterInfo.userface,
|
|
1324
|
-
target: liveRecord[subUID].target,
|
|
1325
|
-
data: liveRoomInfo
|
|
1326
|
-
}, LiveType.StopBroadcast, liveEndMsg);
|
|
1327
|
-
}
|
|
1328
1475
|
}
|
|
1329
1476
|
finally {
|
|
1330
1477
|
// 执行完方法体不论如何都把flag设置为true
|
|
@@ -1332,7 +1479,7 @@ class ComRegister {
|
|
|
1332
1479
|
}
|
|
1333
1480
|
};
|
|
1334
1481
|
}
|
|
1335
|
-
|
|
1482
|
+
liveDetectWithListener(ctx, roomId, target) {
|
|
1336
1483
|
// 定义开播时间
|
|
1337
1484
|
let liveTime;
|
|
1338
1485
|
// 定义定时推送定时器
|
|
@@ -1340,48 +1487,35 @@ class ComRegister {
|
|
|
1340
1487
|
// 定义弹幕存放数组
|
|
1341
1488
|
const currentLiveDanmakuArr = [];
|
|
1342
1489
|
const temporaryLiveDanmakuArr = [];
|
|
1343
|
-
// 处理target
|
|
1344
|
-
// 找到频道/群组对应的
|
|
1345
|
-
const newTarget = target.map(channel => {
|
|
1346
|
-
const liveDanmakuArr = channel.channelIdArr.filter(channelId => channelId.liveDanmaku);
|
|
1347
|
-
return {
|
|
1348
|
-
channelIdArr: liveDanmakuArr,
|
|
1349
|
-
platform: channel.platform
|
|
1350
|
-
};
|
|
1351
|
-
});
|
|
1352
1490
|
// 定义定时推送函数
|
|
1353
|
-
const
|
|
1491
|
+
const pushAtTime = async () => {
|
|
1354
1492
|
// 获取直播间信息
|
|
1355
|
-
const liveRoomInfo = await this.useLiveRoomInfo(roomId);
|
|
1493
|
+
const liveRoomInfo = await this.useLiveRoomInfo(ctx, roomId);
|
|
1356
1494
|
// 获取主播信息
|
|
1357
|
-
const masterInfo = await this.useMasterInfo(liveRoomInfo.uid);
|
|
1495
|
+
const masterInfo = await this.useMasterInfo(ctx, liveRoomInfo.uid);
|
|
1358
1496
|
// 定义直播中通知消息
|
|
1359
1497
|
const liveMsg = this.config.customLive ? this.config.customLive
|
|
1360
1498
|
.replace('-name', masterInfo.username)
|
|
1361
|
-
.replace('-time', await
|
|
1499
|
+
.replace('-time', await ctx.gi.getTimeDifference(liveTime))
|
|
1362
1500
|
.replace('-link', `https://live.bilibili.com/${liveRoomInfo.short_id === 0 ? liveRoomInfo.room_id : liveRoomInfo.short_id}`) : null;
|
|
1363
1501
|
// 发送直播通知卡片
|
|
1364
|
-
await this.sendLiveNotifyCard({
|
|
1502
|
+
await this.sendLiveNotifyCard(liveRoomInfo, {
|
|
1365
1503
|
username: masterInfo.username,
|
|
1366
1504
|
userface: masterInfo.userface,
|
|
1367
|
-
target
|
|
1505
|
+
target,
|
|
1368
1506
|
data: liveRoomInfo
|
|
1369
1507
|
}, LiveType.LiveBroadcast, liveMsg);
|
|
1370
1508
|
};
|
|
1371
|
-
//
|
|
1372
|
-
const
|
|
1509
|
+
// 定义10秒推送函数
|
|
1510
|
+
const pushOnceEveryTenS = () => {
|
|
1373
1511
|
// 判断数组是否有内容
|
|
1374
1512
|
if (temporaryLiveDanmakuArr.length > 0) {
|
|
1375
1513
|
// 发送消息
|
|
1376
|
-
this.sendMsg(
|
|
1514
|
+
this.sendMsg(ctx, target, temporaryLiveDanmakuArr.join('\n'));
|
|
1377
1515
|
// 将临时消息数组清空
|
|
1378
1516
|
temporaryLiveDanmakuArr.length = 0;
|
|
1379
1517
|
}
|
|
1380
1518
|
};
|
|
1381
|
-
// 获取直播间信息
|
|
1382
|
-
const liveRoomInfo = await this.useLiveRoomInfo(roomId);
|
|
1383
|
-
// 获取主播信息
|
|
1384
|
-
const masterInfo = await this.useMasterInfo(liveRoomInfo.uid);
|
|
1385
1519
|
// 构建消息处理函数
|
|
1386
1520
|
const handler = {
|
|
1387
1521
|
onOpen: () => {
|
|
@@ -1392,89 +1526,62 @@ class ComRegister {
|
|
|
1392
1526
|
},
|
|
1393
1527
|
onIncomeDanmu: ({ body }) => {
|
|
1394
1528
|
// 处理消息,只需要UP主名字和消息内容
|
|
1395
|
-
const content =
|
|
1396
|
-
// 保存消息到数组
|
|
1397
|
-
currentLiveDanmakuArr.push(body.content);
|
|
1398
|
-
temporaryLiveDanmakuArr.push(content);
|
|
1399
|
-
},
|
|
1400
|
-
onIncomeSuperChat: ({ body }) => {
|
|
1401
|
-
// 处理SC消息
|
|
1402
|
-
const content = `【${masterInfo.username}的直播间】${body.user.uname}发送了一条SC:${body.content}`;
|
|
1403
|
-
// 保存消息到数组
|
|
1404
|
-
currentLiveDanmakuArr.push(body.content);
|
|
1405
|
-
temporaryLiveDanmakuArr.push(content);
|
|
1406
|
-
},
|
|
1407
|
-
onGuardBuy: ({ body }) => {
|
|
1408
|
-
const content = `【${masterInfo.username}的直播间】${body.user.uname}加入了大航海(${body.gift_name})`;
|
|
1529
|
+
const content = `${body.user.uname}:${body.content}`;
|
|
1409
1530
|
// 保存消息到数组
|
|
1410
1531
|
currentLiveDanmakuArr.push(content);
|
|
1411
1532
|
temporaryLiveDanmakuArr.push(content);
|
|
1412
1533
|
},
|
|
1534
|
+
onIncomeSuperChat: (msg) => {
|
|
1535
|
+
console.log(msg.id, msg.body);
|
|
1536
|
+
},
|
|
1413
1537
|
onLiveStart: async () => {
|
|
1414
1538
|
// 获取直播间信息
|
|
1415
|
-
const liveRoomInfo = await this.useLiveRoomInfo(roomId);
|
|
1539
|
+
const liveRoomInfo = await this.useLiveRoomInfo(ctx, roomId);
|
|
1416
1540
|
// 获取主播信息
|
|
1417
|
-
const masterInfo = await this.useMasterInfo(liveRoomInfo.uid);
|
|
1541
|
+
const masterInfo = await this.useMasterInfo(ctx, liveRoomInfo.uid);
|
|
1418
1542
|
// 定义下播通知消息
|
|
1419
1543
|
const liveEndMsg = this.config.customLiveEnd ? this.config.customLiveEnd
|
|
1420
1544
|
.replace('-name', masterInfo.username)
|
|
1421
|
-
.replace('-time', await
|
|
1545
|
+
.replace('-time', await ctx.gi.getTimeDifference(liveTime)) : null;
|
|
1422
1546
|
// 更改直播时长
|
|
1423
1547
|
liveRoomInfo.live_time = liveTime;
|
|
1424
1548
|
// 推送下播通知
|
|
1425
|
-
await this.sendLiveNotifyCard({
|
|
1549
|
+
await this.sendLiveNotifyCard(liveRoomInfo, {
|
|
1426
1550
|
username: masterInfo.username,
|
|
1427
1551
|
userface: masterInfo.userface,
|
|
1428
|
-
target
|
|
1552
|
+
target,
|
|
1429
1553
|
data: liveRoomInfo
|
|
1430
1554
|
}, LiveType.StopBroadcast, liveEndMsg);
|
|
1555
|
+
// 关闭定时器
|
|
1556
|
+
pushAtTimeTimer();
|
|
1557
|
+
// 定时器变量置空
|
|
1558
|
+
pushAtTimeTimer = null;
|
|
1431
1559
|
},
|
|
1432
1560
|
onLiveEnd: async () => {
|
|
1433
1561
|
// 获取直播间消息
|
|
1434
|
-
const liveRoomInfo = await this.useLiveRoomInfo(roomId);
|
|
1562
|
+
const liveRoomInfo = await this.useLiveRoomInfo(ctx, roomId);
|
|
1435
1563
|
// 获取主播信息
|
|
1436
|
-
const masterInfo = await this.useMasterInfo(liveRoomInfo.uid);
|
|
1437
|
-
//
|
|
1438
|
-
|
|
1439
|
-
//
|
|
1440
|
-
pushAtTimeTimer =
|
|
1441
|
-
//
|
|
1442
|
-
const
|
|
1564
|
+
const masterInfo = await this.useMasterInfo(ctx, liveRoomInfo.uid);
|
|
1565
|
+
// 设置开播时间
|
|
1566
|
+
liveTime = liveRoomInfo.live_time;
|
|
1567
|
+
// 开启推送定时器
|
|
1568
|
+
pushAtTimeTimer = ctx.setInterval(pushAtTime, 3600 * 1000);
|
|
1569
|
+
// 定义开播通知语
|
|
1570
|
+
const liveStartMsg = this.config.customLiveStart ? this.config.customLiveStart
|
|
1443
1571
|
.replace('-name', masterInfo.username)
|
|
1444
|
-
.replace('-time', await
|
|
1572
|
+
.replace('-time', await ctx.gi.getTimeDifference(liveTime))
|
|
1445
1573
|
.replace('-link', `https://live.bilibili.com/${liveRoomInfo.short_id === 0 ? liveRoomInfo.room_id : liveRoomInfo.short_id}`) : null;
|
|
1446
1574
|
// 推送通知卡片
|
|
1447
|
-
await this.sendLiveNotifyCard({
|
|
1575
|
+
await this.sendLiveNotifyCard(ctx, {
|
|
1448
1576
|
username: masterInfo.username,
|
|
1449
1577
|
userface: masterInfo.userface,
|
|
1450
|
-
target
|
|
1578
|
+
target,
|
|
1451
1579
|
data: liveRoomInfo
|
|
1452
|
-
}, LiveType.StartBroadcasting,
|
|
1580
|
+
}, LiveType.StartBroadcasting, liveStartMsg);
|
|
1453
1581
|
}
|
|
1454
1582
|
};
|
|
1455
1583
|
// 启动直播间弹幕监测
|
|
1456
|
-
|
|
1457
|
-
// 判断直播状态
|
|
1458
|
-
if (liveRoomInfo.live_status === 1) {
|
|
1459
|
-
// 设置开播时间
|
|
1460
|
-
liveTime = liveRoomInfo.live_time;
|
|
1461
|
-
// 定义直播中通知消息
|
|
1462
|
-
const liveMsg = this.config.customLive ? this.config.customLive
|
|
1463
|
-
.replace('-name', masterInfo.username)
|
|
1464
|
-
.replace('-time', await this.ctx.gi.getTimeDifference(liveTime))
|
|
1465
|
-
.replace('-link', `https://live.bilibili.com/${liveRoomInfo.short_id === 0 ? liveRoomInfo.room_id : liveRoomInfo.short_id}`) : null;
|
|
1466
|
-
// 发送直播通知卡片
|
|
1467
|
-
if (this.config.restartPush) {
|
|
1468
|
-
await this.sendLiveNotifyCard({
|
|
1469
|
-
username: masterInfo.username,
|
|
1470
|
-
userface: masterInfo.userface,
|
|
1471
|
-
target: newTarget,
|
|
1472
|
-
data: liveRoomInfo
|
|
1473
|
-
}, LiveType.LiveBroadcast, liveMsg);
|
|
1474
|
-
}
|
|
1475
|
-
// 正在直播,开启定时器
|
|
1476
|
-
pushAtTimeTimer = this.ctx.setInterval(pushAtTimeFunc, this.config.pushTime * 1000 * 60 * 60);
|
|
1477
|
-
}
|
|
1584
|
+
ctx.bl.startLiveRoomListener(roomId, handler, pushOnceEveryTenS);
|
|
1478
1585
|
}
|
|
1479
1586
|
subShow() {
|
|
1480
1587
|
// 在控制台中显示订阅对象
|
|
@@ -1514,7 +1621,7 @@ class ComRegister {
|
|
|
1514
1621
|
// 只订阅动态
|
|
1515
1622
|
return [false, true];
|
|
1516
1623
|
}
|
|
1517
|
-
updateSubNotifier() {
|
|
1624
|
+
updateSubNotifier(ctx) {
|
|
1518
1625
|
// 更新控制台提示
|
|
1519
1626
|
if (this.subNotifier)
|
|
1520
1627
|
this.subNotifier.dispose();
|
|
@@ -1533,13 +1640,13 @@ class ComRegister {
|
|
|
1533
1640
|
table = (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("p", { children: "\u5F53\u524D\u8BA2\u9605\u5BF9\u8C61\uFF1A" }), (0, jsx_runtime_1.jsx)("ul", { children: subTableArray.map(str => ((0, jsx_runtime_1.jsx)("li", { children: str }))) })] });
|
|
1534
1641
|
}
|
|
1535
1642
|
// 设置更新后的提示
|
|
1536
|
-
this.subNotifier =
|
|
1643
|
+
this.subNotifier = ctx.notifier.create(table);
|
|
1537
1644
|
}
|
|
1538
|
-
async checkIfLoginInfoIsLoaded() {
|
|
1645
|
+
async checkIfLoginInfoIsLoaded(ctx) {
|
|
1539
1646
|
return new Promise(resolve => {
|
|
1540
1647
|
const check = () => {
|
|
1541
|
-
if (!
|
|
1542
|
-
|
|
1648
|
+
if (!ctx.ba.getLoginInfoIsLoaded()) {
|
|
1649
|
+
ctx.setTimeout(check, 500);
|
|
1543
1650
|
}
|
|
1544
1651
|
else {
|
|
1545
1652
|
resolve('success');
|
|
@@ -1548,17 +1655,17 @@ class ComRegister {
|
|
|
1548
1655
|
check();
|
|
1549
1656
|
});
|
|
1550
1657
|
}
|
|
1551
|
-
async subUserInBili(mid) {
|
|
1658
|
+
async subUserInBili(ctx, mid) {
|
|
1552
1659
|
// 获取关注分组信息
|
|
1553
1660
|
const checkGroupIsReady = async () => {
|
|
1554
1661
|
// 判断是否有数据
|
|
1555
1662
|
if (this.loginDBData.dynamic_group_id === '' || this.loginDBData.dynamic_group_id === null) {
|
|
1556
1663
|
// 没有数据,没有创建分组,尝试创建分组
|
|
1557
|
-
const createGroupData = await
|
|
1664
|
+
const createGroupData = await ctx.ba.createGroup("订阅");
|
|
1558
1665
|
// 如果分组已创建,则获取分组id
|
|
1559
1666
|
if (createGroupData.code === 22106) {
|
|
1560
1667
|
// 分组已存在,拿到之前的分组id
|
|
1561
|
-
const allGroupData = await
|
|
1668
|
+
const allGroupData = await ctx.ba.getAllGroup();
|
|
1562
1669
|
// 遍历所有分组
|
|
1563
1670
|
for (const group of allGroupData.data) {
|
|
1564
1671
|
// 找到订阅分组
|
|
@@ -1576,7 +1683,7 @@ class ComRegister {
|
|
|
1576
1683
|
return false;
|
|
1577
1684
|
}
|
|
1578
1685
|
// 创建成功,保存到数据库
|
|
1579
|
-
|
|
1686
|
+
ctx.database.set('loginBili', 1, { dynamic_group_id: this.loginDBData.dynamic_group_id });
|
|
1580
1687
|
// 创建成功
|
|
1581
1688
|
return true;
|
|
1582
1689
|
}
|
|
@@ -1590,7 +1697,7 @@ class ComRegister {
|
|
|
1590
1697
|
return { flag: false, msg: '创建分组失败,请尝试重启插件' };
|
|
1591
1698
|
}
|
|
1592
1699
|
// 获取分组明细
|
|
1593
|
-
const relationGroupDetailData = await
|
|
1700
|
+
const relationGroupDetailData = await ctx.ba.getRelationGroupDetail(this.loginDBData.dynamic_group_id);
|
|
1594
1701
|
// 判断分组信息是否获取成功
|
|
1595
1702
|
if (relationGroupDetailData.code !== 0) {
|
|
1596
1703
|
if (relationGroupDetailData.code === 22104) {
|
|
@@ -1615,7 +1722,7 @@ class ComRegister {
|
|
|
1615
1722
|
}
|
|
1616
1723
|
});
|
|
1617
1724
|
// 订阅对象
|
|
1618
|
-
const subUserData = await
|
|
1725
|
+
const subUserData = await ctx.ba.follow(mid);
|
|
1619
1726
|
// 判断是否订阅成功
|
|
1620
1727
|
switch (subUserData.code) {
|
|
1621
1728
|
case -101: return { flag: false, msg: '账号未登录,请使用指令bili login登录后再进行订阅操作' };
|
|
@@ -1628,7 +1735,7 @@ class ComRegister {
|
|
|
1628
1735
|
case 22014: // 已关注订阅对象 无需再次关注
|
|
1629
1736
|
case 0: { // 执行订阅成功
|
|
1630
1737
|
// 把订阅对象添加到分组中
|
|
1631
|
-
const copyUserToGroupData = await
|
|
1738
|
+
const copyUserToGroupData = await ctx.ba.copyUserToGroup(mid, this.loginDBData.dynamic_group_id);
|
|
1632
1739
|
// 判断是否添加成功
|
|
1633
1740
|
if (copyUserToGroupData.code !== 0) {
|
|
1634
1741
|
// 添加失败
|
|
@@ -1639,10 +1746,12 @@ class ComRegister {
|
|
|
1639
1746
|
// 订阅成功
|
|
1640
1747
|
return { flag: true, msg: '用户订阅成功' };
|
|
1641
1748
|
}
|
|
1642
|
-
async loadSubFromConfig(subs) {
|
|
1749
|
+
async loadSubFromConfig(ctx, subs) {
|
|
1643
1750
|
for (const sub of subs) {
|
|
1644
1751
|
// 定义Data
|
|
1645
1752
|
let data;
|
|
1753
|
+
// 定义直播销毁函数
|
|
1754
|
+
let liveDispose;
|
|
1646
1755
|
// 判断是否需要订阅直播
|
|
1647
1756
|
if (sub.live) {
|
|
1648
1757
|
// 获取用户信息
|
|
@@ -1652,7 +1761,7 @@ class ComRegister {
|
|
|
1652
1761
|
for (let i = 0; i < attempts; i++) {
|
|
1653
1762
|
try {
|
|
1654
1763
|
// 获取用户信息
|
|
1655
|
-
content = await
|
|
1764
|
+
content = await ctx.ba.getUserInfo(sub.uid);
|
|
1656
1765
|
// 成功则跳出循环
|
|
1657
1766
|
break;
|
|
1658
1767
|
}
|
|
@@ -1660,7 +1769,7 @@ class ComRegister {
|
|
|
1660
1769
|
this.logger.error('getSubFromDatabase() getUserInfo() 发生了错误,错误为:' + e.message);
|
|
1661
1770
|
if (i === attempts - 1) { // 已尝试三次
|
|
1662
1771
|
// 发送私聊消息并重启服务
|
|
1663
|
-
return await this.sendPrivateMsgAndStopService();
|
|
1772
|
+
return await this.sendPrivateMsgAndStopService(ctx);
|
|
1664
1773
|
}
|
|
1665
1774
|
}
|
|
1666
1775
|
}
|
|
@@ -1676,11 +1785,11 @@ class ComRegister {
|
|
|
1676
1785
|
// 判断是否订阅直播
|
|
1677
1786
|
if (sub.live) {
|
|
1678
1787
|
// 订阅直播
|
|
1679
|
-
this.liveDetectWithListener(data.live_room.roomid, sub.target);
|
|
1788
|
+
this.liveDetectWithListener(ctx, data.live_room.roomid, sub.target);
|
|
1680
1789
|
}
|
|
1681
1790
|
}
|
|
1682
1791
|
// 在B站中订阅该对象
|
|
1683
|
-
const subInfo = await this.subUserInBili(sub.uid);
|
|
1792
|
+
const subInfo = await this.subUserInBili(ctx, sub.uid);
|
|
1684
1793
|
// 判断订阅是否成功
|
|
1685
1794
|
if (!subInfo.flag)
|
|
1686
1795
|
this.logger.warn(subInfo.msg);
|
|
@@ -1692,15 +1801,16 @@ class ComRegister {
|
|
|
1692
1801
|
target: sub.target,
|
|
1693
1802
|
platform: '',
|
|
1694
1803
|
live: sub.live,
|
|
1695
|
-
dynamic: sub.dynamic
|
|
1804
|
+
dynamic: sub.dynamic,
|
|
1805
|
+
liveDispose
|
|
1696
1806
|
});
|
|
1697
1807
|
}
|
|
1698
1808
|
}
|
|
1699
|
-
async loadSubFromDatabase() {
|
|
1809
|
+
async loadSubFromDatabase(ctx) {
|
|
1700
1810
|
// 判断登录信息是否已加载完毕
|
|
1701
|
-
await this.checkIfLoginInfoIsLoaded();
|
|
1811
|
+
await this.checkIfLoginInfoIsLoaded(ctx);
|
|
1702
1812
|
// 如果未登录,则直接返回
|
|
1703
|
-
if (!(await this.checkIfIsLogin())) {
|
|
1813
|
+
if (!(await this.checkIfIsLogin(ctx))) {
|
|
1704
1814
|
// log
|
|
1705
1815
|
this.logger.info(`账号未登录,请登录`);
|
|
1706
1816
|
return;
|
|
@@ -1709,7 +1819,7 @@ class ComRegister {
|
|
|
1709
1819
|
if (this.subManager.length !== 0)
|
|
1710
1820
|
return;
|
|
1711
1821
|
// 从数据库中获取数据
|
|
1712
|
-
const subData = await
|
|
1822
|
+
const subData = await ctx.database.get('bilibili', { id: { $gt: 0 } });
|
|
1713
1823
|
// 定义变量:订阅直播数
|
|
1714
1824
|
let liveSubNum = 0;
|
|
1715
1825
|
// 循环遍历
|
|
@@ -1717,14 +1827,14 @@ class ComRegister {
|
|
|
1717
1827
|
// 判断是否存在没有任何订阅的数据
|
|
1718
1828
|
if (!sub.dynamic && !sub.live) { // 存在未订阅任何项目的数据
|
|
1719
1829
|
// 删除该条数据
|
|
1720
|
-
|
|
1830
|
+
ctx.database.remove('bilibili', { id: sub.id });
|
|
1721
1831
|
// log
|
|
1722
1832
|
this.logger.warn(`UID:${sub.uid} 该条数据没有任何订阅数据,自动取消订阅`);
|
|
1723
1833
|
// 跳过下面的步骤
|
|
1724
1834
|
continue;
|
|
1725
1835
|
}
|
|
1726
1836
|
// 判断用户是否在B站中订阅了
|
|
1727
|
-
const subUserData = await this.subUserInBili(sub.uid);
|
|
1837
|
+
const subUserData = await this.subUserInBili(ctx, sub.uid);
|
|
1728
1838
|
// 判断是否订阅
|
|
1729
1839
|
if (!subUserData.flag) {
|
|
1730
1840
|
// log
|
|
@@ -1732,7 +1842,7 @@ class ComRegister {
|
|
|
1732
1842
|
// 发送私聊消息
|
|
1733
1843
|
await this.sendPrivateMsg(`UID:${sub.uid} ${subUserData.msg},自动取消订阅`);
|
|
1734
1844
|
// 删除该条数据
|
|
1735
|
-
await
|
|
1845
|
+
await ctx.database.remove('bilibili', { id: sub.id });
|
|
1736
1846
|
// 跳过下面的步骤
|
|
1737
1847
|
continue;
|
|
1738
1848
|
}
|
|
@@ -1745,7 +1855,7 @@ class ComRegister {
|
|
|
1745
1855
|
for (let i = 0; i < attempts; i++) {
|
|
1746
1856
|
try {
|
|
1747
1857
|
// 获取用户信息
|
|
1748
|
-
content = await
|
|
1858
|
+
content = await ctx.ba.getUserInfo(sub.uid);
|
|
1749
1859
|
// 成功则跳出循环
|
|
1750
1860
|
break;
|
|
1751
1861
|
}
|
|
@@ -1753,7 +1863,7 @@ class ComRegister {
|
|
|
1753
1863
|
this.logger.error('getSubFromDatabase() getUserInfo() 发生了错误,错误为:' + e.message);
|
|
1754
1864
|
if (i === attempts - 1) { // 已尝试三次
|
|
1755
1865
|
// 发送私聊消息并重启服务
|
|
1756
|
-
return await this.sendPrivateMsgAndStopService();
|
|
1866
|
+
return await this.sendPrivateMsgAndStopService(ctx);
|
|
1757
1867
|
}
|
|
1758
1868
|
}
|
|
1759
1869
|
}
|
|
@@ -1762,7 +1872,7 @@ class ComRegister {
|
|
|
1762
1872
|
// 定义函数删除数据和发送提示
|
|
1763
1873
|
const deleteSub = async () => {
|
|
1764
1874
|
// 从数据库删除该条数据
|
|
1765
|
-
await
|
|
1875
|
+
await ctx.database.remove('bilibili', { id: sub.id });
|
|
1766
1876
|
// 给用户发送提示
|
|
1767
1877
|
await this.sendPrivateMsg(`UID:${sub.uid} 数据库内容被篡改,已取消对该UP主的订阅`);
|
|
1768
1878
|
};
|
|
@@ -1823,28 +1933,28 @@ class ComRegister {
|
|
|
1823
1933
|
// 直播订阅数+1
|
|
1824
1934
|
liveSubNum++;
|
|
1825
1935
|
// 订阅直播,开始循环检测
|
|
1826
|
-
this.liveDetectWithListener(sub.room_id, target);
|
|
1936
|
+
this.liveDetectWithListener(ctx, sub.room_id, target);
|
|
1827
1937
|
}
|
|
1828
1938
|
}
|
|
1829
1939
|
// 保存新订阅对象
|
|
1830
1940
|
this.subManager.push(subManagerItem);
|
|
1831
1941
|
}
|
|
1832
1942
|
}
|
|
1833
|
-
checkIfDynamicDetectIsNeeded() {
|
|
1943
|
+
checkIfDynamicDetectIsNeeded(ctx) {
|
|
1834
1944
|
// 检查是否有订阅对象需要动态监测
|
|
1835
1945
|
if (this.subManager.some(sub => sub.dynamic))
|
|
1836
|
-
this.enableDynamicDetect();
|
|
1946
|
+
this.enableDynamicDetect(ctx);
|
|
1837
1947
|
}
|
|
1838
|
-
enableDynamicDetect() {
|
|
1948
|
+
enableDynamicDetect(ctx) {
|
|
1839
1949
|
// 开始动态监测
|
|
1840
1950
|
if (this.config.dynamicDebugMode) {
|
|
1841
|
-
this.dynamicDispose =
|
|
1951
|
+
this.dynamicDispose = ctx.setInterval(this.debug_dynamicDetect(ctx), this.config.dynamicLoopTime * 1000);
|
|
1842
1952
|
}
|
|
1843
1953
|
else {
|
|
1844
|
-
this.dynamicDispose =
|
|
1954
|
+
this.dynamicDispose = ctx.setInterval(this.dynamicDetect(ctx), this.config.dynamicLoopTime * 1000);
|
|
1845
1955
|
}
|
|
1846
1956
|
}
|
|
1847
|
-
unsubSingle(id /* UID或RoomId */, type /* 0取消Live订阅,1取消Dynamic订阅 */) {
|
|
1957
|
+
unsubSingle(ctx, id /* UID或RoomId */, type /* 0取消Live订阅,1取消Dynamic订阅 */) {
|
|
1848
1958
|
// 定义返回消息
|
|
1849
1959
|
let msg;
|
|
1850
1960
|
// 定义方法:检查是否没有任何订阅
|
|
@@ -1854,7 +1964,7 @@ class ComRegister {
|
|
|
1854
1964
|
// 从管理对象中移除
|
|
1855
1965
|
this.subManager.splice(index, 1);
|
|
1856
1966
|
// 从数据库中删除
|
|
1857
|
-
|
|
1967
|
+
ctx.database.remove('bilibili', [this.subManager[index].id]);
|
|
1858
1968
|
// num--
|
|
1859
1969
|
this.num--;
|
|
1860
1970
|
// 判断是否还存在订阅了动态的对象,不存在则停止动态监测
|
|
@@ -1873,6 +1983,9 @@ class ComRegister {
|
|
|
1873
1983
|
return msg;
|
|
1874
1984
|
}
|
|
1875
1985
|
// 取消订阅
|
|
1986
|
+
if (sub.live)
|
|
1987
|
+
sub.liveDispose();
|
|
1988
|
+
sub.liveDispose = null;
|
|
1876
1989
|
sub.live = false;
|
|
1877
1990
|
// 如果没有对这个UP的任何订阅,则移除
|
|
1878
1991
|
if (checkIfNoSubExist(sub)) {
|
|
@@ -1881,7 +1994,7 @@ class ComRegister {
|
|
|
1881
1994
|
return '已取消订阅该用户';
|
|
1882
1995
|
}
|
|
1883
1996
|
// 更新数据库
|
|
1884
|
-
|
|
1997
|
+
ctx.database.upsert('bilibili', [{
|
|
1885
1998
|
id: +`${sub.id}`,
|
|
1886
1999
|
live: 0
|
|
1887
2000
|
}]);
|
|
@@ -1908,7 +2021,7 @@ class ComRegister {
|
|
|
1908
2021
|
return '已取消订阅该用户';
|
|
1909
2022
|
}
|
|
1910
2023
|
// 更新数据库
|
|
1911
|
-
|
|
2024
|
+
ctx.database.upsert('bilibili', [{
|
|
1912
2025
|
id: sub.id,
|
|
1913
2026
|
dynamic: 0
|
|
1914
2027
|
}]);
|
|
@@ -1918,7 +2031,7 @@ class ComRegister {
|
|
|
1918
2031
|
}
|
|
1919
2032
|
finally {
|
|
1920
2033
|
// 执行完该方法后,保证执行一次updateSubNotifier()
|
|
1921
|
-
this.updateSubNotifier();
|
|
2034
|
+
this.updateSubNotifier(ctx);
|
|
1922
2035
|
}
|
|
1923
2036
|
}
|
|
1924
2037
|
checkIfUserIsTheLastOneWhoSubDyn() {
|
|
@@ -1928,12 +2041,15 @@ class ComRegister {
|
|
|
1928
2041
|
this.dynamicDispose = null;
|
|
1929
2042
|
}
|
|
1930
2043
|
}
|
|
1931
|
-
unsubAll(uid) {
|
|
2044
|
+
unsubAll(ctx, uid) {
|
|
1932
2045
|
this.subManager.filter(sub => sub.uid === uid).map(async (sub, i) => {
|
|
2046
|
+
// 取消全部订阅 执行dispose方法,销毁定时器
|
|
2047
|
+
if (sub.live)
|
|
2048
|
+
await this.subManager[i].liveDispose();
|
|
1933
2049
|
// 判断是否还存在订阅了动态的对象,不存在则停止动态监测
|
|
1934
2050
|
this.checkIfUserIsTheLastOneWhoSubDyn();
|
|
1935
2051
|
// 从数据库中删除订阅
|
|
1936
|
-
await
|
|
2052
|
+
await ctx.database.remove('bilibili', { uid: this.subManager[i].uid });
|
|
1937
2053
|
// 将该订阅对象从订阅管理对象中移除
|
|
1938
2054
|
this.subManager.splice(i, 1);
|
|
1939
2055
|
// id--
|
|
@@ -1941,13 +2057,13 @@ class ComRegister {
|
|
|
1941
2057
|
// 发送成功通知
|
|
1942
2058
|
this.sendPrivateMsg(`UID:${uid},已取消订阅该用户`);
|
|
1943
2059
|
// 更新控制台提示
|
|
1944
|
-
this.updateSubNotifier();
|
|
2060
|
+
this.updateSubNotifier(ctx);
|
|
1945
2061
|
});
|
|
1946
2062
|
}
|
|
1947
|
-
async checkIfIsLogin() {
|
|
1948
|
-
if ((await
|
|
2063
|
+
async checkIfIsLogin(ctx) {
|
|
2064
|
+
if ((await ctx.database.get('loginBili', 1)).length !== 0) { // 数据库中有数据
|
|
1949
2065
|
// 检查cookie中是否有值
|
|
1950
|
-
if (
|
|
2066
|
+
if (ctx.ba.getCookies() !== '[]') { // 有值说明已登录
|
|
1951
2067
|
return true;
|
|
1952
2068
|
}
|
|
1953
2069
|
}
|
|
@@ -1965,7 +2081,6 @@ class ComRegister {
|
|
|
1965
2081
|
channelId: koishi_1.Schema.string().description('频道/群组号'),
|
|
1966
2082
|
dynamic: koishi_1.Schema.boolean().description('该频道/群组是否推送动态信息'),
|
|
1967
2083
|
live: koishi_1.Schema.boolean().description('该频道/群组是否推送直播通知'),
|
|
1968
|
-
liveDanmaku: koishi_1.Schema.boolean().description('该频道/群组是否推送弹幕消息'),
|
|
1969
2084
|
atAll: koishi_1.Schema.boolean().description('推送开播通知时是否艾特全体成员')
|
|
1970
2085
|
})).description('频道/群组信息'),
|
|
1971
2086
|
platform: koishi_1.Schema.string().description('推送平台')
|
|
@@ -1979,6 +2094,7 @@ class ComRegister {
|
|
|
1979
2094
|
}),
|
|
1980
2095
|
unlockSubLimits: koishi_1.Schema.boolean().required(),
|
|
1981
2096
|
automaticResend: koishi_1.Schema.boolean().required(),
|
|
2097
|
+
changeMasterInfoApi: koishi_1.Schema.boolean().required(),
|
|
1982
2098
|
restartPush: koishi_1.Schema.boolean().required(),
|
|
1983
2099
|
pushTime: koishi_1.Schema.number().required(),
|
|
1984
2100
|
liveLoopTime: koishi_1.Schema.number().default(10),
|