@alemonjs/qq-bot 0.0.3 → 0.0.5

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 CHANGED
@@ -23,17 +23,49 @@ qq-bot:
23
23
  ```
24
24
 
25
25
  ```sh
26
- qq-group-bot:
26
+ qq-bot:
27
27
  master_key:
28
28
  - ''
29
- # 默认(请使用nginx进行代理)
29
+ # 默认 推荐nginx进行代理 http://localhost:17157/webhook
30
+ route: '/webhook'
30
31
  port: 17157
31
- # 如果在服务器也启动了机器人,可以进行互相调用
32
- ws: 'ws://...'
32
+ # 当配置ws的时候,会连接另一台hook机器人的消息。不会再启动本地端口。
33
+ # 推荐nginx进行代理 http://localhost:17157/
34
+ ws: 'wss://xxxx/websocket'
33
35
  # 频道沙盒
34
36
  sandbox: false
35
37
  ```
36
38
 
39
+ ```conf
40
+ server {
41
+ listen 443 ssl http2;
42
+ server_name bundle.com;
43
+ ssl_certificate /usr/local/nginx/bundle.crt;
44
+ ssl_certificate_key /usr/local/nginx/bundle.key;
45
+
46
+ location /webhook {
47
+ # 指向 webhook 服务的端口
48
+ proxy_pass http://localhost:17157;
49
+ proxy_set_header Host $host;
50
+ proxy_set_header X-Real-IP $remote_addr;
51
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
52
+ proxy_set_header X-Forwarded-Proto $scheme;
53
+ }
54
+
55
+ location /websocket {
56
+ # 指向 WebSocket 服务的端口
57
+ proxy_pass http://localhost:17157;
58
+ proxy_http_version 1.1;
59
+ proxy_set_header Upgrade $http_upgrade;
60
+ proxy_set_header Connection "upgrade";
61
+ proxy_set_header Host $host;
62
+ proxy_set_header X-Real-IP $remote_addr;
63
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
64
+ proxy_set_header X-Forwarded-Proto $scheme;
65
+ }
66
+ }
67
+ ```
68
+
37
69
  ## Community
38
70
 
39
71
  QQ Group 806943302
package/lib/api.js CHANGED
@@ -83,7 +83,6 @@ class QQBotAPI {
83
83
  * 0 文本 1 图文 2 md 3 ark 4 embed
84
84
  */
85
85
  async usersOpenMessages(openid, data, msg_id) {
86
- console.log('msg_id', msg_id);
87
86
  return this.groupService({
88
87
  url: `/v2/users/${openid}/messages`,
89
88
  method: 'post',
package/lib/client.js CHANGED
@@ -70,133 +70,143 @@ class QQBotClient extends QQBotAPI {
70
70
  */
71
71
  connect() {
72
72
  try {
73
- this.#setTimeoutBotConfig();
74
- this.#app = new Koa();
75
- this.#app.use(bodyParser());
76
- const router = new Router();
77
- const port = config.get('port');
78
- const secret = config.get('secret');
79
- const cfg = {
80
- secret: secret ?? '',
81
- port: port ? Number(port) : 17157
82
- };
83
- const ntqqWebhook = new WebhookAPI({
84
- secret: cfg.secret
85
- });
86
- this.#app.use(async (ctx, next) => {
87
- let rawData = '';
88
- ctx.req.on('data', chunk => (rawData += chunk));
89
- ctx.req.on('end', () => (ctx.request.rawBody = rawData));
90
- await next();
91
- });
92
- // 启动服务
93
- router.post('/webhook', async (ctx) => {
94
- const sign = ctx.req.headers['x-signature-ed25519'];
95
- const timestamp = ctx.req.headers['x-signature-timestamp'];
96
- const rawBody = ctx.request.rawBody;
97
- const isValid = ntqqWebhook.validSign(timestamp, rawBody, String(sign));
98
- if (!isValid) {
99
- ctx.status = 400;
100
- ctx.body = { msg: 'invalid signature' };
101
- return;
102
- }
103
- const body = ctx.request.body;
104
- if (body.op == 13) {
105
- ctx.status = 200;
106
- ctx.body = {
107
- // 返回明文 token
108
- plain_token: body.d.plain_token,
109
- // 生成签名
110
- signature: ntqqWebhook.getSign(body.d.event_ts, body.d.plain_token)
111
- };
112
- }
113
- else if (body.op == 0) {
114
- ctx.status = 204;
115
- // 根据事件类型,处理事件
116
- for (const event of this.#events[body.t] || []) {
117
- event(body.d);
73
+ const ws = config.get('ws');
74
+ if (!ws) {
75
+ this.#setTimeoutBotConfig();
76
+ this.#app = new Koa();
77
+ this.#app.use(bodyParser());
78
+ const router = new Router();
79
+ const port = config.get('port');
80
+ const secret = config.get('secret');
81
+ const route = config.get('route') ?? '/webhook';
82
+ const cfg = {
83
+ secret: secret ?? '',
84
+ port: port ? Number(port) : 17157
85
+ };
86
+ const ntqqWebhook = new WebhookAPI({
87
+ secret: cfg.secret
88
+ });
89
+ this.#app.use(async (ctx, next) => {
90
+ let rawData = '';
91
+ ctx.req.on('data', chunk => (rawData += chunk));
92
+ ctx.req.on('end', () => (ctx.request.rawBody = rawData));
93
+ await next();
94
+ });
95
+ // 启动服务
96
+ router.post(route, async (ctx) => {
97
+ const sign = ctx.req.headers['x-signature-ed25519'];
98
+ const timestamp = ctx.req.headers['x-signature-timestamp'];
99
+ const rawBody = ctx.request.rawBody;
100
+ const isValid = ntqqWebhook.validSign(timestamp, rawBody, String(sign));
101
+ if (!isValid) {
102
+ ctx.status = 400;
103
+ ctx.body = { msg: 'invalid signature' };
104
+ return;
118
105
  }
119
- // 也可以分法到客户端。 发送失败需要处理 或清理调
120
- for (const client of this.#client) {
121
- try {
122
- client.ws.send(JSON.stringify(body.d));
123
- }
124
- catch (e) {
125
- this.#error(e);
126
- }
106
+ const body = ctx.request.body;
107
+ if (body.op == 13) {
108
+ ctx.status = 200;
109
+ ctx.body = {
110
+ // 返回明文 token
111
+ plain_token: body.d.plain_token,
112
+ // 生成签名
113
+ signature: ntqqWebhook.getSign(body.d.event_ts, body.d.plain_token)
114
+ };
127
115
  }
128
- }
129
- });
130
- this.#app.use(router.routes());
131
- this.#app.use(router.allowedMethods());
132
- // 启动服务
133
- const server = this.#app.listen(cfg.port, () => {
134
- console.log('Server running at http://localhost:' + cfg.port + '/webhook');
135
- });
136
- // 创建 WebSocketServer 并监听同一个端口
137
- const wss = new WebSocketServer({ server: server });
138
- // 处理客户端连接
139
- wss.on('connection', ws => {
140
- const clientId = v4();
141
- ws['clientId'] = clientId;
142
- console.log(clientId, 'connection');
143
- this.#client.push({ id: clientId, ws });
144
- // 处理消息事件
145
- ws.on('message', (message) => {
146
- // 拿到消息
147
- try {
148
- const body = JSON.parse(message.toString());
116
+ else if (body.op == 0) {
117
+ ctx.status = 204;
118
+ // 根据事件类型,处理事件
149
119
  for (const event of this.#events[body.t] || []) {
150
- event(body);
120
+ event(body.d);
121
+ }
122
+ const access_token = config.get('access_token');
123
+ // 也可以分法到客户端。 发送失败需要处理 或清理调
124
+ for (const client of this.#client) {
125
+ try {
126
+ if (access_token)
127
+ body['access_token'] = access_token;
128
+ client.ws.send(JSON.stringify(body));
129
+ }
130
+ catch (e) {
131
+ this.#error(e);
132
+ }
151
133
  }
152
- }
153
- catch (e) {
154
- this.#error(e);
155
134
  }
156
135
  });
157
- // 处理关闭事件
158
- ws.on('close', () => {
159
- console.log(`Client ${clientId} disconnected`);
160
- this.#client = this.#client.filter(client => client.id !== clientId);
136
+ this.#app.use(router.routes());
137
+ this.#app.use(router.allowedMethods());
138
+ // 启动服务
139
+ const server = this.#app.listen(cfg.port, () => {
140
+ console.log('Server running at http://localhost:' + cfg.port + route);
161
141
  });
162
- });
163
- const reconnect = () => {
164
- const ws = config.get('ws');
165
- if (!ws)
166
- return;
167
- // 使用了ws服务器
168
- this.#ws = new WebSocket(ws);
169
- this.#ws.on('open', () => {
170
- this.#count = 0;
171
- console.log('ws connected');
172
- });
173
- this.#ws.on('message', data => {
174
- try {
142
+ // 创建 WebSocketServer 并监听同一个端口
143
+ const wss = new WebSocketServer({ server: server });
144
+ console.log('Server running at wss://localhost:' + cfg.port + '/');
145
+ // 处理客户端连接
146
+ wss.on('connection', ws => {
147
+ const clientId = v4();
148
+ ws['clientId'] = clientId;
149
+ console.log(clientId, 'connection');
150
+ this.#client.push({ id: clientId, ws });
151
+ // 处理消息事件
152
+ ws.on('message', (message) => {
175
153
  // 拿到消息
176
- const body = JSON.parse(data.toString());
177
- for (const event of this.#events[body.t] || []) {
178
- event(body);
154
+ try {
155
+ const body = JSON.parse(message.toString());
156
+ for (const event of this.#events[body.t] || []) {
157
+ event(body.d);
158
+ }
179
159
  }
180
- }
181
- catch (e) {
182
- this.#error(e);
183
- }
184
- });
185
- this.#ws.on('close', () => {
186
- console.log('ws closed');
187
- // 重连5次,超过5次不再重连
188
- if (this.#count > 5)
189
- return;
190
- // 23s 后重连
191
- setTimeout(() => {
192
- reconnect();
193
- }, 23000);
194
- });
195
- this.#ws.on('error', e => {
196
- this.#error(e);
160
+ catch (e) {
161
+ this.#error(e);
162
+ }
163
+ });
164
+ // 处理关闭事件
165
+ ws.on('close', () => {
166
+ console.log(`${clientId} disconnected`);
167
+ this.#client = this.#client.filter(client => client.id !== clientId);
168
+ });
197
169
  });
198
- };
199
- reconnect();
170
+ }
171
+ else {
172
+ const reconnect = () => {
173
+ // 使用了ws服务器
174
+ this.#ws = new WebSocket(ws);
175
+ this.#ws.on('open', () => {
176
+ this.#count = 0;
177
+ console.log('ws connected');
178
+ });
179
+ this.#ws.on('message', data => {
180
+ try {
181
+ // 拿到消息
182
+ const body = JSON.parse(data.toString());
183
+ const access_token = body['access_token'];
184
+ if (access_token)
185
+ config.set('access_token', access_token);
186
+ for (const event of this.#events[body.t] || []) {
187
+ event(body.d);
188
+ }
189
+ }
190
+ catch (e) {
191
+ this.#error(e);
192
+ }
193
+ });
194
+ this.#ws.on('close', () => {
195
+ console.log('ws closed');
196
+ // 重连5次,超过5次不再重连
197
+ if (this.#count > 5)
198
+ return;
199
+ // 23s 后重连
200
+ setTimeout(() => {
201
+ reconnect();
202
+ }, 23000);
203
+ });
204
+ this.#ws.on('error', e => {
205
+ this.#error(e);
206
+ });
207
+ };
208
+ reconnect();
209
+ }
200
210
  }
201
211
  catch (e) {
202
212
  this.#error(e);
package/lib/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import * as alemonjs from 'alemonjs';
1
+ import * as alemonjs from 'alemonjs'
2
2
 
3
- declare const platform = "qq-bot";
4
- declare const _default: () => alemonjs.ClientAPI;
3
+ declare const platform = 'qq-bot'
4
+ declare const _default: () => alemonjs.ClientAPI
5
5
 
6
- export { _default as default, platform };
6
+ export { _default as default, platform }
package/lib/index.js CHANGED
@@ -1,6 +1,15 @@
1
1
  import { defineBot, getConfigValue, useUserHashKey, OnProcessor } from 'alemonjs';
2
2
  import { QQBotClient } from './client.js';
3
+ import { GROUP_AT_MESSAGE_CREATE, C2C_MESSAGE_CREATE, DIRECT_MESSAGE_CREATE, AT_MESSAGE_CREATE, MESSAGE_CREATE } from './send/index.js';
3
4
 
5
+ const client = new Proxy({}, {
6
+ get: (_, prop) => {
7
+ if (prop in global.client) {
8
+ return global.client[prop];
9
+ }
10
+ return undefined;
11
+ }
12
+ });
4
13
  const platform = 'qq-bot';
5
14
  var index = defineBot(() => {
6
15
  const value = getConfigValue();
@@ -8,12 +17,19 @@ var index = defineBot(() => {
8
17
  const client = new QQBotClient({
9
18
  secret: config?.secret,
10
19
  app_id: config?.app_id,
20
+ route: config?.route,
11
21
  token: config?.token,
12
22
  port: config?.port,
13
23
  ws: config?.ws
14
24
  });
15
25
  // 连接
16
26
  client.connect();
27
+ /**
28
+ * group
29
+ *
30
+ * GROUP_AT_MESSAGE_CREATE
31
+ * C2C_MESSAGE_CREATE
32
+ */
17
33
  // 监听消息
18
34
  client.on('GROUP_AT_MESSAGE_CREATE', async (event) => {
19
35
  const master_key = config?.master_key ?? [];
@@ -55,7 +71,7 @@ var index = defineBot(() => {
55
71
  MessageText: event.content?.trim(),
56
72
  OpenId: event.author.member_openid,
57
73
  CreateAt: Date.now(),
58
- tag: 'group',
74
+ tag: 'GROUP_AT_MESSAGE_CREATE',
59
75
  value: null
60
76
  };
61
77
  // 当访问的时候获取
@@ -103,9 +119,9 @@ var index = defineBot(() => {
103
119
  MessageId: event.id,
104
120
  MessageText: event.content?.trim(),
105
121
  CreateAt: Date.now(),
106
- OpenId: '',
122
+ OpenId: event.author.user_openid,
107
123
  //
108
- tag: 'group',
124
+ tag: 'C2C_MESSAGE_CREATE',
109
125
  value: null
110
126
  };
111
127
  // 当访问的时候获取
@@ -117,6 +133,9 @@ var index = defineBot(() => {
117
133
  // 处理消息
118
134
  OnProcessor(e, 'private.message.create');
119
135
  });
136
+ /**
137
+ * guild
138
+ */
120
139
  client.on('DIRECT_MESSAGE_CREATE', async (event) => {
121
140
  // 屏蔽其他机器人的消息
122
141
  if (event?.author?.bot)
@@ -162,7 +181,7 @@ var index = defineBot(() => {
162
181
  OpenId: event.guild_id,
163
182
  CreateAt: Date.now(),
164
183
  //
165
- tag: 'guild',
184
+ tag: 'DIRECT_MESSAGE_CREATE',
166
185
  //
167
186
  value: null
168
187
  };
@@ -220,7 +239,7 @@ var index = defineBot(() => {
220
239
  OpenId: event.guild_id,
221
240
  CreateAt: Date.now(),
222
241
  //
223
- tag: 'guild',
242
+ tag: 'AT_MESSAGE_CREATE',
224
243
  //
225
244
  value: null
226
245
  };
@@ -304,7 +323,7 @@ var index = defineBot(() => {
304
323
  OpenId: event.guild_id,
305
324
  CreateAt: Date.now(),
306
325
  //
307
- tag: 'guild',
326
+ tag: 'MESSAGE_CREATE',
308
327
  value: null
309
328
  };
310
329
  // 当访问的时候获取
@@ -319,122 +338,48 @@ var index = defineBot(() => {
319
338
  client.on('ERROR', console.error);
320
339
  // FRIEND_ADD
321
340
  global.client = client;
322
- //
323
341
  return {
324
342
  api: {
325
343
  use: {
326
- send: (event, val) => {
344
+ send: async (event, val) => {
327
345
  if (val.length < 0)
328
- return Promise.all([]);
346
+ ;
329
347
  // 打 tag
330
348
  const tag = event.tag;
331
- if (tag == 'group') {
332
- const content = val
333
- .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
334
- .map(item => {
335
- if (item.type == 'Link') {
336
- return `[${item.options?.title ?? item.value}](${item.value})`;
337
- }
338
- else if (item.type == 'Mention') {
339
- if (item.value == 'everyone' ||
340
- item.value == 'all' ||
341
- item.value == '' ||
342
- typeof item.value != 'string') {
343
- return ``;
344
- }
345
- if (item.options?.belong == 'user') {
346
- return `<@${item.value}>`;
347
- }
348
- return '';
349
- // return `<qqbot-at-everyone />`
350
- }
351
- else if (item.type == 'Text') {
352
- return item.value;
353
- }
354
- })
355
- .join('');
356
- if (content) {
357
- return Promise.all([content].map(item => client.groupOpenMessages(event.GuildId, {
358
- content: item,
359
- msg_id: event.MessageId,
360
- msg_type: 0,
361
- msg_seq: client.getMessageSeq(event.MessageId)
362
- })));
349
+ try {
350
+ if (tag == 'GROUP_AT_MESSAGE_CREATE') {
351
+ return await GROUP_AT_MESSAGE_CREATE(client, event, val);
363
352
  }
364
- const images = val.filter(item => item.type == 'Image').map(item => item.value);
365
- if (images) {
366
- return Promise.all(images.map(async (msg) => {
367
- const file_info = await client
368
- .postRichMediaByGroup(event.GuildId, {
369
- file_type: 1,
370
- file_data: msg.toString('base64')
371
- })
372
- .then(res => res?.file_info);
373
- if (!file_info)
374
- return Promise.resolve(null);
375
- return client.groupOpenMessages(event.GuildId, {
376
- content: '',
377
- media: {
378
- file_info
379
- },
380
- msg_id: event.MessageId,
381
- msg_type: 7,
382
- msg_seq: client.getMessageSeq(event.MessageId)
383
- });
384
- }));
353
+ if (tag == 'C2C_MESSAGE_CREATE') {
354
+ return await C2C_MESSAGE_CREATE(client, event, val);
385
355
  }
386
- }
387
- else {
388
- const content = val
389
- .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
390
- .map(item => {
391
- if (item.type == 'Link') {
392
- return `[${item.options?.title ?? item.value}](${item.value})`;
393
- }
394
- else if (item.type == 'Mention') {
395
- if (item.value == 'everyone' ||
396
- item.value == 'all' ||
397
- item.value == '' ||
398
- typeof item.value != 'string') {
399
- return `@everyone`;
400
- }
401
- if (item.options?.belong == 'user') {
402
- return `<@!${item.value}>`;
403
- }
404
- else if (item.options?.belong == 'channel') {
405
- return `<#${item.value}>`;
406
- }
407
- return '';
408
- }
409
- else if (item.type == 'Text') {
410
- return item.value;
411
- }
412
- })
413
- .join('');
414
- if (content) {
415
- return Promise.all([content].map(item => client.channelsMessagesPost(event.ChannelId, {
416
- content: item,
417
- msg_id: event.MessageId
418
- })));
356
+ if (tag == 'DIRECT_MESSAGE_CREATE') {
357
+ return await DIRECT_MESSAGE_CREATE(client, event, val);
358
+ }
359
+ if (tag == 'AT_MESSAGE_CREATE') {
360
+ return await AT_MESSAGE_CREATE(client, event, val);
419
361
  }
420
- const images = val.filter(item => item.type == 'Image').map(item => item.value);
421
- if (images) {
422
- return Promise.all(images.map(item => client.postImage(event.ChannelId, {
423
- msg_id: event.MessageId,
424
- image: item
425
- })));
362
+ if (tag == 'MESSAGE_CREATE') {
363
+ return await AT_MESSAGE_CREATE(client, event, val);
426
364
  }
365
+ if (tag == 'MESSAGE_CREATE') {
366
+ return await MESSAGE_CREATE(client, event, val);
367
+ }
368
+ }
369
+ catch (error) {
370
+ return [error];
427
371
  }
428
- return Promise.all([]);
372
+ return [];
429
373
  },
