@alemonjs/qq-bot 0.0.17 → 0.0.19

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/dist/index.html CHANGED
@@ -6,7 +6,7 @@
6
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
7
  <title>AlemonJS</title>
8
8
  <script type="module" crossorigin src="/assets/index.js"></script>
9
- <link rel="stylesheet" crossorigin href="/assets/index.css">
9
+ <link rel="stylesheet" crossorigin href="/assets/index.css" />
10
10
  </head>
11
11
 
12
12
  <body>
package/lib/hook.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { EventKeys, Events } from 'alemonjs';
2
+
3
+ /**
4
+ * 判断当前模式
5
+ * @param event
6
+ * @returns
7
+ */
8
+ declare const useMode: <T extends EventKeys>(event: Events[T]) => (mode: "guild" | "group") => boolean;
9
+
10
+ export { useMode };
package/lib/hook.js ADDED
@@ -0,0 +1,35 @@
1
+ /**
2
+ * 判断当前模式
3
+ * @param event
4
+ * @returns
5
+ */
6
+ const useMode = (event) => {
7
+ const tag = event.tag;
8
+ let currentMode = 'group';
9
+ // 群at
10
+ if (tag == 'GROUP_AT_MESSAGE_CREATE') {
11
+ currentMode = 'group';
12
+ }
13
+ // 私聊
14
+ if (tag == 'C2C_MESSAGE_CREATE') {
15
+ currentMode = 'group';
16
+ }
17
+ // 频道私聊
18
+ if (tag == 'DIRECT_MESSAGE_CREATE') {
19
+ currentMode = 'guild';
20
+ }
21
+ // 频道at
22
+ if (tag == 'AT_MESSAGE_CREATE') {
23
+ currentMode = 'guild';
24
+ }
25
+ // 频道消息
26
+ if (tag == 'MESSAGE_CREATE') {
27
+ currentMode = 'guild';
28
+ }
29
+ const isMode = (mode) => {
30
+ return currentMode == mode;
31
+ };
32
+ return isMode;
33
+ };
34
+
35
+ export { useMode };
package/lib/index.d.ts CHANGED
@@ -1,9 +1,11 @@
1
1
  import * as alemonjs from 'alemonjs';
2
2
  import { QQBotAPI } from './sdk/api.js';
3
+ export { useMode } from './hook.js';
3
4
 
4
5
  type Client = typeof QQBotAPI.prototype;
5
6
  declare const client: Client;
6
7
  declare const platform = "qq-bot";
8
+
7
9
  declare const _default: alemonjs.DefinePlatformValue;
8
10
 
9
11
  export { type Client, client, _default as default, platform };
@@ -1,10 +1,10 @@
1
- import { defineBot, getConfigValue, useUserHashKey, onProcessor } from 'alemonjs';
1
+ import { getConfigValue, useUserHashKey, onProcessor } from 'alemonjs';
2
2
  import { AT_MESSAGE_CREATE, GROUP_AT_MESSAGE_CREATE, DIRECT_MESSAGE_CREATE, C2C_MESSAGE_CREATE, MESSAGE_CREATE } from './send/index.js';
3
3
  import { QQBotGroupClient } from './sdk/websoket.group.js';
4
4
  import { isGuild } from './utils.js';
5
5
 
6
6
  const platform = 'qq-bot';
