@alemonjs/qq-bot 2.1.33 → 2.1.34
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/README.md +46 -8
- package/dist/assets/index.css +1 -1
- package/dist/assets/index.js +12 -5
- package/dist/index.html +1 -1
- package/lib/config.d.ts +44 -19
- package/lib/config.js +39 -35
- package/lib/desktop.d.ts +4 -1
- package/lib/desktop.js +53 -57
- package/lib/format.d.ts +26 -5
- package/lib/format.js +84 -101
- package/lib/hook.d.ts +82 -65
- package/lib/hook.js +35 -32
- package/lib/index.d.ts +6 -4
- package/lib/index.js +13 -16
- package/lib/index.webhook.d.ts +4 -1
- package/lib/index.webhook.js +12 -14
- package/lib/index.websoket.d.ts +6 -3
- package/lib/index.websoket.js +131 -41
- package/lib/message/AT_MESSAGE_CREATE.d.ts +36 -33
- package/lib/message/AT_MESSAGE_CREATE.js +1 -1
- package/lib/message/CHANNEL_CREATE.d.ts +17 -14
- package/lib/message/CHANNEL_CREATE.js +1 -1
- package/lib/message/CHANNEL_DELETE.d.ts +22 -14
- package/lib/message/CHANNEL_DELETE.js +1 -1
- package/lib/message/CHANNEL_UPDATE.d.ts +22 -14
- package/lib/message/CHANNEL_UPDATE.js +1 -1
- package/lib/message/DIRECT_MESSAGE_CREATE.d.ts +35 -27
- package/lib/message/DIRECT_MESSAGE_CREATE.js +1 -1
- package/lib/message/DIRECT_MESSAGE_DELETE.d.ts +23 -15
- package/lib/message/DIRECT_MESSAGE_DELETE.js +1 -1
- package/lib/message/ERROR.d.ts +4 -1
- package/lib/message/ERROR.js +1 -1
- package/lib/message/GUILD_CREATE.d.ts +22 -14
- package/lib/message/GUILD_CREATE.js +1 -1
- package/lib/message/GUILD_DELETE.d.ts +22 -14
- package/lib/message/GUILD_DELETE.js +1 -1
- package/lib/message/GUILD_MEMBER_ADD.d.ts +21 -13
- package/lib/message/GUILD_MEMBER_ADD.js +1 -1
- package/lib/message/GUILD_MEMBER_REMOVE.d.ts +21 -13
- package/lib/message/GUILD_MEMBER_REMOVE.js +1 -1
- package/lib/message/GUILD_MEMBER_UPDATE.d.ts +21 -13
- package/lib/message/GUILD_MEMBER_UPDATE.js +1 -1
- package/lib/message/GUILD_UPDATE.d.ts +22 -14
- package/lib/message/GUILD_UPDATE.js +1 -1
- package/lib/message/INTERACTION_CREATE.d.ts +52 -44
- package/lib/message/INTERACTION_CREATE.js +1 -1
- package/lib/message/MESSAGE_CREATE.d.ts +12 -1
- package/lib/message/MESSAGE_CREATE.js +1 -1
- package/lib/message/MESSAGE_DELETE.d.ts +12 -1
- package/lib/message/MESSAGE_DELETE.js +1 -1
- package/lib/message/MESSAGE_REACTION_ADD.d.ts +15 -12
- package/lib/message/MESSAGE_REACTION_ADD.js +1 -1
- package/lib/message/MESSAGE_REACTION_REMOVE.d.ts +15 -12
- package/lib/message/MESSAGE_REACTION_REMOVE.js +1 -1
- package/lib/message/PUBLIC_MESSAGE_DELETE.d.ts +20 -13
- package/lib/message/PUBLIC_MESSAGE_DELETE.js +1 -1
- package/lib/message/READY.d.ts +11 -5
- package/lib/message/READY.js +1 -1
- package/lib/message/group/C2C_MESSAGE_CREATE.d.ts +18 -15
- package/lib/message/group/C2C_MESSAGE_CREATE.js +1 -1
- package/lib/message/group/C2C_MSG_RECEIVE.d.ts +13 -3
- package/lib/message/group/C2C_MSG_RECEIVE.js +1 -1
- package/lib/message/group/C2C_MSG_REJECT.d.ts +9 -3
- package/lib/message/group/C2C_MSG_REJECT.js +1 -1
- package/lib/message/group/FRIEND_ADD.d.ts +13 -3
- package/lib/message/group/FRIEND_ADD.js +1 -1
- package/lib/message/group/FRIEND_DEL.d.ts +9 -3
- package/lib/message/group/FRIEND_DEL.js +1 -1
- package/lib/message/group/GROUP_ADD_ROBOT.d.ts +23 -4
- package/lib/message/group/GROUP_ADD_ROBOT.js +1 -1
- package/lib/message/group/GROUP_AT_MESSAGE_CREATE.d.ts +38 -32
- package/lib/message/group/GROUP_AT_MESSAGE_CREATE.js +1 -1
- package/lib/message/group/GROUP_DEL_ROBOT.d.ts +19 -4
- package/lib/message/group/GROUP_DEL_ROBOT.js +1 -1
- package/lib/message/group/GROUP_JOIN_REQUEST.d.ts +83 -22
- package/lib/message/group/GROUP_JOIN_REQUEST.js +1 -1
- package/lib/message/group/GROUP_MEMBER_ADD.d.ts +8 -5
- package/lib/message/group/GROUP_MEMBER_ADD.js +1 -1
- package/lib/message/group/GROUP_MEMBER_REMOVE.d.ts +8 -5
- package/lib/message/group/GROUP_MEMBER_REMOVE.js +1 -1
- package/lib/message/group/GROUP_MESSAGE_CREATE.d.ts +38 -32
- package/lib/message/group/GROUP_MESSAGE_CREATE.js +1 -1
- package/lib/message/group/GROUP_MSG_RECEIVE.d.ts +14 -4
- package/lib/message/group/GROUP_MSG_RECEIVE.js +1 -1
- package/lib/message/group/GROUP_MSG_REJECT.d.ts +10 -4
- package/lib/message/group/GROUP_MSG_REJECT.js +1 -1
- package/lib/message/group/MESSAGE_AUDIT_PASS.d.ts +12 -6
- package/lib/message/group/MESSAGE_AUDIT_PASS.js +1 -1
- package/lib/message/group/MESSAGE_AUDIT_REJECT.d.ts +12 -6
- package/lib/message/group/MESSAGE_AUDIT_REJECT.js +1 -1
- package/lib/register.d.ts +12 -9
- package/lib/register.js +1246 -1404
- package/lib/sdk/api.d.ts +937 -306
- package/lib/sdk/api.js +1481 -893
- package/lib/sdk/client.webhook.d.ts +25 -8
- package/lib/sdk/client.webhook.js +185 -189
- package/lib/sdk/client.websoket.d.ts +31 -9
- package/lib/sdk/client.websoket.js +369 -392
- package/lib/sdk/config.d.ts +6 -2
- package/lib/sdk/config.js +9 -6
- package/lib/sdk/instance.d.ts +11 -3
- package/lib/sdk/instance.js +72 -84
- package/lib/sdk/intents.d.ts +89 -3
- package/lib/sdk/intents.js +116 -33
- package/lib/sdk/message.d.ts +7 -4
- package/lib/sdk/message.group.d.ts +38 -35
- package/lib/sdk/message.group.js +1 -1
- package/lib/sdk/message.guild.d.ts +40 -37
- package/lib/sdk/message.guild.js +1 -1
- package/lib/sdk/message.js +1 -1
- package/lib/sdk/message.public.d.ts +8 -5
- package/lib/sdk/message.public.js +1 -1
- package/lib/sdk/qr-auth.d.ts +55 -0
- package/lib/sdk/qr-auth.js +169 -0
- package/lib/sdk/registry.d.ts +34 -24
- package/lib/sdk/registry.js +134 -114
- package/lib/sdk/session.d.ts +19 -15
- package/lib/sdk/session.js +51 -54
- package/lib/sdk/typing.d.ts +410 -184
- package/lib/sdk/typing.js +1 -1
- package/lib/sdk/webhook.secret.d.ts +33 -13
- package/lib/sdk/webhook.secret.js +50 -36
- package/lib/sends.d.ts +42 -25
- package/lib/sends.js +519 -541
- package/lib/types.d.ts +52 -41
- package/lib/types.js +1 -1
- package/lib/upload.d.ts +47 -18
- package/lib/upload.js +80 -51
- package/lib/utils.d.ts +9 -1
- package/lib/utils.js +10 -5
- package/package.json +9 -6
|
@@ -1,9 +1,26 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { Options } from "../config.js";
|
|
2
|
+
import { QQBotAPI } from "./api.js";
|
|
3
|
+
import { QQBotEventMap } from "./message.js";
|
|
4
|
+
//#region src/sdk/client.webhook.d.ts
|
|
5
|
+
declare class QQBotClient extends QQBotAPI {
|
|
6
|
+
#private;
|
|
7
|
+
/**
|
|
8
|
+
* 设置配置
|
|
9
|
+
* @param opstion
|
|
10
|
+
*/
|
|
11
|
+
constructor(opstion: Options);
|
|
12
|
+
/**
|
|
13
|
+
* 注册事件处理程序
|
|
14
|
+
* @param key 事件名称
|
|
15
|
+
* @param val 事件处理函数
|
|
16
|
+
*/
|
|
17
|
+
on<T extends keyof QQBotEventMap>(key: T, val: (event: QQBotEventMap[T]) => any): this;
|
|
18
|
+
/**
|
|
19
|
+
*
|
|
20
|
+
* @param cfg
|
|
21
|
+
* @param conversation
|
|
22
|
+
*/
|
|
23
|
+
connect(): void;
|
|
9
24
|
}
|
|
25
|
+
//#endregion
|
|
26
|
+
export { QQBotClient };
|
|
@@ -1,193 +1,189 @@
|
|
|
1
|
-
import { QQBotAPI } from
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import {
|
|
1
|
+
import { QQBotAPI } from "./api.js";
|
|
2
|
+
import { WebhookAPI } from "./webhook.secret.js";
|
|
3
|
+
import { WebSocket, WebSocketServer } from "ws";
|
|
4
|
+
import bodyParser from "koa-bodyparser";
|
|
5
|
+
import Router from "koa-router";
|
|
6
|
+
import Koa from "koa";
|
|
7
|
+
import { v4 } from "uuid";
|
|
8
8
|
|
|
9
|
+
//#region src/sdk/client.webhook.ts
|
|
9
10
|
const normalizeGatewayMessage = (message) => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
if (!message?.id || !message?.d || typeof message.d !== "object" || Array.isArray(message.d)) return message;
|
|
12
|
+
if (message.d.id === void 0 || message.d.id === null || message.d.id === "") message.d.id = message.id;
|
|
13
|
+
return message;
|
|
14
|
+
};
|
|
15
|
+
var QQBotClient = class extends QQBotAPI {
|
|
16
|
+
#events = {};
|
|
17
|
+
#app = null;
|
|
18
|
+
#count = 0;
|
|
19
|
+
#client = [];
|
|
20
|
+
#ws = null;
|
|
21
|
+
/**
|
|
22
|
+
* 设置配置
|
|
23
|
+
* @param opstion
|
|
24
|
+
*/
|
|
25
|
+
constructor(opstion) {
|
|
26
|
+
super(opstion);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* 注册事件处理程序
|
|
30
|
+
* @param key 事件名称
|
|
31
|
+
* @param val 事件处理函数
|
|
32
|
+
*/
|
|
33
|
+
on(key, val) {
|
|
34
|
+
if (!this.#events[key]) this.#events[key] = [];
|
|
35
|
+
this.#events[key].push(val);
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 定时鉴权
|
|
40
|
+
* @param cfg
|
|
41
|
+
* @returns
|
|
42
|
+
*/
|
|
43
|
+
async #setTimeoutBotConfig() {
|
|
44
|
+
const callBack = async () => {
|
|
45
|
+
const app_id = this.config.get("app_id");
|
|
46
|
+
const secret = this.config.get("secret");
|
|
47
|
+
if (!app_id || !secret) return;
|
|
48
|
+
const data = await this.getAuthentication();
|
|
49
|
+
this.config.set("access_token", data.access_token);
|
|
50
|
+
console.info("refresh", data.expires_in, "s");
|
|
51
|
+
setTimeout(() => void callBack(), data.expires_in * 1e3);
|
|
52
|
+
};
|
|
53
|
+
await callBack();
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
*
|
|
57
|
+
* @param cfg
|
|
58
|
+
* @param conversation
|
|
59
|
+
*/
|
|
60
|
+
connect() {
|
|
61
|
+
try {
|
|
62
|
+
const ws = this.config.get("ws");
|
|
63
|
+
this.updateConnectionStatus({
|
|
64
|
+
state: "connecting",
|
|
65
|
+
transport: ws ? "proxy" : "webhook"
|
|
66
|
+
});
|
|
67
|
+
if (!ws) {
|
|
68
|
+
this.#setTimeoutBotConfig();
|
|
69
|
+
this.#app = new Koa();
|
|
70
|
+
this.#app.use(bodyParser());
|
|
71
|
+
const router = new Router();
|
|
72
|
+
const port = this.config.get("port");
|
|
73
|
+
const secret = this.config.get("secret");
|
|
74
|
+
const route = this.config.get("route") ?? "/webhook";
|
|
75
|
+
const cfg = {
|
|
76
|
+
secret: secret ?? "",
|
|
77
|
+
port: port ? Number(port) : 17157
|
|
78
|
+
};
|
|
79
|
+
const ntqqWebhook = new WebhookAPI({ secret: cfg.secret });
|
|
80
|
+
this.#app.use(async (ctx, next) => {
|
|
81
|
+
let rawData = "";
|
|
82
|
+
ctx.req.on("data", (chunk) => rawData += chunk);
|
|
83
|
+
ctx.req.on("end", () => ctx.request.rawBody = rawData);
|
|
84
|
+
await next();
|
|
85
|
+
});
|
|
86
|
+
router.post(route, (ctx) => {
|
|
87
|
+
const sign = ctx.req.headers["x-signature-ed25519"];
|
|
88
|
+
const timestamp = ctx.req.headers["x-signature-timestamp"];
|
|
89
|
+
const rawBody = ctx.request.rawBody;
|
|
90
|
+
if (!ntqqWebhook.validSign(timestamp, rawBody, String(sign))) {
|
|
91
|
+
ctx.status = 400;
|
|
92
|
+
ctx.body = { msg: "invalid signature" };
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const body = normalizeGatewayMessage(ctx.request.body);
|
|
96
|
+
if (+body.op === 13) {
|
|
97
|
+
ctx.status = 200;
|
|
98
|
+
ctx.body = {
|
|
99
|
+
plain_token: body.d.plain_token,
|
|
100
|
+
signature: ntqqWebhook.getSign(body.d.event_ts, body.d.plain_token)
|
|
101
|
+
};
|
|
102
|
+
} else if (+body.op === 0) {
|
|
103
|
+
ctx.status = 204;
|
|
104
|
+
for (const event of this.#events[body.t] || []) event(body.d);
|
|
105
|
+
const accessToken = this.config.get("access_token");
|
|
106
|
+
for (const client of this.#client) try {
|
|
107
|
+
if (accessToken) body["access_token"] = accessToken;
|
|
108
|
+
client.ws.send(JSON.stringify(body));
|
|
109
|
+
} catch (e) {
|
|
110
|
+
this.#error(e);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
this.#app.use(router.routes());
|
|
115
|
+
this.#app.use(router.allowedMethods());
|
|
116
|
+
const server = this.#app.listen(cfg.port, () => {
|
|
117
|
+
console.log("Server running at http://localhost:" + cfg.port + route);
|
|
118
|
+
this.updateConnectionStatus({ state: "ready" });
|
|
119
|
+
});
|
|
120
|
+
const wss = new WebSocketServer({ server });
|
|
121
|
+
console.log("Server running at ws://localhost:" + cfg.port + "/");
|
|
122
|
+
wss.on("connection", (ws) => {
|
|
123
|
+
const clientId = v4();
|
|
124
|
+
ws["clientId"] = clientId;
|
|
125
|
+
console.log(clientId, "connection");
|
|
126
|
+
this.#client.push({
|
|
127
|
+
id: clientId,
|
|
128
|
+
ws
|
|
129
|
+
});
|
|
130
|
+
ws.on("message", (message) => {
|
|
131
|
+
try {
|
|
132
|
+
const body = normalizeGatewayMessage(JSON.parse(message.toString()));
|
|
133
|
+
for (const event of this.#events[body.t] || []) event(body.d);
|
|
134
|
+
} catch (e) {
|
|
135
|
+
this.#error(e);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
ws.on("close", () => {
|
|
139
|
+
console.log(`${clientId} disconnected`);
|
|
140
|
+
this.#client = this.#client.filter((client) => client.id !== clientId);
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
} else {
|
|
144
|
+
const reConnect = () => {
|
|
145
|
+
this.#ws = new WebSocket(ws);
|
|
146
|
+
this.#ws.on("open", () => {
|
|
147
|
+
this.#count = 0;
|
|
148
|
+
this.updateConnectionStatus({ state: "ready" });
|
|
149
|
+
console.log("ws connected");
|
|
150
|
+
});
|
|
151
|
+
this.#ws.on("message", (data) => {
|
|
152
|
+
try {
|
|
153
|
+
const body = normalizeGatewayMessage(JSON.parse(data.toString()));
|
|
154
|
+
const accessToken = body["access_token"];
|
|
155
|
+
if (accessToken) this.config.set("access_token", accessToken);
|
|
156
|
+
for (const event of this.#events[body.t] || []) event(body.d);
|
|
157
|
+
} catch (e) {
|
|
158
|
+
this.#error(e);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
this.#ws.on("close", () => {
|
|
162
|
+
this.updateConnectionStatus({ state: "offline" });
|
|
163
|
+
console.log("ws closed");
|
|
164
|
+
if (this.#count > 5) return;
|
|
165
|
+
setTimeout(() => {
|
|
166
|
+
reConnect();
|
|
167
|
+
}, 1300);
|
|
168
|
+
});
|
|
169
|
+
this.#ws.on("error", (e) => {
|
|
170
|
+
this.#error(e);
|
|
171
|
+
});
|
|
172
|
+
};
|
|
173
|
+
reConnect();
|
|
174
|
+
}
|
|
175
|
+
} catch (e) {
|
|
176
|
+
this.#error(e);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
*
|
|
181
|
+
* @param error
|
|
182
|
+
*/
|
|
183
|
+
#error(error) {
|
|
184
|
+
if (this.#events["ERROR"]) for (const event of this.#events["ERROR"] || []) event(error);
|
|
185
|
+
}
|
|
17
186
|
};
|
|
18
|
-
class QQBotClient extends QQBotAPI {
|
|
19
|
-
#events = {};
|
|
20
|
-
#app = null;
|
|
21
|
-
#count = 0;
|
|
22
|
-
#client = [];
|
|
23
|
-
#ws = null;
|
|
24
|
-
constructor(opstion) {
|
|
25
|
-
super(opstion);
|
|
26
|
-
}
|
|
27
|
-
on(key, val) {
|
|
28
|
-
if (!this.#events[key]) {
|
|
29
|
-
this.#events[key] = [];
|
|
30
|
-
}
|
|
31
|
-
this.#events[key].push(val);
|
|
32
|
-
return this;
|
|
33
|
-
}
|
|
34
|
-
async #setTimeoutBotConfig() {
|
|
35
|
-
const callBack = async () => {
|
|
36
|
-
const app_id = this.config.get('app_id');
|
|
37
|
-
const secret = this.config.get('secret');
|
|
38
|
-
if (!app_id || !secret) {
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
const data = await this.getAuthentication();
|
|
42
|
-
this.config.set('access_token', data.access_token);
|
|
43
|
-
console.info('refresh', data.expires_in, 's');
|
|
44
|
-
setTimeout(() => void callBack(), data.expires_in * 1000);
|
|
45
|
-
};
|
|
46
|
-
await callBack();
|
|
47
|
-
}
|
|
48
|
-
connect() {
|
|
49
|
-
try {
|
|
50
|
-
const ws = this.config.get('ws');
|
|
51
|
-
this.updateConnectionStatus({ state: 'connecting', transport: ws ? 'proxy' : 'webhook' });
|
|
52
|
-
if (!ws) {
|
|
53
|
-
void this.#setTimeoutBotConfig();
|
|
54
|
-
this.#app = new Koa();
|
|
55
|
-
this.#app.use(bodyParser());
|
|
56
|
-
const router = new Router();
|
|
57
|
-
const port = this.config.get('port');
|
|
58
|
-
const secret = this.config.get('secret');
|
|
59
|
-
const route = this.config.get('route') ?? '/webhook';
|
|
60
|
-
const cfg = {
|
|
61
|
-
secret: secret ?? '',
|
|
62
|
-
port: port ? Number(port) : 17157
|
|
63
|
-
};
|
|
64
|
-
const ntqqWebhook = new WebhookAPI({
|
|
65
|
-
secret: cfg.secret
|
|
66
|
-
});
|
|
67
|
-
this.#app.use(async (ctx, next) => {
|
|
68
|
-
let rawData = '';
|
|
69
|
-
ctx.req.on('data', chunk => (rawData += chunk));
|
|
70
|
-
ctx.req.on('end', () => (ctx.request.rawBody = rawData));
|
|
71
|
-
await next();
|
|
72
|
-
});
|
|
73
|
-
router.post(route, ctx => {
|
|
74
|
-
const sign = ctx.req.headers['x-signature-ed25519'];
|
|
75
|
-
const timestamp = ctx.req.headers['x-signature-timestamp'];
|
|
76
|
-
const rawBody = ctx.request.rawBody;
|
|
77
|
-
const isValid = ntqqWebhook.validSign(timestamp, rawBody, String(sign));
|
|
78
|
-
if (!isValid) {
|
|
79
|
-
ctx.status = 400;
|
|
80
|
-
ctx.body = { msg: 'invalid signature' };
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
const body = normalizeGatewayMessage(ctx.request.body);
|
|
84
|
-
if (+body.op === 13) {
|
|
85
|
-
ctx.status = 200;
|
|
86
|
-
ctx.body = {
|
|
87
|
-
plain_token: body.d.plain_token,
|
|
88
|
-
signature: ntqqWebhook.getSign(body.d.event_ts, body.d.plain_token)
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
else if (+body.op === 0) {
|
|
92
|
-
ctx.status = 204;
|
|
93
|
-
for (const event of this.#events[body.t] || []) {
|
|
94
|
-
event(body.d);
|
|
95
|
-
}
|
|
96
|
-
const accessToken = this.config.get('access_token');
|
|
97
|
-
for (const client of this.#client) {
|
|
98
|
-
try {
|
|
99
|
-
if (accessToken) {
|
|
100
|
-
body['access_token'] = accessToken;
|
|
101
|
-
}
|
|
102
|
-
client.ws.send(JSON.stringify(body));
|
|
103
|
-
}
|
|
104
|
-
catch (e) {
|
|
105
|
-
this.#error(e);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
this.#app.use(router.routes());
|
|
111
|
-
this.#app.use(router.allowedMethods());
|
|
112
|
-
const server = this.#app.listen(cfg.port, () => {
|
|
113
|
-
console.log('Server running at http://localhost:' + cfg.port + route);
|
|
114
|
-
this.updateConnectionStatus({ state: 'ready' });
|
|
115
|
-
});
|
|
116
|
-
const wss = new WebSocketServer({ server: server });
|
|
117
|
-
console.log('Server running at ws://localhost:' + cfg.port + '/');
|
|
118
|
-
wss.on('connection', ws => {
|
|
119
|
-
const clientId = v4();
|
|
120
|
-
ws['clientId'] = clientId;
|
|
121
|
-
console.log(clientId, 'connection');
|
|
122
|
-
this.#client.push({ id: clientId, ws });
|
|
123
|
-
ws.on('message', (message) => {
|
|
124
|
-
try {
|
|
125
|
-
const body = normalizeGatewayMessage(JSON.parse(message.toString()));
|
|
126
|
-
for (const event of this.#events[body.t] || []) {
|
|
127
|
-
event(body.d);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
catch (e) {
|
|
131
|
-
this.#error(e);
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
ws.on('close', () => {
|
|
135
|
-
console.log(`${clientId} disconnected`);
|
|
136
|
-
this.#client = this.#client.filter(client => client.id !== clientId);
|
|
137
|
-
});
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
141
|
-
const reConnect = () => {
|
|
142
|
-
this.#ws = new WebSocket(ws);
|
|
143
|
-
this.#ws.on('open', () => {
|
|
144
|
-
this.#count = 0;
|
|
145
|
-
this.updateConnectionStatus({ state: 'ready' });
|
|
146
|
-
console.log('ws connected');
|
|
147
|
-
});
|
|
148
|
-
this.#ws.on('message', data => {
|
|
149
|
-
try {
|
|
150
|
-
const body = normalizeGatewayMessage(JSON.parse(data.toString()));
|
|
151
|
-
const accessToken = body['access_token'];
|
|
152
|
-
if (accessToken) {
|
|
153
|
-
this.config.set('access_token', accessToken);
|
|
154
|
-
}
|
|
155
|
-
for (const event of this.#events[body.t] || []) {
|
|
156
|
-
event(body.d);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
catch (e) {
|
|
160
|
-
this.#error(e);
|
|
161
|
-
}
|
|
162
|
-
});
|
|
163
|
-
this.#ws.on('close', () => {
|
|
164
|
-
this.updateConnectionStatus({ state: 'offline' });
|
|
165
|
-
console.log('ws closed');
|
|
166
|
-
if (this.#count > 5) {
|
|
167
|
-
return;
|
|
168
|
-
}
|
|
169
|
-
setTimeout(() => {
|
|
170
|
-
reConnect();
|
|
171
|
-
}, 1300);
|
|
172
|
-
});
|
|
173
|
-
this.#ws.on('error', e => {
|
|
174
|
-
this.#error(e);
|
|
175
|
-
});
|
|
176
|
-
};
|
|
177
|
-
reConnect();
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
catch (e) {
|
|
181
|
-
this.#error(e);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
#error(error) {
|
|
185
|
-
if (this.#events['ERROR']) {
|
|
186
|
-
for (const event of this.#events['ERROR'] || []) {
|
|
187
|
-
event(error);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
187
|
|
|
193
|
-
|
|
188
|
+
//#endregion
|
|
189
|
+
export { QQBotClient };
|
|
@@ -1,10 +1,32 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { Options } from "./typing.js";
|
|
2
|
+
import { QQBotAPI } from "./api.js";
|
|
3
|
+
import { QQBotEventMap } from "./message.js";
|
|
4
|
+
//#region src/sdk/client.websoket.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* QQ 机器人网关连接。
|
|
7
|
+
*
|
|
8
|
+
* 网关会主动发送 op=7 要求客户端重新连接,也可能因网络或重复连接被关闭。
|
|
9
|
+
* 因此重连必须由客户端统一调度,避免旧 socket 的 close 事件重复创建连接。
|
|
10
|
+
*/
|
|
11
|
+
declare class QQBotClients extends QQBotAPI {
|
|
12
|
+
#private;
|
|
13
|
+
/**
|
|
14
|
+
* 设置配置
|
|
15
|
+
* @param option
|
|
16
|
+
*/
|
|
17
|
+
constructor(option: Options);
|
|
18
|
+
/** Stops reconnecting and releases the active gateway socket. */
|
|
19
|
+
disconnect(): void;
|
|
20
|
+
/**
|
|
21
|
+
* 注册事件处理程序
|
|
22
|
+
* @param key 事件名称
|
|
23
|
+
* @param val 事件处理函数
|
|
24
|
+
*/
|
|
25
|
+
on<T extends keyof QQBotEventMap>(key: T, val: (event: QQBotEventMap[T]) => any): this;
|
|
26
|
+
/**
|
|
27
|
+
* 建立或恢复网关连接。
|
|
28
|
+
*/
|
|
29
|
+
connect(gatewayURL?: string): Promise<void>;
|
|
10
30
|
}
|
|
31
|
+
//#endregion
|
|
32
|
+
export { QQBotClients };
|