430
374
  mention: async (e) => {
431
375
  const event = e.value;
432
376
  const tag = e.tag;
433
377
  // const event = e.value
434
378
  const Metions = [];
435
- if (tag == 'group') {
379
+ // group
380
+ if (tag == 'GROUP_AT_MESSAGE_CREATE' || 'C2C_MESSAGE_CREATE')
436
381
  return Metions;
437
- }
382
+ // guild
438
383
  if (event.mentions) {
439
384
  const mentions = e.value['mentions'];
440
385
  // 艾特消息处理
@@ -461,4 +406,4 @@ var index = defineBot(() => {
461
406
  };
462
407
  });
463
408
 
464
- export { index as default, platform };
409
+ export { client, index as default, platform };
@@ -0,0 +1,222 @@
1
+ const GROUP_AT_MESSAGE_CREATE = (client, event, val) => {
2
+ const content = val
3
+ .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
4
+ .map(item => {
5
+ if (item.type == 'Link') {
6
+ return `[${item.options?.title ?? item.value}](${item.value})`;
7
+ }
8
+ else if (item.type == 'Mention') {
9
+ if (item.value == 'everyone' ||
10
+ item.value == 'all' ||
11
+ item.value == '' ||
12
+ typeof item.value != 'string') {
13
+ return ``;
14
+ }
15
+ if (item.options?.belong == 'user') {
16
+ return `<@${item.value}>`;
17
+ }
18
+ return '';
19
+ }
20
+ else if (item.type == 'Text') {
21
+ return item.value;
22
+ }
23
+ })
24
+ .join('');
25
+ if (content) {
26
+ return Promise.all([content].map(item => client.groupOpenMessages(event.GuildId, {
27
+ content: item,
28
+ msg_id: event.MessageId,
29
+ msg_type: 0,
30
+ msg_seq: client.getMessageSeq(event.MessageId)
31
+ })));
32
+ }
33
+ const images = val.filter(item => item.type == 'Image').map(item => item.value);
34
+ if (images) {
35
+ return Promise.all(images.map(async (msg) => {
36
+ const file_info = await client
37
+ .postRichMediaByGroup(event.GuildId, {
38
+ file_type: 1,
39
+ file_data: msg.toString('base64')
40
+ })
41
+ .then(res => res?.file_info);
42
+ if (!file_info)
43
+ return Promise.resolve(null);
44
+ return client.groupOpenMessages(event.GuildId, {
45
+ content: '',
46
+ media: {
47
+ file_info
48
+ },
49
+ msg_id: event.MessageId,
50
+ msg_type: 7,
51
+ msg_seq: client.getMessageSeq(event.MessageId)
52
+ });
53
+ }));
54
+ }
55
+ return [];
56
+ };
57
+ const C2C_MESSAGE_CREATE = (client, event, val) => {
58
+ const content = val
59
+ .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
60
+ .map(item => {
61
+ if (item.type == 'Text') {
62
+ return item.value;
63
+ }
64
+ return '';
65
+ })
66
+ .join('');
67
+ if (content) {
68
+ return Promise.all([content].map(item => client.usersOpenMessages(event.OpenId, {
69
+ content: item,
70
+ msg_id: event.MessageId,
71
+ msg_type: 0,
72
+ msg_seq: client.getMessageSeq(event.MessageId)
73
+ })));
74
+ }
75
+ const images = val.filter(item => item.type == 'Image').map(item => item.value);
76
+ if (images) {
77
+ return Promise.all(images.map(async (msg) => {
78
+ const file_info = await client
79
+ .postRichMediaByUsers(event.OpenId, {
80
+ file_type: 1,
81
+ file_data: msg.toString('base64')
82
+ })
83
+ .then(res => res?.file_info);
84
+ if (!file_info)
85
+ return Promise.resolve(null);
86
+ return client.usersOpenMessages(event.OpenId, {
87
+ content: '',
88
+ media: {
89
+ file_info
90
+ },
91
+ msg_id: event.MessageId,
92
+ msg_type: 7,
93
+ msg_seq: client.getMessageSeq(event.MessageId)
94
+ });
95
+ }));
96
+ }
97
+ return [];
98
+ };
99
+ /**
100
+ * 频道私聊
101
+ * @param client
102
+ * @param event
103
+ * @param val
104
+ * @returns
105
+ */
106
+ const DIRECT_MESSAGE_CREATE = (client, event, val) => {
107
+ const content = val
108
+ .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
109
+ .map(item => {
110
+ if (item.type == 'Text') {
111
+ return item.value;
112
+ }
113
+ return '';
114
+ })
115
+ .join('');
116
+ if (content) {
117
+ return Promise.all([content].map(item => client.dmsMessage(event.OpenId, {
118
+ content: item,
119
+ msg_id: event.MessageId
120
+ })));
121
+ }
122
+ const images = val.filter(item => item.type == 'Image').map(item => item.value);
123
+ if (images) {
124
+ return Promise.all(images.map(item => client.postDirectImage(event.OpenId, {
125
+ msg_id: event.MessageId,
126
+ image: item
127
+ })));
128
+ }
129
+ return [];
130
+ };
131
+ const AT_MESSAGE_CREATE = (client, event, val) => {
132
+ const content = val
133
+ .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
134
+ .map(item => {
135
+ if (item.type == 'Link') {
136
+ return `[${item.options?.title ?? item.value}](${item.value})`;
137
+ }
138
+ else if (item.type == 'Mention') {
139
+ if (item.value == 'everyone' ||
140
+ item.value == 'all' ||
141
+ item.value == '' ||
142
+ typeof item.value != 'string') {
143
+ return `@everyone`;
144
+ }
145
+ if (item.options?.belong == 'user') {
146
+ return `<@!${item.value}>`;
147
+ }
148
+ else if (item.options?.belong == 'channel') {
149
+ return `<#${item.value}>`;
150
+ }
151
+ return '';
152
+ }
153
+ else if (item.type == 'Text') {
154
+ return item.value;
155
+ }
156
+ })
157
+ .join('');
158
+ if (content) {
159
+ return Promise.all([content].map(item => client.channelsMessagesPost(event.ChannelId, {
160
+ content: item,
161
+ msg_id: event.MessageId
162
+ })));
163
+ }
164
+ const images = val.filter(item => item.type == 'Image').map(item => item.value);
165
+ if (images) {
166
+ return Promise.all(images.map(item => client.postImage(event.ChannelId, {
167
+ msg_id: event.MessageId,
168
+ image: item
169
+ })));
170
+ }
171
+ return [];
172
+ };
173
+ /**
174
+ *
175
+ * @param event
176
+ * @param val
177
+ * @returns
178
+ */
179
+ const MESSAGE_CREATE = (client, event, val) => {
180
+ const content = val
181
+ .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
182
+ .map(item => {
183
+ if (item.type == 'Link') {
184
+ return `[${item.options?.title ?? item.value}](${item.value})`;
185
+ }
186
+ else if (item.type == 'Mention') {
187
+ if (item.value == 'everyone' ||
188
+ item.value == 'all' ||
189
+ item.value == '' ||
190
+ typeof item.value != 'string') {
191
+ return `@everyone`;
192
+ }
193
+ if (item.options?.belong == 'user') {
194
+ return `<@!${item.value}>`;
195
+ }
196
+ else if (item.options?.belong == 'channel') {
197
+ return `<#${item.value}>`;
198
+ }
199
+ return '';
200
+ }
201
+ else if (item.type == 'Text') {
202
+ return item.value;
203
+ }
204
+ })
205
+ .join('');
206
+ if (content) {
207
+ return Promise.all([content].map(item => client.channelsMessagesPost(event.ChannelId, {
208
+ content: item,
209
+ msg_id: event.MessageId
210
+ })));
211
+ }
212
+ const images = val.filter(item => item.type == 'Image').map(item => item.value);
213
+ if (images) {
214
+ return Promise.all(images.map(item => client.postImage(event.ChannelId, {
215
+ msg_id: event.MessageId,
216
+ image: item
217
+ })));
218
+ }
219
+ return [];
220
+ };
221
+
222
+ export { AT_MESSAGE_CREATE, C2C_MESSAGE_CREATE, DIRECT_MESSAGE_CREATE, GROUP_AT_MESSAGE_CREATE, MESSAGE_CREATE };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/qq-bot",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "qq-bot",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",