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

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,277 @@ 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.at': (value, options) => {
95
+ const { belong } = options || {};
96
+ if (belong === 'channel') {
97
+ return '';
98
+ }
99
+ if (belong === 'all') {
100
+ return '<qqbot-at-everyone />';
101
+ }
102
+ if (value === 'everyone' || value === 'all' || value === '' || typeof value !== 'string') {
103
+ return '<qqbot-at-everyone />';
104
+ }
105
+ return `<qqbot-at-user id="${value}" />`;
106
+ },
107
+ 'MD.content': value => `${value}`,
108
+ 'MD.button': (title, options) => {
109
+ const { data } = options || {};
110
+ return `<qqbot-cmd-input text="${data}" show="${title}" />`;
111
+ }
163
112
  };
164
113
  const createMarkdownText = (data) => {
165
- const content = data
114
+ return data
166
115
  .map(mdItem => {
167
- if (map[mdItem.type]) {
168
- const value = mdItem?.value;
169
- return map[mdItem.type](value);
116
+ if (mdFormatters[mdItem.type]) {
117
+ return mdFormatters[mdItem.type](mdItem?.value, mdItem?.options);
170
118
  }
171
119
  if (mdItem.type === 'MD.image') {
172
120
  return `\n![text #${mdItem.options?.width || 208}px #${mdItem.options?.height || 320}px](${mdItem.value})\n`;
173
121
  }
174
- else if (mdItem.type === 'MD.list') {
122
+ if (mdItem.type === 'MD.list' && typeof mdItem.value !== 'string') {
175
123
  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}`;
124
+ return typeof listItem.value === 'object'
125
+ ? `\n${listItem.value.index}. ${listItem.value.text}`
126
+ : `\n- ${listItem.value}`;
180
127
  });
181
128
  return `${listStr}\n`;
182
129
  }
183
- else if (mdItem.type === 'MD.code') {
130
+ if (mdItem.type === 'MD.code') {
184
131
  const language = mdItem?.options?.language || '';
185
132
  return `\n\`\`\`${language}\n${mdItem.value}\n\`\`\`\n`;
186
133
  }
187
- else {
188
- const value = mdItem['value'] || '';
189
- return String(value);
134
+ return String(mdItem['value'] || '');
135
+ })
136
+ .join('');
137
+ };
138
+ const formatMention = (item, mode) => {
139
+ if (mode === 'guild-direct') {
140
+ return '';
141
+ }
142
+ const value = item.value;
143
+ const isEmptyMention = value === 'everyone' || value === 'all' || value === '' || typeof value !== 'string';
144
+ if (mode === 'guild-public') {
145
+ if (isEmptyMention) {
146
+ return '@everyone';
190
147
  }
148
+ if (item.options?.belong === 'user') {
149
+ return `<@!${value}>`;
150
+ }
151
+ if (item.options?.belong === 'channel') {
152
+ return `<#${value}>`;
153
+ }
154
+ return '';
155
+ }
156
+ if (isEmptyMention) {
157
+ return '';
158
+ }
159
+ if (item.options?.belong === 'user') {
160
+ return `<@${value}>`;
161
+ }
162
+ return '';
163
+ };
164
+ const extractContent = (val, mode) => {
165
+ return val
166
+ .filter(item => item.type === 'Mention' || item.type === 'Text' || item.type === 'Link')
167
+ .map(item => {
168
+ if (item.type === 'Link') {
169
+ return `[${item.value}](${item?.options?.link})`;
170
+ }
171
+ if (item.type === 'Mention') {
172
+ return formatMention(item, mode);
173
+ }
174
+ if (item.type === 'Text') {
175
+ return item.value;
176
+ }
177
+ return '';
191
178
  })
192
179
  .join('');
193
- return content;
194
180
  };
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;
181
+ const buildBaseParams = (tag, messageId, interactionTag) => {
182
+ if (tag === interactionTag) {
183
+ return { event_id: messageId };
199
184
  }
200
- else {
201
- baseParams['msg_id'] = event.MessageId;
185
+ return { msg_id: messageId };
186
+ };
187
+ const buildMdAndButtonsParams = (val) => {
188
+ const items = val.filter(item => item.type === 'Markdown' || item.type === 'BT.group' || item.type === 'ButtonTemplate');
189
+ if (items.length === 0) {
190
+ return null;
202
191
  }
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 '';
192
+ const params = {};
193
+ for (const item of items) {
194
+ if (item.type === 'ButtonTemplate') {
195
+ if (item?.value) {
196
+ params['keyboard'] = { id: item.value };
218
197
  }
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
- }
198
+ }
199
+ else if (item.type === 'BT.group' && typeof item?.value !== 'string') {
200
+ params['keyboard'] = { content: createButtonsData(item.value) };
201
+ }
202
+ else if (item.type === 'Markdown' && typeof item?.value !== 'string') {
203
+ const content = createMarkdownText(item.value);
204
+ if (content) {
205
+ params['markdown'] = { content };
248
206
  }
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
207
  }
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 })];
208
+ }
209
+ return params;
210
+ };
211
+ const buildArkParams = (val) => {
212
+ const items = val.filter(item => item.type === 'Ark.BigCard' || item.type === 'Ark.Card' || item.type === 'Ark.list');
213
+ if (items.length === 0) {
214
+ return null;
215
+ }
216
+ const params = {};
217
+ for (const item of items) {
218
+ if (item.type === 'Ark.Card' && typeof item?.value !== 'string') {
219
+ params['ark'] = createArkCardData(item.value);
298
220
  }
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 })];
221
+ else if (item.type === 'Ark.BigCard' && typeof item?.value !== 'string') {
222
+ params['ark'] = createArkBigCardData(item.value);
323
223
  }
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
- ];
224
+ else if (item.type === 'Ark.list' && typeof item?.value !== 'string') {
225
+ params['ark'] = createArkListData(item.value);
335
226
  }
