@alemonjs/qq-bot 2.1.0-alpha.21 → 2.1.0-alpha.23

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/sends.js CHANGED
@@ -4,143 +4,74 @@ import axios from 'axios';
4
4
 
5
5
  const createButtonsData = (rows) => {
6
6
  let id = 0;
7
- const data = {
8
- rows: rows.map(row => {
9
- const val = row.value;
10
- return {
11
- buttons: val.map(button => {
12
- const value = button.value;
13
- const options = button.options;
14
- id++;
15
- const typing = options?.type ?? 'command';
16
- const map = {
17
- command: 2,
18
- link: 0,
19
- call: 1
20
- };
21
- return {
22
- id: String(id),
23
- render_data: {
24
- label: typeof value === 'object' ? value.title : value,
25
- visited_label: typeof value === 'object' ? value.label : value,
26
- style: 0
27
- },
28
- action: {
29
- type: map[typing],
30
- permission: {
31
- type: 2
32
- },
33
- unsupport_tips: options?.toolTip ?? '',
34
- data: options?.data ?? '',
35
- at_bot_show_channel_list: options.showList ?? false,
36
- enter: options?.autoEnter ?? false
37
- }
38
- };
39
- })
40
- };
41
- })
42
- };
43
- return data;
44
- };
45
- const createArkCardData = (value) => {
46
- return {
47
- template_id: 24,
48
- kv: [
49
- {
50
- key: '#DESC#',
51
- value: value.decs
52
- },
53
- {
54
- key: '#PROMPT#',
55
- value: value.prompt
56
- },
57
- {
58
- key: '#TITLE#',
59
- value: value.title
60
- },
61
- {
62
- key: '#METADESC#',
63
- value: value.metadecs
64
- },
65
- {
66
- key: '#IMG#',
67
- value: value.cover
68
- },
69
- {
70
- key: '#LINK#',
71
- value: value.link
72
- },
73
- {
74
- key: '#SUBTITLE#',
75
- value: value.subtitle
76
- }
77
- ]
78
- };
79
- };
80
- const createArkBigCardData = (value) => {
81
7
  return {
82
- template_id: 37,
83
- kv: [
84
- {
85
- key: '#PROMPT#',
86
- value: value.prompt
87
- },
88
- {
89
- key: '#METATITLE#',
90
- value: value.title
91
- },
92
- {
93
- key: '#METASUBTITLE#',
94
- value: value.subtitle
95
- },
96
- {
97
- key: '#METACOVER#',
98
- value: value.cover
99
- },
100
- {
101
- key: '#METAURL#',
102
- value: value.link
103
- }
104
- ]
8
+ rows: rows.map(row => ({
9
+ buttons: row.value.map(button => {
10
+ const value = button.value;
11
+ const options = button.options;
12
+ id++;
13
+ const typing = options?.type ?? 'command';
14
+ const typeMap = { command: 2, link: 0, call: 1 };
15
+ return {
16
+ id: String(id),
17
+ render_data: {
18
+ label: value,
19
+ visited_label: value,
20
+ style: 0
21
+ },
22
+ action: {
23
+ type: typeMap[typing],
24
+ permission: { type: 2 },
25
+ unsupport_tips: options?.toolTip ?? '',
26
+ data: options?.data ?? '',
27
+ at_bot_show_channel_list: false,
28
+ enter: options?.autoEnter ?? false
29
+ }
30
+ };
31
+ })
32
+ }))
105
33
  };
106
34
  };
