@alemonjs/qq-bot 0.0.12 → 0.0.14

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 (54) hide show
  1. package/README.md +27 -12
  2. package/dist/assets/index.css +472 -1
  3. package/dist/assets/index.js +11002 -14
  4. package/dist/index.html +1 -1
  5. package/lib/api.d.ts +971 -843
  6. package/lib/api.js +1172 -1156
  7. package/lib/client.d.ts +22 -22
  8. package/lib/client.js +201 -217
  9. package/lib/config.js +2 -2
  10. package/lib/desktop.js +3 -1
  11. package/lib/from.js +27 -34
  12. package/lib/index.d.ts +3 -3
  13. package/lib/index.group.js +226 -0
  14. package/lib/index.guild.js +320 -0
  15. package/lib/index.js +59 -38
  16. package/lib/message/AT_MESSAGE_CREATE.d.ts +35 -35
  17. package/lib/message/C2C_MESSAGE_CREATE.d.ts +9 -9
  18. package/lib/message/CHANNEL_CREATE.d.ts +15 -15
  19. package/lib/message/CHANNEL_DELETE.d.ts +15 -15
  20. package/lib/message/CHANNEL_UPDATE.d.ts +15 -15
  21. package/lib/message/DIRECT_MESSAGE_CREATE.d.ts +29 -29
  22. package/lib/message/DIRECT_MESSAGE_DELETE.d.ts +17 -17
  23. package/lib/message/ERROR.d.ts +2 -2
  24. package/lib/message/GROUP_AT_MESSAGE_CREATE.d.ts +10 -10
  25. package/lib/message/GUILD_CREATE.d.ts +15 -15
  26. package/lib/message/GUILD_DELETE.d.ts +15 -15
  27. package/lib/message/GUILD_MEMBER_ADD.d.ts +14 -14
  28. package/lib/message/GUILD_MEMBER_REMOVE.d.ts +14 -14
  29. package/lib/message/GUILD_MEMBER_UPDATE.d.ts +14 -14
  30. package/lib/message/GUILD_UPDATE.d.ts +15 -15
  31. package/lib/message/INTERACTION_CREATE.d.ts +2 -2
  32. package/lib/message/MESSAGE_CREATE.d.ts +2 -2
  33. package/lib/message/MESSAGE_DELETE.d.ts +2 -2
  34. package/lib/message/MESSAGE_REACTION_ADD.d.ts +13 -13
  35. package/lib/message/MESSAGE_REACTION_REMOVE.d.ts +13 -13
  36. package/lib/message/PUBLIC_MESSAGE_DELETE.d.ts +15 -15
  37. package/lib/message/READY.d.ts +6 -6
  38. package/lib/message.d.ts +46 -46
  39. package/lib/sdk/api.d.ts +847 -0
  40. package/lib/sdk/api.js +1184 -0
  41. package/lib/sdk/client.js +228 -0
  42. package/lib/sdk/config.js +3 -0
  43. package/lib/sdk/counter.js +19 -0
  44. package/lib/sdk/from.js +44 -0
  45. package/lib/sdk/intents.js +102 -0
  46. package/lib/sdk/typing.d.ts +56 -0
  47. package/lib/sdk/webhook.secret.js +53 -0
  48. package/lib/sdk/websoket.group.js +221 -0
  49. package/lib/sdk/websoket.guild.js +203 -0
  50. package/lib/send/index.js +97 -21
  51. package/lib/typing.d.ts +62 -54
  52. package/lib/utils.js +14 -0
  53. package/lib/webhook.js +46 -48
  54. package/package.json +4 -1
