@karinjs/adapter-qqbot 1.2.9 → 1.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/config/comment/config.json +3 -0
  2. package/config/config/config.yaml +53 -0
  3. package/lib/cli/pr.d.ts +2 -0
  4. package/lib/cli/pr.js +73 -0
  5. package/lib/core/adapter/adapter.d.ts +91 -0
  6. package/lib/core/adapter/adapter.js +144 -0
  7. package/lib/core/adapter/handler.d.ts +49 -0
  8. package/lib/core/adapter/handler.js +140 -0
  9. package/lib/core/adapter/markdown.d.ts +38 -0
  10. package/lib/core/adapter/markdown.js +220 -0
  11. package/lib/core/adapter/normal.d.ts +30 -0
  12. package/lib/core/adapter/normal.js +263 -0
  13. package/lib/core/api/index.d.ts +167 -0
  14. package/lib/core/api/index.js +180 -0
  15. package/lib/core/api/sign.d.ts +22 -0
  16. package/lib/core/api/sign.js +40 -0
  17. package/lib/core/api/types.d.ts +236 -0
  18. package/lib/core/event/conver.d.ts +10 -0
  19. package/lib/core/event/conver.js +57 -0
  20. package/lib/core/event/index.d.ts +9 -0
  21. package/lib/core/event/index.js +98 -0
  22. package/lib/core/event/message.d.ts +27 -0
  23. package/lib/core/event/message.js +95 -0
  24. package/lib/core/event/notice.d.ts +50 -0
  25. package/lib/core/event/notice.js +133 -0
  26. package/lib/{types/event.d.ts → core/event/types.d.ts} +255 -14
  27. package/lib/core/internal/axios.d.ts +25 -0
  28. package/lib/core/internal/axios.js +46 -0
  29. package/lib/core/internal/sign.d.ts +22 -0
  30. package/lib/core/internal/sign.js +40 -0
  31. package/lib/core/webhook/c2c.d.ts +2 -0
  32. package/lib/core/webhook/c2c.js +14 -0
  33. package/lib/core/webhook/http.d.ts +2 -0
  34. package/lib/core/webhook/http.js +35 -0
  35. package/lib/core/webhook/index.d.ts +17 -0
  36. package/lib/core/webhook/index.js +87 -0
  37. package/lib/core/webhook/ws.d.ts +2 -0
  38. package/lib/core/webhook/ws.js +79 -0
  39. package/lib/index.d.ts +1 -3
  40. package/lib/index.js +5 -7
  41. package/lib/root.d.ts +5 -0
  42. package/lib/root.js +9 -0
  43. package/lib/types/config.d.ts +53 -0
  44. package/lib/utils/common.d.ts +75 -61
  45. package/lib/utils/common.js +190 -202
  46. package/lib/utils/config.d.ts +5 -130
  47. package/lib/utils/config.js +50 -172
  48. package/lib/utils/dir.d.ts +1 -5
  49. package/lib/utils/dir.js +1 -10
  50. package/lib/utils/index.d.ts +4 -3
  51. package/lib/utils/index.js +4 -3
  52. package/package.json +41 -23
  53. package/config/config.yaml +0 -69
  54. package/lib/core/api.d.ts +0 -180
  55. package/lib/core/api.js +0 -454
  56. package/lib/core/index.d.ts +0 -90
  57. package/lib/core/index.js +0 -349
  58. package/lib/core/intents.d.ts +0 -6
  59. package/lib/core/intents.js +0 -57
  60. package/lib/core/markdown.d.ts +0 -23
  61. package/lib/core/markdown.js +0 -265
  62. package/lib/types/index.d.ts +0 -3
  63. package/lib/types/index.js +0 -3
  64. package/lib/types/media.d.ts +0 -100
  65. package/lib/types/message.d.ts +0 -85
  66. /package/lib/{types/event.js → core/api/types.js} +0 -0
  67. /package/lib/{types/media.js → core/event/types.js} +0 -0
  68. /package/lib/types/{message.js → config.js} +0 -0
