koishi-plugin-bilibili-notify 2.0.0-alpha.13 → 2.0.0-alpha.15

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 ADDED
@@ -0,0 +1,9 @@
1
+ import { Context, Logger } from "koishi";
2
+ declare class BLive {
3
+ static inject: string[];
4
+ ctx: Context;
5
+ logger: Logger;
6
+ constructor(ctx: Context);
7
+ connectToLiveBroadcastRoom(roomId: string): void;
8
+ }
9
+ export default BLive;
package/lib/blive.js ADDED
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class BLive {
4
+ // 必须服务
5
+ static inject = [''];
6
+ // 定义类属性
7
+ ctx;
8
+ logger;
9
+ // 构造函数
10
+ constructor(ctx) {
11
+ // 将Context赋值给类属性
12
+ this.ctx = ctx;
13
+ // 将logger赋值给类属性
14
+ this.logger = ctx.logger('bl');
15
+ }
16
+ // 定义方法
17
+ connectToLiveBroadcastRoom(roomId) {
18
+ console.log(roomId);
19
+ }
20
+ }
21
+ exports.default = BLive;
@@ -36,6 +36,7 @@ declare class ComRegister {
36
36
  dynamicDispose: Function;
37
37
  sendMsgFunc: (bot: Bot<Context, any>, channelId: string, content: any) => Promise<void>;
38
38
  constructor(ctx: Context, config: ComRegister.Config);
39
+ init(ctx: Context, config: ComRegister.Config): Promise<void>;
39
40
  splitMultiPlatformStr(str: string): Target;
40
41
  getBot(ctx: Context, pf: string): Bot<Context, any>;
41
42
  sendPrivateMsg(content: string): Promise<void>;
@@ -53,7 +54,10 @@ declare class ComRegister {
53
54
  flag: boolean;
54
55
  msg: string;
55
56
  }>;
56
- getSubFromDatabase(ctx: Context): Promise<void>;
57
+ loadSubFromConfig(ctx: Context, subs: ComRegister.Config["sub"]): Promise<void>;
58
+ loadSubFromDatabase(ctx: Context): Promise<void>;
59
+ checkIfDynamicDetectIsNeeded(ctx: Context): void;
60
+ enableDynamicDetect(ctx: Context): void;
57
61
  unsubSingle(ctx: Context, id: string, type: number): string;
58
62
  checkIfUserIsTheLastOneWhoSubDyn(): void;
59
63
  unsubAll(ctx: Context, uid: string): void;
@@ -61,6 +65,12 @@ declare class ComRegister {
61
65
  }
62
66
  declare namespace ComRegister {
63
67
  interface Config {
68
+ sub: Array<{
69
+ uid: string;
70
+ dynamic: boolean;
71
+ live: boolean;
72
+ target: string;
73
+ }>;
64
74
  master: {
65
75
  enable: boolean;
66
76
  platform: string;
@@ -37,56 +37,9 @@ class ComRegister {
37
37
  sendMsgFunc;
38
38
  // 构造函数
39
39
  constructor(ctx, config) {
40
- this.logger = ctx.logger('cr');
41
- this.config = config;
42
- // 拿到私人机器人实例
43
- this.privateBot = ctx.bots.find(bot => bot.platform === config.master.platform);
44
- if (!this.privateBot) {
45
- ctx.notifier.create({
46
- content: '您未配置私人机器人,将无法向您推送机器人状态!'
47
- });
48
- this.logger.error('您未配置私人机器人,将无法向您推送机器人状态!');
49
- }
50
- // 检查登录数据库是否有数据
51
- ctx.database.get('loginBili', 1, ['dynamic_group_id']).then(data => this.loginDBData = data[0]);
52
- // 从数据库获取订阅
53
- this.getSubFromDatabase(ctx);
54
- // 判断消息发送方式
55
- if (config.automaticResend) {
56
- this.sendMsgFunc = async (bot, channelId, content) => {
57
- // 多次尝试发送消息
58
- const attempts = 3;
59
- for (let i = 0; i < attempts; i++) {
60
- try {
61
- // 发送消息
62
- await bot.sendMessage(channelId, content);
63
- // 防止消息发送速度过快被忽略
64
- await ctx.sleep(500);
65
- // 成功发送消息,跳出循环
66
- break;
67
- }
68
- catch (e) {
69
- if (i === attempts - 1) { // 已尝试三次
70
- this.logger.error(`发送群组ID:${channelId}消息失败!原因: ` + e.message);
71
- console.log(e);
72
- this.sendPrivateMsg(`发送群组ID:${channelId}消息失败,请查看日志`);
73
- }
74
- }
75
- }
76
- };
77
- }
78
- else {
79
- this.sendMsgFunc = async (bot, guild, content) => {
80
- try {
81
- // 发送消息
82
- await bot.sendMessage(guild, content);
83
- }
84
- catch (e) {
85
- this.logger.error(`发送群组ID:${guild}消息失败!原因: ` + e.message);
86
- await this.sendPrivateMsg(`发送群组ID:${guild}消息失败,请查看日志`);
87
- }
88
- };
89
- }
40
+ // 初始化
41
+ this.init(ctx, config);
42
+ // 注册指令
90
43
  const statusCom = ctx.command('status', '插件状态相关指令', { permissions: ['authority:5'] });
91
44
  statusCom.subcommand('.dyn', '查看动态监测运行状态')
92
45
  .usage('查看动态监测运行状态')
@@ -198,7 +151,7 @@ class ComRegister {
198
151
  // 销毁定时器
199
152
  this.loginTimer();
200
153
  // 订阅之前的订阅
201
- await this.getSubFromDatabase(ctx);
154
+ await this.loadSubFromDatabase(ctx);
202
155
  // 清除控制台通知
203
156
  ctx.ba.disposeNotifier();
204
157
  // 发送成功登录推送
@@ -265,12 +218,7 @@ class ComRegister {
265
218
  // id--
266
219
  this.num--;
267
220
  // 判断是否还有动态订阅
268
- if (this.dynamicDispose && !this.subManager.find((sub) => sub.dynamic === true)) { // 没有动态订阅
269
- // 将动态检测关闭
270
- this.dynamicDispose();
271
- // 将动态监测置为空
272
- this.dynamicDispose = null;
273
- }
221
+ this.checkIfUserIsTheLastOneWhoSubDyn();
274
222
  // 发送成功通知
275
223
  await session.send('已取消订阅该用户');
276
224
  // 更新控制台提示
@@ -293,7 +241,7 @@ class ComRegister {
293
241
  });
294
242
  biliCom
295
243
  .subcommand('.sub <mid:string> [...groupId:string]', '订阅用户动态和直播通知')
296
- .option('multiplatform', '-m <value:string>', { type: /^(-?[a-zA-Z0-9]+@?[a-zA-Z0-9]*\.[a-zA-Z0-9]+(?:,-?[a-zA-Z0-9]+@?[a-zA-Z0-9]*\.[a-zA-Z0-9]+)*)(;(-?[a-zA-Z0-9]+@?[a-zA-Z0-9]*\.[a-zA-Z0-9]+(?:,-?[a-zA-Z0-9]+@?[a-zA-Z0-9]*\.[a-zA-Z0-9]+)*))*$/ })
244
+ .option('multiplatform', '-m <value:string>', { type: /^(?:-?[A-Za-z0-9]+@?(?:,-?[A-Za-z0-9]+@?)*\.[A-Za-z0-9]+)(?:;(?:-?[A-Za-z0-9]+@?(?:,-?[A-Za-z0-9]+@?)*\.[A-Za-z0-9]+))*$/ })
297
245
  .option('live', '-l')
298
246
  .option('dynamic', '-d')
299
247
  .option('atAll', '-a')
@@ -489,13 +437,7 @@ class ComRegister {
489
437
  if (dynamicMsg) {
490
438
  // 判断是否开启动态监测
491
439
  if (!this.dynamicDispose) {
492
- // 开启动态监测
493
- if (this.config.dynamicDebugMode) {
494
- this.dynamicDispose = ctx.setInterval(this.debug_dynamicDetect(ctx), config.dynamicLoopTime * 1000);
495
- }
496
- else {
497
- this.dynamicDispose = ctx.setInterval(this.dynamicDetect(ctx), config.dynamicLoopTime * 1000);
498
- }
440
+ this.enableDynamicDetect(ctx);
499
441
  }
500
442
  // 发送订阅消息通知
501
443
  await session.send(`订阅${userData.info.uname}动态通知`);
@@ -580,6 +522,66 @@ class ComRegister {
580
522
  await session.send('已发送消息,如未收到则说明您的机器人不支持发送私聊消息或您的信息填写有误');
581
523
  });
582
524
  }
525
+ async init(ctx, config) {
526
+ // 设置logger
527
+ this.logger = ctx.logger('cr');
528
+ // 将config设置给类属性
529
+ this.config = config;
530
+ // 拿到私人机器人实例
531
+ this.privateBot = ctx.bots.find(bot => bot.platform === config.master.platform);
532
+ if (!this.privateBot) {
533
+ ctx.notifier.create({
534
+ content: '您未配置私人机器人,将无法向您推送机器人状态!'
535
+ });
536
+ this.logger.error('您未配置私人机器人,将无法向您推送机器人状态!');
537
+ }
538
+ // 检查登录数据库是否有数据
539
+ this.loginDBData = (await ctx.database.get('loginBili', 1, ['dynamic_group_id']))[0];
540
+ // 从配置获取订阅
541
+ config.sub && await this.loadSubFromConfig(ctx, config.sub);
542
+ // 从数据库获取订阅
543
+ await this.loadSubFromDatabase(ctx);
544
+ // 判断消息发送方式
545
+ if (config.automaticResend) {
546
+ this.sendMsgFunc = async (bot, channelId, content) => {
547
+ // 多次尝试发送消息
548
+ const attempts = 3;
549
+ for (let i = 0; i < attempts; i++) {
550
+ try {
551
+ // 发送消息
552
+ await bot.sendMessage(channelId, content);
553
+ // 防止消息发送速度过快被忽略
554
+ await ctx.sleep(500);
555
+ // 成功发送消息,跳出循环
556
+ break;
557
+ }
558
+ catch (e) {
559
+ if (i === attempts - 1) { // 已尝试三次
560
+ this.logger.error(`发送群组ID:${channelId}消息失败!原因: ` + e.message);
561
+ console.log(e);
562
+ this.sendPrivateMsg(`发送群组ID:${channelId}消息失败,请查看日志`);
563
+ }
564
+ }
565
+ }
566
+ };
567
+ }
568
+ else {
569
+ this.sendMsgFunc = async (bot, guild, content) => {
570
+ try {
571
+ // 发送消息
572
+ await bot.sendMessage(guild, content);
573
+ }
574
+ catch (e) {
575
+ this.logger.error(`发送群组ID:${guild}消息失败!原因: ` + e.message);
576
+ await this.sendPrivateMsg(`发送群组ID:${guild}消息失败,请查看日志`);
577
+ }
578
+ };
579
+ }
580
+ // 检查是否需要动态监测
581
+ this.checkIfDynamicDetectIsNeeded(ctx);
582
+ // 在控制台中显示订阅对象
583
+ this.updateSubNotifier(ctx);
584
+ }
583
585
  splitMultiPlatformStr(str) {
584
586
  return str.split(';').map(cv => cv.split('.')).map(([idStr, platform]) => {
585
587
  const channelIdArr = idStr.split(',').map(id => {
@@ -1401,7 +1403,71 @@ class ComRegister {
1401
1403
  // 订阅成功
1402
1404
  return { flag: true, msg: '用户订阅成功' };
1403
1405
  }
1404
- async getSubFromDatabase(ctx) {
1406
+ async loadSubFromConfig(ctx, subs) {
1407
+ for (const sub of subs) {
1408
+ // 整理target
1409
+ const target = this.splitMultiPlatformStr(sub.target);
1410
+ // 定义Data
1411
+ let data;
1412
+ // 定义直播销毁函数
1413
+ let liveDispose;
1414
+ // 判断是否需要订阅直播
1415
+ if (sub.live) {
1416
+ // 获取用户信息
1417
+ let content;
1418
+ // 设置重试次数
1419
+ const attempts = 3;
1420
+ for (let i = 0; i < attempts; i++) {
1421
+ try {
1422
+ // 获取用户信息
1423
+ content = await ctx.ba.getUserInfo(sub.uid);
1424
+ // 成功则跳出循环
1425
+ break;
1426
+ }
1427
+ catch (e) {
1428
+ this.logger.error('getSubFromDatabase() getUserInfo() 发生了错误,错误为:' + e.message);
1429
+ if (i === attempts - 1) { // 已尝试三次
1430
+ // 发送私聊消息并重启服务
1431
+ return await this.sendPrivateMsgAndStopService(ctx);
1432
+ }
1433
+ }
1434
+ }
1435
+ // 获取data
1436
+ data = content.data;
1437
+ // 检查roomid是否存在
1438
+ if (!data.live_room) {
1439
+ // 用户没有开通直播间,无法订阅直播
1440
+ sub.live = false;
1441
+ // 发送提示
1442
+ this.logger.warn(`UID:${sub.uid} 用户没有开通直播间,无法订阅直播!`);
1443
+ }
1444
+ // 判断是否订阅直播
1445
+ if (sub.live) {
1446
+ // 订阅直播
1447
+ liveDispose = ctx.setInterval(() => {
1448
+ this.liveDetect(ctx, data.live_room.room_id, target);
1449
+ }, this.config.liveLoopTime * 1000);
1450
+ }
1451
+ }
1452
+ // 在B站中订阅该对象
1453
+ const subInfo = await this.subUserInBili(ctx, sub.uid);
1454
+ // 判断订阅是否成功
1455
+ if (!subInfo.flag)
1456
+ this.logger.warn(subInfo.msg);
1457
+ // 将该订阅添加到sm中
1458
+ this.subManager.push({
1459
+ id: +sub.uid,
1460
+ uid: sub.uid,
1461
+ roomId: sub.live ? data.live_room.roomid : '',
1462
+ target,
1463
+ platform: '',
1464
+ live: sub.live,
1465
+ dynamic: sub.dynamic,
1466
+ liveDispose
1467
+ });
1468
+ }
1469
+ }
1470
+ async loadSubFromDatabase(ctx) {
1405
1471
  // 判断登录信息是否已加载完毕
1406
1472
  await this.checkIfLoginInfoIsLoaded(ctx);
1407
1473
  // 如果未登录,则直接返回
@@ -1499,6 +1565,7 @@ class ComRegister {
1499
1565
  this.logger.info(`UID:${sub.uid} 房间号被篡改,自动取消订阅`);
1500
1566
  // Send msg
1501
1567
  await this.sendPrivateMsg(`UID:${sub.uid} 房间号被篡改,自动取消订阅`);
1568
+ // 直接返回
1502
1569
  return;
1503
1570
  }
1504
1571
  // 构建订阅对象
@@ -1516,11 +1583,12 @@ class ComRegister {
1516
1583
  if (sub.live) {
1517
1584
  // 判断订阅直播数是否超过限制
1518
1585
  if (!this.config.unlockSubLimits && liveSubNum >= 3) {
1586
+ // 将live改为false
1519
1587
  subManagerItem.live = false;
1520
1588
  // log
1521
1589
  this.logger.warn(`UID:${sub.uid} 订阅直播数超过限制,自动取消订阅`);
1522
1590
  // 发送错误消息
1523
- this.sendPrivateMsg(`UID:${sub.uid} 订阅直播数超过限制,自动取消订阅`);
1591
+ await this.sendPrivateMsg(`UID:${sub.uid} 订阅直播数超过限制,自动取消订阅`);
1524
1592
  }
1525
1593
  else {
1526
1594
  // 直播订阅数+1
@@ -1534,18 +1602,20 @@ class ComRegister {
1534
1602
  // 保存新订阅对象
1535
1603
  this.subManager.push(subManagerItem);
1536
1604
  }
1605
+ }
1606
+ checkIfDynamicDetectIsNeeded(ctx) {
1537
1607
  // 检查是否有订阅对象需要动态监测
1538
- if (this.subManager.some(sub => sub.dynamic)) {
1539
- // 开始动态监测
1540
- if (this.config.dynamicDebugMode) {
1541
- this.dynamicDispose = ctx.setInterval(this.debug_dynamicDetect(ctx), 10000);
1542
- }
1543
- else {
1544
- this.dynamicDispose = ctx.setInterval(this.dynamicDetect(ctx), 10000 /* this.config.dynamicLoopTime * 1000 */);
1545
- }
1608
+ if (this.subManager.some(sub => sub.dynamic))
1609
+ this.enableDynamicDetect(ctx);
1610
+ }
1611
+ enableDynamicDetect(ctx) {
1612
+ // 开始动态监测
1613
+ if (this.config.dynamicDebugMode) {
1614
+ this.dynamicDispose = ctx.setInterval(this.debug_dynamicDetect(ctx), this.config.dynamicLoopTime * 1000);
1615
+ }
1616
+ else {
1617
+ this.dynamicDispose = ctx.setInterval(this.dynamicDetect(ctx), this.config.dynamicLoopTime * 1000);
1546
1618
  }
1547
- // 在控制台中显示订阅对象
1548
- this.updateSubNotifier(ctx);
1549
1619
  }
1550
1620
  unsubSingle(ctx, id /* UID或RoomId */, type /* 0取消Live订阅,1取消Dynamic订阅 */) {
1551
1621
  // 定义返回消息
@@ -1628,7 +1698,7 @@ class ComRegister {
1628
1698
  }
1629
1699
  }
1630
1700
  checkIfUserIsTheLastOneWhoSubDyn() {
1631
- if (this.subManager.some(sub => sub.dynamic)) {
1701
+ if (this.dynamicDispose && !this.subManager.some(sub => sub.dynamic)) {
1632
1702
  // 停止动态监测
1633
1703
  this.dynamicDispose();
1634
1704
  this.dynamicDispose = null;
@@ -1665,6 +1735,13 @@ class ComRegister {
1665
1735
  }
1666
1736
  (function (ComRegister) {
1667
1737
  ComRegister.Config = koishi_1.Schema.object({
1738
+ sub: koishi_1.Schema.array(koishi_1.Schema.object({
1739
+ uid: koishi_1.Schema.string(),
1740
+ roomid: koishi_1.Schema.string(),
1741
+ dynamic: koishi_1.Schema.boolean(),
1742
+ live: koishi_1.Schema.boolean(),
1743
+ target: koishi_1.Schema.string(),
1744
+ })),
1668
1745
  master: koishi_1.Schema.object({
1669
1746
  enable: koishi_1.Schema.boolean(),
1670
1747
  platform: koishi_1.Schema.string(),
package/lib/index.d.ts CHANGED
@@ -15,6 +15,13 @@ export interface Config {
15
15
  automaticResend: boolean;
16
16
  renderType: 'render' | 'page';
17
17
  userAgent: string;
18
+ subTitle: {};
19
+ sub: Array<{
20
+ uid: string;
21
+ dynamic: boolean;
22
+ live: boolean;
23
+ target: string;
24
+ }>;
18
25
  dynamic: {};
19
26
  dynamicUrl: boolean;
20
27
  dynamicCheckNumber: number;
package/lib/index.js CHANGED
@@ -93,6 +93,13 @@ exports.Config = koishi_1.Schema.object({
93
93
  userAgent: koishi_1.Schema.string()
94
94
  .required()
95
95
  .description('设置请求头User-Agen,请求出现-352时可以尝试修改,UA获取方法可参考:https://blog.csdn.net/qq_44503987/article/details/104929111'),
96
+ subTitle: koishi_1.Schema.object({}).description('手动订阅'),
97
+ sub: koishi_1.Schema.array(koishi_1.Schema.object({
98
+ uid: koishi_1.Schema.string(),
99
+ dynamic: koishi_1.Schema.boolean(),
100
+ live: koishi_1.Schema.boolean(),
101
+ target: koishi_1.Schema.string()
102
+ })).role('table').description('手动输入订阅信息,方便自定义订阅内容,这里的订阅内容不会存入数据库。uid: 订阅用户UID,dynamic: 是否需要订阅动态,live: 是否需要订阅直播,target:推送目标群组/频道号,若有多个请用逗号分隔'),
96
103
  dynamic: koishi_1.Schema.object({}).description('动态推送设置'),
97
104
  dynamicUrl: koishi_1.Schema.boolean()
98
105
  .default(false)
@@ -290,6 +297,7 @@ class ServerManager extends koishi_1.Service {
290
297
  });
291
298
  // CR = ComRegister
292
299
  const cr = this.ctx.plugin(comRegister_1.default, {
300
+ sub: globalConfig.sub,
293
301
  master: globalConfig.master,
294
302
  unlockSubLimits: globalConfig.unlockSubLimits,
295
303
  automaticResend: globalConfig.automaticResend,
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": "2.0.0-alpha.13",
4
+ "version": "2.0.0-alpha.15",
5
5
  "contributors": [
6
6
  "Akokko <admin@akokko.com>"
7
7
  ],
@@ -60,7 +60,7 @@
60
60
  - 选项说明:`bili sub <uid>` 有四个选项:-l -d -m -a
61
61
  - `-l` 为订阅UP主直播间,包括直播开播通知,定时推送直播内容,下播通知
62
62
  - `-d` 为订阅UP主动态推送,目前实现推送的动态类型有:普通图文动态,转发动态,直播预约动态
63
- - `-m` 为多平台动态推送,格式为:群号/频道号,群号/频道号,群号/频道号.平台名;群号/频道号,群号/频道号.平台号。如果群号/频道号为 `all` 代表推送该平台下的所有群聊或/频道。在群号/频道号后加 `@` 则代表这个群聊/频道需要@全体成员
63
+ - `-m` 为多平台动态推送,格式为:群号/频道号,群号/频道号,群号/频道号.平台名;群号/频道号,群号/频道号.平台号。如果群号/频道号为 `all` 代表推送该平台下的所有群聊或/频道。在群号/频道号后加 `@` 则代表这个群聊/频道需要@全体成员。订阅TG群组时请使用 `bili sub xxx -m "xxxxxxx"` 这样的格式
64
64
  - `-a` 为是否艾特全体成员,对 `-m` 参数不生效
65
65
 
66
66
  例如:
@@ -200,6 +200,8 @@
200
200
  - ver 2.0.0-alpha.11 回档:订阅时可直接接收群号/频道号 修复:直播过程推送消息不成功的bug
201
201
  - ver 2.0.0-alpha.12 更改:开启艾特全体成员后,只有在开播时才艾特全体成员
202
202
  - ver 2.0.0-alpha.13 修复:无法对TG群组的特殊频道号进行订阅处理;提示 `您未配置对应平台的机器人,不能在该平台进行订阅操作` 仍进行订阅操作
203
+ - ver 2.0.0-alpha.14 修复:订阅TG群组时提示输入无效
204
+ - ver 2.0.0-alpha.15 新增:手动订阅功能 修复:一些潜在的bug
203
205
 
204
206
  ## 交流群
205
207