koishi-plugin-bilibili-notify 0.1.2 → 0.1.3
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/authority.d.ts +5 -0
- package/lib/authority.js +11 -0
- package/lib/biliAPI.d.ts +1 -1
- package/lib/biliAPI.js +80 -21
- package/lib/comRegister.d.ts +5 -2
- package/lib/comRegister.js +249 -93
- package/lib/index.js +2 -0
- package/package.json +1 -1
package/lib/authority.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class Authority {
|
|
4
|
+
constructor(ctx) {
|
|
5
|
+
// 授予权限
|
|
6
|
+
ctx.permissions.provide('qqguild.admin', async (name, session) => {
|
|
7
|
+
return session.event.user.id === '12814193631283946447';
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.default = Authority;
|
package/lib/biliAPI.d.ts
CHANGED
|
@@ -24,6 +24,6 @@ declare class BiliAPI extends Service {
|
|
|
24
24
|
createNewClient(): void;
|
|
25
25
|
getCookies(): Promise<string>;
|
|
26
26
|
loadCookiesFromDatabase(): Promise<void>;
|
|
27
|
-
checkIfTokenNeedRefresh(refreshToken: string, csrf: string): Promise<void>;
|
|
27
|
+
checkIfTokenNeedRefresh(refreshToken: string, csrf: string, times?: number): Promise<void>;
|
|
28
28
|
}
|
|
29
29
|
export default BiliAPI;
|
package/lib/biliAPI.js
CHANGED
|
@@ -35,42 +35,87 @@ class BiliAPI extends koishi_1.Service {
|
|
|
35
35
|
this.logger.info('BiliAPI已被注册到Context中');
|
|
36
36
|
}
|
|
37
37
|
async getTimeNow() {
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
try {
|
|
39
|
+
const { data } = await this.client.get(GET_TIME_NOW);
|
|
40
|
+
return data;
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
throw new Error('网络异常,本次请求失败!');
|
|
44
|
+
}
|
|
40
45
|
}
|
|
41
46
|
async getUserSpaceDynamic(mid) {
|
|
42
|
-
|
|
43
|
-
|
|
47
|
+
try {
|
|
48
|
+
const { data } = await this.client.get(`${GET_USER_SPACE_DYNAMIC_LIST}?host_mid=${mid}`);
|
|
49
|
+
return data;
|
|
50
|
+
}
|
|
51
|
+
catch (e) {
|
|
52
|
+
throw new Error('网络异常,本次请求失败!');
|
|
53
|
+
}
|
|
44
54
|
}
|
|
45
55
|
// Check if Token need refresh
|
|
46
56
|
async getCookieInfo(refreshToken) {
|
|
47
|
-
|
|
48
|
-
|
|
57
|
+
try {
|
|
58
|
+
const { data } = await this.client.get(`${GET_COOKIES_INFO}?csrf=${refreshToken}`);
|
|
59
|
+
return data;
|
|
60
|
+
}
|
|
61
|
+
catch (e) {
|
|
62
|
+
throw new Error('网络异常,本次请求失败!');
|
|
63
|
+
}
|
|
49
64
|
}
|
|
50
65
|
async getUserInfo(mid) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
66
|
+
try {
|
|
67
|
+
const wbi = await this.ctx.wbi.getWbi({ mid });
|
|
68
|
+
const { data } = await this.client.get(`${GET_USER_INFO}?${wbi}`);
|
|
69
|
+
return data;
|
|
70
|
+
}
|
|
71
|
+
catch (e) {
|
|
72
|
+
throw new Error('网络异常,本次请求失败!');
|
|
73
|
+
}
|
|
54
74
|
}
|
|
55
75
|
async getMyselfInfo() {
|
|
56
|
-
|
|
57
|
-
|
|
76
|
+
try {
|
|
77
|
+
const { data } = await this.client.get(GET_MYSELF_INFO);
|
|
78
|
+
return data;
|
|
79
|
+
}
|
|
80
|
+
catch (e) {
|
|
81
|
+
throw new Error('网络异常,本次请求失败!');
|
|
82
|
+
}
|
|
58
83
|
}
|
|
59
84
|
async getLoginQRCode() {
|
|
60
|
-
|
|
61
|
-
|
|
85
|
+
try {
|
|
86
|
+
const { data } = await this.client.get(GET_LOGIN_QRCODE);
|
|
87
|
+
return data;
|
|
88
|
+
}
|
|
89
|
+
catch (e) {
|
|
90
|
+
throw new Error('网络异常,本次请求失败!');
|
|
91
|
+
}
|
|
62
92
|
}
|
|
63
93
|
async getLoginStatus(qrcodeKey) {
|
|
64
|
-
|
|
65
|
-
|
|
94
|
+
try {
|
|
95
|
+
const { data } = await this.client.get(`${GET_LOGIN_STATUS}?qrcode_key=${qrcodeKey}`);
|
|
96
|
+
return data;
|
|
97
|
+
}
|
|
98
|
+
catch (e) {
|
|
99
|
+
throw new Error('网络异常,本次请求失败!');
|
|
100
|
+
}
|
|
66
101
|
}
|
|
67
102
|
async getLiveRoomInfo(roomId) {
|
|
68
|
-
|
|
69
|
-
|
|
103
|
+
try {
|
|
104
|
+
const { data } = await this.client.get(`${GET_LIVE_ROOM_INFO}?room_id=${roomId}`);
|
|
105
|
+
return data;
|
|
106
|
+
}
|
|
107
|
+
catch (e) {
|
|
108
|
+
throw new Error('网络异常,本次请求失败!');
|
|
109
|
+
}
|
|
70
110
|
}
|
|
71
111
|
async getMasterInfo(mid) {
|
|
72
|
-
|
|
73
|
-
|
|
112
|
+
try {
|
|
113
|
+
const { data } = await this.client.get(`${GET_MASTER_INFO}?uid=${mid}`);
|
|
114
|
+
return data;
|
|
115
|
+
}
|
|
116
|
+
catch (e) {
|
|
117
|
+
throw new Error('网络异常,本次请求失败!');
|
|
118
|
+
}
|
|
74
119
|
}
|
|
75
120
|
createNewClient() {
|
|
76
121
|
this.jar = new tough_cookie_1.CookieJar();
|
|
@@ -120,8 +165,22 @@ class BiliAPI extends koishi_1.Service {
|
|
|
120
165
|
// Check if token need refresh
|
|
121
166
|
this.checkIfTokenNeedRefresh(decryptedRefreshToken, csrf);
|
|
122
167
|
}
|
|
123
|
-
async checkIfTokenNeedRefresh(refreshToken, csrf) {
|
|
124
|
-
|
|
168
|
+
async checkIfTokenNeedRefresh(refreshToken, csrf, times = 0) {
|
|
169
|
+
let data;
|
|
170
|
+
try {
|
|
171
|
+
const { data: cookieData } = await this.getCookieInfo(refreshToken);
|
|
172
|
+
data = cookieData;
|
|
173
|
+
}
|
|
174
|
+
catch (e) {
|
|
175
|
+
// 发送三次仍网络错误则给管理员发送错误信息
|
|
176
|
+
if (times > 3)
|
|
177
|
+
return;
|
|
178
|
+
// 等待3秒再次尝试
|
|
179
|
+
this.ctx.setTimeout(() => {
|
|
180
|
+
this.checkIfTokenNeedRefresh(refreshToken, csrf, times + 1);
|
|
181
|
+
}, 3000);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
125
184
|
// 不需要刷新,直接返回
|
|
126
185
|
if (!data.refresh)
|
|
127
186
|
return;
|
package/lib/comRegister.d.ts
CHANGED
|
@@ -17,14 +17,17 @@ declare class ComRegister {
|
|
|
17
17
|
qqBot: Bot<Context>;
|
|
18
18
|
qqguildBot: Bot<Context>;
|
|
19
19
|
constructor(ctx: Context, config: ComRegister.Config);
|
|
20
|
-
dynamicDetect(ctx: Context, bot: Bot<Context>,
|
|
21
|
-
liveDetect(ctx: Context, bot: Bot<Context>,
|
|
20
|
+
dynamicDetect(ctx: Context, bot: Bot<Context>, guildId: string, uid: string): () => Promise<void>;
|
|
21
|
+
liveDetect(ctx: Context, bot: Bot<Context>, guildId: string, roomId: string): () => Promise<void>;
|
|
22
22
|
checkIfNeedSub(comNeed: boolean, subType: string, session: Session, data?: any): Promise<boolean>;
|
|
23
23
|
getSubFromDatabase(ctx: Context): Promise<void>;
|
|
24
|
+
unsubSingle(ctx: Context, id: string, type: number): string;
|
|
24
25
|
}
|
|
25
26
|
declare namespace ComRegister {
|
|
26
27
|
interface Config {
|
|
27
28
|
pushTime: number;
|
|
29
|
+
liveLoopTime: number;
|
|
30
|
+
dynamicLoopTime: number;
|
|
28
31
|
}
|
|
29
32
|
const Config: Schema<Config>;
|
|
30
33
|
}
|
package/lib/comRegister.js
CHANGED
|
@@ -69,10 +69,10 @@ class ComRegister {
|
|
|
69
69
|
console.log(num && `Hello World`);
|
|
70
70
|
});
|
|
71
71
|
ctx.command('test')
|
|
72
|
-
.subcommand('.gimg <uid:string> <index:number>')
|
|
72
|
+
.subcommand('.gimg <uid:string> <index:number>', '生成图片', { permissions: ['qqguild.admin'] })
|
|
73
73
|
.usage('测试图片生成')
|
|
74
74
|
.example('test.gimg')
|
|
75
|
-
.action(async (
|
|
75
|
+
.action(async ({ session }, uid, index) => {
|
|
76
76
|
// 获取用户空间动态数据
|
|
77
77
|
const { data } = await ctx.biliAPI.getUserSpaceDynamic(uid);
|
|
78
78
|
const [pic] = await ctx.gimg.generateDynamicImg(data.items[index]);
|
|
@@ -106,7 +106,13 @@ class ComRegister {
|
|
|
106
106
|
.action(async ({ session }) => {
|
|
107
107
|
this.logger.info('调用bili login指令');
|
|
108
108
|
// 获取二维码
|
|
109
|
-
|
|
109
|
+
let content;
|
|
110
|
+
try {
|
|
111
|
+
content = await ctx.biliAPI.getLoginQRCode();
|
|
112
|
+
}
|
|
113
|
+
catch (e) {
|
|
114
|
+
return 'bili login getLoginQRCode() 本次网络请求失败';
|
|
115
|
+
}
|
|
110
116
|
// 判断是否出问题
|
|
111
117
|
if (content.code !== 0)
|
|
112
118
|
return await session.send('出问题咯,请联系管理员解决!');
|
|
@@ -131,7 +137,14 @@ class ComRegister {
|
|
|
131
137
|
let dispose;
|
|
132
138
|
// 发起登录请求检查登录状态
|
|
133
139
|
dispose = ctx.setInterval(async () => {
|
|
134
|
-
|
|
140
|
+
let loginContent;
|
|
141
|
+
try {
|
|
142
|
+
loginContent = await ctx.biliAPI.getLoginStatus(content.data.qrcode_key);
|
|
143
|
+
}
|
|
144
|
+
catch (e) {
|
|
145
|
+
this.logger.error(e);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
135
148
|
if (loginContent.code !== 0) {
|
|
136
149
|
dispose();
|
|
137
150
|
return await session.send('登录失败!请联系管理员解决!');
|
|
@@ -154,10 +167,12 @@ class ComRegister {
|
|
|
154
167
|
}, 1000);
|
|
155
168
|
});
|
|
156
169
|
ctx.command('bili')
|
|
157
|
-
.subcommand('.unsub <uid:string>')
|
|
158
|
-
.usage('
|
|
159
|
-
.
|
|
160
|
-
.
|
|
170
|
+
.subcommand('.unsub <uid:string> 取消订阅')
|
|
171
|
+
.usage('取消订阅,加-l为取消直播订阅,加-d为取消动态订阅,什么都不加则为全部取消')
|
|
172
|
+
.option('live', '-l')
|
|
173
|
+
.option('dynamic', '-d')
|
|
174
|
+
.example('bili unsub 用户UID -ld')
|
|
175
|
+
.action(async ({ session, options }, uid) => {
|
|
161
176
|
this.logger.info('调用bili.unsub指令');
|
|
162
177
|
// 若用户UID为空则直接返回
|
|
163
178
|
if (!uid)
|
|
@@ -166,9 +181,21 @@ class ComRegister {
|
|
|
166
181
|
let exist;
|
|
167
182
|
await Promise.all(this.subManager.map(async (sub, i) => {
|
|
168
183
|
if (sub.uid === uid) {
|
|
169
|
-
//
|
|
170
|
-
|
|
171
|
-
|
|
184
|
+
// 取消单个订阅
|
|
185
|
+
if (options.live || options.dynamic) {
|
|
186
|
+
if (options.live)
|
|
187
|
+
await session.send(this.unsubSingle(ctx, sub.roomId, 0)); /* 0为取消订阅Live */
|
|
188
|
+
if (options.dynamic)
|
|
189
|
+
await session.send(this.unsubSingle(ctx, sub.uid, 1)); /* 1为取消订阅Dynamic */
|
|
190
|
+
// 将存在flag设置为true
|
|
191
|
+
exist = true;
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
// 取消全部订阅 执行dispose方法,销毁定时器
|
|
195
|
+
if (sub.dynamic)
|
|
196
|
+
this.subManager[i].dynamicDispose();
|
|
197
|
+
if (sub.live)
|
|
198
|
+
this.subManager[i].liveDispose();
|
|
172
199
|
// 从数据库中删除订阅
|
|
173
200
|
await ctx.database.remove('bilibili', { uid: this.subManager[i].uid });
|
|
174
201
|
// 将该订阅对象从订阅管理对象中移除
|
|
@@ -191,7 +218,7 @@ class ComRegister {
|
|
|
191
218
|
.action(async ({ session }) => {
|
|
192
219
|
let table = ``;
|
|
193
220
|
this.subManager.forEach(sub => {
|
|
194
|
-
table +=
|
|
221
|
+
table += `UID:${sub.uid} ${sub.dynamic ? '已订阅动态' : ''} ${sub.live ? '已订阅直播' : ''}` + '\n';
|
|
195
222
|
});
|
|
196
223
|
!table && session.send('没有订阅任何UP');
|
|
197
224
|
table && session.send(table);
|
|
@@ -217,7 +244,13 @@ class ComRegister {
|
|
|
217
244
|
// 定义是否需要直播通知,动态订阅,视频推送
|
|
218
245
|
let liveMsg, dynamicMsg;
|
|
219
246
|
// 获取用户信息
|
|
220
|
-
|
|
247
|
+
let content;
|
|
248
|
+
try {
|
|
249
|
+
content = await ctx.biliAPI.getUserInfo(mid);
|
|
250
|
+
}
|
|
251
|
+
catch (e) {
|
|
252
|
+
return 'bili sub getUserInfo() 本次网络请求失败';
|
|
253
|
+
}
|
|
221
254
|
// 判断是否有其他问题
|
|
222
255
|
if (content.code !== 0) {
|
|
223
256
|
let msg;
|
|
@@ -299,7 +332,14 @@ class ComRegister {
|
|
|
299
332
|
dynamicDispose: null
|
|
300
333
|
});
|
|
301
334
|
// 获取用户信息
|
|
302
|
-
|
|
335
|
+
let userData;
|
|
336
|
+
try {
|
|
337
|
+
const { data } = await ctx.biliAPI.getMasterInfo(sub.uid);
|
|
338
|
+
userData = data;
|
|
339
|
+
}
|
|
340
|
+
catch (e) {
|
|
341
|
+
return 'bili sub指令 getMasterInfo() 网络请求失败';
|
|
342
|
+
}
|
|
303
343
|
// 需要订阅直播
|
|
304
344
|
if (liveMsg) {
|
|
305
345
|
await session.execute(`bili live ${data.live_room.roomid} ${guildId} -b ${platform}`);
|
|
@@ -314,7 +354,7 @@ class ComRegister {
|
|
|
314
354
|
}
|
|
315
355
|
});
|
|
316
356
|
ctx.command('bili')
|
|
317
|
-
.subcommand('.dynamic <uid:string>')
|
|
357
|
+
.subcommand('.dynamic <uid:string> <guildId:string>')
|
|
318
358
|
.option('bot', '-b <type:string>')
|
|
319
359
|
.usage('订阅用户动态推送')
|
|
320
360
|
.example('bili dynamic 1')
|
|
@@ -323,6 +363,8 @@ class ComRegister {
|
|
|
323
363
|
// 如果uid为空则返回
|
|
324
364
|
if (!uid)
|
|
325
365
|
return '用户uid不能为空';
|
|
366
|
+
if (!guildId)
|
|
367
|
+
return '目标群组或频道不能为空';
|
|
326
368
|
if (!options.bot)
|
|
327
369
|
return '非法调用';
|
|
328
370
|
// 保存到订阅管理对象
|
|
@@ -353,7 +395,7 @@ class ComRegister {
|
|
|
353
395
|
.option('bot', '-b <type:string>')
|
|
354
396
|
.usage('订阅主播开播通知')
|
|
355
397
|
.example('bili live 732')
|
|
356
|
-
.action(async ({
|
|
398
|
+
.action(async ({ options }, roomId, guildId) => {
|
|
357
399
|
this.logger.info('调用bili.live指令');
|
|
358
400
|
// 如果room_id为空则返回
|
|
359
401
|
if (!roomId)
|
|
@@ -391,9 +433,22 @@ class ComRegister {
|
|
|
391
433
|
this.logger.info('调用bili.status指令');
|
|
392
434
|
if (!roomId)
|
|
393
435
|
return session.send('请输入房间号!');
|
|
394
|
-
|
|
436
|
+
let content;
|
|
437
|
+
try {
|
|
438
|
+
content = await ctx.biliAPI.getLiveRoomInfo(roomId);
|
|
439
|
+
}
|
|
440
|
+
catch (e) {
|
|
441
|
+
return 'bili status指令 getLiveRoomInfo() 本次网络请求失败';
|
|
442
|
+
}
|
|
395
443
|
const { data } = content;
|
|
396
|
-
|
|
444
|
+
let userData;
|
|
445
|
+
try {
|
|
446
|
+
const { data: userInfo } = await ctx.biliAPI.getMasterInfo(data.uid);
|
|
447
|
+
userData = userInfo;
|
|
448
|
+
}
|
|
449
|
+
catch (e) {
|
|
450
|
+
return 'bili status指令 getMasterInfo() 网络请求失败';
|
|
451
|
+
}
|
|
397
452
|
// B站出问题了
|
|
398
453
|
if (content.code !== 0) {
|
|
399
454
|
if (content.msg === '未找到该房间') {
|
|
@@ -411,7 +466,7 @@ class ComRegister {
|
|
|
411
466
|
session.send(string);
|
|
412
467
|
});
|
|
413
468
|
}
|
|
414
|
-
dynamicDetect(ctx, bot,
|
|
469
|
+
dynamicDetect(ctx, bot, guildId, uid) {
|
|
415
470
|
let firstSubscription = true;
|
|
416
471
|
let timePoint;
|
|
417
472
|
return async () => {
|
|
@@ -424,17 +479,26 @@ class ComRegister {
|
|
|
424
479
|
return;
|
|
425
480
|
}
|
|
426
481
|
// 获取用户空间动态数据
|
|
427
|
-
|
|
482
|
+
let content;
|
|
483
|
+
try {
|
|
484
|
+
content = await ctx.biliAPI.getUserSpaceDynamic(uid);
|
|
485
|
+
}
|
|
486
|
+
catch (e) {
|
|
487
|
+
return this.logger.error('dynamicDetect getUserSpaceDynamic() 网络请求失败');
|
|
488
|
+
}
|
|
428
489
|
// 判断是否出现其他问题
|
|
429
490
|
if (content.code !== 0) {
|
|
430
491
|
switch (content.code) {
|
|
431
492
|
case -101: { // 账号未登录
|
|
432
|
-
|
|
493
|
+
bot.sendMessage(guildId, '账号未登录,请登录后重新订阅动态');
|
|
433
494
|
}
|
|
434
495
|
default: { // 未知错误
|
|
435
|
-
|
|
496
|
+
bot.sendMessage(guildId, '未知错误,请重新订阅动态');
|
|
436
497
|
}
|
|
437
498
|
}
|
|
499
|
+
// 取消订阅
|
|
500
|
+
this.unsubSingle(ctx, uid, 1); /* 1为取消动态订阅 */
|
|
501
|
+
return;
|
|
438
502
|
}
|
|
439
503
|
// 获取数据内容
|
|
440
504
|
const items = content.data.items;
|
|
@@ -458,94 +522,126 @@ class ComRegister {
|
|
|
458
522
|
}
|
|
459
523
|
// 推送该条动态
|
|
460
524
|
const [pic] = await ctx.gimg.generateDynamicImg(items[num]);
|
|
461
|
-
await bot.sendMessage(
|
|
525
|
+
await bot.sendMessage(guildId, pic);
|
|
462
526
|
}
|
|
463
527
|
}
|
|
464
528
|
};
|
|
465
529
|
}
|
|
466
|
-
liveDetect(ctx, bot,
|
|
530
|
+
liveDetect(ctx, bot, guildId, roomId) {
|
|
467
531
|
let firstSubscription = true;
|
|
468
532
|
let timer = 0;
|
|
469
533
|
let open = false;
|
|
470
534
|
let liveTime;
|
|
471
535
|
let uData;
|
|
536
|
+
// 相当于锁的作用,防止上一个循环没处理完
|
|
537
|
+
let flag = true;
|
|
472
538
|
return async () => {
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
539
|
+
try {
|
|
540
|
+
// 如果flag为false则说明前面的代码还未执行完,则直接返回
|
|
541
|
+
if (!flag)
|
|
542
|
+
return;
|
|
543
|
+
// 发送请求检测直播状态
|
|
544
|
+
let content;
|
|
545
|
+
try {
|
|
546
|
+
content = await ctx.biliAPI.getLiveRoomInfo(roomId);
|
|
480
547
|
}
|
|
481
|
-
|
|
482
|
-
|
|
548
|
+
catch (e) {
|
|
549
|
+
return this.logger.error('liveDetect getLiveRoomInfo 网络请求失败');
|
|
483
550
|
}
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
// 获取主播信息
|
|
490
|
-
const { data: userData } = await ctx.biliAPI.getMasterInfo(data.uid);
|
|
491
|
-
// 主播信息不会变,请求一次即可
|
|
492
|
-
uData = userData;
|
|
493
|
-
// 判断直播状态
|
|
494
|
-
if (data.live_status === 1) { // 当前正在直播
|
|
495
|
-
// 推送直播信息
|
|
496
|
-
await bot.sendMessage(groupId, await ctx
|
|
497
|
-
.gimg
|
|
498
|
-
.generateLiveImg(data, uData, LiveType.LiveBroadcast));
|
|
499
|
-
// 改变开播状态
|
|
500
|
-
open = true;
|
|
501
|
-
} // 未开播,直接返回
|
|
502
|
-
return;
|
|
503
|
-
}
|
|
504
|
-
// 检查直播状态
|
|
505
|
-
switch (data.live_status) {
|
|
506
|
-
case 0:
|
|
507
|
-
case 2: { // 状态 0 和 2 说明未开播
|
|
508
|
-
if (open) { // 之前开播,现在下播了
|
|
509
|
-
// 更改直播状态
|
|
510
|
-
open = false;
|
|
511
|
-
// 下播了将定时器清零
|
|
512
|
-
timer = 0;
|
|
513
|
-
// 发送下播通知
|
|
514
|
-
bot.sendMessage(groupId, `${uData.info.uname}下播啦,本次直播了${ctx.gimg.getTimeDifference(liveTime)}`);
|
|
551
|
+
const { data } = content;
|
|
552
|
+
// B站出问题了
|
|
553
|
+
if (content.code !== 0) {
|
|
554
|
+
if (content.msg === '未找到该房间') {
|
|
555
|
+
await bot.sendMessage(guildId, '未找到该房间,请检查房间号后重新订阅');
|
|
515
556
|
}
|
|
516
|
-
|
|
517
|
-
|
|
557
|
+
else {
|
|
558
|
+
await bot.sendMessage(guildId, '未知错误,请呼叫管理员检查问题后重新订阅');
|
|
559
|
+
}
|
|
560
|
+
// dispose
|
|
561
|
+
this.unsubSingle(ctx, roomId, 0); /* 0为取消Live订阅 */
|
|
562
|
+
return;
|
|
518
563
|
}
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
564
|
+
if (firstSubscription) {
|
|
565
|
+
firstSubscription = false;
|
|
566
|
+
// 获取主播信息
|
|
567
|
+
let userData;
|
|
568
|
+
try {
|
|
569
|
+
const { data: userInfo } = await ctx.biliAPI.getMasterInfo(data.uid);
|
|
570
|
+
userData = userInfo;
|
|
571
|
+
}
|
|
572
|
+
catch (e) {
|
|
573
|
+
return this.logger.error('liveDetect first sub getMasterInfo() 网络请求错误');
|
|
574
|
+
}
|
|
575
|
+
// 主播信息不会变,请求一次即可
|
|
576
|
+
uData = userData;
|
|
577
|
+
// 判断直播状态
|
|
578
|
+
if (data.live_status === 1) { // 当前正在直播
|
|
579
|
+
// 推送直播信息
|
|
580
|
+
await bot.sendMessage(guildId, await ctx
|
|
581
|
+
.gimg
|
|
582
|
+
.generateLiveImg(data, uData, LiveType.LiveBroadcast));
|
|
583
|
+
// 改变开播状态
|
|
522
584
|
open = true;
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
585
|
+
} // 未开播,直接返回
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
// 检查直播状态
|
|
589
|
+
switch (data.live_status) {
|
|
590
|
+
case 0:
|
|
591
|
+
case 2: { // 状态 0 和 2 说明未开播
|
|
592
|
+
if (open) { // 之前开播,现在下播了
|
|
593
|
+
// 更改直播状态
|
|
594
|
+
open = false;
|
|
595
|
+
// 下播了将定时器清零
|
|
596
|
+
timer = 0;
|
|
597
|
+
// 发送下播通知
|
|
598
|
+
bot.sendMessage(guildId, `${uData.info.uname}下播啦,本次直播了${ctx.gimg.getTimeDifference(liveTime)}`);
|
|
599
|
+
}
|
|
600
|
+
// 未进循环,还未开播,继续循环
|
|
601
|
+
break;
|
|
531
602
|
}
|
|
532
|
-
|
|
533
|
-
if (
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
603
|
+
case 1: {
|
|
604
|
+
if (!open) { // 之前未开播,现在开播了
|
|
605
|
+
// 更改直播状态
|
|
606
|
+
open = true;
|
|
607
|
+
// 设置开播时间
|
|
608
|
+
liveTime = data.live_time;
|
|
609
|
+
// 获取主播信息
|
|
610
|
+
let userData;
|
|
611
|
+
try {
|
|
612
|
+
const { data: userInfo } = await ctx.biliAPI.getMasterInfo(data.uid);
|
|
613
|
+
userData = userInfo;
|
|
543
614
|
}
|
|
615
|
+
catch (e) {
|
|
616
|
+
return this.logger.error('liveDetect open getMasterInfo() 网络请求错误');
|
|
617
|
+
}
|
|
618
|
+
// 主播信息不会变,开播时刷新一次即可
|
|
619
|
+
uData = userData;
|
|
620
|
+
// 发送直播通知
|
|
621
|
+
await bot.sendMessage(guildId, await ctx.gimg.generateLiveImg(data, uData, LiveType.StartBroadcasting));
|
|
622
|
+
}
|
|
623
|
+
else { // 还在直播
|
|
624
|
+
if (this.config.pushTime > 0) {
|
|
625
|
+
timer++;
|
|
626
|
+
// 开始记录时间
|
|
627
|
+
if (timer >= (12 * 30 * this.config.pushTime)) { // 到时间推送直播消息
|
|
628
|
+
// 到时间重新计时
|
|
629
|
+
timer = 0;
|
|
630
|
+
// 发送状态信息
|
|
631
|
+
bot.sendMessage(guildId, await ctx
|
|
632
|
+
.gimg
|
|
633
|
+
.generateLiveImg(data, uData, LiveType.LiveBroadcast));
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
// 否则继续循环
|
|
544
637
|
}
|
|
545
|
-
// 否则继续循环
|
|
546
638
|
}
|
|
547
639
|
}
|
|
548
640
|
}
|
|
641
|
+
finally {
|
|
642
|
+
// 执行完方法体不论如何都把flag设置为true
|
|
643
|
+
flag = true;
|
|
644
|
+
}
|
|
549
645
|
};
|
|
550
646
|
}
|
|
551
647
|
async checkIfNeedSub(comNeed, subType, session, data) {
|
|
@@ -614,26 +710,86 @@ class ComRegister {
|
|
|
614
710
|
// 判断需要订阅的服务
|
|
615
711
|
if (sub.dynamic) { // 需要订阅动态
|
|
616
712
|
// 开始循环检测
|
|
617
|
-
const dispose = ctx.setInterval(this.dynamicDetect(ctx, bot, sub.targetId, sub.uid),
|
|
713
|
+
const dispose = ctx.setInterval(this.dynamicDetect(ctx, bot, sub.targetId, sub.uid), this.config.dynamicLoopTime * 1000);
|
|
618
714
|
// 保存销毁函数
|
|
619
715
|
subManagerItem.dynamicDispose = dispose;
|
|
620
716
|
}
|
|
621
717
|
if (sub.live) { // 需要订阅动态
|
|
622
718
|
// 开始循环检测
|
|
623
|
-
const dispose = ctx.setInterval(this.liveDetect(ctx, bot, sub.targetId, sub.room_id),
|
|
719
|
+
const dispose = ctx.setInterval(this.liveDetect(ctx, bot, sub.targetId, sub.room_id), this.config.liveLoopTime * 1000);
|
|
624
720
|
// 保存销毁函数
|
|
625
721
|
subManagerItem.liveDispose = dispose;
|
|
626
722
|
}
|
|
627
723
|
// 保存新订阅对象
|
|
628
724
|
this.subManager.push(subManagerItem);
|
|
629
725
|
// 发送订阅成功通知
|
|
630
|
-
bot.sendMessage(sub.targetId, '重新加载测试发送');
|
|
631
726
|
});
|
|
632
727
|
}
|
|
728
|
+
unsubSingle(ctx, id /* UID或RoomId */, type /* 0取消Live订阅,1取消Dynamic订阅 */) {
|
|
729
|
+
let index;
|
|
730
|
+
switch (type) {
|
|
731
|
+
case 0: { // 取消Live订阅
|
|
732
|
+
index = this.subManager.findIndex(sub => sub.roomId === id);
|
|
733
|
+
if (index === -1)
|
|
734
|
+
return '未订阅该用户,无需取消订阅';
|
|
735
|
+
// 取消订阅
|
|
736
|
+
this.subManager[index].live && this.subManager[index].liveDispose();
|
|
737
|
+
// 如果没有对这个UP的任何订阅,则移除
|
|
738
|
+
if (!this.subManager[index].dynamic) {
|
|
739
|
+
// 获取要删除行的id
|
|
740
|
+
const id = this.subManager[index].id;
|
|
741
|
+
// 从管理对象中移除
|
|
742
|
+
this.subManager = this.subManager.splice(index, index);
|
|
743
|
+
// 从数据库中删除
|
|
744
|
+
ctx.database.remove('bilibili', [id]);
|
|
745
|
+
// num--
|
|
746
|
+
this.num--;
|
|
747
|
+
return '已取消订阅该用户';
|
|
748
|
+
}
|
|
749
|
+
this.subManager[index].liveDispose = null;
|
|
750
|
+
this.subManager[index].live = false;
|
|
751
|
+
// 更新数据库
|
|
752
|
+
ctx.database.upsert('bilibili', [{
|
|
753
|
+
id: +`${this.subManager[index].id}`,
|
|
754
|
+
live: 0
|
|
755
|
+
}]);
|
|
756
|
+
return '已取消订阅Live';
|
|
757
|
+
}
|
|
758
|
+
case 1: { // 取消Dynamic订阅
|
|
759
|
+
index = this.subManager.findIndex(sub => sub.uid === id);
|
|
760
|
+
if (index === -1)
|
|
761
|
+
return '未订阅该用户,无需取消订阅';
|
|
762
|
+
// 取消订阅
|
|
763
|
+
this.subManager[index].dynamic && this.subManager[index].dynamicDispose();
|
|
764
|
+
// 如果没有对这个UP的任何订阅,则移除
|
|
765
|
+
if (!this.subManager[index].live) {
|
|
766
|
+
// 获取要删除行的id
|
|
767
|
+
const id = this.subManager[index].id;
|
|
768
|
+
// 从管理对象中移除
|
|
769
|
+
this.subManager = this.subManager.splice(index, index);
|
|
770
|
+
// 从数据库中删除
|
|
771
|
+
ctx.database.remove('bilibili', [id]);
|
|
772
|
+
// num--
|
|
773
|
+
this.num--;
|
|
774
|
+
return '已取消订阅该用户';
|
|
775
|
+
}
|
|
776
|
+
this.subManager[index].dynamicDispose = null;
|
|
777
|
+
this.subManager[index].dynamic = false;
|
|
778
|
+
// 更新数据库
|
|
779
|
+
ctx.database.upsert('bilibili', [{
|
|
780
|
+
id: +`${this.subManager[index].id}`,
|
|
781
|
+
dynamic: 0
|
|
782
|
+
}]);
|
|
783
|
+
return '已取消订阅Dynamic';
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
}
|
|
633
787
|
}
|
|
634
788
|
(function (ComRegister) {
|
|
635
789
|
ComRegister.Config = koishi_1.Schema.object({
|
|
636
|
-
pushTime: koishi_1.Schema.number().required()
|
|
790
|
+
pushTime: koishi_1.Schema.number().required(),
|
|
791
|
+
liveLoopTime: koishi_1.Schema.number().default(5),
|
|
792
|
+
dynamicLoopTime: koishi_1.Schema.number().default(60)
|
|
637
793
|
});
|
|
638
794
|
})(ComRegister || (ComRegister = {}));
|
|
639
795
|
exports.default = ComRegister;
|
package/lib/index.js
CHANGED
|
@@ -31,6 +31,7 @@ const koishi_1 = require("koishi");
|
|
|
31
31
|
// import crypto
|
|
32
32
|
const crypto_1 = __importDefault(require("crypto"));
|
|
33
33
|
// import plugins
|
|
34
|
+
const authority_1 = __importDefault(require("./authority"));
|
|
34
35
|
const comRegister_1 = __importDefault(require("./comRegister"));
|
|
35
36
|
const Database = __importStar(require("./database"));
|
|
36
37
|
// import Service
|
|
@@ -68,6 +69,7 @@ function apply(ctx, config) {
|
|
|
68
69
|
ctx.plugin(generateImg_1.default, { cardColorStart: config.cardColorStart, cardColorEnd: config.cardColorEnd });
|
|
69
70
|
ctx.plugin(biliAPI_1.default);
|
|
70
71
|
// load plugin
|
|
72
|
+
ctx.plugin(authority_1.default);
|
|
71
73
|
ctx.plugin(comRegister_1.default, { pushTime: config.pushTime });
|
|
72
74
|
// 当用户输入“恶魔兔,启动!”时,执行 help 指令
|
|
73
75
|
ctx.middleware((session, next) => {
|