koishi-plugin-bilibili-notify 2.0.0-alpha.8 → 3.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/biliAPI.d.ts +17 -0
- package/lib/biliAPI.js +41 -0
- package/lib/blive.d.ts +24 -0
- package/lib/blive.js +81 -0
- package/lib/comRegister.d.ts +63 -23
- package/lib/comRegister.js +745 -414
- package/lib/database.d.ts +0 -1
- package/lib/database.js +0 -1
- package/lib/generateImg.js +3 -3
- package/lib/index.d.ts +17 -2
- package/lib/index.js +34 -12
- package/package.json +2 -1
- package/{README.md → readme.md} +26 -4
package/lib/biliAPI.d.ts
CHANGED
|
@@ -26,6 +26,22 @@ declare class BiliAPI extends Service {
|
|
|
26
26
|
}): Promise<string>;
|
|
27
27
|
encrypt(text: string): string;
|
|
28
28
|
decrypt(text: string): string;
|
|
29
|
+
getTheUserWhoIsLiveStreaming(): Promise<{
|
|
30
|
+
count: number;
|
|
31
|
+
group: string;
|
|
32
|
+
items: [
|
|
33
|
+
{
|
|
34
|
+
face: string;
|
|
35
|
+
is_reserve_recall: boolean;
|
|
36
|
+
jump_url: string;
|
|
37
|
+
mid: number;
|
|
38
|
+
room_id: number;
|
|
39
|
+
title: string;
|
|
40
|
+
uname: string;
|
|
41
|
+
}
|
|
42
|
+
];
|
|
43
|
+
}>;
|
|
44
|
+
getLiveRoomInfoStreamKey(roomId: string): Promise<any>;
|
|
29
45
|
getServerUTCTime(): Promise<number>;
|
|
30
46
|
getTimeNow(): Promise<any>;
|
|
31
47
|
getAllGroup(): Promise<any>;
|
|
@@ -53,6 +69,7 @@ declare class BiliAPI extends Service {
|
|
|
53
69
|
createNewClient(): void;
|
|
54
70
|
getTimeOfUTC8(): number;
|
|
55
71
|
getCookies(): string;
|
|
72
|
+
getCookiesForHeader(): Promise<string>;
|
|
56
73
|
getLoginInfoIsLoaded(): boolean;
|
|
57
74
|
getLoginInfoFromDB(): Promise<{
|
|
58
75
|
cookies: any;
|
package/lib/biliAPI.js
CHANGED
|
@@ -34,6 +34,8 @@ const GET_LIVE_ROOM_INFO = 'https://api.live.bilibili.com/room/v1/Room/get_info'
|
|
|
34
34
|
const GET_MASTER_INFO = 'https://api.live.bilibili.com/live_user/v1/Master/info';
|
|
35
35
|
const GET_TIME_NOW = 'https://api.bilibili.com/x/report/click/now';
|
|
36
36
|
const GET_SERVER_UTC_TIME = 'https://interface.bilibili.com/serverdate.js';
|
|
37
|
+
// 最近更新UP
|
|
38
|
+
const GET_LATEST_UPDATED_UPS = 'https://api.bilibili.com/x/polymer/web-dynamic/v1/portal';
|
|
37
39
|
// 操作
|
|
38
40
|
const MODIFY_RELATION = 'https://api.bilibili.com/x/relation/modify';
|
|
39
41
|
const CREATE_GROUP = 'https://api.bilibili.com/x/relation/tag/create';
|
|
@@ -41,6 +43,8 @@ const MODIFY_GROUP_MEMBER = 'https://api.bilibili.com/x/relation/tags/addUsers';
|
|
|
41
43
|
const GET_ALL_GROUP = 'https://api.bilibili.com/x/relation/tags';
|
|
42
44
|
const COPY_USER_TO_GROUP = 'https://api.bilibili.com/x/relation/tags/copyUsers';
|
|
43
45
|
const GET_RELATION_GROUP_DETAIL = 'https://api.bilibili.com/x/relation/tag';
|
|
46
|
+
// 直播
|
|
47
|
+
const GET_LIVE_ROOM_INFO_STREAM_KEY = 'https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo';
|
|
44
48
|
class BiliAPI extends koishi_1.Service {
|
|
45
49
|
static inject = ['database', 'notifier'];
|
|
46
50
|
jar;
|
|
@@ -103,6 +107,28 @@ class BiliAPI extends koishi_1.Service {
|
|
|
103
107
|
return decrypted.toString();
|
|
104
108
|
}
|
|
105
109
|
// BA API
|
|
110
|
+
async getTheUserWhoIsLiveStreaming() {
|
|
111
|
+
try {
|
|
112
|
+
// 获取直播间信息流密钥
|
|
113
|
+
const { data: { live_users } } = await this.client.get(GET_LATEST_UPDATED_UPS);
|
|
114
|
+
// 返回data
|
|
115
|
+
return live_users;
|
|
116
|
+
}
|
|
117
|
+
catch (e) {
|
|
118
|
+
throw new Error('网络异常,本次请求失败!');
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
async getLiveRoomInfoStreamKey(roomId) {
|
|
122
|
+
try {
|
|
123
|
+
// 获取直播间信息流密钥
|
|
124
|
+
const { data } = await this.client.get(`${GET_LIVE_ROOM_INFO_STREAM_KEY}?id=${roomId}`);
|
|
125
|
+
// 返回data
|
|
126
|
+
return data;
|
|
127
|
+
}
|
|
128
|
+
catch (e) {
|
|
129
|
+
throw new Error('网络异常,本次请求失败!');
|
|
130
|
+
}
|
|
131
|
+
}
|
|
106
132
|
async getServerUTCTime() {
|
|
107
133
|
try {
|
|
108
134
|
const { data } = await this.client.get(GET_SERVER_UTC_TIME);
|
|
@@ -362,6 +388,21 @@ class BiliAPI extends koishi_1.Service {
|
|
|
362
388
|
const cookies = JSON.stringify(this.jar.serializeSync().cookies);
|
|
363
389
|
return cookies;
|
|
364
390
|
}
|
|
391
|
+
async getCookiesForHeader() {
|
|
392
|
+
try {
|
|
393
|
+
// 获取cookies对象
|
|
394
|
+
const cookies = this.jar.serializeSync().cookies;
|
|
395
|
+
// 将每个 cookie 对象转换为 "key=value" 形式,并用 "; " 连接起来
|
|
396
|
+
const cookieHeader = cookies
|
|
397
|
+
.map(cookie => `${cookie.key}=${cookie.value}`)
|
|
398
|
+
.join('; ');
|
|
399
|
+
return cookieHeader;
|
|
400
|
+
}
|
|
401
|
+
catch (e) {
|
|
402
|
+
console.error("无效的 JSON 格式:", e);
|
|
403
|
+
return "";
|
|
404
|
+
}
|
|
405
|
+
}
|
|
365
406
|
getLoginInfoIsLoaded() {
|
|
366
407
|
return this.loginInfoIsLoaded;
|
|
367
408
|
}
|
package/lib/blive.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Awaitable, Context, Schema, Service } from "koishi";
|
|
2
|
+
import { type MsgHandler } from 'blive-message-listener';
|
|
3
|
+
declare module 'koishi' {
|
|
4
|
+
interface Context {
|
|
5
|
+
bl: BLive;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
declare class BLive extends Service {
|
|
9
|
+
static inject: string[];
|
|
10
|
+
private blConfig;
|
|
11
|
+
private listenerRecord;
|
|
12
|
+
private timerRecord;
|
|
13
|
+
constructor(ctx: Context, config: BLive.Config);
|
|
14
|
+
protected stop(): Awaitable<void>;
|
|
15
|
+
startLiveRoomListener(roomId: string, handler: MsgHandler, danmakuPushTime: () => void): Promise<void>;
|
|
16
|
+
closeListener(roomId: string): void;
|
|
17
|
+
}
|
|
18
|
+
declare namespace BLive {
|
|
19
|
+
interface Config {
|
|
20
|
+
danmakuPushTime: number;
|
|
21
|
+
}
|
|
22
|
+
const Config: Schema<Config>;
|
|
23
|
+
}
|
|
24
|
+
export default BLive;
|
package/lib/blive.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const koishi_1 = require("koishi");
|
|
4
|
+
const blive_message_listener_1 = require("blive-message-listener");
|
|
5
|
+
class BLive extends koishi_1.Service {
|
|
6
|
+
// 必要服务
|
|
7
|
+
static inject = ['ba'];
|
|
8
|
+
// 配置
|
|
9
|
+
blConfig;
|
|
10
|
+
// 定义类属性
|
|
11
|
+
listenerRecord = {};
|
|
12
|
+
timerRecord = {};
|
|
13
|
+
constructor(ctx, config) {
|
|
14
|
+
// Extends super
|
|
15
|
+
super(ctx, 'bl');
|
|
16
|
+
// 将config赋值给类属性
|
|
17
|
+
this.blConfig = config;
|
|
18
|
+
}
|
|
19
|
+
// 注册插件dispose逻辑
|
|
20
|
+
stop() {
|
|
21
|
+
// 清除所有监听器
|
|
22
|
+
for (const key of Object.keys(this.listenerRecord)) {
|
|
23
|
+
this.closeListener(key);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
async startLiveRoomListener(roomId, handler, danmakuPushTime) {
|
|
27
|
+
// 获取cookieStr
|
|
28
|
+
const cookiesStr = await this.ctx.ba.getCookiesForHeader();
|
|
29
|
+
// 获取自身信息
|
|
30
|
+
const mySelfInfo = await this.ctx.ba.getMyselfInfo();
|
|
31
|
+
// 创建实例并保存到Record中
|
|
32
|
+
this.listenerRecord[roomId] = (0, blive_message_listener_1.startListen)(parseInt(roomId), handler, {
|
|
33
|
+
ws: {
|
|
34
|
+
headers: {
|
|
35
|
+
Cookie: cookiesStr
|
|
36
|
+
},
|
|
37
|
+
uid: mySelfInfo.data.mid
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
// 默认30s推送一次弹幕消息到群组并将dispose函数保存到Record中
|
|
41
|
+
this.timerRecord[roomId] = this.ctx.setInterval(danmakuPushTime, this.blConfig.danmakuPushTime * 1000 * 60);
|
|
42
|
+
// logger
|
|
43
|
+
this.logger.info(`${roomId}直播间弹幕监听已开启`);
|
|
44
|
+
}
|
|
45
|
+
closeListener(roomId) {
|
|
46
|
+
// 判断直播间监听器是否关闭
|
|
47
|
+
if (!this.listenerRecord || !this.listenerRecord[roomId] || !this.listenerRecord[roomId].closed) {
|
|
48
|
+
// 输出logger
|
|
49
|
+
this.logger.info(`${roomId}直播间弹幕监听器无需关闭`);
|
|
50
|
+
}
|
|
51
|
+
// 判断消息发送定时器是否关闭
|
|
52
|
+
if (!this.timerRecord || !this.timerRecord[roomId]) {
|
|
53
|
+
// 输出logger
|
|
54
|
+
this.logger.info(`${roomId}直播间消息发送定时器无需关闭`);
|
|
55
|
+
}
|
|
56
|
+
// 关闭直播间监听器
|
|
57
|
+
this.listenerRecord[roomId].close();
|
|
58
|
+
// 关闭消息发送定时器
|
|
59
|
+
this.timerRecord[roomId]();
|
|
60
|
+
// 判断是否关闭成功
|
|
61
|
+
if (this.listenerRecord[roomId].closed) {
|
|
62
|
+
// 删除直播间监听器
|
|
63
|
+
delete this.listenerRecord[roomId];
|
|
64
|
+
// 删除消息发送定时器
|
|
65
|
+
delete this.timerRecord[roomId];
|
|
66
|
+
// 输出logger
|
|
67
|
+
this.logger.info(`${roomId}直播间弹幕监听已关闭`);
|
|
68
|
+
// 直接返回
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
// 未关闭成功
|
|
72
|
+
this.logger.warn(`${roomId}直播间弹幕监听未成功关闭`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
// eslint-disable-next-line @typescript-eslint/no-namespace
|
|
76
|
+
(function (BLive) {
|
|
77
|
+
BLive.Config = koishi_1.Schema.object({
|
|
78
|
+
danmakuPushTime: koishi_1.Schema.number().required()
|
|
79
|
+
});
|
|
80
|
+
})(BLive || (BLive = {}));
|
|
81
|
+
exports.default = BLive;
|
package/lib/comRegister.d.ts
CHANGED
|
@@ -1,8 +1,21 @@
|
|
|
1
1
|
import { Bot, Context, FlatPick, Logger, Schema, Session } from "koishi";
|
|
2
2
|
import { Notifier } from "@koishijs/plugin-notifier";
|
|
3
3
|
import { LoginBili } from "./database";
|
|
4
|
+
declare enum LiveType {
|
|
5
|
+
NotLiveBroadcast = 0,
|
|
6
|
+
StartBroadcasting = 1,
|
|
7
|
+
LiveBroadcast = 2,
|
|
8
|
+
StopBroadcast = 3
|
|
9
|
+
}
|
|
10
|
+
type ChannelIdArr = Array<{
|
|
11
|
+
channelId: string;
|
|
12
|
+
dynamic: boolean;
|
|
13
|
+
live: boolean;
|
|
14
|
+
liveDanmaku: boolean;
|
|
15
|
+
atAll: boolean;
|
|
16
|
+
}>;
|
|
4
17
|
type TargetItem = {
|
|
5
|
-
|
|
18
|
+
channelIdArr: ChannelIdArr;
|
|
6
19
|
platform: string;
|
|
7
20
|
};
|
|
8
21
|
type Target = Array<TargetItem>;
|
|
@@ -14,7 +27,6 @@ type SubItem = {
|
|
|
14
27
|
platform: string;
|
|
15
28
|
live: boolean;
|
|
16
29
|
dynamic: boolean;
|
|
17
|
-
liveDispose: Function;
|
|
18
30
|
};
|
|
19
31
|
type SubManager = Array<SubItem>;
|
|
20
32
|
declare class ComRegister {
|
|
@@ -26,40 +38,70 @@ declare class ComRegister {
|
|
|
26
38
|
num: number;
|
|
27
39
|
rebootCount: number;
|
|
28
40
|
subNotifier: Notifier;
|
|
41
|
+
ctx: Context;
|
|
29
42
|
subManager: SubManager;
|
|
30
43
|
loginDBData: FlatPick<LoginBili, "dynamic_group_id">;
|
|
31
44
|
privateBot: Bot<Context>;
|
|
32
45
|
dynamicDispose: Function;
|
|
33
|
-
sendMsgFunc: (bot: Bot<Context, any>,
|
|
46
|
+
sendMsgFunc: (bot: Bot<Context, any>, channelId: string, content: any) => Promise<void>;
|
|
34
47
|
constructor(ctx: Context, config: ComRegister.Config);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}>;
|
|
39
|
-
getBot(ctx: Context, pf: string): Bot<Context, any>;
|
|
48
|
+
init(config: ComRegister.Config): Promise<void>;
|
|
49
|
+
splitMultiPlatformStr(str: string): Target;
|
|
50
|
+
getBot(pf: string): Bot<Context, any>;
|
|
40
51
|
sendPrivateMsg(content: string): Promise<void>;
|
|
41
|
-
sendPrivateMsgAndRebootService(
|
|
42
|
-
sendPrivateMsgAndStopService(
|
|
43
|
-
sendMsg(
|
|
44
|
-
dynamicDetect(
|
|
45
|
-
debug_dynamicDetect(
|
|
46
|
-
|
|
52
|
+
sendPrivateMsgAndRebootService(): Promise<void>;
|
|
53
|
+
sendPrivateMsgAndStopService(): Promise<void>;
|
|
54
|
+
sendMsg(targets: Target, content: any, live?: boolean): Promise<void>;
|
|
55
|
+
dynamicDetect(): () => Promise<void>;
|
|
56
|
+
debug_dynamicDetect(): () => Promise<void>;
|
|
57
|
+
sendLiveNotifyCard(info: {
|
|
58
|
+
username: string;
|
|
59
|
+
userface: string;
|
|
60
|
+
target: Target;
|
|
61
|
+
data: any;
|
|
62
|
+
}, liveType: LiveType, liveNotifyMsg?: string): Promise<void>;
|
|
63
|
+
useMasterInfo(uid: string): Promise<{
|
|
64
|
+
username: string;
|
|
65
|
+
userface: string;
|
|
66
|
+
roomId: number;
|
|
67
|
+
}>;
|
|
68
|
+
useLiveRoomInfo(roomId: string): Promise<any>;
|
|
69
|
+
liveDetectWithAPI(): Promise<() => Promise<void>>;
|
|
70
|
+
liveDetectWithListener(roomId: string, target: Target): Promise<void>;
|
|
47
71
|
subShow(): string;
|
|
48
72
|
checkIfNeedSub(liveSub: boolean, dynamicSub: boolean, session: Session, liveRoomData: any): Promise<Array<boolean>>;
|
|
49
|
-
updateSubNotifier(
|
|
50
|
-
checkIfLoginInfoIsLoaded(
|
|
51
|
-
subUserInBili(
|
|
73
|
+
updateSubNotifier(): void;
|
|
74
|
+
checkIfLoginInfoIsLoaded(): Promise<unknown>;
|
|
75
|
+
subUserInBili(mid: string): Promise<{
|
|
52
76
|
flag: boolean;
|
|
53
77
|
msg: string;
|
|
54
78
|
}>;
|
|
55
|
-
|
|
56
|
-
|
|
79
|
+
loadSubFromConfig(subs: ComRegister.Config["sub"]): Promise<void>;
|
|
80
|
+
loadSubFromDatabase(): Promise<void>;
|
|
81
|
+
checkIfDynamicDetectIsNeeded(): void;
|
|
82
|
+
enableDynamicDetect(): void;
|
|
83
|
+
unsubSingle(id: string, type: number): string;
|
|
57
84
|
checkIfUserIsTheLastOneWhoSubDyn(): void;
|
|
58
|
-
unsubAll(
|
|
59
|
-
checkIfIsLogin(
|
|
85
|
+
unsubAll(uid: string): void;
|
|
86
|
+
checkIfIsLogin(): Promise<boolean>;
|
|
60
87
|
}
|
|
61
88
|
declare namespace ComRegister {
|
|
62
89
|
interface Config {
|
|
90
|
+
sub: Array<{
|
|
91
|
+
uid: string;
|
|
92
|
+
dynamic: boolean;
|
|
93
|
+
live: boolean;
|
|
94
|
+
target: Array<{
|
|
95
|
+
channelIdArr: Array<{
|
|
96
|
+
channelId: string;
|
|
97
|
+
dynamic: boolean;
|
|
98
|
+
live: boolean;
|
|
99
|
+
liveDanmaku: boolean;
|
|
100
|
+
atAll: boolean;
|
|
101
|
+
}>;
|
|
102
|
+
platform: string;
|
|
103
|
+
}>;
|
|
104
|
+
}>;
|
|
63
105
|
master: {
|
|
64
106
|
enable: boolean;
|
|
65
107
|
platform: string;
|
|
@@ -68,8 +110,6 @@ declare namespace ComRegister {
|
|
|
68
110
|
};
|
|
69
111
|
unlockSubLimits: boolean;
|
|
70
112
|
automaticResend: boolean;
|
|
71
|
-
changeMasterInfoApi: boolean;
|
|
72
|
-
liveStartAtAll: boolean;
|
|
73
113
|
restartPush: boolean;
|
|
74
114
|
pushTime: number;
|
|
75
115
|
liveLoopTime: number;
|