336
227
  }
337
- catch (err) {
338
- return [createResult(ResultCode.Fail, err?.response?.data ?? err?.message ?? err, null)];
228
+ return params;
229
+ };
230
+ const filterImages = (val) => {
231
+ return val.filter(item => item.type === 'Image' || item.type === 'ImageFile' || item.type === 'ImageURL');
232
+ };
233
+ const resolveRichMediaUrl = async (images, uploadMedia) => {
234
+ let url = '';
235
+ for (const item of images) {
236
+ if (url) {
237
+ break;
238
+ }
239
+ if (item.type === 'ImageURL') {
240
+ url = item.value;
241
+ }
242
+ else if (item.type === 'ImageFile' || item.type === 'Image') {
243
+ const fileData = item.type === 'ImageFile' ? readFileSync(item.value, 'base64') : item.value;
244
+ const fileInfo = await uploadMedia({ file_type: 1, file_data: fileData }).then(res => res?.file_info);
245
+ if (fileInfo) {
246
+ url = fileInfo;
247
+ }
248
+ }
339
249
  }
340
- return [];
250
+ return url;
341
251
  };
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;
252
+ const sendOpenApiMessage = async (content, val, baseParams, uploadMedia, sendMessage, label) => {
253
+ const images = filterImages(val);
254
+ if (images.length > 0) {
255
+ const url = await resolveRichMediaUrl(images, uploadMedia);
256
+ const res = await sendMessage({
257
+ content,
258
+ media: { file_info: url },
259
+ msg_type: 7,
260
+ ...baseParams
261
+ });
262
+ return [createResult(ResultCode.Ok, label, { id: res.id })];
346
263
  }
347
- else {
348
- baseParams['msg_id'] = event.MessageId;
264
+ const mdParams = buildMdAndButtonsParams(val);
265
+ if (mdParams) {
266
+ const res = await sendMessage({ content, msg_type: 2, ...mdParams, ...baseParams });
267
+ return [createResult(ResultCode.Ok, label, { id: res.id })];
349
268
  }
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 '';
269
+ const arkParams = buildArkParams(val);
270
+ if (arkParams) {
271
+ const res = await sendMessage({ content, msg_type: 3, ...arkParams, ...baseParams });
272
+ return [createResult(ResultCode.Ok, label, { id: res.id })];
273
+ }
274
+ if (content) {
275
+ const res = await sendMessage({ content, msg_type: 0, ...baseParams });
276
+ return [createResult(ResultCode.Ok, label, { id: res.id })];
277
+ }
278
+ return [];
279
+ };
280
+ const resolveImageBuffer = async (images) => {
281
+ for (const item of images) {
282
+ if (item.type === 'ImageURL') {
283
+ return await axios.get(item.value, { responseType: 'arraybuffer' }).then(res => res?.data);
284
+ }
285
+ if (item.type === 'ImageFile') {
286
+ return readFileSync(item.value);
287
+ }
288
+ if (typeof item.value === 'string') {
289
+ if (item.value.startsWith('https://') || item.value.startsWith('http://')) {
290
+ return await axios.get(item.value, { responseType: 'arraybuffer' }).then(res => res?.data);
365
291
  }
366
- else if (item.type === 'Text') {
367
- return item.value;
292
+ if (item.value.startsWith('base64://')) {
293
+ return Buffer.from(item.value.replace('base64://', ''), 'base64');
368
294
  }
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
- }
295
+ if (item.value.startsWith('file://')) {
296
+ return readFileSync(item.value.replace('file://', ''));
395
297
  }
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
- ];
298
+ return Buffer.from(item.value, 'base64');
409
299
  }
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
- ];
300
+ if (Buffer.isBuffer(item.value)) {
301
+ return item.value;
482
302
  }
483
303
  }