@@ -0,0 +1,221 @@
1
+ import WebSocket from 'ws';
2
+ import { QQBotAPI } from './api.js';
3
+ import { config } from './config.js';
4
+ import { getIntentsMask } from './intents.js';
5
+ import { Counter } from './counter.js';
6
+
7
+ /**
8
+ * 连接
9
+ */
10
+ class QQBotGroupClient extends QQBotAPI {
11
+ //
12
+ #counter = new Counter(1); // 初始值为1
13
+ // 标记是否已连接
14
+ #isConnected = false;
15
+ // 存储最新的消息序号
16
+ #heartbeat_interval = 30000;
17
+ // 鉴权
18
+ #IntervalId = null;
19
+ // url
20
+ #gatewayUrl = null;
21
+ /**
22
+ * 设置配置
23
+ * @param opstion
24
+ */
25
+ constructor(opstion) {
26
+ super();
27
+ for (const key in opstion) {
28
+ config.set(key, opstion[key]);
29
+ }
30
+ }
31
+ /**
32
+ * 定时鉴权
33
+ * @param cfg
34
+ * @returns
35
+ */
36
+ async #setTimeoutBotConfig() {
37
+ const accessToken = async () => {
38
+ const app_id = config.get('app_id');
39
+ const secret = config.get('secret');
40
+ if (!app_id || !secret)
41
+ return;
42
+ // 发送请求
43
+ const data = await this.getAuthentication(app_id, secret).then(res => res.data);
44
+ config.set('access_token', data.access_token);
45
+ console.info('refresh', data.expires_in, 's');
46
+ setTimeout(accessToken, data.expires_in * 1000);
47
+ };
48
+ await accessToken();
49
+ return;
50
+ }
51
+ /**
52
+ * 鉴权数据
53
+ * @returns
54
+ */
55
+ #aut() {
56
+ const token = config.get('token');
57
+ const intents = config.get('intents');
58
+ const shard = config.get('shard');
59
+ return {
60
+ op: 2, // op = 2
61
+ d: {
62
+ token: `QQBot ${token}`,
63
+ intents: getIntentsMask(intents),
64
+ shard: shard,
65
+ properties: {
66
+ $os: process.platform,
67
+ $browser: 'alemonjs',
68
+ $device: 'alemonjs'
69
+ }
70
+ }
71
+ };
72
+ }
73
+ #ws = null;
74
+ #events = {};
75
+ /**
76
+ * 注册事件处理程序
77
+ * @param key 事件名称
78
+ * @param val 事件处理函数
79
+ */
80
+ on(key, val) {
81
+ if (!this.#events[key])
82
+ this.#events[key] = [];
83
+ this.#events[key].push(val);
84
+ return this;
85
+ }
86
+ /**
87
+ *
88
+ * @param cfg
89
+ * @param conversation
90
+ */
91
+ async connect() {
92
+ // 定时模式
93
+ await this.#setTimeoutBotConfig();
94
+ // 请求url
95
+ if (!this.#gatewayUrl) {
96
+ this.#gatewayUrl = await this.gateway().then(res => res?.url);
97
+ }
98
+ if (!this.#gatewayUrl)
99
+ return;
100
+ // 重新连接的逻辑
101
+ const reconnect = async () => {
102
+ if (this.#counter.get() >= 5) {
103
+ console.info('The maximum number of reconnections has been reached, cancel reconnection');
104
+ return;
105
+ }
106
+ setTimeout(() => {
107
+ console.info('[ws] reconnecting...');
108
+ // 重新starrt
109
+ start();
110
+ // 记录
111
+ this.#counter.getNextId();
112
+ }, 5000);
113
+ };
114
+ const start = () => {
115
+ if (this.#gatewayUrl) {
116
+ const map = {
117
+ 0: async ({ t, d }) => {
118
+ if (this.#events[t]) {
119
+ try {
120
+ for (const event of this.#events[t]) {
121
+ // 是否是函数
122
+ if (typeof event != 'function')
123
+ continue;
124
+ event(d);
125
+ }
126
+ }
127
+ catch (err) {
128
+ if (this.#events['ERROR']) {
129
+ for (const event of this.#events['ERROR']) {
130
+ // 是否是函数
131
+ if (typeof event != 'function')
132
+ continue;
133
+ event(err);
134
+ }
135
+ }
136
+ }
137
+ }
138
+ // Ready Event,鉴权成功
139
+ if (t === 'READY') {
140
+ this.#IntervalId = setInterval(() => {
141
+ if (this.#isConnected) {
142
+ this.#ws &&
143
+ this.#ws.send(JSON.stringify({
144
+ op: 1, // op = 1
145
+ d: null // 如果是第一次连接,传null
146
+ }));
147
+ }
148
+ }, this.#heartbeat_interval);
149
+ }
150
+ // Resumed Event,恢复连接成功
151
+ if (t === 'RESUMED') {
152
+ console.info('[ws] restore connection');
153
+ // 重制次数
154
+ this.#counter.reStart();
155
+ }
156
+ return;
157
+ },
158
+ 6: ({ d }) => {
159
+ console.info('[ws] connection attempt', d);
160
+ return;
161
+ },
162
+ 7: async ({ d }) => {
163
+ // 执行重新连接
164
+ console.info('[ws] reconnect', d);
165
+ // 取消鉴权发送
166
+ if (this.#IntervalId)
167
+ clearInterval(this.#IntervalId);
168
+ return;
169
+ },
170
+ 9: ({ d }) => {
171
+ console.info('[ws] parameter error', d);
172
+ return;
173
+ },
174
+ 10: ({ d }) => {
175
+ // 重制次数
176
+ this.#isConnected = true;
177
+ // 记录新循环
178
+ this.#heartbeat_interval = d.heartbeat_interval;
179
+ // 发送鉴权
180
+ this.#ws && this.#ws.send(JSON.stringify(this.#aut()));
181
+ return;
182
+ },
183
+ 11: () => {
184
+ // OpCode 11 Heartbeat ACK 消息,心跳发送成功
185
+ console.info('[ws] heartbeat transmission');
186
+ // 重制次数
187
+ this.#counter.reStart();
188
+ return;
189
+ },
190
+ 12: ({ d }) => {
191
+ console.info('[ws] platform data', d);
192
+ return;
193
+ }
194
+ };
195
+ // 连接
196
+ this.#ws = new WebSocket(this.#gatewayUrl);
197
+ this.#ws.on('open', () => {
198
+ console.info('[ws] open');
199
+ });
200
+ // 监听消息
201
+ this.#ws.on('message', async (msg) => {
202
+ const message = JSON.parse(msg.toString('utf8'));
203
+ if (process.env.NTQQ_WS == 'dev')
204
+ console.info('message', message);
205
+ // 根据 opcode 进行处理
206
+ if (map[message.op]) {
207
+ map[message.op](message);
208
+ }
209
+ });
210
+ // 关闭
211
+ this.#ws.on('close', async (err) => {
212
+ await reconnect();
213
+ console.info('[ws] close', err);
214
+ });
215
+ }
216
+ };
217
+ start();
218
+ }
219
+ }
220
+
221
+ export { QQBotGroupClient };
@@ -0,0 +1,203 @@
1
+ import WebSocket from 'ws';
2
+ import { config } from './config.js';
3
+ import { getIntentsMask } from './intents.js';
4
+ import { QQBotAPI } from './api.js';
5
+ import { Counter } from './counter.js';
6
+
7
+ /**
8
+ * 连接
9
+ */
10
+ class QQBotGuildClient extends QQBotAPI {
11
+ //
12
+ #counter = new Counter(1); // 初始值为1
13
+ // 标记是否已连接
14
+ #isConnected = false;
15
+ // 存储最新的消息序号
16
+ #heartbeat_interval = 30000;
17
+ // 鉴权
18
+ #IntervalId = null;
19
+ // url
20
+ #gatewayUrl = null;
21
+ #ws;
22
+ /**
23
+ *
24
+ * @param opstion
25
+ */
26
+ constructor(opstion) {
27
+ super();
28
+ for (const key in opstion) {
29
+ config.set(key, opstion[key]);
30
+ }
31
+ }
32
+ /**
33
+ * 鉴权数据
34
+ * @returns
35
+ */
36
+ #aut() {
37
+ const app_id = config.get('app_id');
38
+ const token = config.get('token');
39
+ const intents = config.get('intents');
40
+ const shard = config.get('shard');
41
+ return {
42
+ op: 2, // op = 2
43
+ d: {
44
+ token: `Bot ${app_id}.${token}`,
45
+ intents: getIntentsMask(intents),
46
+ shard,
47
+ properties: {
48
+ $os: process.platform,
49
+ $browser: 'alemonjs',
50
+ $device: 'alemonjs'
51
+ }
52
+ }
53
+ };
54
+ }
55
+ #events = {};
56
+ /**
57
+ * 注册事件处理程序
58
+ * @param key 事件名称
59
+ * @param val 事件处理函数
60
+ */
61
+ on(key, val) {
62
+ if (!this.#events[key])
63
+ this.#events[key] = [];
64
+ this.#events[key].push(val);
65
+ return this;
66
+ }
67
+ /**
68
+ *
69
+ * @param cfg
70
+ */
71
+ async connect() {
72
+ //
73
+ this.#gatewayUrl = await this.gateway()
74
+ .then(res => res.url)
75
+ .catch(err => {
76
+ if (this.#events['ERROR']) {
77
+ for (const item of this.#events['ERROR']) {
78
+ item(err);
79
+ }
80
+ }
81
+ });
82
+ // 请求url
83
+ if (!this.#gatewayUrl)
84
+ return this;
85
+ // 重新连接的逻辑
86
+ const reconnect = async () => {
87
+ if (this.#counter.get() >= 5) {
88
+ console.info('The maximum number of reconnections has been reached, cancel reconnection');
89
+ return;
90
+ }
91
+ setTimeout(() => {
92
+ console.info('[ws] reconnecting...');
93
+ // 重新starrt
94
+ start();
95
+ // 记录
96
+ this.#counter.getNextId();
97
+ }, 5000);
98
+ };
99
+ const start = () => {
100
+ if (this.#gatewayUrl) {
101
+ const map = {
102
+ 0: async ({ t, d }) => {
103
+ // 执行
104
+ if (this.#events[t]) {
105
+ try {
106
+ for (const event of this.#events[t]) {
107
+ // 是否是函数
108
+ if (typeof event != 'function')
109
+ continue;
110
+ event(d);
111
+ }
112
+ }
113
+ catch (err) {
114
+ if (this.#events['ERROR']) {
115
+ for (const item of this.#events['ERROR']) {
116
+ item(err);
117
+ }
118
+ }
119
+ }
120
+ }
121
+ // Ready Event,鉴权成功
122
+ if (t === 'READY') {
123
+ this.#IntervalId = setInterval(() => {
124
+ if (this.#isConnected) {
125
+ this.#ws.send(JSON.stringify({
126
+ op: 1, // op = 1
127
+ d: null // 如果是第一次连接,传null
128
+ }));
129
+ }
130
+ }, this.#heartbeat_interval);
131
+ }
132
+ // Resumed Event,恢复连接成功
133
+ if (t === 'RESUMED') {
134
+ console.info('[ws] restore connection');
135
+ // 重制次数
136
+ this.#counter.reStart();
137
+ }
138
+ return;
139
+ },
140
+ 6: ({ d }) => {
141
+ console.info('[ws] connection attempt', d);
142
+ return;
143
+ },
144
+ 7: async ({ d }) => {
145
+ // 执行重新连接
146
+ console.info('[ws] reconnect', d);
147
+ // 取消鉴权发送
148
+ if (this.#IntervalId)
149
+ clearInterval(this.#IntervalId);
150
+ return;
151
+ },
152
+ 9: ({ d }) => {
153
+ console.info('[ws] parameter error', d);
154
+ return;
155
+ },
156
+ 10: ({ d }) => {
157
+ // 重制次数
158
+ this.#isConnected = true;
159
+ // 记录新循环
160
+ this.#heartbeat_interval = d.heartbeat_interval;
161
+ // 发送鉴权
162
+ this.#ws.send(JSON.stringify(this.#aut()));
163
+ return;
164
+ },
165
+ 11: () => {
166
+ // OpCode 11 Heartbeat ACK 消息,心跳发送成功
167
+ console.info('[ws] heartbeat transmission');
168
+ // 重制次数
169
+ this.#counter.reStart();
170
+ return;
171
+ },
172
+ 12: ({ d }) => {
173
+ console.info('[ws] platform data', d);
174
+ return;
175
+ }
176
+ };
177
+ // 连接
178
+ this.#ws = new WebSocket(this.#gatewayUrl);
179
+ this.#ws.on('open', () => {
180
+ console.info('[ws] open');
181
+ });
182
+ // 监听消息
183
+ this.#ws.on('message', async (msg) => {
184
+ const message = JSON.parse(msg.toString('utf8'));
185
+ if (process.env.QQ_WS == 'dev')
186
+ console.info('message', message);
187
+ if (map[message.op]) {
188
+ map[message.op](message);
189
+ }
190
+ });
191
+ // 关闭
192
+ this.#ws.on('close', async (err) => {
193
+ await reconnect();
194
+ console.info('[ws] close', err);
195
+ });
196
+ }
197
+ };
198
+ start();
199
+ return this;
200
+ }
201
+ }
202
+
203
+ export { QQBotGuildClient };
package/lib/send/index.js CHANGED
@@ -1,3 +1,6 @@
1
+ import { readFileSync } from 'fs';
2
+ import axios from 'axios';
3
+
1
4
  const GROUP_AT_MESSAGE_CREATE = (client, event, val) => {
2
5
  const content = val
3
6
  .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
@@ -30,13 +33,27 @@ const GROUP_AT_MESSAGE_CREATE = (client, event, val) => {
30
33
  msg_seq: client.getMessageSeq(event.MessageId)
31
34
  })));
