koishi-plugin-bilibili-notify 3.2.4 → 3.2.5-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.
Files changed (62) hide show
  1. package/lib/biliAPI-BB6Ih4EM.mjs +659 -0
  2. package/lib/biliAPI-GF2e2-2V.js +664 -0
  3. package/lib/biliAPI.d.mts +82 -0
  4. package/lib/biliAPI.d.ts +75 -70
  5. package/lib/biliAPI.js +5 -762
  6. package/lib/biliAPI.mjs +4 -0
  7. package/lib/blive-BBVUYbxX.js +43 -0
  8. package/lib/blive-CyC_0-9A.mjs +37 -0
  9. package/lib/blive.d.mts +19 -0
  10. package/lib/blive.d.ts +15 -12
  11. package/lib/blive.js +4 -59
  12. package/lib/blive.mjs +3 -0
  13. package/lib/chunk-CrFLpmCJ.js +67 -0
  14. package/lib/chunk-DBmNgn_A.mjs +40 -0
  15. package/lib/comRegister-CcqOpkwB.mjs +1421 -0
  16. package/lib/comRegister-t7UnYFig.js +1426 -0
  17. package/lib/comRegister.d.mts +109 -0
  18. package/lib/comRegister.d.ts +103 -100
  19. package/lib/comRegister.js +6 -1896
  20. package/lib/comRegister.mjs +5 -0
  21. package/lib/database-4f9U--Ji.mjs +20 -0
  22. package/lib/database-Ct2yIdxk.js +37 -0
  23. package/lib/database-Do3O9MDA.d.mts +18 -0
  24. package/lib/database-KWDs372o.d.ts +18 -0
  25. package/lib/database.d.mts +2 -0
  26. package/lib/database.d.ts +2 -14
  27. package/lib/database.js +5 -14
  28. package/lib/database.mjs +3 -0
  29. package/lib/font/HYZhengYuan-75W.js +5 -0
  30. package/lib/font/HYZhengYuan-75W.mjs +5 -0
  31. package/lib/generateImg-E9JeFpQX.js +92628 -0
  32. package/lib/generateImg-zwzMYjw6.mjs +92623 -0
  33. package/lib/generateImg.d.mts +65 -0
  34. package/lib/generateImg.d.ts +59 -46
  35. package/lib/generateImg.js +4 -1504
  36. package/lib/generateImg.mjs +4 -0
  37. package/lib/img/arrow.js +5 -0
  38. package/lib/img/arrow.mjs +5 -0
  39. package/lib/index-DeGUe4jM.d.mts +265 -0
  40. package/lib/index-MJKYbMf7.d.ts +265 -0
  41. package/lib/index.d.mts +73 -0
  42. package/lib/index.d.ts +65 -62
  43. package/lib/index.js +296 -394
  44. package/lib/index.mjs +300 -0
  45. package/lib/page/0.mjs +0 -0
  46. package/lib/type/index.d.mts +2 -0
  47. package/lib/type/index.d.ts +2 -262
  48. package/lib/type/index.js +5 -18
  49. package/lib/type/index.mjs +3 -0
  50. package/lib/type-C5ZxPrgY.mjs +19 -0
  51. package/lib/type-WjCy4YMc.js +31 -0
  52. package/lib/utils/index.d.mts +15 -0
  53. package/lib/utils/index.d.ts +8 -6
  54. package/lib/utils/index.js +6 -102
  55. package/lib/utils/index.mjs +3 -0
  56. package/lib/utils-BoaYrSvG.js +79 -0
  57. package/lib/utils-C95cnBJG.mjs +61 -0
  58. package/package.json +16 -11
  59. package/readme.md +12 -2
  60. package/lib/img/arrow.png +0 -0
  61. /package/lib/{font/HYZhengYuan-75W.ttf → assets/HYZhengYuan-75W-CAgMN3FH.ttf} +0 -0
  62. /package/lib/page/{0.html → 0.js} +0 -0