7
- var QQBotGroup = defineBot(() => {
7
+ var QQBotGroup = definePlatform(() => {
8
8
  let value = getConfigValue();
9
9
  if (!value)
10
10
  value = {};
@@ -21,7 +21,7 @@ var QQBotGroup = defineBot(() => {
21
21
  mode: config?.mode
22
22
  });
23
23
  // 连接
24
- client.connect();
24
+ client.connect(config?.gatewayURL);
25
25
  /**
26
26
  * group
27
27
  *
@@ -1,10 +1,10 @@
1
- import { defineBot, getConfigValue, useUserHashKey, onProcessor } from 'alemonjs';
1
+ import { getConfigValue, useUserHashKey, onProcessor } from 'alemonjs';
2
2
  import { AT_MESSAGE_CREATE, GROUP_AT_MESSAGE_CREATE, DIRECT_MESSAGE_CREATE, C2C_MESSAGE_CREATE, MESSAGE_CREATE } from './send/index.js';
3
3
  import { QQBotGuildClient } from './sdk/websoket.guild.js';
4
4
  import { isGuild } from './utils.js';
5
5
 
6
6
  const platform = 'qq-bot';
7
- var QQBotGuild = defineBot(() => {
7
+ var QQBotGuild = definePlatform(() => {
8
8
  let value = getConfigValue();
9
9
  if (!value)
10
10
  value = {};
@@ -27,7 +27,7 @@ var QQBotGuild = defineBot(() => {
27
27
  mode: config?.mode
28
28
  });
29
29
  // 连接
30
- client.connect();
30
+ client.connect(config?.gatewayURL);
31
31
  /**
32
32
  * guild
33
33
  */
package/lib/index.js CHANGED
@@ -1,9 +1,10 @@
1
- import { defineBot, getConfigValue, useUserHashKey, onProcessor } from 'alemonjs';
1
+ import { getConfigValue, useUserHashKey, onProcessor } from 'alemonjs';
2
2
  import { QQBotClient } from './sdk/client.js';
3
3
  import { AT_MESSAGE_CREATE, GROUP_AT_MESSAGE_CREATE, DIRECT_MESSAGE_CREATE, C2C_MESSAGE_CREATE, MESSAGE_CREATE } from './send/index.js';
4
4
  import QQBotGroup from './index.group.js';
5
5
  import QQBotGuild from './index.guild.js';
6
6
  import { isGuild } from './utils.js';
7
+ export { useMode } from './hook.js';
7
8
 
8
9
  const client = new Proxy({}, {
9
10
  get: (_, prop) => {
@@ -16,7 +17,7 @@ const client = new Proxy({}, {
16
17
  }
17
18
  });
18
19
  const platform = 'qq-bot';
19
- var index = defineBot(() => {
20
+ var index = definePlatform(() => {
20
21
  let value = getConfigValue();
21
22
  if (!value)
22
23
  value = {};
@@ -53,11 +54,14 @@ var index = defineBot(() => {
53
54
  * GROUP_AT_MESSAGE_CREATE
54
55
  * C2C_MESSAGE_CREATE
55
56
  */
57
+ const createUserAvatarURL = (author_id) => {
58
+ return `https://q.qlogo.cn/qqapp/${config.app_id}/${author_id}/ 640`;
59
+ };
56
60
  // 监听消息
57
61
  client.on('GROUP_AT_MESSAGE_CREATE', async (event) => {
58
62
  const master_key = config?.master_key ?? [];
59
63
  const isMaster = master_key.includes(event.author.id);
60
- const url = `https://q.qlogo.cn/qqapp/${config.app_id}/${event.author.id}/640`;
64
+ const url = createUserAvatarURL(event.author.id);
61
65
  const UserAvatar = {
62
66
  toBuffer: async () => {
63
67
  const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
@@ -104,7 +108,7 @@ var index = defineBot(() => {
104
108
  client.on('C2C_MESSAGE_CREATE', async (event) => {
105
109
  const master_key = config?.master_key ?? [];
106
110
  const isMaster = master_key.includes(event.author.id);
107
- const url = `https://q.qlogo.cn/qqapp/${config.app_id}/${event.author.id}/640`;
111
+ const url = createUserAvatarURL(event.author.id);
108
112
  const UserAvatar = {
109
113
  toBuffer: async () => {
110
114
  const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
@@ -377,21 +381,23 @@ var index = defineBot(() => {
377
381
  // 打 tag
378
382
  const tag = event.tag;
379
383
  try {
384
+ // 群at
380
385
  if (tag == 'GROUP_AT_MESSAGE_CREATE') {
381
386
  return await GROUP_AT_MESSAGE_CREATE(client, event, val);
382
387
  }
388
+ // 私聊
383
389
  if (tag == 'C2C_MESSAGE_CREATE') {
384
390
  return await C2C_MESSAGE_CREATE(client, event, val);
385
391
  }
392
+ // 频道私聊
386
393
  if (tag == 'DIRECT_MESSAGE_CREATE') {
387
394
  return await DIRECT_MESSAGE_CREATE(client, event, val);
388
395
  }
396
+ // 频道at
389
397
  if (tag == 'AT_MESSAGE_CREATE') {
390
398
  return await AT_MESSAGE_CREATE(client, event, val);
391
399
  }
392
- if (tag == 'MESSAGE_CREATE') {
393
- return await AT_MESSAGE_CREATE(client, event, val);
394
- }
400
+ // 频道消息
395
401
  if (tag == 'MESSAGE_CREATE') {
396
402
  return await MESSAGE_CREATE(client, event, val);
397
403
  }
package/lib/sdk/client.js CHANGED
@@ -169,7 +169,7 @@ class QQBotClient extends QQBotAPI {
169
169
  });
170
170
  }
171
171
  else {
172
- const reconnect = () => {
172
+ const reConnect = () => {
173
173
  // 使用了ws服务器
174
174
  this.#ws = new WebSocket(ws);
175
175
  this.#ws.on('open', () => {
@@ -198,14 +198,14 @@ class QQBotClient extends QQBotAPI {
198
198
  return;
199
199
  // 1.3s 后重连
200
200
  setTimeout(() => {
201
- reconnect();
201
+ reConnect();
202
202
  }, 1300);
203
203
  });
204
204
  this.#ws.on('error', e => {
205
205
  this.#error(e);
206
206
  });
207
207
  };
208
- reconnect();
208
+ reConnect();
209
209
  }
210
210
  }
211
211
  catch (e) {
@@ -93,7 +93,7 @@ const intentsMap = {
93
93
  GROUP_ADD_ROBOT: 1 << 25,
94
94
  GROUP_DEL_ROBOT: 1 << 25,
95
95
  GROUP_MSG_REJECT: 1 << 25,
96
- GROUP_MSG_RECEIVE: 1 << 25,
96
+ GROUP_MSG_RECEIVE: 1 << 25
97
97
  };
98
98
  /**
99
99
  *
@@ -5,7 +5,7 @@ interface ButtonType {
5
5
  render_data: {
6
6
  label: string;
7
7
  visited_label: string;
8
- style: number;
8
+ style?: number;
9
9
  };
10
10
  action: {
11
11
  type: number;
@@ -14,8 +14,13 @@ interface ButtonType {
14
14
  };
15
15
  reply?: boolean;
16
16
  enter?: boolean;
17
- unsupport_tips: string;
18
- data: string;
17
+ unsupport_tips?: string;
18
+ data: string | {
19
+ click: string;
20
+ confirm: string;
21
+ cancel: string;
22
+ };
23
+ at_bot_show_channel_list?: boolean;
19
24
  };
20
25
  }
21
26
  interface KeyboardType {
@@ -88,12 +88,12 @@ class QQBotGroupClient extends QQBotAPI {
88
88
  * @param cfg
89
89
  * @param conversation
90
90
  */
91
- async connect() {
91
+ async connect(gatewayURL) {
92
92
  // 定时模式
93
93
  await this.#setTimeoutBotConfig();
94
94
  // 请求url
95
95
  if (!this.#gatewayUrl) {
96
- this.#gatewayUrl = await this.gateway().then(res => res?.url);
96
+ this.#gatewayUrl = gatewayURL ?? (await this.gateway().then(res => res?.url));
97
97
  }
98
98
  if (!this.#gatewayUrl)
99
99
  return;
@@ -68,17 +68,19 @@ class QQBotGuildClient extends QQBotAPI {
68
68
  *
69
69
  * @param cfg
70
70
  */
71
- async connect() {
71
+ async connect(gatewayURL) {
72
72
  //
73
- this.#gatewayUrl = await this.gateway()
74
- .then(res => res.url)
75
- .catch(err => {
76
- if (this.#events['ERROR']) {
77
- for (const item of this.#events['ERROR']) {
78
- item(err);
79
- }
80
- }
81
- });
73
+ this.#gatewayUrl =
74
+ gatewayURL ??
75
+ (await this.gateway()
76
+ .then(res => res.url)
77
+ .catch(err => {
78
+ if (this.#events['ERROR']) {
79
+ for (const item of this.#events['ERROR']) {
80
+ item(err);
81
+ }
82
+ }
83
+ }));
82
84
  // 请求url
83
85
  if (!this.#gatewayUrl)
84
86
  return this;
package/lib/send/index.js CHANGED
@@ -1,14 +1,54 @@
1
1
  import { readFileSync } from 'fs';
2
2
  import axios from 'axios';
3
3
 
4
+ const createButtonsData = (rows) => {
5
+ let id = 0;
6
+ const data = {
7
+ rows: rows.map(row => {
8
+ const val = row.value;
9
+ return {
10
+ buttons: val.map(button => {
11
+ const value = button.value;
12
+ const options = button.options;
13
+ id++;
14
+ return {
15
+ id: String(id),
16
+ render_data: {
17
+ label: typeof value == 'object' ? value.title : value,
18
+ visited_label: typeof value == 'object' ? value.label : value,
19
+ // tudo
20
+ style: 0
21
+ },
22
+ action: {
23
+ // 0 link 1 callback , 2 command
24
+ type: typeof options.data === 'object' ? 1 : options?.isLink ? 0 : 2,
25
+ permission: {
26
+ // 所有人
27
+ type: 2
28
+ // "specify_role_ids": ["1", "2", "3"]
29
+ },
30
+ // "click_limit": 10,
31
+ unsupport_tips: options?.toolTip ?? '',
32
+ data: options?.data ?? '',
33
+ // reply: true,
34
+ at_bot_show_channel_list: options.showList ?? false,
35
+ enter: options?.autoEnter ?? false
36
+ }
37
+ };
38
+ })
39
+ };
40
+ })
41
+ };
42
+ return data;
43
+ };
4
44
  const GROUP_AT_MESSAGE_CREATE = (client, event, val) => {
5
45
  const content = val
6
- .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
46
+ .filter(item => item.type == 'Mention' || item.type == 'Text')
7
47
  .map(item => {
8
- if (item.type == 'Link') {
9
- return `[${item.options?.title ?? item.value}](${item.value})`;
10
- }
11
- else if (item.type == 'Mention') {
48
+ // if (item.type == 'Link') {
49
+ // return `[${item.options?.title ?? item.value}](${item.value})`
50
+ // } else
51
+ if (item.type == 'Mention') {
12
52
  if (item.value == 'everyone' ||
13
53
  item.value == 'all' ||
14
54
  item.value == '' ||
@@ -34,7 +74,7 @@ const GROUP_AT_MESSAGE_CREATE = (client, event, val) => {
34
74
  })));
35
75
  }
36
76
  const images = val.filter(item => item.type == 'Image' || item.type == 'ImageFile' || item.type == 'ImageURL');
37
- if (images) {
77
+ if (images && images.length > 0) {
38
78
  return Promise.all(images.map(async (item) => {
39
79
  if (item.type == 'ImageURL') {
40
80
  return client.groupOpenMessages(event.GuildId, {
@@ -69,11 +109,41 @@ const GROUP_AT_MESSAGE_CREATE = (client, event, val) => {
69
109
  });
70
110
  }));
71
111
  }
72
- return [];
112
+ // buttons
113
+ const buttons = val.filter(item => item.type == 'BT.group');
114
+ if (buttons && buttons.length > 0) {
115
+ return Promise.all(buttons.map(async (item) => {
116
+ const template_id = item?.options?.template_id;
117
+ if (template_id) {
118
+ return client.groupOpenMessages(event.GuildId, {
119
+ content: '',
120
+ msg_id: event.MessageId,
121
+ keyboard: {
122
+ id: template_id
123
+ },
124
+ msg_type: 2,
125
+ msg_seq: client.getMessageSeq(event.MessageId)
126
+ });
127
+ }
128
+ const rows = item.value;
129
+ // 构造成按钮
130
+ const data = createButtonsData(rows);
131
+ return client.groupOpenMessages(event.GuildId, {
132
+ content: '',
133
+ msg_id: event.MessageId,
134
+ keyboard: {
135
+ content: data
136
+ },
137
+ msg_type: 2,
138
+ msg_seq: client.getMessageSeq(event.MessageId)
139
+ });
140
+ }));
141
+ }
142
+ return Promise.all([]);
73
143
  };
74
144
  const C2C_MESSAGE_CREATE = (client, event, val) => {
75
145
  const content = val
76
- .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
146
+ .filter(item => item.type == 'Mention' || item.type == 'Text')
77
147
  .map(item => {
78
148
  if (item.type == 'Text') {
79
149
  return item.value;
@@ -90,7 +160,7 @@ const C2C_MESSAGE_CREATE = (client, event, val) => {
90
160
  })));
91
161
  }
92
162
  const images = val.filter(item => item.type == 'Image' || item.type == 'ImageFile' || item.type == 'ImageURL');
93
- if (images) {
163
+ if (images && images.length > 0) {
94
164
  return Promise.all(images.map(async (item) => {
95
165
  if (item.type == 'ImageURL') {
96
166
  return client.usersOpenMessages(event.OpenId, {
@@ -125,7 +195,41 @@ const C2C_MESSAGE_CREATE = (client, event, val) => {
125
195
  });
126
196
  }));
127
197
  }
128
- return [];
198
+ // buttons
199
+ const buttons = val.filter(item => item.type == 'BT.group');
200
+ if (buttons && buttons.length > 0) {
201
+ return Promise.all(buttons.map(async (item) => {
202
+ const template_id = item?.options?.template_id;
203
+ if (template_id) {
204
+ return client.groupOpenMessages(event.GuildId, {
205
+ content: '',
206
+ msg_id: event.MessageId,
207
+ keyboard: {
208
+ id: template_id
209
+ },
210
+ msg_type: 2,
211
+ msg_seq: client.getMessageSeq(event.MessageId)
212
+ });
213
+ }
214
+ const rows = item.value;
215
+ // 看看是否是模板id的
216
+ rows.map(row => {
217
+ return row;
218
+ });
219
+ // 构造成按钮
220
+ const data = createButtonsData(rows);
221
+ return client.groupOpenMessages(event.GuildId, {
222
+ content: '',
223
+ msg_id: event.MessageId,
224
+ keyboard: {
225
+ content: data
226
+ },
227
+ msg_type: 2,
228
+ msg_seq: client.getMessageSeq(event.MessageId)
229
+ });
230
+ }));
231
+ }
232
+ return Promise.all([]);
129
233
  };
130
234
  /**
131
235
  * 频道私聊
@@ -136,7 +240,7 @@ const C2C_MESSAGE_CREATE = (client, event, val) => {
136
240
  */
137
241
  const DIRECT_MESSAGE_CREATE = (client, event, val) => {
138
242
  const content = val
139
- .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
243
+ .filter(item => item.type == 'Mention' || item.type == 'Text')
140
244
  .map(item => {
141
245
  if (item.type == 'Text') {
142
246
  return item.value;
@@ -176,12 +280,12 @@ const DIRECT_MESSAGE_CREATE = (client, event, val) => {
176
280
  };
177
281
  const AT_MESSAGE_CREATE = (client, event, val) => {
178
282
  const content = val
179
- .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
283
+ .filter(item => item.type == 'Mention' || item.type == 'Text')
180
284
  .map(item => {
181
- if (item.type == 'Link') {
182
- return `[${item.options?.title ?? item.value}](${item.value})`;
183
- }
184
- else if (item.type == 'Mention') {
285
+ // if (item.type == 'Link') {
286
+ // return `[${item.options?.title ?? item.value}](${item.value})`
287
+ // } else
288
+ if (item.type == 'Mention') {
185
289
  if (item.value == 'everyone' ||
186
290
  item.value == 'all' ||
187
291
  item.value == '' ||
@@ -208,7 +312,7 @@ const AT_MESSAGE_CREATE = (client, event, val) => {
208
312
  })));
209
313
  }
210
314
  const images = val.filter(item => item.type == 'Image' || item.type == 'ImageFile' || item.type == 'ImageURL');
211
- if (images) {
315
+ if (images && images.length > 0) {
212
316
  return Promise.all(images.map(async (item) => {
213
317
  if (item.value == 'ImageURL') {
214
318
  // 请求得到buffer
@@ -229,7 +333,7 @@ const AT_MESSAGE_CREATE = (client, event, val) => {
229
333
  });
230
334
  }));
231
335
  }
232
- return [];
336
+ return Promise.all([]);
233
337
  };
234
338
  /**
235
339
  *
@@ -239,12 +343,12 @@ const AT_MESSAGE_CREATE = (client, event, val) => {
239
343
  */
240
344
  const MESSAGE_CREATE = (client, event, val) => {
241
345
  const content = val
242
- .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
346
+ .filter(item => item.type == 'Mention' || item.type == 'Text')
243
347
  .map(item => {
244
- if (item.type == 'Link') {
245
- return `[${item.options?.title ?? item.value}](${item.value})`;
246
- }
247
- else if (item.type == 'Mention') {
348
+ // if (item.type == 'Link') {
349
+ // return `[${item.options?.title ?? item.value}](${item.value})`
350
+ // } else
351
+ if (item.type == 'Mention') {
248
352
  if (item.value == 'everyone' ||
249
353
  item.value == 'all' ||
250
354
  item.value == '' ||
@@ -271,7 +375,7 @@ const MESSAGE_CREATE = (client, event, val) => {
271
375
  })));
272
376
  }
273
377
  const images = val.filter(item => item.type == 'Image' || item.type == 'ImageFile' || item.type == 'ImageURL');
274
- if (images) {
378
+ if (images && images.length > 0) {
275
379
  return Promise.all(images.map(async (item) => {
276
380
  if (item.value == 'ImageURL') {
277
381
  // 请求得到buffer
@@ -292,7 +396,7 @@ const MESSAGE_CREATE = (client, event, val) => {
292
396
  });
293
397
  }));
294
398
  }
295
- return [];
399
+ return Promise.all([]);
296
400
  };
297
401
 
298
402
  export { AT_MESSAGE_CREATE, C2C_MESSAGE_CREATE, DIRECT_MESSAGE_CREATE, GROUP_AT_MESSAGE_CREATE, MESSAGE_CREATE };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/qq-bot",
3
- "version": "0.0.17",
3
+ "version": "0.0.19",
4
4
  "description": "阿柠檬qqbot平台连接",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",