32
35
  }
33
- const images = val.filter(item => item.type == 'Image').map(item => item.value);
36
+ const images = val.filter(item => item.type == 'Image' || item.type == 'ImageFile' || item.type == 'ImageURL');
34
37
  if (images) {
35
- return Promise.all(images.map(async (msg) => {
38
+ return Promise.all(images.map(async (item) => {
39
+ if (item.type == 'ImageURL') {
40
+ return client.groupOpenMessages(event.GuildId, {
41
+ content: '',
42
+ media: {
43
+ file_info: item.value
44
+ },
45
+ msg_id: event.MessageId,
46
+ msg_type: 7,
47
+ msg_seq: client.getMessageSeq(event.MessageId)
48
+ });
49
+ }
50
+ const file_data = item.type == 'ImageFile'
51
+ ? readFileSync(item.value, 'base64')
52
+ : item.value.toString('base64');
36
53
  const file_info = await client
37
54
  .postRichMediaByGroup(event.GuildId, {
38
55
  file_type: 1,
39
- file_data: msg.toString('base64')
56
+ file_data: file_data
40
57
  })
41
58
  .then(res => res?.file_info);
42
59
  if (!file_info)
@@ -72,13 +89,27 @@ const C2C_MESSAGE_CREATE = (client, event, val) => {
72
89
  msg_seq: client.getMessageSeq(event.MessageId)
73
90
  })));
74
91
  }
75
- const images = val.filter(item => item.type == 'Image').map(item => item.value);
92
+ const images = val.filter(item => item.type == 'Image' || item.type == 'ImageFile' || item.type == 'ImageURL');
76
93
  if (images) {
77
- return Promise.all(images.map(async (msg) => {
94
+ return Promise.all(images.map(async (item) => {
95
+ if (item.type == 'ImageURL') {
96
+ return client.usersOpenMessages(event.OpenId, {
97
+ content: '',
98
+ media: {
99
+ file_info: item.value
100
+ },
101
+ msg_id: event.MessageId,
102
+ msg_type: 7,
103
+ msg_seq: client.getMessageSeq(event.MessageId)
104
+ });
105
+ }
106
+ const file_data = item.type == 'ImageFile'
107
+ ? readFileSync(item.value, 'base64')
108
+ : item.value.toString('base64');
78
109
  const file_info = await client
79
110
  .postRichMediaByUsers(event.OpenId, {
80
111
  file_type: 1,
81
- file_data: msg.toString('base64')
112
+ file_data: file_data
82
113
  })
83
114
  .then(res => res?.file_info);
84
115
  if (!file_info)
@@ -119,12 +150,27 @@ const DIRECT_MESSAGE_CREATE = (client, event, val) => {
119
150
  msg_id: event.MessageId
120
151
  })));
121
152
  }
122
- const images = val.filter(item => item.type == 'Image').map(item => item.value);
153
+ const images = val.filter(item => item.type == 'Image' || item.type == 'ImageFile' || item.type == 'ImageURL');
123
154
  if (images) {
124
- return Promise.all(images.map(item => client.postDirectImage(event.OpenId, {
125
- msg_id: event.MessageId,
126
- image: item
127
- })));
155
+ return Promise.all(images.map(async (item) => {
156
+ if (item.value == 'ImageURL') {
157
+ // 请求得到buffer
158
+ const data = await axios
159
+ .get(item.value, {
160
+ responseType: 'arraybuffer'
161
+ })
162
+ .then(res => res.data);
163
+ return client.postDirectImage(event.OpenId, {
164
+ msg_id: event.MessageId,
165
+ image: data
166
+ });
167
+ }
168
+ const file_data = item.type == 'ImageFile' ? readFileSync(item.value) : item.value;
169
+ return client.postDirectImage(event.OpenId, {
170
+ msg_id: event.MessageId,
171
+ image: Buffer.isBuffer(file_data) ? file_data : Buffer.from(file_data)
172
+ });
173
+ }));
128
174
  }
129
175
  return [];
130
176
  };
@@ -161,12 +207,27 @@ const AT_MESSAGE_CREATE = (client, event, val) => {
161
207
  msg_id: event.MessageId
162
208
  })));
163
209
  }
164
- const images = val.filter(item => item.type == 'Image').map(item => item.value);
210
+ const images = val.filter(item => item.type == 'Image' || item.type == 'ImageFile' || item.type == 'ImageURL');
165
211
  if (images) {
166
- return Promise.all(images.map(item => client.postImage(event.ChannelId, {
167
- msg_id: event.MessageId,
168
- image: item
169
- })));
212
+ return Promise.all(images.map(async (item) => {
213
+ if (item.value == 'ImageURL') {
214
+ // 请求得到buffer
215
+ const data = await axios
216
+ .get(item.value, {
217
+ responseType: 'arraybuffer'
218
+ })
219
+ .then(res => res.data);
220
+ return client.postImage(event.ChannelId, {
221
+ msg_id: event.MessageId,
222
+ image: data
223
+ });
224
+ }
225
+ const file_data = item.type == 'ImageFile' ? readFileSync(item.value) : item.value;
226
+ return client.postImage(event.ChannelId, {
227
+ msg_id: event.MessageId,
228
+ image: Buffer.isBuffer(file_data) ? file_data : Buffer.from(file_data)
229
+ });
230
+ }));
170
231
  }
171
232
  return [];
172
233
  };