@@ -1,1896 +1,6 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const jsx_runtime_1 = require("@satorijs/element/jsx-runtime");
7
- // Koishi核心依赖
8
- const koishi_1 = require("koishi");
9
- const qrcode_1 = __importDefault(require("qrcode"));
10
- const cron_1 = require("cron");
11
- // Utils
12
- const utils_1 = require("./utils");
13
- // Types
14
- const type_1 = require("./type");
15
- const luxon_1 = require("luxon");
16
- class ComRegister {
17
- // 必须服务
18
- static inject = ["ba", "gi", "database", "bl", "sm"];
19
- // 定义数组:QQ相关bot
20
- qqRelatedBotList = [
21
- "qq",
22
- "onebot",
23
- "red",
24
- "satori",
25
- "chronocat",
26
- ];
27
- // logger
28
- logger;
29
- // config
30
- config;
31
- // 登录定时器
32
- loginTimer;
33
- // 订阅数量
34
- num = 0;
35
- // 重启次数
36
- rebootCount = 0;
37
- // 订阅通知
38
- subNotifier;
39
- // Context
40
- ctx;
41
- // 订阅管理器
42
- subManager = [];
43
- // 动态时间线管理器
44
- dynamicTimelineManager = new Map();
45
- // 直播状态管理器
46
- liveStatusManager = new Map();
47
- // 检查登录数据库是否有数据
48
- loginDBData;
49
- // 机器人实例
50
- privateBot;
51
- // 动态检测销毁函数
52
- dynamicJob;
53
- // 直播检测销毁函数
54
- liveJob;
55
- // 构造函数
56
- constructor(ctx, config) {
57
- // 将ctx赋值给类属性
58
- this.ctx = ctx;
59
- // 初始化
60
- this.init(config);
61
- // 注册指令
62
- const statusCom = ctx.command("status", "插件状态相关指令", {
63
- permissions: ["authority:5"],
64
- });
65
- statusCom
66
- .subcommand(".dyn", "查看动态监测运行状态")
67
- .usage("查看动态监测运行状态")
68
- .example("status dyn")
69
- .action(() => {
70
- if (this.dynamicJob?.isActive) {
71
- return "动态监测正在运行";
72
- }
73
- return "动态监测未运行";
74
- });
75
- statusCom
76
- .subcommand(".sm", "查看订阅管理对象")
77
- .usage("查看订阅管理对象")
78
- .example("status sm")
79
- .action(async () => {
80
- this.logger.info(this.subManager);
81
- return "查看控制台";
82
- });
83
- statusCom
84
- .subcommand(".bot", "查询当前拥有的机器人信息", { hidden: true })
85
- .usage("查询当前拥有的机器人信息")
86
- .example("status bot 查询当前拥有的机器人信息")
87
- .action(() => {
88
- this.logger.info("开始输出BOT信息");
89
- for (const bot of ctx.bots) {
90
- this.logger.info("--------------------------------");
91
- this.logger.info(`平台:${bot.platform}`);
92
- this.logger.info(`名称:${bot.user.name}`);
93
- this.logger.info("--------------------------------");
94
- }
95
- });
96
- statusCom
97
- .subcommand(".env", "查询当前环境的信息", { hidden: true })
98
- .usage("查询当前环境的信息")
99
- .example("status env 查询当前环境的信息")
100
- .action(async ({ session }) => {
101
- await session.send(`Guild ID:${session.event.guild.id}`);
102
- await session.send(`Channel ID: ${session.event.channel.id}`);
103
- });
104
- const biliCom = ctx.command("bili", "bili-notify插件相关指令", {
105
- permissions: ["authority:3"],
106
- });
107
- biliCom
108
- .subcommand(".login", "登录B站之后才可以进行之后的操作")
109
- .usage("使用二维码登录,登录B站之后才可以进行之后的操作")
110
- .example("bili login")
111
- .action(async ({ session }) => {
112
- this.logger.info("调用bili login指令");
113
- // 获取二维码
114
- // biome-ignore lint/suspicious/noExplicitAny: <explanation>
115
- let content;
116
- try {
117
- content = await ctx.ba.getLoginQRCode();
118
- }
119
- catch (e) {
120
- return "bili login getLoginQRCode() 本次网络请求失败";
121
- }
122
- // 判断是否出问题
123
- if (content.code !== 0)
124
- return await session.send("出问题咯,请联系管理员解决");
125
- // 生成二维码
126
- qrcode_1.default.toBuffer(content.data.url, {
127
- errorCorrectionLevel: "H", // 错误更正水平
128
- type: "png", // 输出类型
129
- margin: 1, // 边距大小
130
- color: {
131
- dark: "#000000", // 二维码颜色
132
- light: "#FFFFFF", // 背景颜色
133
- },
134
- }, async (err, buffer) => {
135
- if (err)
136
- return await session.send("二维码生成出错,请重新尝试");
137
- await session.send(koishi_1.h.image(buffer, "image/jpeg"));
138
- });
139
- // 检查之前是否存在登录定时器
140
- if (this.loginTimer)
141
- this.loginTimer();
142
- // 设置flag
143
- let flag = true;
144
- // 发起登录请求检查登录状态
145
- this.loginTimer = ctx.setInterval(async () => {
146
- try {
147
- // 判断上一个循环是否完成
148
- if (!flag)
149
- return;
150
- flag = false;
151
- // 获取登录信息
152
- // biome-ignore lint/suspicious/noExplicitAny: <explanation>
153
- let loginContent;
154
- try {
155
- loginContent = await ctx.ba.getLoginStatus(content.data.qrcode_key);
156
- }
157
- catch (e) {
158
- this.logger.error(e);
159
- return;
160
- }
161
- if (loginContent.code !== 0) {
162
- this.loginTimer();
163
- return await session.send("登录失败请重试");
164
- }
165
- if (loginContent.data.code === 86038) {
166
- this.loginTimer();
167
- return await session.send("二维码已失效,请重新登录");
168
- }
169
- if (loginContent.data.code === 0) {
170
- // 登录成功
171
- const encryptedCookies = ctx.ba.encrypt(ctx.ba.getCookies());
172
- const encryptedRefreshToken = ctx.ba.encrypt(loginContent.data.refresh_token);
173
- await ctx.database.upsert("loginBili", [
174
- {
175
- id: 1,
176
- bili_cookies: encryptedCookies,
177
- bili_refresh_token: encryptedRefreshToken,
178
- },
179
- ]);
180
- // 检查登录数据库是否有数据
181
- this.loginDBData = (await this.ctx.database.get("loginBili", 1, [
182
- "dynamic_group_id",
183
- ]))[0];
184
- // ba重新加载登录信息
185
- await this.ctx.ba.loadCookiesFromDatabase();
186
- // 判断登录信息是否已加载完毕
187
- await this.checkIfLoginInfoIsLoaded();
188
- // 销毁定时器
189
- this.loginTimer();
190
- // 订阅手动订阅中的订阅
191
- const { code, msg } = await this.loadSubFromConfig(config.sub);
192
- // 判断是否加载成功
193
- if (code !== 0)
194
- this.logger.error(msg);
195
- // 清除控制台通知
196
- ctx.ba.disposeNotifier();
197
- // 发送成功登录推送
198
- await session.send("登录成功");
199
- // bili show
200
- await session.execute("bili list");
201
- // 开启cookies刷新检测
202
- ctx.ba.enableRefreshCookiesDetect();
203
- }
204
- }
205
- finally {
206
- flag = true;
207
- }
208
- }, 1000);
209
- });
210
- biliCom
211
- .subcommand(".list", "展示订阅对象")
212
- .usage("展示订阅对象")
213
- .example("bili list")
214
- .action(() => {
215
- const subTable = this.subShow();
216
- return subTable;
217
- });
218
- biliCom
219
- .subcommand(".private", "向主人账号发送一条测试消息", { hidden: true })
220
- .usage("向主人账号发送一条测试消息")
221
- .example("bili private 向主人账号发送一条测试消息")
222
- .action(async ({ session }) => {
223
- // 发送消息
224
- await this.sendPrivateMsg("Hello World");
225
- // 发送提示
226
- await session.send("已发送消息,如未收到则说明您的机器人不支持发送私聊消息或您的信息填写有误");
227
- });
228
- biliCom
229
- .subcommand(".ll")
230
- .usage("展示当前正在直播的订阅对象")
231
- .example("bili ll")
232
- .action(async () => {
233
- // 获取liveUsers
234
- const { data: { live_users }, } = (await ctx.ba.getTheUserWhoIsLiveStreaming());
235
- // 定义当前正在直播且订阅的UP主列表
236
- const subLiveUsers = [];
237
- // 获取当前订阅的UP主
238
- for (const sub of this.subManager) {
239
- // 定义开播标志位
240
- let onLive = false;
241
- // 判断items是否存在
242
- if (live_users.items) {
243
- // 遍历liveUsers
244
- for (const user of live_users.items) {
245
- // 判断是否是订阅直播的UP
246
- if (user.mid.toString() === sub.uid && sub.live) {
247
- // 设置标志位为true
248
- onLive = true;
249
- // break
250
- break;
251
- }
252
- }
253
- }
254
- // 判断是否未开播
255
- subLiveUsers.push({
256
- uid: Number.parseInt(sub.uid),
257
- uname: sub.uname,
258
- onLive,
259
- });
260
- }
261
- // 定义table字符串
262
- let table = "";
263
- // 遍历liveUsers
264
- for (const user of subLiveUsers) {
265
- table += `[UID:${user.uid}] 「${user.uname}」 ${user.onLive ? "正在直播" : "未开播"}\n`;
266
- }
267
- return table;
268
- });
269
- biliCom
270
- .subcommand(".dyn <uid:string> [index:number]", "手动推送一条动态信息", {
271
- hidden: true,
272
- })
273
- .usage("手动推送一条动态信息")
274
- .example("bili dyn 233 1 手动推送UID为233用户空间的第一条动态信息")
275
- .action(async ({ session }, uid, index) => {
276
- // 获取index
277
- const i = (index && index - 1) || 0;
278
- // 获取动态
279
- const content = await this.ctx.ba.getUserSpaceDynamic(uid);
280
- // 获取动态内容
281
- const item = content.data.items[i];
282
- // 生成图片
283
- const buffer = await (0, utils_1.withRetry)(async () => {
284
- // 渲染图片
285
- return await this.ctx.gi.generateDynamicImg(item);
286
- }, 1).catch(async (e) => {
287
- // 直播开播动态,不做处理
288
- if (e.message === "直播开播动态,不做处理") {
289
- await session.send("直播开播动态,不做处理");
290
- return;
291
- }
292
- if (e.message === "出现关键词,屏蔽该动态") {
293
- await session.send("已屏蔽该动态");
294
- return;
295
- }
296
- if (e.message === "已屏蔽转发动态") {
297
- await session.send("已屏蔽转发动态");
298
- return;
299
- }
300
- if (e.message === "已屏蔽专栏动态") {
301
- await session.send("已屏蔽专栏动态");
302
- return;
303
- }
304
- // 未知错误
305
- this.logger.error(`dynamicDetect generateDynamicImg() 推送卡片发送失败,原因:${e.message}`);
306
- });
307
- // 发送图片
308
- buffer && (await session.send(koishi_1.h.image(buffer, "image/jpeg")));
309
- });
310
- }
311
- async init(config) {
312
- // 设置logger
313
- this.logger = this.ctx.logger("cr");
314
- // logger
315
- this.logger.info("初始化插件中...");
316
- // 将config设置给类属性
317
- this.config = config;
318
- // 拿到私人机器人实例
319
- this.privateBot = this.ctx.bots.find((bot) => bot.platform === config.master.platform);
320
- if (!this.privateBot) {
321
- this.ctx.notifier.create({
322
- content: "您未配置私人机器人,将无法向您推送机器人状态!",
323
- });
324
- }
325
- // 检查登录数据库是否有数据
326
- this.loginDBData = (await this.ctx.database.get("loginBili", 1, ["dynamic_group_id"]))[0];
327
- // 判断登录信息是否已加载完毕
328
- await this.checkIfLoginInfoIsLoaded();
329
- // 如果未登录,则直接返回
330
- if (!(await this.checkIfIsLogin())) {
331
- // log
332
- this.logger.info("账号未登录,请登录");
333
- return;
334
- }
335
- // 从配置获取订阅
336
- if (config.sub) {
337
- const { code, msg } = await this.loadSubFromConfig(config.sub);
338
- // 判断是否加载成功
339
- if (code !== 0) {
340
- this.logger.error(msg);
341
- this.logger.error("订阅对象加载失败,插件初始化失败!");
342
- // 发送私聊消息
343
- await this.sendPrivateMsg("订阅对象加载失败,插件初始化失败!");
344
- return;
345
- }
346
- }
347
- // 初始化管理器
348
- this.initManager();
349
- // 检查是否需要动态监测
350
- this.checkIfDynamicDetectIsNeeded();
351
- // 检查是否需要直播监测(仅API模式)
352
- this.checkIfLiveDetectIsNeeded();
353
- // 在控制台中显示订阅对象
354
- this.updateSubNotifier();
355
- // 注册插件销毁函数
356
- this.ctx.on("dispose", () => {
357
- // 销毁登录定时器
358
- if (this.loginTimer)
359
- this.loginTimer();
360
- // 销毁动态监测
361
- if (this.dynamicJob)
362
- this.dynamicJob.stop();
363
- // 销毁直播监测
364
- if (this.liveJob)
365
- this.liveJob.stop();
366
- });
367
- // logger
368
- this.logger.info("插件初始化完毕!");
369
- }
370
- initManager() {
371
- for (const sub of this.subManager) {
372
- if (sub.dynamic) {
373
- this.dynamicTimelineManager.set(sub.uid, Math.floor(luxon_1.DateTime.now().toSeconds()));
374
- }
375
- if (sub.live) {
376
- this.liveStatusManager.set(sub.uid, {
377
- roomId: sub.roomId,
378
- live: false,
379
- liveRoomInfo: undefined,
380
- masterInfo: undefined,
381
- watchedNum: "0",
382
- liveStartTime: "",
383
- liveStartTimeInit: false,
384
- push: 0,
385
- });
386
- }
387
- }
388
- }
389
- // biome-ignore lint/suspicious/noExplicitAny: <explanation>
390
- getBot(pf, selfId) {
391
- // 判断是否存在selfId
392
- if (!selfId || selfId === "") {
393
- // 不存在则默认第一个bot
394
- return this.ctx.bots.find((bot) => bot.platform === pf);
395
- }
396
- // 存在则返回对应bot
397
- return this.ctx.bots.find((bot) => bot.platform === pf && bot.selfId === selfId);
398
- }
399
- async sendPrivateMsg(content) {
400
- if (this.config.master.enable) {
401
- if (this.config.master.masterAccountGuildId) {
402
- // 向机器人主人发送消息
403
- await this.privateBot.sendPrivateMessage(this.config.master.masterAccount, content, this.config.master.masterAccountGuildId);
404
- }
405
- else {
406
- // 向机器人主人发送消息
407
- await this.privateBot.sendPrivateMessage(this.config.master.masterAccount, content);
408
- }
409
- }
410
- }
411
- async sendPrivateMsgAndRebootService() {
412
- // 判断重启次数是否超过三次
413
- if (this.rebootCount >= 3) {
414
- // logger
415
- this.logger.error("已重启插件三次,请检查机器人状态后使用指令 sys start 启动插件");
416
- // 重启失败,发送消息
417
- await this.sendPrivateMsg("已重启插件三次,请检查机器人状态后使用指令 sys start 启动插件");
418
- // 关闭插件
419
- await this.ctx.sm.disposePlugin();
420
- // 结束
421
- return;
422
- }
423
- // 重启次数+1
424
- this.rebootCount++;
425
- // logger
426
- this.logger.info("插件出现未知错误,正在重启插件");
427
- // 重启插件
428
- const flag = await this.ctx.sm.restartPlugin();
429
- // 判断是否重启成功
430
- if (flag) {
431
- this.logger.info("重启插件成功");
432
- }
433
- else {
434
- // logger
435
- this.logger.error("重启插件失败,请检查机器人状态后使用指令 sys start 启动插件");
436
- // 重启失败,发送消息
437
- await this.sendPrivateMsg("重启插件失败,请检查机器人状态后使用指令 sys start 启动插件");
438
- // 关闭插件
439
- await this.ctx.sm.disposePlugin();
440
- }
441
- }
442
- async sendPrivateMsgAndStopService() {
443
- // 发送消息
444
- await this.sendPrivateMsg("插件发生未知错误,请检查机器人状态后使用指令 sys start 启动插件");
445
- // logger
446
- this.logger.error("插件发生未知错误,请检查机器人状态后使用指令 sys start 启动插件");
447
- // 关闭插件
448
- await this.ctx.sm.disposePlugin();
449
- // 结束
450
- return;
451
- }
452
- async sendMessageWithRetry(bot, channelId,
453
- // biome-ignore lint/suspicious/noExplicitAny: <explanation>
454
- content) {
455
- (0, utils_1.withRetry)(async () => await bot.sendMessage(channelId, content), 1).catch(async (e) => {
456
- if (e.message === "this._request is not a function") {
457
- // 2S之后重新发送消息
458
- this.ctx.setTimeout(async () => {
459
- await this.sendMessageWithRetry(bot, channelId, content);
460
- }, 2000);
461
- // 返回
462
- return;
463
- }
464
- // 打印错误信息
465
- this.logger.error(`发送群组ID:${channelId}消息失败!原因: ${e.message}`);
466
- await this.sendPrivateMsg(`发送群组ID:${channelId}消息失败,请查看日志`);
467
- });
468
- }
469
- getGroupsThatMeetCriteria(targets, type) {
470
- // 定义数组
471
- const pushArr = [];
472
- // 判断类型
473
- if (type === type_1.PushType.Live || type === type_1.PushType.StartBroadcasting) {
474
- for (const target of targets) {
475
- for (const channel of target.channelArr) {
476
- if (channel.live) {
477
- pushArr.push(`${target.platform}:${channel.channelId}`);
478
- }
479
- }
480
- }
481
- return pushArr;
482
- }
483
- if (type === type_1.PushType.Dynamic) {
484
- for (const target of targets) {
485
- for (const channel of target.channelArr) {
486
- if (channel.dynamic) {
487
- pushArr.push(`${target.platform}:${channel.channelId}`);
488
- }
489
- }
490
- }
491
- return pushArr;
492
- }
493
- if (type === type_1.PushType.LiveGuardBuy) {
494
- for (const target of targets) {
495
- for (const channel of target.channelArr) {
496
- if (channel.liveGuardBuy) {
497
- pushArr.push(`${target.platform}:${channel.channelId}`);
498
- }
499
- }
500
- }
501
- return pushArr;
502
- }
503
- }
504
- // biome-ignore lint/suspicious/noExplicitAny: <explanation>
505
- async broadcastToTargets(targets, content, type) {
506
- // 不止一个目标平台或一个目标频道
507
- if (targets.length !== 1 || targets[0].channelArr.length !== 1) {
508
- // 直接使用broadcast
509
- const pushArr = this.getGroupsThatMeetCriteria(targets, type);
510
- // logger
511
- this.logger.info(`推送消息到 ${pushArr.length} 个目标频道,目标频道为:${pushArr.join(", ")}`);
512
- // 推送消息
513
- await (0, utils_1.withRetry)(async () => {
514
- await this.ctx.broadcast(pushArr, content);
515
- }, 1);
516
- // 结束
517
- return;
518
- }
519
- // 获取目标
520
- const targetChannel = targets[0].channelArr[0];
521
- // 获取机器人实例
522
- const bot = this.getBot(targets[0].platform, targetChannel.bot);
523
- // 判断bot是否存在
524
- if (!bot || !bot.isActive) {
525
- // 发送私聊消息
526
- this.sendPrivateMsg("未找到对应bot实例,本次消息推送取消!");
527
- // logger
528
- this.logger.warn("未找到对应bot实例,本次消息推送取消!");
529
- // 直接返回
530
- return;
531
- }
532
- // 模式匹配
533
- const pushTypePatternMatching = {
534
- [type_1.PushType.Live]: async () => {
535
- if (targetChannel.live) {
536
- // 直接推送
537
- await this.sendMessageWithRetry(bot, targetChannel.channelId, content);
538
- }
539
- },
540
- [type_1.PushType.Dynamic]: async () => {
541
- if (targetChannel.dynamic) {
542
- await this.sendMessageWithRetry(bot, targetChannel.channelId, content);
543
- }
544
- },
545
- [type_1.PushType.StartBroadcasting]: async () => {
546
- // 判断是否需要推送直播消息
547
- if (targetChannel.live) {
548
- await this.sendMessageWithRetry(bot, targetChannel.channelId, content);
549
- }
550
- // 判断是否需要艾特全体成员
551
- if (targetChannel.atAll) {
552
- await this.sendMessageWithRetry(bot, targetChannel.channelId, (0, jsx_runtime_1.jsx)("at", { type: "all" }));
553
- }
554
- },
555
- [type_1.PushType.LiveGuardBuy]: async () => {
556
- // 判断是否需要推送直播消息
557
- if (targetChannel.liveGuardBuy) {
558
- await this.sendMessageWithRetry(bot, targetChannel.channelId, content);
559
- }
560
- },
561
- };
562
- // 推送
563
- await pushTypePatternMatching[type]();
564
- }
565
- dynamicDetect() {
566
- // 定义handler
567
- const handler = async () => {
568
- // 定义本次请求推送的动态
569
- const currentPushDyn = {};
570
- // 使用withRetry函数进行重试
571
- const content = await (0, utils_1.withRetry)(async () => {
572
- // 获取动态内容
573
- return (await this.ctx.ba.getAllDynamic());
574
- }, 1).catch((e) => {
575
- // logger
576
- this.logger.error(`dynamicDetect getAllDynamic() 发生了错误,错误为:${e.message}`);
577
- });
578
- // content不存在则直接返回
579
- if (!content)
580
- return;
581
- // 判断获取动态内容是否成功
582
- if (content.code !== 0) {
583
- switch (content.code) {
584
- case -101: {
585
- // 账号未登录
586
- // 输出日志
587
- this.logger.error("账号未登录,插件已停止工作,请登录后,输入指令 sys start 启动插件");
588
- // 发送私聊消息
589
- await this.sendPrivateMsg("账号未登录,插件已停止工作,请登录后,输入指令 sys start 启动插件");
590
- // 停止服务
591
- await this.ctx.sm.disposePlugin();
592
- // 结束循环
593
- break;
594
- }
595
- case -352: {
596
- // 风控
597
- // 输出日志
598
- this.logger.error("账号被风控,插件已停止工作,请确认风控解除后,输入指令 sys start 启动插件");
599
- // 发送私聊消息
600
- await this.sendPrivateMsg("账号被风控,插件已停止工作,请确认风控解除后,输入指令 sys start 启动插件");
601
- // 停止服务
602
- await this.ctx.sm.disposePlugin();
603
- // 结束循环
604
- break;
605
- }
606
- case 4101128:
607
- case 4101129: {
608
- // 获取动态信息错误
609
- // 输出日志
610
- this.logger.error(`获取动态信息错误,错误码为:${content.code},错误为:${content.message}`);
611
- // 发送私聊消息
612
- await this.sendPrivateMsg(`获取动态信息错误,错误码为:${content.code},错误为:${content.message}`); // 未知错误
613
- // 结束循环
614
- break;
615
- }
616
- default: {
617
- // 未知错误
618
- // 发送私聊消息
619
- await this.sendPrivateMsg(`获取动态信息错误,错误码为:${content.code},错误为:${content.message}`); // 未知错误
620
- // 结束循环
621
- break;
622
- }
623
- }
624
- }
625
- const items = content.data.items;
626
- // 检查更新的动态
627
- for (const item of items) {
628
- // 没有动态内容则直接跳过
629
- if (!item)
630
- continue;
631
- // 获取动态发布时间
632
- const postTime = item.modules.module_author.pub_ts;
633
- // 从动态数据中取出UP主名称、UID
634
- const uid = item.modules.module_author.mid.toString();
635
- const name = item.modules.module_author.name;
636
- // 判断是否存在时间线
637
- if (this.dynamicTimelineManager.has(uid)) {
638
- // 寻找关注的UP主
639
- const timeline = this.dynamicTimelineManager.get(uid);
640
- // 判断动态发布时间是否大于时间线
641
- if (timeline < postTime) {
642
- // 获取订阅对象
643
- const sub = this.subManager.find((sub) => sub.uid === uid);
644
- // 推送该条动态
645
- const buffer = await (0, utils_1.withRetry)(async () => {
646
- // 渲染图片
647
- return await this.ctx.gi.generateDynamicImg(item, sub.card.enable ? sub.card : undefined);
648
- }, 1).catch(async (e) => {
649
- // 直播开播动态,不做处理
650
- if (e.message === "直播开播动态,不做处理")
651
- return;
652
- if (e.message === "出现关键词,屏蔽该动态") {
653
- // 如果需要发送才发送
654
- if (this.config.filter.notify) {
655
- await this.broadcastToTargets(sub.target, `${name}发布了一条含有屏蔽关键字的动态`, type_1.PushType.Dynamic);
656
- }
657
- return;
658
- }
659
- if (e.message === "已屏蔽转发动态") {
660
- if (this.config.filter.notify) {
661
- await this.broadcastToTargets(sub.target, `${name}转发了一条动态,已屏蔽`, type_1.PushType.Dynamic);
662
- }
663
- return;
664
- }
665
- if (e.message === "已屏蔽专栏动态") {
666
- if (this.config.filter.notify) {
667
- await this.broadcastToTargets(sub.target, `${name}投稿了一条专栏,已屏蔽`, type_1.PushType.Dynamic);
668
- }
669
- return;
670
- }
671
- // 未知错误
672
- this.logger.error(`dynamicDetect generateDynamicImg() 推送卡片发送失败,原因:${e.message}`);
673
- // 发送私聊消息并重启服务
674
- await this.sendPrivateMsgAndStopService();
675
- });
676
- // 判断是否执行成功,未执行成功直接返回
677
- if (!buffer)
678
- continue;
679
- // 定义动态链接
680
- let dUrl = "";
681
- // 判断是否需要发送URL
682
- if (this.config.dynamicUrl) {
683
- if (item.type === "DYNAMIC_TYPE_AV") {
684
- // 判断是否开启url to bv
685
- if (this.config.dynamicVideoUrlToBV) {
686
- // 截取bv号
687
- const bv = item.modules.module_dynamic.major.archive.jump_url.match(/BV[0-9A-Za-z]+/);
688
- // 获取bv号
689
- dUrl = bv ? bv[0] : "";
690
- }
691
- else {
692
- // 生成视频链接
693
- dUrl = `${name}发布了新视频:https:${item.modules.module_dynamic.major.archive.jump_url}`;
694
- }
695
- }
696
- else {
697
- // 生成动态链接
698
- dUrl = `${name}发布了一条动态:https://t.bilibili.com/${item.id_str}`;
699
- }
700
- }
701
- // logger
702
- this.logger.info("推送动态中...");
703
- // 发送推送卡片
704
- await this.broadcastToTargets(sub.target, (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [koishi_1.h.image(buffer, "image/jpeg"), dUrl] }), type_1.PushType.Dynamic);
705
- // 判断是否需要发送动态中的图片
706
- if (this.config.pushImgsInDynamic) {
707
- // 判断是否为图文动态
708
- if (item.type === "DYNAMIC_TYPE_DRAW") {
709
- // 获取pics
710
- const pics = item.modules?.module_dynamic?.major?.opus?.pics;
711
- // 判断pics是否存在
712
- if (pics) {
713
- // 组合消息
714
- const picsMsg = ((0, jsx_runtime_1.jsx)("message", { forward: true, children: pics.map((pic) => ((0, jsx_runtime_1.jsx)("img", { src: pic.url, alt: "\u52A8\u6001\u56FE\u7247" }, pic.url))) }));
715
- // 发送消息
716
- await this.broadcastToTargets(sub.target, picsMsg, type_1.PushType.Dynamic);
717
- }
718
- }
719
- }
720
- // 如果当前订阅对象已存在更早推送,则无需再更新时间线
721
- if (!currentPushDyn[uid]) {
722
- // 将当前动态存入currentPushDyn
723
- currentPushDyn[uid] = item;
724
- }
725
- // logger
726
- this.logger.info("动态推送完毕!");
727
- }
728
- }
729
- }
730
- // 遍历currentPushDyn
731
- for (const uid in currentPushDyn) {
732
- // 获取动态发布时间
733
- const postTime = currentPushDyn[uid].modules.module_author.pub_ts;
734
- // 更新当前时间线
735
- this.dynamicTimelineManager.set(uid, postTime);
736
- }
737
- };
738
- // 返回一个闭包函数
739
- return (0, utils_1.withLock)(handler);
740
- }
741
- debug_dynamicDetect() {
742
- // 定义handler
743
- const handler = async () => {
744
- // 定义本次请求推送的动态
745
- const currentPushDyn = {};
746
- // logger
747
- this.logger.info("开始获取动态信息...");
748
- // 使用withRetry函数进行重试
749
- const content = await (0, utils_1.withRetry)(async () => {
750
- // 获取动态内容
751
- return (await this.ctx.ba.getAllDynamic());
752
- }, 1).catch((e) => {
753
- // logger
754
- this.logger.error(`dynamicDetect getAllDynamic() 发生了错误,错误为:${e.message}`);
755
- });
756
- // content不存在则直接返回
757
- if (!content)
758
- return;
759
- // 判断获取动态内容是否成功
760
- if (content.code !== 0) {
761
- switch (content.code) {
762
- case -101: {
763
- // 账号未登录
764
- // 输出日志
765
- this.logger.error("账号未登录,插件已停止工作,请登录后,输入指令 sys start 启动插件");
766
- // 发送私聊消息
767
- await this.sendPrivateMsg("账号未登录,插件已停止工作,请登录后,输入指令 sys start 启动插件");
768
- // 停止服务
769
- await this.ctx.sm.disposePlugin();
770
- // 结束循环
771
- break;
772
- }
773
- case -352: {
774
- // 风控
775
- // 输出日志
776
- this.logger.error("账号被风控,插件已停止工作,请确认风控解除后,输入指令 sys start 启动插件");
777
- // 发送私聊消息
778
- await this.sendPrivateMsg("账号被风控,插件已停止工作,请确认风控解除后,输入指令 sys start 启动插件");
779
- // 停止服务
780
- await this.ctx.sm.disposePlugin();
781
- // 结束循环
782
- break;
783
- }
784
- case 4101128:
785
- case 4101129: {
786
- // 获取动态信息错误
787
- // 输出日志
788
- this.logger.error(`获取动态信息错误,错误码为:${content.code},错误为:${content.message}`);
789
- // 发送私聊消息
790
- await this.sendPrivateMsg(`获取动态信息错误,错误码为:${content.code},错误为:${content.message}`); // 未知错误
791
- // 结束循环
792
- break;
793
- }
794
- default: {
795
- // 未知错误
796
- // 发送私聊消息
797
- await this.sendPrivateMsg(`获取动态信息错误,错误码为:${content.code},错误为:${content.message}`); // 未知错误
798
- // 结束循环
799
- break;
800
- }
801
- }
802
- }
803
- // logger
804
- this.logger.info("获取动态信息成功!开始处理动态信息...");
805
- // 获取动态内容
806
- const items = content.data.items;
807
- // 检查更新的动态
808
- for (const item of items) {
809
- // 没有动态内容则直接跳过
810
- if (!item)
811
- continue;
812
- // 获取动态发布时间
813
- const postTime = item.modules.module_author.pub_ts;
814
- // 从动态数据中取出UP主名称、UID
815
- const uid = item.modules.module_author.mid.toString();
816
- const name = item.modules.module_author.name;
817
- // logger
818
- this.logger.info(`获取到动态信息,UP主:${name},UID:${uid},动态发布时间:${luxon_1.DateTime.fromSeconds(postTime).toFormat("yyyy-MM-dd HH:mm:ss")}`);
819
- // 判断是否存在时间线
820
- if (this.dynamicTimelineManager.has(uid)) {
821
- // logger
822
- this.logger.info("订阅该UP主,判断动态时间线...");
823
- // 寻找关注的UP主
824
- const timeline = this.dynamicTimelineManager.get(uid);
825
- // logger
826
- this.logger.info(`上次推送时间线:${luxon_1.DateTime.fromSeconds(timeline).toFormat("yyyy-MM-dd HH:mm:ss")}`);
827
- // 判断动态发布时间是否大于时间线
828
- if (timeline < postTime) {
829
- // logger
830
- this.logger.info("需要推送该条动态,开始推送...");
831
- // 获取订阅对象
832
- const sub = this.subManager.find((sub) => sub.uid === uid);
833
- // logger
834
- this.logger.info("开始渲染推送卡片...");
835
- // 推送该条动态
836
- const buffer = await (0, utils_1.withRetry)(async () => {
837
- // 渲染图片
838
- return await this.ctx.gi.generateDynamicImg(item, sub.card.enable ? sub.card : undefined);
839
- }, 1).catch(async (e) => {
840
- // 直播开播动态,不做处理
841
- if (e.message === "直播开播动态,不做处理")
842
- return;
843
- if (e.message === "出现关键词,屏蔽该动态") {
844
- // 如果需要发送才发送
845
- if (this.config.filter.notify) {
846
- await this.broadcastToTargets(sub.target, `${name}发布了一条含有屏蔽关键字的动态`, type_1.PushType.Dynamic);
847
- }
848
- return;
849
- }
850
- if (e.message === "已屏蔽转发动态") {
851
- if (this.config.filter.notify) {
852
- await this.broadcastToTargets(sub.target, `${name}转发了一条动态,已屏蔽`, type_1.PushType.Dynamic);
853
- }
854
- return;
855
- }
856
- if (e.message === "已屏蔽专栏动态") {
857
- if (this.config.filter.notify) {
858
- await this.broadcastToTargets(sub.target, `${name}投稿了一条专栏,已屏蔽`, type_1.PushType.Dynamic);
859
- }
860
- return;
861
- }
862
- // 未知错误
863
- this.logger.error(`dynamicDetect generateDynamicImg() 推送卡片发送失败,原因:${e.message}`);
864
- // 发送私聊消息并重启服务
865
- await this.sendPrivateMsgAndStopService();
866
- });
867
- // 判断是否执行成功,未执行成功直接返回
868
- if (!buffer)
869
- continue;
870
- // logger
871
- this.logger.info("渲染推送卡片成功!");
872
- // 定义动态链接
873
- let dUrl = "";
874
- // 判断是否需要发送URL
875
- if (this.config.dynamicUrl) {
876
- // logger
877
- this.logger.info("需要发送动态链接,开始生成链接...");
878
- // 判断动态类型
879
- if (item.type === "DYNAMIC_TYPE_AV") {
880
- // 判断是否开启url to bv
881
- if (this.config.dynamicVideoUrlToBV) {
882
- // 截取bv号
883
- const bv = item.modules.module_dynamic.major.archive.jump_url.match(/BV[0-9A-Za-z]+/);
884
- // 获取bv号
885
- dUrl = bv ? bv[0] : "";
886
- }
887
- else {
888
- // 生成视频链接
889
- dUrl = `${name}发布了新视频:https:${item.modules.module_dynamic.major.archive.jump_url}`;
890
- }
891
- }
892
- else {
893
- // 生成动态链接
894
- dUrl = `${name}发布了一条动态:https://t.bilibili.com/${item.id_str}`;
895
- }
896
- // logger
897
- this.logger.info("动态链接生成成功!");
898
- }
899
- // logger
900
- this.logger.info("推送动态中...");
901
- // 发送推送卡片
902
- await this.broadcastToTargets(sub.target, (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [koishi_1.h.image(buffer, "image/jpeg"), dUrl] }), type_1.PushType.Dynamic);
903
- // 判断是否需要发送动态中的图片
904
- if (this.config.pushImgsInDynamic) {
905
- // logger
906
- this.logger.info("需要发送动态中的图片,开始发送...");
907
- // 判断是否为图文动态
908
- if (item.type === "DYNAMIC_TYPE_DRAW") {
909
- // 获取pics
910
- const pics = item.modules?.module_dynamic?.major?.opus?.pics;
911
- // 判断pics是否存在
912
- if (pics) {
913
- // 组合消息
914
- const picsMsg = ((0, jsx_runtime_1.jsx)("message", { forward: true, children: pics.map((pic) => ((0, jsx_runtime_1.jsx)("img", { src: pic.url, alt: "\u52A8\u6001\u56FE\u7247" }, pic.url))) }));
915
- // 发送消息
916
- await this.broadcastToTargets(sub.target, picsMsg, type_1.PushType.Dynamic);
917
- }
918
- }
919
- // logger
920
- this.logger.info("动态中的图片发送完毕!");
921
- }
922
- // 如果当前订阅对象已存在更早推送,则无需再更新时间线
923
- if (!currentPushDyn[uid]) {
924
- // 将当前动态存入currentPushDyn
925
- currentPushDyn[uid] = item;
926
- }
927
- // logger
928
- this.logger.info("动态推送完毕!");
929
- }
930
- }
931
- }
932
- // logger
933
- this.logger.info("动态信息处理完毕!");
934
- // 遍历currentPushDyn
935
- for (const uid in currentPushDyn) {
936
- // 获取动态发布时间
937
- const postTime = currentPushDyn[uid].modules.module_author.pub_ts;
938
- // 更新当前时间线
939
- this.dynamicTimelineManager.set(uid, postTime);
940
- // logger
941
- this.logger.info(`更新时间线成功,UP主:${uid},时间线:${luxon_1.DateTime.fromSeconds(postTime).toFormat("yyyy-MM-dd HH:mm:ss")}`);
942
- }
943
- // logger
944
- this.logger.info(`本次推送动态数量:${Object.keys(currentPushDyn).length}`);
945
- };
946
- // 返回一个闭包函数
947
- return (0, utils_1.withLock)(handler);
948
- }
949
- async useMasterInfo(uid, masterInfo, liveType) {
950
- // 获取主播信息
951
- const { data } = await this.ctx.ba.getMasterInfo(uid);
952
- // 定义粉丝数变量
953
- let liveOpenFollowerNum;
954
- let liveEndFollowerNum;
955
- let liveFollowerChange;
956
- // 判断直播状态
957
- if (liveType === type_1.LiveType.StartBroadcasting ||
958
- liveType === type_1.LiveType.FirstLiveBroadcast) {
959
- // 第一次启动或刚开播
960
- // 将当前粉丝数赋值给liveOpenFollowerNum、liveEndFollowerNum
961
- liveOpenFollowerNum = data.follower_num;
962
- liveEndFollowerNum = data.follower_num;
963
- // 将粉丝数变化赋值为0
964
- liveFollowerChange = 0;
965
- }
966
- if (liveType === type_1.LiveType.StopBroadcast ||
967
- liveType === type_1.LiveType.LiveBroadcast) {
968
- // 将上一次的liveOpenFollowerNum赋值给本次的liveOpenFollowerNum
969
- liveOpenFollowerNum = masterInfo.liveOpenFollowerNum;
970
- // 将当前粉丝数赋值给liveEndFollowerNum
971
- liveEndFollowerNum = data.follower_num;
972
- // 计算粉丝数变化量
973
- liveFollowerChange = liveEndFollowerNum - masterInfo.liveOpenFollowerNum;
974
- }
975
- // 返回值
976
- return {
977
- username: data.info.uname,
978
- userface: data.info.face,
979
- roomId: data.room_id,
980
- liveOpenFollowerNum,
981
- liveEndFollowerNum,
982
- liveFollowerChange,
983
- };
984
- }
985
- async useLiveRoomInfo(roomId) {
986
- // 发送请求获取直播间信息
987
- const data = await (0, utils_1.withRetry)(async () => await this.ctx.ba.getLiveRoomInfo(roomId))
988
- .then((content) => content.data)
989
- .catch((e) => {
990
- this.logger.error(`liveDetect getLiveRoomInfo 发生了错误,错误为:${e.message}`);
991
- // 返回错误
992
- return false;
993
- });
994
- // 发送私聊消息并重启服务
995
- if (!data)
996
- return await this.sendPrivateMsgAndStopService();
997
- // 返回
998
- return data;
999
- }
1000
- async sendLiveNotifyCard(liveType, followerDisplay, liveInfo, target, liveNotifyMsg) {
1001
- // 生成图片
1002
- const buffer = await (0, utils_1.withRetry)(async () => {
1003
- // 获取直播通知卡片
1004
- return await this.ctx.gi.generateLiveImg(liveInfo.liveRoomInfo, liveInfo.masterInfo.username, liveInfo.masterInfo.userface, followerDisplay, liveType, liveInfo.cardStyle.enable ? liveInfo.cardStyle : undefined);
1005
- }, 1).catch((e) => {
1006
- this.logger.error(`liveDetect generateLiveImg() 推送卡片生成失败,原因:${e.message}`);
1007
- });
1008
- // 发送私聊消息并重启服务
1009
- if (!buffer)
1010
- return await this.sendPrivateMsgAndStopService();
1011
- // 推送直播信息
1012
- const msg = ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [koishi_1.h.image(buffer, "image/jpeg"), liveNotifyMsg || ""] }));
1013
- // 只有在开播时才艾特全体成员
1014
- return await this.broadcastToTargets(target, msg, liveType === type_1.LiveType.StartBroadcasting
1015
- ? type_1.PushType.StartBroadcasting
1016
- : type_1.PushType.Live);
1017
- }
1018
- async liveDetectWithListener(roomId, target, cardStyle) {
1019
- // 定义开播时间
1020
- let liveTime;
1021
- // 定义定时推送定时器
1022
- let pushAtTimeTimer;
1023
- // 定义弹幕存放数组
1024
- const currentLiveDanmakuArr = [];
1025
- // 定义开播状态
1026
- let liveStatus = false;
1027
- // 定义channelIdArr总长度
1028
- let channelArrLen = 0;
1029
- // 定义数据
1030
- // biome-ignore lint/suspicious/noExplicitAny: <explanation>
1031
- let liveRoomInfo;
1032
- let masterInfo;
1033
- let watchedNum;
1034
- // 定义发送直播通知卡片方法
1035
- // 找到频道/群组对应的
1036
- const liveGuardBuyPushTargetArr = target.map((channel) => {
1037
- // 获取符合条件的target
1038
- const liveGuardBuyArr = channel.channelArr.filter((channelId) => channelId.liveGuardBuy);
1039
- // 将当前liveDanmakuArr的长度+到channelIdArrLen中
1040
- channelArrLen += liveGuardBuyArr.length;
1041
- // 返回符合的target
1042
- return {
1043
- channelArr: liveGuardBuyArr,
1044
- platform: channel.platform,
1045
- };
1046
- });
1047
- // 定义定时推送函数
1048
- const pushAtTimeFunc = async () => {
1049
- // 判断是否信息是否获取成功
1050
- if (!(await useMasterAndLiveRoomInfo(type_1.LiveType.LiveBroadcast))) {
1051
- // 未获取成功,直接返回
1052
- await this.sendPrivateMsg("获取直播间信息失败,推送直播卡片失败!");
1053
- // 停止服务
1054
- return await this.sendPrivateMsgAndStopService();
1055
- }
1056
- // 判断是否已经下播
1057
- if (liveRoomInfo.live_status === 0 || liveRoomInfo.live_status === 2) {
1058
- // 设置开播状态为false
1059
- liveStatus = false;
1060
- // 清除定时器
1061
- pushAtTimeTimer?.();
1062
- // 发送私聊消息
1063
- await this.sendPrivateMsg("直播间已下播!与直播间的连接可能已断开,请使用指令 sys restart 重启插件");
1064
- // 返回
1065
- return;
1066
- }
1067
- // 设置开播时间
1068
- liveTime = liveRoomInfo.live_time;
1069
- // 获取watched
1070
- const watched = watchedNum || "暂未获取到";
1071
- // 设置直播中消息
1072
- const liveMsg = this.config.customLive
1073
- ? this.config.customLive
1074
- .replace("-name", masterInfo.username)
1075
- .replace("-time", await this.ctx.gi.getTimeDifference(liveTime))
1076
- .replace("-watched", watched)
1077
- .replace("\\n", "\n")
1078
- .replace("-link", `https://live.bilibili.com/${liveRoomInfo.short_id === 0 ? liveRoomInfo.room_id : liveRoomInfo.short_id}`)
1079
- : null;
1080
- // 发送直播通知卡片
1081
- await this.sendLiveNotifyCard(type_1.LiveType.LiveBroadcast, watched, {
1082
- liveRoomInfo,
1083
- masterInfo,
1084
- cardStyle,
1085
- }, target, liveMsg);
1086
- };
1087
- // 定义直播间信息获取函数
1088
- const useMasterAndLiveRoomInfo = async (liveType) => {
1089
- // 定义函数是否执行成功flag
1090
- let flag = true;
1091
- // 获取直播间信息
1092
- liveRoomInfo = await this.useLiveRoomInfo(roomId).catch(() => {
1093
- // 设置flag为false
1094
- flag = false;
1095
- // 返回空
1096
- return null;
1097
- });
1098
- // 判断是否成功获取信息
1099
- if (!flag || !liveRoomInfo || !liveRoomInfo.uid) {
1100
- // 上一步未成功
1101
- flag = false;
1102
- // 返回flag
1103
- return flag;
1104
- }
1105
- // 获取主播信息(需要满足flag为true,liveRoomInfo.uid有值)
1106
- masterInfo = await this.useMasterInfo(liveRoomInfo.uid, masterInfo, liveType).catch(() => {
1107
- // 设置flag为false
1108
- flag = false;
1109
- // 返回空
1110
- return null;
1111
- });
1112
- // 返回信息
1113
- return flag;
1114
- };
1115
- // 构建消息处理函数
1116
- const handler = {
1117
- onError: async () => {
1118
- // 更直播状态
1119
- liveStatus = false;
1120
- // 关闭定时推送
1121
- pushAtTimeTimer?.();
1122
- // 停止服务
1123
- this.ctx.bl.closeListener(roomId);
1124
- // 发送消息
1125
- await this.sendPrivateMsg(`[${roomId}]直播间连接发生错误!`);
1126
- this.logger.error(`[${roomId}]直播间连接发生错误!`);
1127
- },
1128
- onIncomeDanmu: ({ body }) => {
1129
- // 保存消息到数组
1130
- currentLiveDanmakuArr.push(body.content);
1131
- },
1132
- onIncomeSuperChat: ({ body }) => {
1133
- // 保存消息到数组
1134
- currentLiveDanmakuArr.push(body.content);
1135
- },
1136
- onWatchedChange: ({ body }) => {
1137
- // 保存观看人数到变量
1138
- watchedNum = body.text_small;
1139
- },
1140
- onGuardBuy: ({ body }) => {
1141
- // 定义消息
1142
- const content = `[${masterInfo.username}的直播间]「${body.user.uname}」加入了大航海(${body.gift_name})`;
1143
- // 直接发送消息
1144
- channelArrLen > 0 &&
1145
- this.broadcastToTargets(liveGuardBuyPushTargetArr, content, type_1.PushType.LiveGuardBuy);
1146
- },
1147
- onLiveStart: async () => {
1148
- // 判断是否已经开播
1149
- if (liveStatus)
1150
- return;
1151
- // 设置开播状态为true
1152
- liveStatus = true;
1153
- // 判断是否信息是否获取成功
1154
- if (!(await useMasterAndLiveRoomInfo(type_1.LiveType.StartBroadcasting))) {
1155
- // 设置开播状态为false
1156
- liveStatus = false;
1157
- // 未获取成功,直接返回
1158
- await this.sendPrivateMsg("获取直播间信息失败,推送直播开播卡片失败!");
1159
- // 停止服务
1160
- return await this.sendPrivateMsgAndStopService();
1161
- }
1162
- // 设置开播时间
1163
- liveTime = liveRoomInfo.live_time;
1164
- // 获取当前粉丝数
1165
- const follower = masterInfo.liveOpenFollowerNum >= 10_000
1166
- ? `${(masterInfo.liveOpenFollowerNum / 10000).toFixed(1)}万`
1167
- : masterInfo.liveOpenFollowerNum.toString();
1168
- // 定义开播通知语
1169
- const liveStartMsg = this.config.customLiveStart
1170
- ? this.config.customLiveStart
1171
- .replace("-name", masterInfo.username)
1172
- .replace("-time", await this.ctx.gi.getTimeDifference(liveTime))
1173
- .replace("-follower", follower)
1174
- .replace("\\n", "\n")
1175
- .replace("-link", `https://live.bilibili.com/${liveRoomInfo.short_id === 0 ? liveRoomInfo.room_id : liveRoomInfo.short_id}`)
1176
- : null;
1177
- // 推送开播通知
1178
- await this.sendLiveNotifyCard(type_1.LiveType.StartBroadcasting, follower, {
1179
- liveRoomInfo,
1180
- masterInfo,
1181
- cardStyle,
1182
- }, target, liveStartMsg);
1183
- // 判断定时器是否已开启
1184
- if (!pushAtTimeTimer) {
1185
- // 开始直播,开启定时器
1186
- pushAtTimeTimer = this.ctx.setInterval(pushAtTimeFunc, this.config.pushTime * 1000 * 60 * 60);
1187
- }
1188
- },
1189
- onLiveEnd: async () => {
1190
- // 将直播状态设置为false
1191
- liveStatus = false;
1192
- // 判断是否信息是否获取成功
1193
- if (!(await useMasterAndLiveRoomInfo(type_1.LiveType.StopBroadcast))) {
1194
- // 未获取成功,直接返回
1195
- await this.sendPrivateMsg("获取直播间信息失败,推送直播下播卡片失败!");
1196
- // 停止服务
1197
- return await this.sendPrivateMsgAndStopService();
1198
- }
1199
- // 更改直播时长
1200
- liveRoomInfo.live_time = liveTime;
1201
- // 获取粉丝数变化
1202
- const followerChange = (() => {
1203
- // 获取直播关注变化值
1204
- const liveFollowerChangeNum = masterInfo.liveFollowerChange;
1205
- // 判断是否大于0
1206
- if (liveFollowerChangeNum > 0) {
1207
- // 大于0则加+
1208
- return liveFollowerChangeNum >= 10_000
1209
- ? `+${liveFollowerChangeNum.toFixed(1)}万`
1210
- : `+${liveFollowerChangeNum}`;
1211
- }
1212
- // 小于0
1213
- return liveFollowerChangeNum <= -10_000
1214
- ? `${liveFollowerChangeNum.toFixed(1)}万`
1215
- : liveFollowerChangeNum.toString();
1216
- })();
1217
- // 定义下播播通知语
1218
- const liveEndMsg = this.config.customLiveEnd
1219
- ? this.config.customLiveEnd
1220
- .replace("-name", masterInfo.username)
1221
- .replace("-time", await this.ctx.gi.getTimeDifference(liveTime))
1222
- .replace("-follower_change", followerChange)
1223
- .replace("\\n", "\n")
1224
- : null;
1225
- // 推送通知卡片
1226
- await this.sendLiveNotifyCard(type_1.LiveType.StopBroadcast, followerChange, {
1227
- liveRoomInfo,
1228
- masterInfo,
1229
- cardStyle,
1230
- }, target, liveEndMsg);
1231
- // 关闭定时推送定时器
1232
- pushAtTimeTimer();
1233
- // 将推送定时器变量置空
1234
- pushAtTimeTimer = null;
1235
- },
1236
- };
1237
- // 启动直播间弹幕监测
1238
- await this.ctx.bl.startLiveRoomListener(roomId, handler);
1239
- // 第一次启动获取信息并判信息是否获取成功
1240
- if (!(await useMasterAndLiveRoomInfo(type_1.LiveType.FirstLiveBroadcast))) {
1241
- // 未获取成功,直接返回
1242
- return this.sendPrivateMsg("获取直播间信息失败,启动直播间弹幕检测失败!");
1243
- }
1244
- // 判断直播状态
1245
- if (liveRoomInfo.live_status === 1) {
1246
- // 设置开播时间
1247
- liveTime = liveRoomInfo.live_time;
1248
- // 获取当前累计观看人数
1249
- const watched = watchedNum || "暂未获取到";
1250
- // 定义直播中通知消息
1251
- const liveMsg = this.config.customLive
1252
- ? this.config.customLive
1253
- .replace("-name", masterInfo.username)
1254
- .replace("-time", await this.ctx.gi.getTimeDifference(liveTime))
1255
- .replace("-watched", watched)
1256
- .replace("\\n", "\n")
1257
- .replace("-link", `https://live.bilibili.com/${liveRoomInfo.short_id === 0 ? liveRoomInfo.room_id : liveRoomInfo.short_id}`)
1258
- : null;
1259
- // 发送直播通知卡片
1260
- if (this.config.restartPush) {
1261
- await this.sendLiveNotifyCard(type_1.LiveType.LiveBroadcast, watched, {
1262
- liveRoomInfo,
1263
- masterInfo,
1264
- cardStyle,
1265
- }, target, liveMsg);
1266
- }
1267
- // 正在直播,开启定时器,判断定时器是否已开启
1268
- if (!pushAtTimeTimer) {
1269
- // 开始直播,开启定时器
1270
- pushAtTimeTimer = this.ctx.setInterval(pushAtTimeFunc, this.config.pushTime * 1000 * 60 * 60);
1271
- }
1272
- // 设置直播状态为true
1273
- liveStatus = true;
1274
- }
1275
- }
1276
- async liveDetectWithAPI() {
1277
- // 定义直播间信息获取函数
1278
- const useMasterAndLiveRoomInfo = async (liveType, liveStatus) => {
1279
- // 定义函数是否执行成功flag
1280
- let flag = true;
1281
- // 获取直播间信息
1282
- liveStatus.liveRoomInfo = await this.useLiveRoomInfo(liveStatus.roomId).catch(() => {
1283
- // 设置flag为false
1284
- flag = false;
1285
- // 返回空
1286
- return null;
1287
- });
1288
- // 判断是否成功获取信息
1289
- if (!flag || !liveStatus.liveRoomInfo?.uid) {
1290
- // 上一步未成功
1291
- flag = false;
1292
- // 返回flag
1293
- return flag;
1294
- }
1295
- // 获取主播信息(需要满足flag为true,liveRoomInfo.uid有值)
1296
- liveStatus.masterInfo = await this.useMasterInfo(liveStatus.liveRoomInfo.uid, liveStatus.masterInfo, liveType).catch(() => {
1297
- // 设置flag为false
1298
- flag = false;
1299
- // 返回空
1300
- return null;
1301
- });
1302
- // 返回信息
1303
- return flag;
1304
- };
1305
- const uids = [];
1306
- for (const [uid] of this.liveStatusManager.entries()) {
1307
- uids.push(uid);
1308
- }
1309
- const useLiveInfo = async () => {
1310
- // 发送请求
1311
- const { data } = await (0, utils_1.withRetry)(async () => (await this.ctx.ba.getLiveRoomInfoByUids(uids)), 3).catch(async () => {
1312
- // 返回undefined
1313
- return undefined;
1314
- });
1315
- if (!data) {
1316
- // 停止服务
1317
- await this.sendPrivateMsgAndStopService();
1318
- // 返回
1319
- return;
1320
- }
1321
- return data;
1322
- };
1323
- // 获取信息
1324
- const data = await useLiveInfo();
1325
- // 初始化
1326
- for (const item of Object.values(data)) {
1327
- // 将用户uid转换为string
1328
- const uid = item.uid.toString();
1329
- // 获取用户直播状态
1330
- const liveStatus = this.liveStatusManager.get(uid);
1331
- // 获取用户sub
1332
- const sub = this.subManager.find((sub) => sub.uid === uid);
1333
- // 判断直播状态
1334
- if (item.live_status === 1) {
1335
- // 将直播状态改为true
1336
- liveStatus.live = true;
1337
- // 初始化主播和直播间信息
1338
- await useMasterAndLiveRoomInfo(type_1.LiveType.FirstLiveBroadcast, liveStatus);
1339
- // 判断是否需要设置开播时间
1340
- if (!liveStatus.liveStartTimeInit) {
1341
- // 设置开播时间
1342
- liveStatus.liveStartTime = liveStatus.liveRoomInfo.live_time;
1343
- // 设置开播时间初始化状态
1344
- liveStatus.liveStartTimeInit = true;
1345
- }
1346
- // 设置直播中消息
1347
- const liveMsg = this.config.customLive
1348
- ? this.config.customLive
1349
- .replace("-name", liveStatus.masterInfo.username)
1350
- .replace("-time", await this.ctx.gi.getTimeDifference(liveStatus.liveStartTime))
1351
- .replace("-watched", "API模式无法获取")
1352
- .replace("\\n", "\n")
1353
- .replace("-link", `https://live.bilibili.com/${liveStatus.liveRoomInfo.short_id === 0 ? liveStatus.liveRoomInfo.room_id : liveStatus.liveRoomInfo.short_id}`)
1354
- : null;
1355
- // 发送直播通知卡片
1356
- await this.sendLiveNotifyCard(type_1.LiveType.LiveBroadcast, "API", {
1357
- liveRoomInfo: liveStatus.liveRoomInfo,
1358
- masterInfo: liveStatus.masterInfo,
1359
- cardStyle: sub.card,
1360
- }, sub.target, liveMsg);
1361
- }
1362
- }
1363
- // 定义函数
1364
- const handler = async () => {
1365
- // 发送请求
1366
- const data = await useLiveInfo();
1367
- // 进行处理
1368
- for (const item of Object.values(data)) {
1369
- // 将用户uid转换为string
1370
- const uid = item.uid.toString();
1371
- // 获取用户直播状态
1372
- const liveStatus = this.liveStatusManager.get(uid);
1373
- // 获取sub
1374
- const sub = this.subManager.find((sub) => sub.uid === uid);
1375
- // 如果未找到sub直接返回
1376
- if (!sub)
1377
- return;
1378
- // 判断当前状态和之前状态是否相同
1379
- switch (item.live_status) {
1380
- case 0:
1381
- case 2: {
1382
- // 未开播状态
1383
- if (liveStatus.live === true) {
1384
- // 现在下播了,发送下播通知
1385
- // 判断信息是否获取成功
1386
- if (!(await useMasterAndLiveRoomInfo(type_1.LiveType.StopBroadcast, liveStatus))) {
1387
- // 未获取成功,直接返回
1388
- await this.sendPrivateMsg("获取直播间信息失败,推送直播下播卡片失败!");
1389
- // 停止服务
1390
- return await this.sendPrivateMsgAndStopService();
1391
- }
1392
- // 更改直播时长
1393
- if (liveStatus.liveStartTimeInit) {
1394
- // 设置直播时长
1395
- liveStatus.liveRoomInfo.live_time = liveStatus.liveStartTime;
1396
- // 直播时间初始化改为false
1397
- liveStatus.liveStartTimeInit = false;
1398
- }
1399
- // 获取粉丝数变化
1400
- const followerChange = (() => {
1401
- // 获取直播关注变化值
1402
- const liveFollowerChangeNum = liveStatus.masterInfo.liveFollowerChange;
1403
- // 判断是否大于0
1404
- if (liveFollowerChangeNum > 0) {
1405
- // 大于0则加+
1406
- return liveFollowerChangeNum >= 10_000
1407
- ? `+${liveFollowerChangeNum.toFixed(1)}万`
1408
- : `+${liveFollowerChangeNum}`;
1409
- }
1410
- // 小于0
1411
- return liveFollowerChangeNum <= -10_000
1412
- ? `${liveFollowerChangeNum.toFixed(1)}万`
1413
- : liveFollowerChangeNum.toString();
1414
- })();
1415
- // 定义下播播通知语
1416
- const liveEndMsg = this.config.customLiveEnd
1417
- ? this.config.customLiveEnd
1418
- .replace("-name", liveStatus.masterInfo.username)
1419
- .replace("-time", await this.ctx.gi.getTimeDifference(liveStatus.liveStartTime))
1420
- .replace("-follower_change", followerChange)
1421
- .replace("\\n", "\n")
1422
- : null;
1423
- // 推送通知卡片
1424
- await this.sendLiveNotifyCard(type_1.LiveType.StopBroadcast, followerChange, {
1425
- liveRoomInfo: liveStatus.liveRoomInfo,
1426
- masterInfo: liveStatus.masterInfo,
1427
- cardStyle: sub.card,
1428
- }, sub.target, liveEndMsg);
1429
- // 更改直播状态
1430
- liveStatus.live = false;
1431
- }
1432
- // 还未开播
1433
- break;
1434
- }
1435
- case 1: {
1436
- // 开播状态
1437
- if (liveStatus.live === false) {
1438
- // 开播了
1439
- // 判断信息是否获取成功
1440
- if (!(await useMasterAndLiveRoomInfo(type_1.LiveType.StopBroadcast, liveStatus))) {
1441
- // 未获取成功,直接返回
1442
- await this.sendPrivateMsg("获取直播间信息失败,推送直播开播卡片失败!");
1443
- // 停止服务
1444
- return await this.sendPrivateMsgAndStopService();
1445
- }
1446
- // 设置开播时间
1447
- liveStatus.liveStartTime = liveStatus.liveRoomInfo.live_time;
1448
- // 设置开播时间初始化状态
1449
- liveStatus.liveStartTimeInit = true;
1450
- // 获取当前粉丝数
1451
- const follower = liveStatus.masterInfo.liveOpenFollowerNum >= 10_000
1452
- ? `${(liveStatus.masterInfo.liveOpenFollowerNum / 10000).toFixed(1)}万`
1453
- : liveStatus.masterInfo.liveOpenFollowerNum.toString();
1454
- // 定义开播通知语
1455
- const liveStartMsg = this.config.customLiveStart
1456
- ? this.config.customLiveStart
1457
- .replace("-name", liveStatus.masterInfo.username)
1458
- .replace("-time", await this.ctx.gi.getTimeDifference(liveStatus.liveStartTime))
1459
- .replace("-follower", follower)
1460
- .replace("\\n", "\n")
1461
- .replace("-link", `https://live.bilibili.com/${liveStatus.liveRoomInfo.short_id === 0 ? liveStatus.liveRoomInfo.room_id : liveStatus.liveRoomInfo.short_id}`)
1462
- : null;
1463
- // 推送开播通知
1464
- await this.sendLiveNotifyCard(type_1.LiveType.StartBroadcasting, follower, {
1465
- liveRoomInfo: liveStatus.liveRoomInfo,
1466
- masterInfo: liveStatus.masterInfo,
1467
- cardStyle: sub.card,
1468
- }, sub.target, liveStartMsg);
1469
- // 设置开播状态为true
1470
- liveStatus.live = true;
1471
- }
1472
- if (liveStatus.live === true) {
1473
- // 还在直播
1474
- if (liveStatus.push < (this.config.pushTime * 60 * 60) / 30) {
1475
- // push++
1476
- liveStatus.push++;
1477
- // 结束本次循环
1478
- break;
1479
- }
1480
- // 判断是否信息是否获取成功
1481
- if (!(await useMasterAndLiveRoomInfo(type_1.LiveType.LiveBroadcast, liveStatus))) {
1482
- // 未获取成功,直接返回
1483
- await this.sendPrivateMsg("获取直播间信息失败,推送直播卡片失败!");
1484
- // 停止服务
1485
- return await this.sendPrivateMsgAndStopService();
1486
- }
1487
- // 判断是否需要设置开播时间
1488
- if (!liveStatus.liveStartTimeInit) {
1489
- // 设置开播时间
1490
- liveStatus.liveStartTime = liveStatus.liveRoomInfo.live_time;
1491
- // 设置开播时间初始化状态
1492
- liveStatus.liveStartTimeInit = true;
1493
- }
1494
- // 设置直播中消息
1495
- const liveMsg = this.config.customLive
1496
- ? this.config.customLive
1497
- .replace("-name", liveStatus.masterInfo.username)
1498
- .replace("-time", await this.ctx.gi.getTimeDifference(liveStatus.liveStartTime))
1499
- .replace("-watched", "API模式无法获取")
1500
- .replace("\\n", "\n")
1501
- .replace("-link", `https://live.bilibili.com/${liveStatus.liveRoomInfo.short_id === 0 ? liveStatus.liveRoomInfo.room_id : liveStatus.liveRoomInfo.short_id}`)
1502
- : null;
1503
- // 发送直播通知卡片
1504
- await this.sendLiveNotifyCard(type_1.LiveType.LiveBroadcast, "API", {
1505
- liveRoomInfo: liveStatus.liveRoomInfo,
1506
- masterInfo: liveStatus.masterInfo,
1507
- cardStyle: sub.card,
1508
- }, sub.target, liveMsg);
1509
- // push归零
1510
- liveStatus.push = 0;
1511
- }
1512
- // 结束
1513
- break;
1514
- }
1515
- default:
1516
- break;
1517
- }
1518
- }
1519
- };
1520
- // 返回一个闭包函数
1521
- return (0, utils_1.withLock)(handler);
1522
- }
1523
- subShow() {
1524
- // 在控制台中显示订阅对象
1525
- let table = "";
1526
- for (const sub of this.subManager) {
1527
- table += `UID:${sub.uid} ${sub.dynamic ? "已订阅动态" : ""} ${sub.live ? "已订阅直播" : ""}\n`;
1528
- }
1529
- return table ? table : "没有订阅任何UP";
1530
- }
1531
- updateSubNotifier() {
1532
- // 更新控制台提示
1533
- if (this.subNotifier)
1534
- this.subNotifier.dispose();
1535
- // 获取订阅信息
1536
- const subInfo = this.subShow();
1537
- // 定义table
1538
- let table = "";
1539
- if (subInfo === "没有订阅任何UP") {
1540
- table = subInfo;
1541
- }
1542
- else {
1543
- // 获取subTable
1544
- const subTableArray = subInfo.split("\n");
1545
- subTableArray.splice(subTableArray.length - 1, 1);
1546
- // 定义Table
1547
- 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) => (
1548
- // biome-ignore lint/correctness/useJsxKeyInIterable: <explanation>
1549
- (0, jsx_runtime_1.jsx)("li", { children: str }))) })] }));
1550
- }
1551
- // 设置更新后的提示
1552
- this.subNotifier = this.ctx.notifier.create(table);
1553
- }
1554
- async checkIfLoginInfoIsLoaded() {
1555
- return new Promise((resolve) => {
1556
- const check = () => {
1557
- if (!this.ctx.ba.getLoginInfoIsLoaded()) {
1558
- this.ctx.setTimeout(check, 500);
1559
- }
1560
- else {
1561
- resolve("success");
1562
- }
1563
- };
1564
- check();
1565
- });
1566
- }
1567
- async subUserInBili(mid) {
1568
- // 获取关注分组信息
1569
- const checkGroupIsReady = async () => {
1570
- // 判断是否有数据
1571
- if (!this.loginDBData?.dynamic_group_id) {
1572
- // 没有数据,没有创建分组,尝试创建分组
1573
- const createGroupData = (await this.ctx.ba.createGroup("订阅"));
1574
- // 如果分组已创建,则获取分组id
1575
- if (createGroupData.code === 22106) {
1576
- // 分组已存在,拿到之前的分组id
1577
- const allGroupData = (await this.ctx.ba.getAllGroup());
1578
- // 遍历所有分组
1579
- for (const group of allGroupData.data) {
1580
- // 找到订阅分组
1581
- if (group.name === "订阅") {
1582
- // 拿到分组id
1583
- this.loginDBData.dynamic_group_id = group.tagid.toString();
1584
- // 保存到数据库
1585
- this.ctx.database.set("loginBili", 1, {
1586
- dynamic_group_id: this.loginDBData.dynamic_group_id,
1587
- });
1588
- // 返回分组已存在
1589
- return { code: 0, msg: "分组已存在" };
1590
- }
1591
- }
1592
- }
1593
- else if (createGroupData.code !== 0) {
1594
- // 创建分组失败
1595
- return { code: createGroupData.code, msg: createGroupData.message };
1596
- }
1597
- // 创建成功,保存到数据库
1598
- this.ctx.database.set("loginBili", 1, {
1599
- dynamic_group_id: createGroupData.data.tagid.toString(),
1600
- });
1601
- // 创建成功
1602
- return { code: createGroupData.code, msg: createGroupData.message };
1603
- }
1604
- return { code: 0, msg: "分组已存在" };
1605
- };
1606
- // 判断分组是否准备好
1607
- const resp = await checkGroupIsReady();
1608
- // 判断是否创建成功
1609
- if (resp.code !== 0)
1610
- return resp;
1611
- // 获取分组详情
1612
- const getGroupDetailData = async () => {
1613
- // 获取分组明细
1614
- const relationGroupDetailData = await this.ctx.ba.getRelationGroupDetail(this.loginDBData.dynamic_group_id);
1615
- // 判断分组信息是否获取成功
1616
- if (relationGroupDetailData.code !== 0) {
1617
- if (relationGroupDetailData.code === 22104) {
1618
- // 将原先的分组id置空
1619
- this.loginDBData.dynamic_group_id = null;
1620
- // 分组不存在
1621
- const resp = await checkGroupIsReady();
1622
- // 判断是否创建成功
1623
- if (resp.code !== 0)
1624
- return resp;
1625
- // 再次获取分组明细
1626
- return getGroupDetailData();
1627
- }
1628
- // 获取分组明细失败
1629
- return {
1630
- code: relationGroupDetailData.code,
1631
- msg: relationGroupDetailData.message,
1632
- data: undefined,
1633
- };
1634
- }
1635
- return {
1636
- code: 0,
1637
- msg: "获取分组明细成功",
1638
- data: relationGroupDetailData.data,
1639
- };
1640
- };
1641
- // 获取分组明细
1642
- const { code, msg, data } = await getGroupDetailData();
1643
- // 判断获取分组明细是否成功
1644
- if (code !== 0) {
1645
- return { code, msg };
1646
- }
1647
- // 判断是否已经订阅该对象
1648
- for (const user of data) {
1649
- if (user.mid === mid) {
1650
- // 已关注订阅对象
1651
- return { code: 0, msg: "订阅对象已存在于分组中" };
1652
- }
1653
- }
1654
- // 订阅对象
1655
- const subUserData = (await this.ctx.ba.follow(mid));
1656
- // 模式匹配
1657
- const subUserMatchPattern = {
1658
- [-101]: () => {
1659
- return {
1660
- code: subUserData.code,
1661
- msg: "账号未登录,请使用指令bili login登录后再进行订阅操作",
1662
- };
1663
- },
1664
- [-102]: () => {
1665
- return {
1666
- code: subUserData.code,
1667
- msg: "账号被封停,无法进行订阅操作",
1668
- };
1669
- },
1670
- [22002]: () => {
1671
- return {
1672
- code: subUserData.code,
1673
- msg: "因对方隐私设置,无法进行订阅操作",
1674
- };
1675
- },
1676
- [22003]: () => {
1677
- return {
1678
- code: subUserData.code,
1679
- msg: "你已将对方拉黑,无法进行订阅操作",
1680
- };
1681
- },
1682
- [22013]: () => {
1683
- return {
1684
- code: subUserData.code,
1685
- msg: "账号已注销,无法进行订阅操作",
1686
- };
1687
- },
1688
- [40061]: () => {
1689
- return {
1690
- code: subUserData.code,
1691
- msg: "账号不存在,请检查uid输入是否正确或用户是否存在",
1692
- };
1693
- },
1694
- [22001]: () => {
1695
- return {
1696
- code: 0,
1697
- msg: "订阅对象为自己,无需添加到分组",
1698
- };
1699
- },
1700
- // 已订阅该对象
1701
- [22014]: async () => {
1702
- // 把订阅对象添加到分组中
1703
- const copyUserToGroupData = await this.ctx.ba.copyUserToGroup(mid, this.loginDBData.dynamic_group_id);
1704
- // 判断是否添加成功
1705
- if (copyUserToGroupData.code !== 0) {
1706
- // 添加失败
1707
- return {
1708
- code: copyUserToGroupData.code,
1709
- msg: "添加订阅对象到分组失败,请稍后重试",
1710
- };
1711
- }
1712
- // 添加成功
1713
- return { code: 0, msg: "订阅对象添加成功" };
1714
- },
1715
- // 订阅成功
1716
- [0]: async () => {
1717
- // 把订阅对象添加到分组中
1718
- const copyUserToGroupData = await this.ctx.ba.copyUserToGroup(mid, this.loginDBData.dynamic_group_id);
1719
- // 判断是否添加成功
1720
- if (copyUserToGroupData.code !== 0) {
1721
- // 添加失败
1722
- return {
1723
- code: copyUserToGroupData.code,
1724
- msg: "添加订阅对象到分组失败,请稍后重试",
1725
- };
1726
- }
1727
- // 添加成功
1728
- return { code: 0, msg: "订阅对象添加成功" };
1729
- },
1730
- };
1731
- // 执行函数并返回
1732
- return await subUserMatchPattern[subUserData.code]();
1733
- }
1734
- async loadSubFromConfig(subs) {
1735
- for (const sub of subs) {
1736
- // logger
1737
- this.logger.info(`加载订阅UID:${sub.uid}中...`);
1738
- // 定义Data
1739
- const { code: userInfoCode, msg: userInfoMsg, data: userInfoData, } = await (0, utils_1.withRetry)(async () => {
1740
- // 获取用户信息
1741
- const data = await this.ctx.ba.getUserInfo(sub.uid);
1742
- // 返回用户信息
1743
- return { code: 0, data };
1744
- })
1745
- .then((content) => content.data)
1746
- .catch((e) => {
1747
- this.logger.error(`loadSubFromConfig() getUserInfo() 发生了错误,错误为:${e.message}`);
1748
- // 返回失败
1749
- return { code: -1, message: `加载订阅UID:${sub.uid}失败!` };
1750
- });
1751
- // 判断是否获取成功
1752
- if (userInfoCode !== 0)
1753
- return { code: userInfoCode, msg: userInfoMsg };
1754
- // 判断是否需要订阅直播
1755
- if (this.config.liveDetectType === "WS" && sub.live) {
1756
- // 检查roomid是否存在
1757
- if (!userInfoData.live_room) {
1758
- // 用户没有开通直播间,无法订阅直播
1759
- sub.live = false;
1760
- // 发送提示
1761
- this.logger.warn(`UID:${sub.uid} 用户没有开通直播间,无法订阅直播!`);
1762
- }
1763
- // 判断是否订阅直播
1764
- if (sub.live) {
1765
- // 启动直播监测
1766
- await this.liveDetectWithListener(userInfoData.live_room.roomid, sub.target, sub.card);
1767
- }
1768
- }
1769
- // 在B站中订阅该对象
1770
- const subInfo = await this.subUserInBili(sub.uid);
1771
- // 判断订阅是否成功
1772
- if (subInfo.code !== 0)
1773
- return subInfo;
1774
- // 将该订阅添加到sm中
1775
- this.subManager.push({
1776
- id: +sub.uid,
1777
- uid: sub.uid,
1778
- uname: userInfoData.name,
1779
- roomId: sub.live ? userInfoData.live_room.roomid : "",
1780
- target: sub.target,
1781
- platform: "",
1782
- live: sub.live,
1783
- dynamic: sub.dynamic,
1784
- card: sub.card,
1785
- });
1786
- // logger
1787
- this.logger.info(`UID:${sub.uid}订阅加载完毕!`);
1788
- // 1-3秒随机延迟
1789
- const randomDelay = Math.floor(Math.random() * 3) + 1;
1790
- // logger
1791
- this.logger.info(`随机延迟:${randomDelay}秒`);
1792
- // delay
1793
- await this.ctx.sleep(randomDelay * 1000);
1794
- }
1795
- return { code: 0, msg: "订阅加载完毕!" };
1796
- }
1797
- checkIfDynamicDetectIsNeeded() {
1798
- // 检查是否有订阅对象需要动态监测
1799
- if (this.dynamicTimelineManager.size > 0) {
1800
- // 启动动态监测
1801
- this.enableDynamicDetect();
1802
- }
1803
- }
1804
- checkIfLiveDetectIsNeeded() {
1805
- // 判断直播监测类型
1806
- if (this.config.liveDetectType === "API") {
1807
- // 检查是否有订阅对象需要直播监测
1808
- if (this.liveStatusManager.size > 0) {
1809
- // 启动直播监测
1810
- this.enableLiveDetect();
1811
- }
1812
- }
1813
- }
1814
- enableDynamicDetect() {
1815
- // 定义Job
1816
- this.dynamicJob = new cron_1.CronJob("*/2 * * * *", this.config.dynamicDebugMode
1817
- ? this.debug_dynamicDetect()
1818
- : this.dynamicDetect());
1819
- // logger
1820
- this.logger.info("动态监测已开启");
1821
- // 开始动态监测
1822
- this.dynamicJob.start();
1823
- }
1824
- async enableLiveDetect() {
1825
- // 定义Job
1826
- this.liveJob = new cron_1.CronJob("*/30 * * * * *", await this.liveDetectWithAPI());
1827
- // logger
1828
- this.logger.info("直播监测已开启");
1829
- // 开始直播监测
1830
- this.liveJob.start();
1831
- }
1832
- async checkIfIsLogin() {
1833
- if ((await this.ctx.database.get("loginBili", 1)).length !== 0) {
1834
- // 数据库中有数据
1835
- // 检查cookie中是否有值
1836
- if (this.ctx.ba.getCookies() !== "[]") {
1837
- // 有值说明已登录
1838
- return true;
1839
- }
1840
- }
1841
- return false;
1842
- }
1843
- }
1844
- (function (ComRegister) {
1845
- ComRegister.Config = koishi_1.Schema.object({
1846
- sub: koishi_1.Schema.array(koishi_1.Schema.object({
1847
- uid: koishi_1.Schema.string().description("订阅用户UID"),
1848
- dynamic: koishi_1.Schema.boolean().description("是否订阅用户动态"),
1849
- live: koishi_1.Schema.boolean().description("是否订阅用户直播"),
1850
- target: koishi_1.Schema.array(koishi_1.Schema.object({
1851
- channelArr: koishi_1.Schema.array(koishi_1.Schema.object({
1852
- channelId: koishi_1.Schema.string().description("频道/群组号"),
1853
- dynamic: koishi_1.Schema.boolean().description("该频道/群组是否推送动态信息"),
1854
- live: koishi_1.Schema.boolean().description("该频道/群组是否推送直播通知"),
1855
- liveGuardBuy: koishi_1.Schema.boolean().description("该频道/群组是否推送弹幕消息"),
1856
- atAll: koishi_1.Schema.boolean().description("推送开播通知时是否艾特全体成员"),
1857
- bot: koishi_1.Schema.string().description("若您有多个相同平台机器人,可在此填写当前群聊执行推送的机器人账号。不填则默认第一个"),
1858
- })).description("频道/群组信息"),
1859
- platform: koishi_1.Schema.string().description("推送平台"),
1860
- })).description("订阅用户需要发送的频道/群组信息"),
1861
- card: koishi_1.Schema.object({
1862
- enable: koishi_1.Schema.boolean(),
1863
- cardColorStart: koishi_1.Schema.string(),
1864
- cardColorEnd: koishi_1.Schema.string(),
1865
- cardBasePlateColor: koishi_1.Schema.string(),
1866
- cardBasePlateBorder: koishi_1.Schema.string(),
1867
- }).description("自定义推送卡片颜色,默认使用插件内置的颜色,设置后会覆盖插件内置的颜色"),
1868
- }))
1869
- .role("table")
1870
- .description("手动输入订阅信息,方便自定义订阅内容,这里的订阅内容不会存入数据库。uid: 订阅用户UID,dynamic: 是否需要订阅动态,live: 是否需要订阅直播"),
1871
- master: koishi_1.Schema.object({
1872
- enable: koishi_1.Schema.boolean(),
1873
- platform: koishi_1.Schema.string(),
1874
- masterAccount: koishi_1.Schema.string(),
1875
- masterAccountGuildId: koishi_1.Schema.string(),
1876
- }),
1877
- liveDetectType: koishi_1.Schema.string(),
1878
- restartPush: koishi_1.Schema.boolean().required(),
1879
- pushTime: koishi_1.Schema.number().required(),
1880
- pushImgsInDynamic: koishi_1.Schema.boolean().required(),
1881
- liveLoopTime: koishi_1.Schema.number().default(10),
1882
- customLiveStart: koishi_1.Schema.string().required(),
1883
- customLive: koishi_1.Schema.string(),
1884
- customLiveEnd: koishi_1.Schema.string().required(),
1885
- dynamicUrl: koishi_1.Schema.boolean().required(),
1886
- dynamicVideoUrlToBV: koishi_1.Schema.boolean().required(),
1887
- filter: koishi_1.Schema.object({
1888
- enable: koishi_1.Schema.boolean(),
1889
- notify: koishi_1.Schema.boolean(),
1890
- regex: koishi_1.Schema.string(),
1891
- keywords: koishi_1.Schema.array(String),
1892
- }),
1893
- dynamicDebugMode: koishi_1.Schema.boolean().required(),
1894
- });
1895
- })(ComRegister || (ComRegister = {}));
1896
- exports.default = ComRegister;
1
+ const require_chunk = require('./chunk-CrFLpmCJ.js');
2
+ require('./utils-BoaYrSvG.js');
3
+ const require_comRegister = require('./comRegister-t7UnYFig.js');
4
+ require('./type-WjCy4YMc.js');
5
+
6
+ module.exports = require_comRegister.comRegister_default;