107
- const createArkList = (value) => {
35
+ const createArkCardData = (value) => ({
36
+ template_id: 24,
37
+ kv: [
38
+ { key: '#DESC#', value: value.decs },
39
+ { key: '#PROMPT#', value: value.prompt },
40
+ { key: '#TITLE#', value: value.title },
41
+ { key: '#METADESC#', value: value.metadecs },
42
+ { key: '#IMG#', value: value.cover },
43
+ { key: '#LINK#', value: value.link },
44
+ { key: '#SUBTITLE#', value: value.subtitle }
45
+ ]
46
+ });
47
+ const createArkBigCardData = (value) => ({
48
+ template_id: 37,
49
+ kv: [
50
+ { key: '#PROMPT#', value: value.prompt },
51
+ { key: '#METATITLE#', value: value.title },
52
+ { key: '#METASUBTITLE#', value: value.subtitle },
53
+ { key: '#METACOVER#', value: value.cover },
54
+ { key: '#METAURL#', value: value.link }
55
+ ]
56
+ });
57
+ const createArkListData = (value) => {
108
58
  const [tip, data] = value;
109
59
  return {
110
60
  template_id: 23,
111
61
  kv: [
112
- {
113
- key: '#DESC#',
114
- value: tip.value.desc
115
- },
116
- {
117
- key: '#PROMPT#',
118
- value: tip.value.prompt
119
- },
62
+ { key: '#DESC#', value: tip.value.desc },
63
+ { key: '#PROMPT#', value: tip.value.prompt },
120
64
  {
121
65
  key: '#LIST#',
122
66
  obj: data.value.map(item => {
123
- const value = item.value;
124
- if (typeof value === 'string') {
125
- return {
126
- obj_kv: [
127
- {
128
- key: 'desc',
129
- value: value
130
- }
131
- ]
132
- };
67
+ const v = item.value;
68
+ if (typeof v === 'string') {
69
+ return { obj_kv: [{ key: 'desc', value: v }] };
133
70
  }
134
71
  return {
135
72
  obj_kv: [
136
- {
137
- key: 'desc',
138
- value: value.title
139
- },
140
- {
141
- key: 'link',
142
- value: value.link
143
- }
73
+ { key: 'desc', value: v.title },
74
+ { key: 'link', value: v.link }
144
75
  ]
145
76
  };
146
77
  })
@@ -148,7 +79,7 @@ const createArkList = (value) => {
148
79
  ]
149
80
  };
150
81
  };
151
- const map = {
82
+ const mdFormatters = {
152
83
  'MD.title': value => `# ${value}`,
153
84
  'MD.subtitle': value => `## ${value}`,
154
85
  'MD.text': value => `${value} `,
@@ -159,579 +90,276 @@ const map = {
159
90
  'MD.strikethrough': value => `~~${value}~~ `,
160
91
  'MD.blockquote': value => `\n> ${value}`,
161
92
  'MD.newline': () => '\n',
162
- 'MD.link': value => `[🔗${value.text}](${value.url}) `
93
+ 'MD.link': value => `[🔗${value.text}](${value.url}) `,
94
+ 'MD.image': (value, options) => `\n![text #${options?.width || 208}px #${options?.height || 320}px](${value})\n`,
95
+ 'MD.mention': (value, options) => {
96
+ const { belong } = options || {};
97
+ if (belong === 'channel') {
98
+ return '';
99
+ }
100
+ if (belong === 'all') {
101
+ return '<qqbot-at-everyone />';
102
+ }
103
+ if (value === 'everyone' || value === 'all' || value === '' || typeof value !== 'string') {
104
+ return '<qqbot-at-everyone />';
105
+ }
106
+ return `<qqbot-at-user id="${value}" />`;
107
+ },
108
+ 'MD.content': value => `${value}`,
109
+ 'MD.button': (title, options) => {
110
+ const { data } = options || {};
111
+ return `<qqbot-cmd-input text="${data}" show="${title}" />`;
112
+ }
163
113
  };
164
114
  const createMarkdownText = (data) => {
165
- const content = data
115
+ return data
166
116
  .map(mdItem => {
167
- if (map[mdItem.type]) {
168
- const value = mdItem?.value;
169
- return map[mdItem.type](value);
117
+ if (mdFormatters[mdItem.type]) {
118
+ return mdFormatters[mdItem.type](mdItem?.value, mdItem?.options);
170
119
  }
171
120
  if (mdItem.type === 'MD.image') {
172
121
  return `\n![text #${mdItem.options?.width || 208}px #${mdItem.options?.height || 320}px](${mdItem.value})\n`;
173
122
  }
174
- else if (mdItem.type === 'MD.list') {
123
+ if (mdItem.type === 'MD.list' && typeof mdItem.value !== 'string') {
175
124
  const listStr = mdItem.value.map(listItem => {
176
- if (typeof listItem.value === 'object') {
177
- return `\n${listItem.value.index}. ${listItem.value.text}`;
178
- }
179
- return `\n- ${listItem.value}`;
125
+ return typeof listItem.value === 'object' ? `\n${listItem.value.index}. ${listItem.value.text}` : `\n- ${listItem.value}`;
180
126
  });
181
127
  return `${listStr}\n`;
182
128
  }
183
- else if (mdItem.type === 'MD.code') {
129
+ if (mdItem.type === 'MD.code') {
184
130
  const language = mdItem?.options?.language || '';
185
131
  return `\n\`\`\`${language}\n${mdItem.value}\n\`\`\`\n`;
186
132
  }
187
- else {
188
- const value = mdItem['value'] || '';
189
- return String(value);
133
+ return String(mdItem['value'] || '');
134
+ })
135
+ .join('');
136
+ };
137
+ const formatMention = (item, mode) => {
138
+ if (mode === 'guild-direct') {
139
+ return '';
140
+ }
141
+ const value = item.value;
142
+ const isEmptyMention = value === 'everyone' || value === 'all' || value === '' || typeof value !== 'string';
143
+ if (mode === 'guild-public') {
144
+ if (isEmptyMention) {
145
+ return '@everyone';
190
146
  }
147
+ if (item.options?.belong === 'user') {
148
+ return `<@!${value}>`;
149
+ }
150
+ if (item.options?.belong === 'channel') {
151
+ return `<#${value}>`;
152
+ }
153
+ return '';
154
+ }
155
+ if (isEmptyMention) {
156
+ return '';
157
+ }
158
+ if (item.options?.belong === 'user') {
159
+ return `<@${value}>`;
160
+ }
161
+ return '';
162
+ };
163
+ const extractContent = (val, mode) => {
164
+ return val
165
+ .filter(item => item.type === 'Mention' || item.type === 'Text' || item.type === 'Link')
166
+ .map(item => {
167
+ if (item.type === 'Link') {
168
+ return `[${item.value}](${item?.options?.link})`;
169
+ }
170
+ if (item.type === 'Mention') {
171
+ return formatMention(item, mode);
172
+ }
173
+ if (item.type === 'Text') {
174
+ return item.value;
175
+ }
176
+ return '';
191
177
  })
192
178
  .join('');
193
- return content;
194
179
  };
195
- const GROUP_AT_MESSAGE_CREATE = async (client, event, val) => {
196
- const baseParams = {};
197
- if (event.tag === 'INTERACTION_CREATE_GROUP') {
198
- baseParams['event_id'] = event.MessageId;
180
+ const buildBaseParams = (tag, messageId, interactionTag) => {
181
+ if (tag === interactionTag) {
182
+ return { event_id: messageId };
199
183
  }
200
- else {
201
- baseParams['msg_id'] = event.MessageId;
184
+ return { msg_id: messageId };
185
+ };
186
+ const buildMdAndButtonsParams = (val) => {
187
+ const items = val.filter(item => item.type === 'Markdown' || item.type === 'BT.group' || item.type === 'ButtonTemplate');
188
+ if (items.length === 0) {
189
+ return null;
202
190
  }
203
- try {
204
- const content = val
205
- .filter(item => item.type === 'Mention' || item.type === 'Text' || item.type === 'Link')
206
- .map(item => {
207
- if (item.type === 'Link') {
208
- return `[${item.value}](${item?.options?.link})`;
209
- }
210
- else if (item.type === 'Mention') {
211
- if (item.value === 'everyone' || item.value === 'all' || item.value === '' || typeof item.value !== 'string') {
212
- return '';
213
- }
214
- if (item.options?.belong === 'user') {
215
- return `<@${item.value}>`;
216
- }
217
- return '';
191
+ const params = {};
192
+ for (const item of items) {
193
+ if (item.type === 'ButtonTemplate') {
194
+ if (item?.value) {
195
+ params['keyboard'] = { id: item.value };
218
196
  }
219
- else if (item.type === 'Text') {
220
- return item.value;
221
- }
222
- })
223
- .join('');
224
- const images = val.filter(item => item.type === 'Image' || item.type === 'ImageFile' || item.type === 'ImageURL');
225
- if (images && images.length > 0) {
226
- let url = '';
227
- for (let i = 0; i < images.length; i++) {
228
- if (url) {
229
- break;
230
- }
231
- const item = images[i];
232
- if (item.type === 'ImageURL') {
233
- url = item.value;
234
- }
235
- else if (item.type === 'ImageFile' || item.type === 'Image') {
236
- const getFileBase64 = () => readFileSync(item.value, 'base64');
237
- const file_data = item.type === 'ImageFile' ? getFileBase64() : item.value;
238
- const file_info = await client
239
- .postRichMediaByGroup(event.ChannelId, {
240
- file_type: 1,
241
- file_data: file_data
242
- })
243
- .then(res => res?.file_info);
244
- if (file_info) {
245
- url = file_info;
246
- }
247
- }
197
+ }
198
+ else if (item.type === 'BT.group' && typeof item?.value !== 'string') {
199
+ params['keyboard'] = { content: createButtonsData(item.value) };
200
+ }
201
+ else if (item.type === 'Markdown' && typeof item?.value !== 'string') {
202
+ const content = createMarkdownText(item.value);
203
+ if (content) {
204
+ params['markdown'] = { content };
248
205
  }
249
- const res = await client.groupOpenMessages(event.ChannelId, {
250
- content: content,
251
- media: {
252
- file_info: url
253
- },
254
- msg_type: 7,
255
- ...baseParams
256
- });
257
- return [
258
- createResult(ResultCode.Ok, 'client.groupOpenMessages', {
259
- id: res.id
260
- })
261
- ];
262
206
  }
263
- const mdAndButtons = val.filter(item => item.type === 'Markdown' || item.type === 'BT.group' || item.type === 'ButtonTemplate');
264
- if (mdAndButtons && mdAndButtons.length > 0) {
265
- const params = {};
266
- mdAndButtons.forEach(item => {
267
- if (item.type === 'ButtonTemplate') {
268
- const template_id = item?.value;
269
- if (template_id) {
270
- params['keyboard'] = {
271
- id: template_id
272
- };
273
- }
274
- }
275
- else if (item.type === 'BT.group') {
276
- const rows = item.value;
277
- const content = createButtonsData(rows);
278
- params['keyboard'] = {
279
- content: content
280
- };
281
- }
282
- else if (item.type === 'Markdown') {
283
- const content = createMarkdownText(item.value);
284
- if (content) {
285
- params['markdown'] = {
286
- content: content
287
- };
288
- }
289
- }
290
- });
291
- const res = await client.groupOpenMessages(event.ChannelId, {
292
- content: content,
293
- msg_type: 2,
294
- ...params,
295
- ...baseParams
296
- });
297
- return [createResult(ResultCode.Ok, 'client.groupOpenMessages', { id: res.id })];
207
+ }
208
+ return params;
209
+ };
210
+ const buildArkParams = (val) => {
211
+ const items = val.filter(item => item.type === 'Ark.BigCard' || item.type === 'Ark.Card' || item.type === 'Ark.list');
212
+ if (items.length === 0) {
213
+ return null;
214
+ }
215
+ const params = {};
216
+ for (const item of items) {
217
+ if (item.type === 'Ark.Card' && typeof item?.value !== 'string') {
218
+ params['ark'] = createArkCardData(item.value);
298
219
  }
299
- const ark = val.filter(item => item.type === 'Ark.BigCard' || item.type === 'Ark.Card' || item.type === 'Ark.list');
300
- if (ark && ark.length > 0) {
301
- const params = {};
302
- ark.forEach(item => {
303
- if (item.type === 'Ark.Card') {
304
- const arkData = createArkCardData(item.value);
305
- params['ark'] = arkData;
306
- }
307
- else if (item.type === 'Ark.BigCard') {
308
- const arkData = createArkBigCardData(item.value);
309
- params['ark'] = arkData;
310
- }
311
- else if (item.type === 'Ark.list') {
312
- const arkData = createArkList(item.value);
313
- params['ark'] = arkData;
314
- }
315
- });
316
- const res = await client.groupOpenMessages(event.ChannelId, {
317
- content: content,
318
- msg_type: 3,
319
- ...params,
320
- ...baseParams
321
- });
322
- return [createResult(ResultCode.Ok, 'client.groupOpenMessages', { id: res.id })];
220
+ else if (item.type === 'Ark.BigCard' && typeof item?.value !== 'string') {
221
+ params['ark'] = createArkBigCardData(item.value);
323
222
  }
324
- if (content) {
325
- const res = await client.groupOpenMessages(event.ChannelId, {
326
- content: content,
327
- msg_type: 0,
328
- ...baseParams
329
- });
330
- return [
331
- createResult(ResultCode.Ok, 'client.groupOpenMessages', {
332
- id: res.id
333
- })
334
- ];
223
+ else if (item.type === 'Ark.list' && typeof item?.value !== 'string') {
224
+ params['ark'] = createArkListData(item.value);
335
225
  }
336
226
  }
337
- catch (err) {
338
- return [createResult(ResultCode.Fail, err?.response?.data ?? err?.message ?? err, null)];
227
+ return params;
228
+ };
229
+ const filterImages = (val) => {
230
+ return val.filter(item => item.type === 'Image' || item.type === 'ImageFile' || item.type === 'ImageURL');
231
+ };
232
+ const resolveRichMediaUrl = async (images, uploadMedia) => {
233
+ let url = '';
234
+ for (const item of images) {
235
+ if (url) {
236
+ break;
237
+ }
238
+ if (item.type === 'ImageURL') {
239
+ url = item.value;
240
+ }
241
+ else if (item.type === 'ImageFile' || item.type === 'Image') {
242
+ const fileData = item.type === 'ImageFile' ? readFileSync(item.value, 'base64') : item.value;
243
+ const fileInfo = await uploadMedia({ file_type: 1, file_data: fileData }).then(res => res?.file_info);
244
+ if (fileInfo) {
245
+ url = fileInfo;
246
+ }
247
+ }
339
248
  }
340
- return [];
249
+ return url;
341
250
  };
342
- const C2C_MESSAGE_CREATE = async (client, event, val) => {
343
- const baseParams = {};
344
- if (event.tag === 'INTERACTION_CREATE_C2C') {
345
- baseParams['event_id'] = event.MessageId;
251
+ const sendOpenApiMessage = async (content, val, baseParams, uploadMedia, sendMessage, label) => {
252
+ const images = filterImages(val);
253
+ if (images.length > 0) {
254
+ const url = await resolveRichMediaUrl(images, uploadMedia);
255
+ const res = await sendMessage({
256
+ content,
257
+ media: { file_info: url },
258
+ msg_type: 7,
259
+ ...baseParams
260
+ });
261
+ return [createResult(ResultCode.Ok, label, { id: res.id })];
346
262
  }
347
- else {
348
- baseParams['msg_id'] = event.MessageId;
263
+ const mdParams = buildMdAndButtonsParams(val);
264
+ if (mdParams) {
265
+ const res = await sendMessage({ content, msg_type: 2, ...mdParams, ...baseParams });
266
+ return [createResult(ResultCode.Ok, label, { id: res.id })];
349
267
  }
350
- try {
351
- const content = val
352
- .filter(item => item.type === 'Mention' || item.type === 'Text' || item.type === 'Link')
353
- .map(item => {
354
- if (item.type === 'Link') {
355
- return `[${item.value}](${item?.options?.link})`;
356
- }
357
- else if (item.type === 'Mention') {
358
- if (item.value === 'everyone' || item.value === 'all' || item.value === '' || typeof item.value !== 'string') {
359
- return '';
360
- }
361
- if (item.options?.belong === 'user') {
362
- return `<@${item.value}>`;
363
- }
364
- return '';
268
+ const arkParams = buildArkParams(val);
269
+ if (arkParams) {
270
+ const res = await sendMessage({ content, msg_type: 3, ...arkParams, ...baseParams });
271
+ return [createResult(ResultCode.Ok, label, { id: res.id })];
272
+ }
273
+ if (content) {
274
+ const res = await sendMessage({ content, msg_type: 0, ...baseParams });
275
+ return [createResult(ResultCode.Ok, label, { id: res.id })];
276
+ }
277
+ return [];
278
+ };
279
+ const resolveImageBuffer = async (images) => {
280
+ for (const item of images) {
281
+ if (item.type === 'ImageURL') {
282
+ return await axios.get(item.value, { responseType: 'arraybuffer' }).then(res => res?.data);
283
+ }
284
+ if (item.type === 'ImageFile') {
285
+ return readFileSync(item.value);
286
+ }
287
+ if (typeof item.value === 'string') {
288
+ if (item.value.startsWith('https://') || item.value.startsWith('http://')) {
289
+ return await axios.get(item.value, { responseType: 'arraybuffer' }).then(res => res?.data);
365
290
  }
366
- else if (item.type === 'Text') {
367
- return item.value;
291
+ if (item.value.startsWith('base64://')) {
292
+ return Buffer.from(item.value.replace('base64://', ''), 'base64');
368
293
  }
369
- })
370
- .join('');
371
- const images = val.filter(item => item.type === 'Image' || item.type === 'ImageFile' || item.type === 'ImageURL');
372
- if (images && images.length > 0) {
373
- let url = '';
374
- for (let i = 0; i < images.length; i++) {
375
- if (url) {
376
- break;
377
- }
378
- const item = images[i];
379
- if (item.type === 'ImageURL') {
380
- url = item.value;
381
- }
382
- else if (item.type === 'ImageFile' || item.type === 'Image') {
383
- const getFileBase64 = () => readFileSync(item.value, 'base64');
384
- const file_data = item.type === 'ImageFile' ? getFileBase64() : item.value;
385
- const file_info = await client
386
- .postRichMediaByUser(event.UserId, {
387
- file_type: 1,
388
- file_data: file_data
389
- })
390
- .then(res => res?.file_info);
391
- if (file_info) {
392
- url = file_info;
393
- }
394
- }
294
+ if (item.value.startsWith('file://')) {
295
+ return readFileSync(item.value.replace('file://', ''));
395
296
  }
396
- const res = await client.usersOpenMessages(event.UserId, {
397
- content: content,
398
- media: {
399
- file_info: url
400
- },
401
- msg_type: 7,
402
- ...baseParams
403
- });
404
- return [
405
- createResult(ResultCode.Ok, 'client.usersOpenMessages', {
406
- id: res.id
407
- })
408
- ];
297
+ return Buffer.from(item.value, 'base64');
409
298
  }
410
- const mdAndButtons = val.filter(item => item.type === 'Markdown' || item.type === 'BT.group' || item.type === 'ButtonTemplate');
411
- if (mdAndButtons && mdAndButtons.length > 0) {
412
- const params = {};
413
- mdAndButtons.forEach(item => {
414
- if (item.type === 'ButtonTemplate') {
415
- const template_id = item?.value;
416
- if (template_id) {
417
- params['keyboard'] = {
418
- id: template_id
419
- };
420
- }
421
- }
422
- else if (item.type === 'BT.group') {
423
- const rows = item.value;
424
- const content = createButtonsData(rows);
425
- params['keyboard'] = {
426
- content: content
427
- };
428
- }
429
- else if (item.type === 'Markdown') {
430
- const content = createMarkdownText(item.value);
431
- if (content) {
432
- params['markdown'] = {
433
- content: content
434
- };
435
- }
436
- }
437
- });
438
- const res = await client.usersOpenMessages(event.UserId, {
439
- content: content,
440
- msg_type: 2,
441
- ...params,
442
- ...baseParams
443
- });
444
- return [createResult(ResultCode.Ok, 'client.usersOpenMessages', { id: res.id })];
445
- }
446
- const ark = val.filter(item => item.type === 'Ark.BigCard' || item.type === 'Ark.Card' || item.type === 'Ark.list');
447
- if (ark && ark.length > 0) {
448
- const params = {};
449
- ark.forEach(item => {
450
- if (item.type === 'Ark.Card') {
451
- const arkData = createArkCardData(item.value);
452
- params['ark'] = arkData;
453
- }
454
- else if (item.type === 'Ark.BigCard') {
455
- const arkData = createArkBigCardData(item.value);
456
- params['ark'] = arkData;
457
- }
458
- else if (item.type === 'Ark.list') {
459
- const arkData = createArkList(item.value);
460
- params['ark'] = arkData;
461
- }
462
- });
463
- const res = await client.usersOpenMessages(event.UserId, {
464
- content: content,
465
- msg_type: 3,
466
- ...params,
467
- ...baseParams
468
- });
469
- return [createResult(ResultCode.Ok, 'client.usersOpenMessages', { id: res.id })];
470
- }
471
- if (content) {
472
- const res = await client.usersOpenMessages(event.UserId, {
473
- content: content,
474
- msg_type: 0,
475
- ...baseParams
476
- });
477
- return [
478
- createResult(ResultCode.Ok, 'client.usersOpenMessages', {
479
- id: res.id
480
- })
481
- ];
299
+ if (Buffer.isBuffer(item.value)) {
300
+ return item.value;
482
301
  }
483
302
  }
484
- catch (err) {
485
- return [createResult(ResultCode.Fail, err?.response?.data ?? err?.message ?? err, null)];
303
+ return null;
304
+ };
305
+ const sendGuildMessage = async (content, val, baseParams, sendMessage, label) => {
306
+ const images = filterImages(val);
307
+ if (images.length > 0) {
308
+ const imageBuffer = await resolveImageBuffer(images);
309
+ const res = await sendMessage({ content, ...baseParams }, imageBuffer);
310
+ return [createResult(ResultCode.Ok, label, { id: res?.id })];
311
+ }
312
+ const mdParams = buildMdAndButtonsParams(val);
313
+ if (mdParams) {
314
+ const res = await sendMessage({ content: '', ...mdParams, ...baseParams });
315
+ return [createResult(ResultCode.Ok, label, { id: res.id })];
316
+ }
317
+ const arkParams = buildArkParams(val);
318
+ if (arkParams) {
319
+ const res = await sendMessage({ content, ...arkParams, ...baseParams });
320
+ return [createResult(ResultCode.Ok, label, { id: res.id })];
321
+ }
322
+ if (content) {
323
+ const res = await sendMessage({ content, ...baseParams });
324
+ return [createResult(ResultCode.Ok, label, { id: res?.id })];
486
325
  }
487
326
  return [];
488
327
  };
489
- const DIRECT_MESSAGE_CREATE = async (client, event, val) => {
490
- const baseParams = {};
491
- if (event.tag === 'INTERACTION_CREATE_GUILD') {
492
- baseParams['event_id'] = event.MessageId;
328
+ const GROUP_AT_MESSAGE_CREATE = async (client, event, val) => {
329
+ const baseParams = buildBaseParams(event.tag, event.MessageId, 'INTERACTION_CREATE_GROUP');
330
+ const content = extractContent(val, 'group');
331
+ try {
332
+ return await sendOpenApiMessage(content, val, baseParams, data => client.postRichMediaByGroup(event.ChannelId, data), data => client.groupOpenMessages(event.ChannelId, data), 'client.groupOpenMessages');
493
333
  }
494
- else {
495
- baseParams['msg_id'] = event.MessageId;
334
+ catch (err) {
335
+ return [createResult(ResultCode.Fail, err?.response?.data ?? err?.message ?? err, null)];
496
336
  }
337
+ };
338
+ const C2C_MESSAGE_CREATE = async (client, event, val) => {
339
+ const baseParams = buildBaseParams(event.tag, event.MessageId, 'INTERACTION_CREATE_C2C');
340
+ const content = extractContent(val, 'group');
497
341
  try {
498
- const content = val
499
- .filter(item => item.type === 'Mention' || item.type === 'Text' || item.type === 'Link')
500
- .map(item => {
501
- if (item.type === 'Link') {
502
- return `[${item.value}](${item?.options?.link})`;
503
- }
504
- if (item.type === 'Text') {
505
- return item.value;
506
- }
507
- return '';
508
- })
509
- .join('');
510
- const images = val.filter(item => item.type === 'Image' || item.type === 'ImageFile' || item.type === 'ImageURL');
511
- if (images && images.length > 0) {
512
- let imageBuffer = null;
513
- for (let i = 0; i < images.length; i++) {
514
- if (imageBuffer) {
515
- break;
516
- }
517
- const item = images[i];
518
- if (item.value === 'ImageURL') {
519
- const data = await axios
520
- .get(item.value, {
521
- responseType: 'arraybuffer'
522
- })
523
- .then(res => res?.data);
524
- imageBuffer = data;
525
- }
526
- else {
527
- const file_data = item.type === 'ImageFile' ? readFileSync(item.value) : Buffer.from(item.value, 'base64');
528
- imageBuffer = file_data;
529
- }
530
- }
531
- const res = await client.dmsMessages(event.UserId, {
532
- content: content,
533
- ...baseParams
534
- }, imageBuffer);
535
- return [createResult(ResultCode.Ok, 'client.postDirectImage', { id: res?.id })];
536
- }
537
- const mdAndButtons = val.filter(item => item.type === 'Markdown' || item.type === 'BT.group' || item.type === 'ButtonTemplate');
538
- if (mdAndButtons && mdAndButtons.length > 0) {
539
- const params = {};
540
- mdAndButtons.forEach(item => {
541
- if (item.type === 'ButtonTemplate') {
542
- const template_id = item?.value;
543
- if (template_id) {
544
- params['keyboard'] = {
545
- id: template_id
546
- };
547
- }
548
- }
549
- else if (item.type === 'BT.group') {
550
- const rows = item.value;
551
- const content = createButtonsData(rows);
552
- params['keyboard'] = {
553
- content: content
554
- };
555
- }
556
- else if (item.type === 'Markdown') {
557
- const content = createMarkdownText(item.value);
558
- if (content) {
559
- params['markdown'] = {
560
- content: content
561
- };
562
- }
563
- }
564
- });
565
- const res = await client.dmsMessages(event.UserId, {
566
- content: '',
567
- ...params,
568
- ...baseParams
569
- });
570
- return [createResult(ResultCode.Ok, 'client.dmsMessage', { id: res.id })];
571
- }
572
- const ark = val.filter(item => item.type === 'Ark.BigCard' || item.type === 'Ark.Card' || item.type === 'Ark.list');
573
- if (ark && ark.length > 0) {
574
- const params = {};
575
- ark.forEach(item => {
576
- if (item.type === 'Ark.Card') {
577
- const arkData = createArkCardData(item.value);
578
- params['ark'] = arkData;
579
- }
580
- else if (item.type === 'Ark.BigCard') {
581
- const arkData = createArkBigCardData(item.value);
582
- params['ark'] = arkData;
583
- }
584
- else if (item.type === 'Ark.list') {
585
- const arkData = createArkList(item.value);
586
- params['ark'] = arkData;
587
- }
588
- });
589
- const res = await client.dmsMessages(event.UserId, {
590
- content: content,
591
- ...params,
592
- ...baseParams
593
- });
594
- return [createResult(ResultCode.Ok, 'client.dmsMessage', { id: res.id })];
595
- }
596
- if (content) {
597
- const res = await client.dmsMessages(event.UserId, {
598
- content: content,
599
- ...baseParams
600
- });
601
- return [createResult(ResultCode.Ok, 'client.dmsMessage', { id: res?.id })];
602
- }
603
- return [];
342
+ return await sendOpenApiMessage(content, val, baseParams, data => client.postRichMediaByUser(event.UserId, data), data => client.usersOpenMessages(event.UserId, data), 'client.usersOpenMessages');
604
343
  }
605
344
  catch (err) {
606
345
  return [createResult(ResultCode.Fail, err?.response?.data ?? err?.message ?? err, null)];
607
346
  }
608
347
  };
609
- const MESSAGE_CREATE = async (client, event, val) => {
610
- const baseParams = {};
611
- if (event.tag === 'INTERACTION_CREATE_GUILD') {
612
- baseParams['event_id'] = event.MessageId;
348
+ const DIRECT_MESSAGE_CREATE = async (client, event, val) => {
349
+ const baseParams = buildBaseParams(event.tag, event.MessageId, 'INTERACTION_CREATE_GUILD');
350
+ const content = extractContent(val, 'guild-direct');
351
+ try {
352
+ return await sendGuildMessage(content, val, baseParams, (data, buf) => client.dmsMessages(event.UserId, data, buf), 'client.dmsMessage');
613
353
  }
614
- else {
615
- baseParams['msg_id'] = event.MessageId;
354
+ catch (err) {
355
+ return [createResult(ResultCode.Fail, err?.response?.data ?? err?.message ?? err, null)];
616
356
  }
357
+ };
358
+ const MESSAGE_CREATE = async (client, event, val) => {
359
+ const baseParams = buildBaseParams(event.tag, event.MessageId, 'INTERACTION_CREATE_GUILD');
360
+ const content = extractContent(val, 'guild-public');
617
361
  try {
618
- const content = val
619
- .filter(item => item.type === 'Mention' || item.type === 'Text' || item.type === 'Link')
620
- .map(item => {
621
- if (item.type === 'Link') {
622
- return `[${item.value}](${item?.options?.link})`;
623
- }
624
- if (item.type === 'Mention') {
625
- if (item.value === 'everyone' || item.value === 'all' || item.value === '' || typeof item.value !== 'string') {
626
- return '@everyone';
627
- }
628
- if (item.options?.belong === 'user') {
629
- return `<@!${item.value}>`;
630
- }
631
- else if (item.options?.belong === 'channel') {
632
- return `<#${item.value}>`;
633
- }
634
- return '';
635
- }
636
- else if (item.type === 'Text') {
637
- return item.value;
638
- }
639
- })
640
- .join('');
641
- const images = val.filter(item => item.type === 'Image' || item.type === 'ImageFile' || item.type === 'ImageURL');
642
- if (images && images.length > 0) {
643
- let imageBuffer = null;
644
- for (let i = 0; i < images.length; i++) {
645
- if (imageBuffer) {
646
- break;
647
- }
648
- const item = images[i];
649
- if (item.value === 'ImageURL') {
650
- const data = await axios
651
- .get(item.value, {
652
- responseType: 'arraybuffer'
653
- })
654
- .then(res => res?.data);
655
- imageBuffer = data;
656
- }
657
- else {
658
- const file_data = item.type === 'ImageFile' ? readFileSync(item.value) : Buffer.from(item.value, 'base64');
659
- imageBuffer = file_data;
660
- }
661
- }
662
- const res = await client.channelsMessages(event.ChannelId, {
663
- content: content,
664
- ...baseParams
665
- }, imageBuffer);
666
- return [createResult(ResultCode.Ok, 'client.postImage', { id: res?.id })];
667
- }
668
- const mdAndButtons = val.filter(item => item.type === 'Markdown' || item.type === 'BT.group' || item.type === 'ButtonTemplate');
669
- if (mdAndButtons && mdAndButtons.length > 0) {
670
- const params = {};
671
- mdAndButtons.forEach(item => {
672
- if (item.type === 'ButtonTemplate') {
673
- const template_id = item?.value;
674
- if (template_id) {
675
- params['keyboard'] = {
676
- id: template_id
677
- };
678
- }
679
- }
680
- else if (item.type === 'BT.group') {
681
- const rows = item.value;
682
- const content = createButtonsData(rows);
683
- params['keyboard'] = {
684
- content: content
685
- };
686
- }
687
- else if (item.type === 'Markdown') {
688
- const content = createMarkdownText(item.value);
689
- if (content) {
690
- params['markdown'] = {
691
- content: content
692
- };
693
- }
694
- }
695
- });
696
- const res = await client.channelsMessages(event.ChannelId, {
697
- content: '',
698
- ...params,
699
- ...baseParams
700
- });
701
- return [createResult(ResultCode.Ok, 'client.channelsMessagesPost', { id: res.id })];
702
- }
703
- const ark = val.filter(item => item.type === 'Ark.BigCard' || item.type === 'Ark.Card' || item.type === 'Ark.list');
704
- if (ark && ark.length > 0) {
705
- const params = {};
706
- ark.forEach(item => {
707
- if (item.type === 'Ark.Card') {
708
- const arkData = createArkCardData(item.value);
709
- params['ark'] = arkData;
710
- }
711
- else if (item.type === 'Ark.BigCard') {
712
- const arkData = createArkBigCardData(item.value);
713
- params['ark'] = arkData;
714
- }
715
- else if (item.type === 'Ark.list') {
716
- const arkData = createArkList(item.value);
717
- params['ark'] = arkData;
718
- }
719
- });
720
- const res = await client.channelsMessages(event.ChannelId, {
721
- content: content,
722
- msg_id: event.MessageId,
723
- ...params
724
- });
725
- return [createResult(ResultCode.Ok, 'client.channelsMessagesPost', { id: res.id })];
726
- }
727
- if (content) {
728
- const res = await client.channelsMessages(event.ChannelId, {
729
- content: content,
730
- ...baseParams
731
- });
732
- return [createResult(ResultCode.Ok, 'client.channelsMessagesPost', { id: res?.id })];
733
- }
734
- return [];
362
+ return await sendGuildMessage(content, val, baseParams, (data, buf) => client.channelsMessages(event.ChannelId, data, buf), 'client.channelsMessagesPost');
735
363
  }
736
364
  catch (err) {
737
365
  return [createResult(ResultCode.Fail, err?.response?.data ?? err?.message ?? err, null)];