@@ -209,12 +270,27 @@ const MESSAGE_CREATE = (client, event, val) => {
209
270
  msg_id: event.MessageId
210
271
  })));
211
272
  }
212
- const images = val.filter(item => item.type == 'Image').map(item => item.value);
273
+ const images = val.filter(item => item.type == 'Image' || item.type == 'ImageFile' || item.type == 'ImageURL');
213
274
  if (images) {
214
- return Promise.all(images.map(item => client.postImage(event.ChannelId, {
215
- msg_id: event.MessageId,
216
- image: item
217
- })));
275
+ return Promise.all(images.map(async (item) => {
276
+ if (item.value == 'ImageURL') {
277
+ // 请求得到buffer
278
+ const data = await axios
279
+ .get(item.value, {
280
+ responseType: 'arraybuffer'
281
+ })
282
+ .then(res => res.data);
283
+ return client.postImage(event.ChannelId, {
284
+ msg_id: event.MessageId,
285
+ image: data
286
+ });
287
+ }
288
+ const file_data = item.type == 'ImageFile' ? readFileSync(item.value) : item.value;
289
+ return client.postImage(event.ChannelId, {
290
+ msg_id: event.MessageId,
291
+ image: Buffer.isBuffer(file_data) ? file_data : Buffer.from(file_data)
292
+ });
293
+ }));
218
294
  }
219
295
  return [];
220
296
  };