package/lib/core/index.js DELETED
@@ -1,349 +0,0 @@
1
- import { QQBotApi } from './api.js';
2
- import { markdownTemplate, markdownRaw } from './markdown.js';
3
- import { common, common as Common, config as Config, } from '../utils/index.js';
4
- import { logger, karin, segment, KarinMessage, button, } from 'node-karin';
5
- /**
6
- * - QQBot适配器
7
- */
8
- export class AdapterQQBot {
9
- super;
10
- socket;
11
- /** 账号配置类型定义 */
12
- #config;
13
- account;
14
- adapter;
15
- version;
16
- markdown;
17
- constructor(config) {
18
- this.#config = config;
19
- const { appId, secret, ...restConfig } = this.#config;
20
- this.markdown = restConfig;
21
- this.account = { uid: appId, uin: appId, name: '' };
22
- this.adapter = { id: 'QQBot', name: 'QQBot', type: 'internal', sub_type: 'internal', start_time: Date.now(), connect: '', index: 0 };
23
- this.version = { name: 'QQBot', app_name: 'QQBot', version: Config.package.version };
24
- this.super = new QQBotApi(this.#config);
25
- switch (this.markdown.sendMode) {
26
- /** 原生 */
27
- case 1: {
28
- this._sendNsg = async (e, elements, type, openid, message_id) => {
29
- const list = await button(e.msg, e);
30
- elements.push(...list);
31
- return await markdownRaw({ bot: this, data: elements, type, openid, message_id });
32
- };
33
- return;
34
- }
35
- /** 旧图文模板 */
36
- case 3: {
37
- this._sendNsg = async (e, elements, type, openid, message_id) => {
38
- const list = await button(e.msg, e);
39
- elements.push(...list);
40
- return await markdownTemplate({ bot: this, data: elements, type, openid, message_id });
41
- };
42
- return;
43
- }
44
- /** 纯文 现已不能申请 */
45
- case 4: {
46
- // 后续处理
47
- return;
48
- }
49
- default: {
50
- logger.warn('后续处理');
51
- }
52
- }
53
- }
54
- async init() {
55
- await this.super.getAccessToken();
56
- await this.super.getWssUrl();
57
- this.super.createWss();
58
- this.super.on("GROUP_AT_MESSAGE_CREATE" /* QQBotEventType.GROUP_AT_MESSAGE_CREATE */, (data) => {
59
- const user_id = data.d.author.id;
60
- const group_id = data.d.group_id;
61
- const time = new Date(data.d.timestamp).getTime();
62
- const message = {
63
- event: "message" /* EventType.Message */,
64
- sub_event: "group_message" /* MessageSubType.GroupMessage */,
65
- raw_event: data,
66
- event_id: data.id,
67
- self_id: this.account.uid,
68
- user_id,
69
- time,
70
- message_id: data.d.id,
71
- message_seq: data.s,
72
- sender: {
73
- uid: user_id,
74
- uin: user_id,
75
- nick: '',
76
- role: "unknown" /* Role.Unknown */,
77
- },
78
- elements: this.AdapterConvertKarin(data),
79
- contact: {
80
- scene: "group" /* Scene.Group */,
81
- peer: group_id,
82
- sub_peer: null,
83
- },
84
- group_id,
85
- raw_message: '',
86
- };
87
- const e = new KarinMessage(message);
88
- e.bot = this;
89
- e.replyCallback = async (elements) => await this._sendNsg(e, elements, "groups" /* PathType.Groups */, e.contact.peer, e.message_id);
90
- karin.emit('adapter.message', e);
91
- });
92
- this.super.on("C2C_MESSAGE_CREATE" /* QQBotEventType.C2C_MESSAGE_CREATE */, (data) => {
93
- const user_id = data.d.author.user_openid;
94
- const time = new Date(data.d.timestamp).getTime();
95
- const message = {
96
- event: "message" /* EventType.Message */,
97
- sub_event: "private_message" /* MessageSubType.PrivateMessage */,
98
- raw_event: data,
99
- event_id: data.id,
100
- self_id: this.account.uid,
101
- user_id,
102
- time,
103
- message_id: data.d.id,
104
- message_seq: data.s,
105
- sender: {
106
- uid: user_id,
107
- uin: user_id,
108
- nick: '',
109
- role: "unknown" /* Role.Unknown */,
110
- },
111
- elements: this.AdapterConvertKarin(data),
112
- contact: {
113
- scene: "friend" /* Scene.Private */,
114
- peer: user_id,
115
- sub_peer: null,
116
- },
117
- group_id: '',
118
- raw_message: '',
119
- };
120
- const e = new KarinMessage(message);
121
- e.bot = this;
122
- e.replyCallback = async (elements) => await this._sendNsg(e, elements, "users" /* PathType.Friends */, e.contact.peer, e.message_id);
123
- karin.emit('adapter.message', e);
124
- });
125
- /** 等待建立连接 注册Bot */
126
- this.super.on('start', () => {
127
- this.account.name = this.super.nick;
128
- this.logger('info', `建立连接成功: ${this.account.name}`);
129
- /** 注册bot */
130
- const index = karin.addBot({ type: this.adapter.type, bot: this });
131
- if (index)
132
- this.adapter.index = index;
133
- });
134
- }
135
- async _sendNsg(e, elements, type, openid, message_id) {
136
- return await this.KarinConvertAdapter(elements, type, openid, message_id);
137
- }
138
- get self_id() {
139
- return this.account.uid;
140
- }
141
- logger(level, ...args) {
142
- this.super.logger(level, ...args);
143
- }
144
- async GetVersion() {
145
- const data = this.version;
146
- delete data.name;
147
- return data;
148
- }
149
- /**
150
- * QQBot转karin
151
- * @return karin格式消息
152
- * */
153
- AdapterConvertKarin(event) {
154
- const elements = [];
155
- const data = event.d;
156
- /** 检查是否存在图片 */
157
- for (const v of data.attachments || []) {
158
- elements.push(segment.image(v.url, {
159
- name: v.filename,
160
- // size: v.size,
161
- height: v.height,
162
- width: v.width,
163
- file_type: 'original',
164
- }));
165
- }
166
- const regex = /<faceType=\d+,faceId="\d+",ext="[^"]+">|[^<]+/g;
167
- const result = data.content.match(regex) || [data.content];
168
- result.forEach(v => {
169
- if (v.startsWith('<faceType=')) {
170
- const Match = v.match(/faceId="(\d+)"/);
171
- elements.push(segment.face(Number(Match[1])));
172
- }
173
- else {
174
- const { regex } = Config.getBotConfig(this.account.uid) || { regex: [] };
175
- for (const r of regex) {
176
- const reg = r.reg;
177
- v = v.trim().replace(reg, r.rep);
178
- }
179
- elements.push(segment.text(v));
180
- }
181
- });
182
- return elements;
183
- }
184
- /**
185
- * karin转qqbot
186
- * @param data karin格式消息
187
- * */
188
- async KarinConvertAdapter(data, type, openid, message_id) {
189
- let seq = common.random(1, 999999);
190
- /** 待排版的原始消息队列 */
191
- let msg = [];
192
- /** 待发送列表 */
193
- const send_list = [];
194
- /** 采用并发上传 */
195
- await Promise.all(data.map(async (i, index) => {
196
- const results = [];
197
- switch (i.type) {
198
- case 'text': {
199
- const qr = await Common.getQQBotText(i.text);
200
- results.push({ index, type: i.type, content: qr.text });
201
- if (qr.data) {
202
- const { file_info } = await this.super.uploadMedia(openid, type, qr.data.base64, 1 /* FileType.Image */);
203
- results.push({ index, type: 'image', content: file_info });
204
- }
205
- break;
206
- }
207
- case 'reply':
208
- // 新增一个字段来进行传参进行被动回复事件更合适
209
- // elements.push({ type: 'reply', data: { id: i.message_id } })
210
- break;
211
- case 'record': {
212
- const file = await common.voiceToSilk(i.file);
213
- /** 上传 */
214
- const { file_info } = await this.super.uploadMedia(openid, type, file, 3 /* FileType.Record */);
215
- /** 构建发送参数 */
216
- results.push({ index, type: i.type, content: file_info });
217
- break;
218
- }
219
- case 'image':
220
- case 'video':
221
- case 'file': {
222
- const map = {
223
- image: 1 /* FileType.Image */,
224
- video: 2 /* FileType.Video */,
225
- file: 4 /* FileType.File */,
226
- };
227
- /** 上传 */
228
- const { file_info } = await this.super.uploadMedia(openid, type, i.file, map[i.type]);
229
- /** 构建发送参数 */
230
- results.push({ index, type: i.type, content: file_info });
231
- break;
232
- }
233
- // 不支持的消息类型
234
- default:
235
- break;
236
- }
237
- return results;
238
- })).then((allResults) => {
239
- allResults.flat().sort((a, b) => a.index - b.index).forEach(result => {
240
- switch (result.type) {
241
- case 'text': {
242
- msg.push(result.content);
243
- break;
244
- }
245
- case 'image': {
246
- const content = msg.length ? msg.join('') : '';
247
- msg = [];
248
- send_list.push(this.super.buildMedia(content, result.content, message_id, seq++));
249
- break;
250
- }
251
- default: {
252
- if (msg.length) {
253
- send_list.push(this.super.buildText(msg.join(''), message_id, seq++));
254
- msg = [];
255
- }
256
- send_list.push(this.super.buildMedia('', result.content, message_id, seq++));
257
- break;
258
- }
259
- }
260
- });
261
- if (msg.length) {
262
- send_list.push(this.super.buildText(msg.join(''), message_id, seq++));
263
- }
264
- });
265
- const result = {
266
- message_id: '',
267
- message_time: 0,
268
- raw_data: [],
269
- };
270
- /** 并发发送 */
271
- await Promise.all(send_list.map(async (v, index) => {
272
- index !== 0 && await new Promise(resolve => setTimeout(resolve, index * 100));
273
- const res = await this.super.sendMessage(openid, type, v);
274
- result.raw_data.push(res);
275
- }));
276
- result.message_id = result.raw_data[0].id;
277
- result.message_time = result.raw_data[0].time;
278
- return result;
279
- }
280
- async SendMessage(contact, elements) {
281
- return await this.KarinConvertAdapter(elements, contact.scene === 'group' ? "groups" /* PathType.Groups */ : "users" /* PathType.Friends */, contact.peer);
282
- }
283
- getAvatarUrl(user_id, size = 0) {
284
- if (user_id === this.account.uid)
285
- return this.super.avatar;
286
- return `https://q.qlogo.cn/qqapp/${this.account.uid}/${user_id}/${size}`;
287
- }
288
- getGroupAvatarUrl() {
289
- return 'https://p.qlogo.cn/gh/967068507/967068507/0';
290
- }
291
- async GetCurrentAccount() {
292
- return { account_uid: this.account.uid, account_uin: this.account.uin, account_name: this.account.name };
293
- }
294
- async RecallMessage(contact, message_id) {
295
- return await this.super.recallMessage(contact.peer, contact.scene === 'group' ? "groups" /* PathType.Groups */ : "users" /* PathType.Friends */, message_id);
296
- }
297
- async GetEssenceMessageList() { throw new Error('Method not implemented.'); }
298
- async DownloadForwardMessage() { throw new Error('Method not implemented.'); }
299
- async SetEssenceMessage() { throw new Error('Method not implemented.'); }
300
- async DeleteEssenceMessage() { throw new Error('Method not implemented.'); }
301
- async SetFriendApplyResult() { throw new Error('Method not implemented.'); }
302
- async SetGroupApplyResult() { throw new Error('Method not implemented.'); }
303
- async SetInvitedJoinGroupResult() { throw new Error('Method not implemented.'); }
304
- async ReactMessageWithEmoji() { throw new Error('Method not implemented.'); }
305
- async UploadPrivateFile() { throw new Error('Method not implemented.'); }
306
- async UploadGroupFile() { throw new Error('Method not implemented.'); }
307
- async UploadForwardMessage() { throw new Error('Method not implemented.'); }
308
- async sendForwardMessage() { throw new Error('Method not implemented.'); }
309
- async SendMessageByResId() { throw new Error('Method not implemented.'); }
310
- async GetMessage() { throw new Error('Method not implemented.'); }
311
- async GetHistoryMessage() { throw new Error('Method not implemented.'); }
312
- async VoteUser() { throw new Error('Method not implemented.'); }
313
- async KickMember() { throw new Error('Method not implemented.'); }
314
- async BanMember() { throw new Error('Method not implemented.'); }
315
- async SetGroupWholeBan() { throw new Error('Method not implemented.'); }
316
- async SetGroupAdmin() { throw new Error('Method not implemented.'); }
317
- async ModifyMemberCard() { throw new Error('Method not implemented.'); }
318
- async ModifyGroupName() { throw new Error('Method not implemented.'); }
319
- async LeaveGroup() { throw new Error('Method not implemented.'); }
320
- async SetGroupUniqueTitle() { throw new Error('Method not implemented.'); }
321
- async GetStrangerProfileCard() { throw new Error('Method not implemented.'); }
322
- async GetFriendList() { throw new Error('Method not implemented.'); }
323
- async GetGroupInfo() { throw new Error('Method not implemented.'); }
324
- async GetGroupList() { throw new Error('Method not implemented.'); }
325
- async GetGroupMemberInfo() { throw new Error('Method not implemented.'); }
326
- async GetGroupMemberList() { throw new Error('Method not implemented.'); }
327
- async GetGroupHonor() { throw new Error('Method not implemented.'); }
328
- async DownloadFile() { throw new Error('Method not implemented.'); }
329
- async CreateFolder() { throw new Error('Method not implemented.'); }
330
- async RenameFolder() { throw new Error('Method not implemented.'); }
331
- async DeleteFolde() { throw new Error('Method not implemented.'); }
332
- async DeleteFolder() { throw new Error('Method not implemented.'); }
333
- async UploadFile() { throw new Error('Method not implemented.'); }
334
- async DeleteFile() { throw new Error('Method not implemented.'); }
335
- async GetFileSystemInfo() { throw new Error('Method not implemented.'); }
336
- async GetFileList() { throw new Error('Method not implemented.'); }
337
- async ModifyGroupRemark() { throw new Error('Method not implemented.'); }
338
- async GetRemainCountAtAll() { throw new Error('Method not implemented.'); }
339
- async GetProhibitedUserList() { throw new Error('Method not implemented.'); }
340
- async PokeMember() { throw new Error('Method not implemented.'); }
341
- async SetMessageReaded() { throw new Error('Method not implemented.'); }
342
- }
343
- const list = Object.keys(Config.Config.accounts).filter(v => v !== 'default');
344
- for (const v of list) {
345
- const config = Config.getBotConfig(v);
346
- if (!config)
347
- continue;
348
- new AdapterQQBot(config).init();
349
- }
@@ -1,6 +0,0 @@
1
- /**
2
- * 计算事件名称数组对应的 intents 的值
3
- * @param events 事件名称数组
4
- * @returns intents 的值
5
- */
6
- export declare function Intents(events: string[]): number;
@@ -1,57 +0,0 @@
1
- const eventToIntentMap = {
2
- GUILD_CREATE: 1 << 0,
3
- GUILD_UPDATE: 1 << 0,
4
- GUILD_DELETE: 1 << 0,
5
- CHANNEL_CREATE: 1 << 0,
6
- CHANNEL_UPDATE: 1 << 0,
7
- CHANNEL_DELETE: 1 << 0,
8
- GUILD_MEMBER_ADD: 1 << 1,
9
- GUILD_MEMBER_UPDATE: 1 << 1,
10
- GUILD_MEMBER_REMOVE: 1 << 1,
11
- MESSAGE_CREATE: 1 << 9,
12
- MESSAGE_DELETE: 1 << 9,
13
- MESSAGE_REACTION_ADD: 1 << 10,
14
- MESSAGE_REACTION_REMOVE: 1 << 10,
15
- DIRECT_MESSAGE_CREATE: 1 << 12,
16
- DIRECT_MESSAGE_DELETE: 1 << 12,
17
- C2C_MESSAGE_CREATE: 1 << 25,
18
- FRIEND_ADD: 1 << 25,
19
- FRIEND_DEL: 1 << 25,
20
- C2C_MSG_REJECT: 1 << 25,
21
- C2C_MSG_RECEIVE: 1 << 25,
22
- GROUP_AT_MESSAGE_CREATE: 1 << 25,
23
- GROUP_ADD_ROBOT: 1 << 25,
24
- GROUP_DEL_ROBOT: 1 << 25,
25
- GROUP_MSG_REJECT: 1 << 25,
26
- GROUP_MSG_RECEIVE: 1 << 25,
27
- INTERACTION_CREATE: 1 << 26,
28
- MESSAGE_AUDIT_PASS: 1 << 27,
29
- MESSAGE_AUDIT_REJECT: 1 << 27,
30
- FORUM_THREAD_CREATE: 1 << 28,
31
- FORUM_THREAD_UPDATE: 1 << 28,
32
- FORUM_THREAD_DELETE: 1 << 28,
33
- FORUM_POST_CREATE: 1 << 28,
34
- FORUM_POST_DELETE: 1 << 28,
35
- FORUM_REPLY_CREATE: 1 << 28,
36
- FORUM_REPLY_DELETE: 1 << 28,
37
- FORUM_PUBLISH_AUDIT_RESULT: 1 << 28,
38
- AUDIO_START: 1 << 29,
39
- AUDIO_FINISH: 1 << 29,
40
- AUDIO_ON_MIC: 1 << 29,
41
- AUDIO_OFF_MIC: 1 << 29,
42
- AT_MESSAGE_CREATE: 1 << 30,
43
- PUBLIC_MESSAGE_DELETE: 1 << 30,
44
- };
45
- /**
46
- * 计算事件名称数组对应的 intents 的值
47
- * @param events 事件名称数组
48
- * @returns intents 的值
49
- */
50
- export function Intents(events) {
51
- return events.reduce((acc, event) => {
52
- const intent = eventToIntentMap[event];
53
- if (intent !== undefined)
54
- acc |= intent;
55
- return acc;
56
- }, 0);
57
- }
@@ -1,23 +0,0 @@
1
- import { AdapterQQBot } from './index.js';
2
- import { PathType } from '../types/index.js';
3
- import { KarinElement, ReplyReturn } from 'node-karin';
4
- /**
5
- * 图文模板
6
- */
7
- export declare function markdownTemplate(Opt: {
8
- bot: AdapterQQBot;
9
- data: Array<KarinElement>;
10
- type: PathType;
11
- openid: string;
12
- message_id?: string;
13
- }): Promise<ReplyReturn>;
14
- /**
15
- * 原生markdown
16
- */
17
- export declare function markdownRaw(Opt: {
18
- bot: AdapterQQBot;
19
- data: Array<KarinElement>;
20
- type: PathType;
21
- openid: string;
22
- message_id?: string;
23
- }): Promise<ReplyReturn>;