@alemonjs/qq-bot 0.0.12 → 0.0.13

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 (53) hide show
  1. package/README.md +19 -8
  2. package/dist/assets/index.css +1 -1
  3. package/dist/assets/index.js +1 -1
  4. package/lib/api.d.ts +971 -843
  5. package/lib/api.js +1172 -1156
  6. package/lib/client.d.ts +22 -22
  7. package/lib/client.js +201 -217
  8. package/lib/config.js +2 -2
  9. package/lib/desktop.js +3 -1
  10. package/lib/from.js +27 -34
  11. package/lib/index.d.ts +3 -3
  12. package/lib/index.group.js +238 -0
  13. package/lib/index.guild.js +338 -0
  14. package/lib/index.js +43 -2
  15. package/lib/message/AT_MESSAGE_CREATE.d.ts +35 -35
  16. package/lib/message/C2C_MESSAGE_CREATE.d.ts +9 -9
  17. package/lib/message/CHANNEL_CREATE.d.ts +15 -15
  18. package/lib/message/CHANNEL_DELETE.d.ts +15 -15
  19. package/lib/message/CHANNEL_UPDATE.d.ts +15 -15
  20. package/lib/message/DIRECT_MESSAGE_CREATE.d.ts +29 -29
  21. package/lib/message/DIRECT_MESSAGE_DELETE.d.ts +17 -17
  22. package/lib/message/ERROR.d.ts +2 -2
  23. package/lib/message/GROUP_AT_MESSAGE_CREATE.d.ts +10 -10
  24. package/lib/message/GUILD_CREATE.d.ts +15 -15
  25. package/lib/message/GUILD_DELETE.d.ts +15 -15
  26. package/lib/message/GUILD_MEMBER_ADD.d.ts +14 -14
  27. package/lib/message/GUILD_MEMBER_REMOVE.d.ts +14 -14
  28. package/lib/message/GUILD_MEMBER_UPDATE.d.ts +14 -14
  29. package/lib/message/GUILD_UPDATE.d.ts +15 -15
  30. package/lib/message/INTERACTION_CREATE.d.ts +2 -2
  31. package/lib/message/MESSAGE_CREATE.d.ts +2 -2
  32. package/lib/message/MESSAGE_DELETE.d.ts +2 -2
  33. package/lib/message/MESSAGE_REACTION_ADD.d.ts +13 -13
  34. package/lib/message/MESSAGE_REACTION_REMOVE.d.ts +13 -13
  35. package/lib/message/PUBLIC_MESSAGE_DELETE.d.ts +15 -15
  36. package/lib/message/READY.d.ts +6 -6
  37. package/lib/message.d.ts +46 -46
  38. package/lib/sdk/api.d.ts +847 -0
  39. package/lib/sdk/api.js +1183 -0
  40. package/lib/sdk/client.js +228 -0
  41. package/lib/sdk/config.js +3 -0
  42. package/lib/sdk/counter.js +19 -0
  43. package/lib/sdk/from.js +44 -0
  44. package/lib/sdk/intents.js +102 -0
  45. package/lib/sdk/typing.d.ts +56 -0
  46. package/lib/sdk/webhook.secret.js +53 -0
  47. package/lib/sdk/websoket.group.js +221 -0
  48. package/lib/sdk/websoket.guild.js +203 -0
  49. package/lib/send/index.js +87 -21
  50. package/lib/typing.d.ts +62 -54
  51. package/lib/utils.js +14 -0
  52. package/lib/webhook.js +46 -48
  53. package/package.json +1 -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('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,25 @@ 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' ? readFileSync(item.value, 'base64') : item.value.toString('base64');
36
51
  const file_info = await client
37
52
  .postRichMediaByGroup(event.GuildId, {
38
53
  file_type: 1,
39
- file_data: msg.toString('base64')
54
+ file_data: file_data
40
55
  })
41
56
  .then(res => res?.file_info);
42
57
  if (!file_info)
@@ -72,13 +87,25 @@ const C2C_MESSAGE_CREATE = (client, event, val) => {
72
87
  msg_seq: client.getMessageSeq(event.MessageId)
73
88
  })));
74
89
  }