484
- catch (err) {
485
- return [createResult(ResultCode.Fail, err?.response?.data ?? err?.message ?? err, null)];
304
+ return null;
305
+ };
306
+ const sendGuildMessage = async (content, val, baseParams, sendMessage, label) => {
307
+ const images = filterImages(val);
308
+ if (images.length > 0) {
309
+ const imageBuffer = await resolveImageBuffer(images);
310
+ const res = await sendMessage({ content, ...baseParams }, imageBuffer);
311
+ return [createResult(ResultCode.Ok, label, { id: res?.id })];
312
+ }
313
+ const mdParams = buildMdAndButtonsParams(val);
314
+ if (mdParams) {
315
+ const res = await sendMessage({ content: '', ...mdParams, ...baseParams });
316
+ return [createResult(ResultCode.Ok, label, { id: res.id })];
317
+ }
318
+ const arkParams = buildArkParams(val);
319
+ if (arkParams) {
320
+ const res = await sendMessage({ content, ...arkParams, ...baseParams });
321
+ return [createResult(ResultCode.Ok, label, { id: res.id })];
322
+ }
323
+ if (content) {
324
+ const res = await sendMessage({ content, ...baseParams });
325
+ return [createResult(ResultCode.Ok, label, { id: res?.id })];
486
326
  }
487
327
  return [];
488
328
  };
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;
329
+ const GROUP_AT_MESSAGE_CREATE = async (client, event, val) => {
330
+ const baseParams = buildBaseParams(event.tag, event.MessageId, 'INTERACTION_CREATE_GROUP');
331
+ const content = extractContent(val, 'group');
332
+ try {
333
+ return await sendOpenApiMessage(content, val, baseParams, data => client.postRichMediaByGroup(event.ChannelId, data), data => client.groupOpenMessages(event.ChannelId, data), 'client.groupOpenMessages');
493
334
  }
494
- else {
495
- baseParams['msg_id'] = event.MessageId;
335
+ catch (err) {
336
+ return [createResult(ResultCode.Fail, err?.response?.data ?? err?.message ?? err, null)];
496
337
  }
338
+ };
339
+ const C2C_MESSAGE_CREATE = async (client, event, val) => {
340
+ const baseParams = buildBaseParams(event.tag, event.MessageId, 'INTERACTION_CREATE_C2C');
341
+ const content = extractContent(val, 'group');
497
342
  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 [];
343
+ return await sendOpenApiMessage(content, val, baseParams, data => client.postRichMediaByUser(event.UserId, data), data => client.usersOpenMessages(event.UserId, data), 'client.usersOpenMessages');
604
344
  }
605
345
  catch (err) {
606
346
  return [createResult(ResultCode.Fail, err?.response?.data ?? err?.message ?? err, null)];
607
347
  }
608
348
  };
609
- const MESSAGE_CREATE = async (client, event, val) => {
610
- const baseParams = {};
611
- if (event.tag === 'INTERACTION_CREATE_GUILD') {
612
- baseParams['event_id'] = event.MessageId;
349
+ const DIRECT_MESSAGE_CREATE = async (client, event, val) => {
350
+ const baseParams = buildBaseParams(event.tag, event.MessageId, 'INTERACTION_CREATE_GUILD');
351
+ const content = extractContent(val, 'guild-direct');
352
+ try {
353
+ return await sendGuildMessage(content, val, baseParams, (data, buf) => client.dmsMessages(event.UserId, data, buf), 'client.dmsMessage');
613
354
  }
614
- else {
615
- baseParams['msg_id'] = event.MessageId;
355
+ catch (err) {
356
+ return [createResult(ResultCode.Fail, err?.response?.data ?? err?.message ?? err, null)];
616
357
  }
358
+ };
359
+ const MESSAGE_CREATE = async (client, event, val) => {
360
+ const baseParams = buildBaseParams(event.tag, event.MessageId, 'INTERACTION_CREATE_GUILD');
361
+ const content = extractContent(val, 'guild-public');
617
362
  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 [];
363
+ return await sendGuildMessage(content, val, baseParams, (data, buf) => client.channelsMessages(event.ChannelId, data, buf), 'client.channelsMessagesPost');
735
364
  }
736
365
  catch (err) {
737
366
  return [createResult(ResultCode.Fail, err?.response?.data ?? err?.message ?? err, null)];