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