75
- const images = val.filter(item => item.type == 'Image').map(item => item.value);
90
+ const images = val.filter(item => (item.type == 'Image' || item.type == 'ImageFile' || item.type == 'ImageURL'));
76
91
  if (images) {
77
- return Promise.all(images.map(async (msg) => {
92
+ return Promise.all(images.map(async (item) => {
93
+ if (item.type == 'ImageURL') {
94
+ return client.usersOpenMessages(event.OpenId, {
95
+ content: '',
96
+ media: {
97
+ file_info: item.value
98
+ },
99
+ msg_id: event.MessageId,
100
+ msg_type: 7,
101
+ msg_seq: client.getMessageSeq(event.MessageId)
102
+ });
103
+ }
104
+ const file_data = item.type == 'ImageFile' ? readFileSync(item.value, 'base64') : item.value.toString('base64');
78
105
  const file_info = await client
79
106
  .postRichMediaByUsers(event.OpenId, {
80
107
  file_type: 1,
81
- file_data: msg.toString('base64')
108
+ file_data: file_data
82
109
  })
83
110
  .then(res => res?.file_info);
84
111
  if (!file_info)
@@ -119,12 +146,25 @@ const DIRECT_MESSAGE_CREATE = (client, event, val) => {
119
146
  msg_id: event.MessageId
120
147
  })));
121
148
  }
122
- const images = val.filter(item => item.type == 'Image').map(item => item.value);
149
+ const images = val.filter(item => (item.type == 'Image' || item.type == 'ImageFile' || item.type == 'ImageURL'));
123
150
  if (images) {
124
- return Promise.all(images.map(item => client.postDirectImage(event.OpenId, {
125
- msg_id: event.MessageId,
126
- image: item
127
- })));
151
+ return Promise.all(images.map(async (item) => {
152
+ if (item.value == 'ImageURL') {
153
+ // 请求得到buffer
154
+ const data = await axios.get(item.value, {
155
+ responseType: 'arraybuffer'
156
+ }).then(res => res.data);
157
+ return client.postDirectImage(event.OpenId, {
158
+ msg_id: event.MessageId,
159
+ image: data
160
+ });
161
+ }
162
+ const file_data = item.type == 'ImageFile' ? readFileSync(item.value) : item.value;
163
+ return client.postDirectImage(event.OpenId, {
164
+ msg_id: event.MessageId,
165
+ image: Buffer.isBuffer(file_data) ? file_data : Buffer.from(file_data)
166
+ });
167
+ }));
128
168
  }
129
169
  return [];
130
170
  };
@@ -161,12 +201,25 @@ const AT_MESSAGE_CREATE = (client, event, val) => {
161
201
  msg_id: event.MessageId
162
202
  })));
163
203
  }
164
- const images = val.filter(item => item.type == 'Image').map(item => item.value);
204
+ const images = val.filter(item => (item.type == 'Image' || item.type == 'ImageFile' || item.type == 'ImageURL'));
165
205
  if (images) {
166
- return Promise.all(images.map(item => client.postImage(event.ChannelId, {
167
- msg_id: event.MessageId,
168
- image: item
169
- })));
206
+ return Promise.all(images.map(async (item) => {
207
+ if (item.value == 'ImageURL') {
208
+ // 请求得到buffer
209
+ const data = await axios.get(item.value, {
210
+ responseType: 'arraybuffer'
211
+ }).then(res => res.data);
212
+ return client.postImage(event.ChannelId, {
213
+ msg_id: event.MessageId,
214
+ image: data
215
+ });
216
+ }
217
+ const file_data = item.type == 'ImageFile' ? readFileSync(item.value) : item.value;
218
+ return client.postImage(event.ChannelId, {
219
+ msg_id: event.MessageId,
220
+ image: Buffer.isBuffer(file_data) ? file_data : Buffer.from(file_data)
221
+ });
222
+ }));
170
223
  }
171
224
  return [];
172
225
  };
@@ -209,12 +262,25 @@ const MESSAGE_CREATE = (client, event, val) => {
209
262
  msg_id: event.MessageId
210
263
  })));
211
264
  }
212
- const images = val.filter(item => item.type == 'Image').map(item => item.value);
265
+ const images = val.filter(item => (item.type == 'Image' || item.type == 'ImageFile' || item.type == 'ImageURL'));
213
266
  if (images) {
214
- return Promise.all(images.map(item => client.postImage(event.ChannelId, {
215
- msg_id: event.MessageId,
216
- image: item
217
- })));
267
+ return Promise.all(images.map(async (item) => {
268
+ if (item.value == 'ImageURL') {
269
+ // 请求得到buffer
270
+ const data = await axios.get(item.value, {
271
+ responseType: 'arraybuffer'
272
+ }).then(res => res.data);
273
+ return client.postImage(event.ChannelId, {
274
+ msg_id: event.MessageId,
275
+ image: data
276
+ });
277
+ }
278
+ const file_data = item.type == 'ImageFile' ? readFileSync(item.value) : item.value;
279
+ return client.postImage(event.ChannelId, {
280
+ msg_id: event.MessageId,
281
+ image: Buffer.isBuffer(file_data) ? file_data : Buffer.from(file_data)
282
+ });
283
+ }));
218
284
  }
219
285
  return [];
220
286
  };