@alemonjs/qq-bot 0.0.12 → 0.0.13

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 (53) hide show
  1. package/README.md +19 -8
  2. package/dist/assets/index.css +1 -1
  3. package/dist/assets/index.js +1 -1
  4. package/lib/api.d.ts +971 -843
  5. package/lib/api.js +1172 -1156
  6. package/lib/client.d.ts +22 -22
  7. package/lib/client.js +201 -217
  8. package/lib/config.js +2 -2
  9. package/lib/desktop.js +3 -1
  10. package/lib/from.js +27 -34
  11. package/lib/index.d.ts +3 -3
  12. package/lib/index.group.js +238 -0
  13. package/lib/index.guild.js +338 -0
  14. package/lib/index.js +43 -2
  15. package/lib/message/AT_MESSAGE_CREATE.d.ts +35 -35
  16. package/lib/message/C2C_MESSAGE_CREATE.d.ts +9 -9
  17. package/lib/message/CHANNEL_CREATE.d.ts +15 -15
  18. package/lib/message/CHANNEL_DELETE.d.ts +15 -15
  19. package/lib/message/CHANNEL_UPDATE.d.ts +15 -15
  20. package/lib/message/DIRECT_MESSAGE_CREATE.d.ts +29 -29
  21. package/lib/message/DIRECT_MESSAGE_DELETE.d.ts +17 -17
  22. package/lib/message/ERROR.d.ts +2 -2
  23. package/lib/message/GROUP_AT_MESSAGE_CREATE.d.ts +10 -10
  24. package/lib/message/GUILD_CREATE.d.ts +15 -15
  25. package/lib/message/GUILD_DELETE.d.ts +15 -15
  26. package/lib/message/GUILD_MEMBER_ADD.d.ts +14 -14
  27. package/lib/message/GUILD_MEMBER_REMOVE.d.ts +14 -14
  28. package/lib/message/GUILD_MEMBER_UPDATE.d.ts +14 -14
  29. package/lib/message/GUILD_UPDATE.d.ts +15 -15
  30. package/lib/message/INTERACTION_CREATE.d.ts +2 -2
  31. package/lib/message/MESSAGE_CREATE.d.ts +2 -2
  32. package/lib/message/MESSAGE_DELETE.d.ts +2 -2
  33. package/lib/message/MESSAGE_REACTION_ADD.d.ts +13 -13
  34. package/lib/message/MESSAGE_REACTION_REMOVE.d.ts +13 -13
  35. package/lib/message/PUBLIC_MESSAGE_DELETE.d.ts +15 -15
  36. package/lib/message/READY.d.ts +6 -6
  37. package/lib/message.d.ts +46 -46
  38. package/lib/sdk/api.d.ts +847 -0
  39. package/lib/sdk/api.js +1183 -0
  40. package/lib/sdk/client.js +228 -0
  41. package/lib/sdk/config.js +3 -0
  42. package/lib/sdk/counter.js +19 -0
  43. package/lib/sdk/from.js +44 -0
  44. package/lib/sdk/intents.js +102 -0
  45. package/lib/sdk/typing.d.ts +56 -0
  46. package/lib/sdk/webhook.secret.js +53 -0
  47. package/lib/sdk/websoket.group.js +221 -0
  48. package/lib/sdk/websoket.guild.js +203 -0
  49. package/lib/send/index.js +87 -21
  50. package/lib/typing.d.ts +62 -54
  51. package/lib/utils.js +14 -0
  52. package/lib/webhook.js +46 -48
  53. package/package.json +1 -1
package/lib/client.d.ts CHANGED
@@ -1,26 +1,26 @@
1
- import { QQBotAPI } from './api.js';
2
- import { QQBotEventMap } from './message.js';
3
- import { Options } from './typing.js';
1
+ import { QQBotAPI } from './api.js'
2
+ import { QQBotEventMap } from './message.js'
3
+ import { Options } from './typing.js'
4
4
 
5
5
  declare class QQBotClient extends QQBotAPI {
6
- #private;
7
- /**
8
- * 设置配置
9
- * @param opstion
10
- */
11
- constructor(opstion: Options);
12
- /**
13
- * 注册事件处理程序
14
- * @param key 事件名称
15
- * @param val 事件处理函数
16
- */
17
- on<T extends keyof QQBotEventMap>(key: T, val: (event: QQBotEventMap[T]) => any): this;
18
- /**
19
- *
20
- * @param cfg
21
- * @param conversation
22
- */
23
- connect(): void;
6
+ #private
7
+ /**
8
+ * 设置配置
9
+ * @param opstion
10
+ */
11
+ constructor(opstion: Options)
12
+ /**
13
+ * 注册事件处理程序
14
+ * @param key 事件名称
15
+ * @param val 事件处理函数
16
+ */
17
+ on<T extends keyof QQBotEventMap>(key: T, val: (event: QQBotEventMap[T]) => any): this
18
+ /**
19
+ *
20
+ * @param cfg
21
+ * @param conversation
22
+ */
23
+ connect(): void
24
24
  }
25
25
 
26
- export { QQBotClient };
26
+ export { QQBotClient }
package/lib/client.js CHANGED
@@ -1,228 +1,212 @@
1
- import { QQBotAPI } from './api.js';
2
- import bodyParser from 'koa-bodyparser';
3
- import Router from 'koa-router';
4
- import { WebhookAPI } from './webhook.js';
5
- import Koa from 'koa';
6
- import { config } from './config.js';
7
- import { v4 } from 'uuid';
8
- import { WebSocketServer, WebSocket } from 'ws';
1
+ import { QQBotAPI } from './api.js'
2
+ import bodyParser from 'koa-bodyparser'
3
+ import Router from 'koa-router'
4
+ import { WebhookAPI } from './webhook.js'
5
+ import Koa from 'koa'
6
+ import { config } from './config.js'
7
+ import { v4 } from 'uuid'
8
+ import { WebSocketServer, WebSocket } from 'ws'
9
9
 
10
10
  class QQBotClient extends QQBotAPI {
11
- #events = {};
12
- #app = null;
13
- #count = 0;
14
- #client = [];
15
- #ws = null;
16
- /**
17
- * 设置配置
18
- * @param opstion
19
- */
20
- constructor(opstion) {
21
- super();
22
- if (opstion.secret)
23
- config.set('secret', opstion.secret);
24
- if (opstion.app_id)
25
- config.set('app_id', opstion.app_id);
26
- if (opstion.token)
27
- config.set('token', opstion.token);
28
- if (opstion.port)
29
- config.set('port', opstion.port);
30
- if (opstion.ws)
31
- config.set('ws', opstion.ws);
11
+ #events = {}
12
+ #app = null
13
+ #count = 0
14
+ #client = []
15
+ #ws = null
16
+ /**
17
+ * 设置配置
18
+ * @param opstion
19
+ */
20
+ constructor(opstion) {
21
+ super()
22
+ if (opstion.secret) config.set('secret', opstion.secret)
23
+ if (opstion.app_id) config.set('app_id', opstion.app_id)
24
+ if (opstion.token) config.set('token', opstion.token)
25
+ if (opstion.port) config.set('port', opstion.port)
26
+ if (opstion.ws) config.set('ws', opstion.ws)
27
+ }
28
+ /**
29
+ * 注册事件处理程序
30
+ * @param key 事件名称
31
+ * @param val 事件处理函数
32
+ */
33
+ on(key, val) {
34
+ if (!this.#events[key]) {
35
+ this.#events[key] = []
32
36
  }
33
- /**
34
- * 注册事件处理程序
35
- * @param key 事件名称
36
- * @param val 事件处理函数
37
- */
38
- on(key, val) {
39
- if (!this.#events[key]) {
40
- this.#events[key] = [];
41
- }
42
- this.#events[key].push(val);
43
- return this;
44
- }
45
- /**
46
- * 定时鉴权
47
- * @param cfg
48
- * @returns
49
- */
50
- async #setTimeoutBotConfig() {
51
- const callBack = async () => {
52
- const app_id = config.get('app_id');
53
- if (!app_id)
54
- return;
55
- const secret = config.get('secret');
56
- if (!secret)
57
- return;
58
- // 发送请求
59
- const data = await this.getAuthentication(app_id, secret).then(res => res.data);
60
- config.set('access_token', data.access_token);
61
- console.info('refresh', data.expires_in, 's');
62
- setTimeout(callBack, data.expires_in * 1000);
63
- };
64
- await callBack();
37
+ this.#events[key].push(val)
38
+ return this
39
+ }
40
+ /**
41
+ * 定时鉴权
42
+ * @param cfg
43
+ * @returns
44
+ */
45
+ async #setTimeoutBotConfig() {
46
+ const callBack = async () => {
47
+ const app_id = config.get('app_id')
48
+ if (!app_id) return
49
+ const secret = config.get('secret')
50
+ if (!secret) return
51
+ // 发送请求
52
+ const data = await this.getAuthentication(app_id, secret).then(res => res.data)
53
+ config.set('access_token', data.access_token)
54
+ console.info('refresh', data.expires_in, 's')
55
+ setTimeout(callBack, data.expires_in * 1000)
65
56
  }
66
- /**
67
- *
68
- * @param cfg
69
- * @param conversation
70
- */
71
- connect() {
72
- try {
73
- const ws = config.get('ws');
74
- if (!ws) {
75
- this.#setTimeoutBotConfig();
76
- this.#app = new Koa();
77
- this.#app.use(bodyParser());
78
- const router = new Router();
79
- const port = config.get('port');
80
- const secret = config.get('secret');
81
- const route = config.get('route') ?? '/webhook';
82
- const cfg = {
83
- secret: secret ?? '',
84
- port: port ? Number(port) : 17157
85
- };
86
- const ntqqWebhook = new WebhookAPI({
87
- secret: cfg.secret
88
- });
89
- this.#app.use(async (ctx, next) => {
90
- let rawData = '';
91
- ctx.req.on('data', chunk => (rawData += chunk));
92
- ctx.req.on('end', () => (ctx.request.rawBody = rawData));
93
- await next();
94
- });
95
- // 启动服务
96
- router.post(route, async (ctx) => {
97
- const sign = ctx.req.headers['x-signature-ed25519'];
98
- const timestamp = ctx.req.headers['x-signature-timestamp'];
99
- const rawBody = ctx.request.rawBody;
100
- const isValid = ntqqWebhook.validSign(timestamp, rawBody, String(sign));
101
- if (!isValid) {
102
- ctx.status = 400;
103
- ctx.body = { msg: 'invalid signature' };
104
- return;
105
- }
106
- const body = ctx.request.body;
107
- if (body.op == 13) {
108
- ctx.status = 200;
109
- ctx.body = {
110
- // 返回明文 token
111
- plain_token: body.d.plain_token,
112
- // 生成签名
113
- signature: ntqqWebhook.getSign(body.d.event_ts, body.d.plain_token)
114
- };
115
- }
116
- else if (body.op == 0) {
117
- ctx.status = 204;
118
- // 根据事件类型,处理事件
119
- for (const event of this.#events[body.t] || []) {
120
- event(body.d);
121
- }
122
- const access_token = config.get('access_token');
123
- // 也可以分法到客户端。 发送失败需要处理 或清理调
124
- for (const client of this.#client) {
125
- try {
126
- if (access_token)
127
- body['access_token'] = access_token;
128
- client.ws.send(JSON.stringify(body));
129
- }
130
- catch (e) {
131
- this.#error(e);
132
- }
133
- }
134
- }
135
- });
136
- this.#app.use(router.routes());
137
- this.#app.use(router.allowedMethods());
138
- // 启动服务
139
- const server = this.#app.listen(cfg.port, () => {
140
- console.log('Server running at http://localhost:' + cfg.port + route);
141
- });
142
- // 创建 WebSocketServer 并监听同一个端口
143
- const wss = new WebSocketServer({ server: server });
144
- console.log('Server running at wss://localhost:' + cfg.port + '/');
145
- // 处理客户端连接
146
- wss.on('connection', ws => {
147
- const clientId = v4();
148
- ws['clientId'] = clientId;
149
- console.log(clientId, 'connection');
150
- this.#client.push({ id: clientId, ws });
151
- // 处理消息事件
152
- ws.on('message', (message) => {
153
- // 拿到消息
154
- try {
155
- const body = JSON.parse(message.toString());
156
- for (const event of this.#events[body.t] || []) {
157
- event(body.d);
158
- }
159
- }
160
- catch (e) {
161
- this.#error(e);
162
- }
163
- });
164
- // 处理关闭事件
165
- ws.on('close', () => {
166
- console.log(`${clientId} disconnected`);
167
- this.#client = this.#client.filter(client => client.id !== clientId);
168
- });
169
- });
57
+ await callBack()
58
+ }
59
+ /**
60
+ *
61
+ * @param cfg
62
+ * @param conversation
63
+ */
64
+ connect() {
65
+ try {
66
+ const ws = config.get('ws')
67
+ if (!ws) {
68
+ this.#setTimeoutBotConfig()
69
+ this.#app = new Koa()
70
+ this.#app.use(bodyParser())
71
+ const router = new Router()
72
+ const port = config.get('port')
73
+ const secret = config.get('secret')
74
+ const route = config.get('route') ?? '/webhook'
75
+ const cfg = {
76
+ secret: secret ?? '',
77
+ port: port ? Number(port) : 17157
78
+ }
79
+ const ntqqWebhook = new WebhookAPI({
80
+ secret: cfg.secret
81
+ })
82
+ this.#app.use(async (ctx, next) => {
83
+ let rawData = ''
84
+ ctx.req.on('data', chunk => (rawData += chunk))
85
+ ctx.req.on('end', () => (ctx.request.rawBody = rawData))
86
+ await next()
87
+ })
88
+ // 启动服务
89
+ router.post(route, async ctx => {
90
+ const sign = ctx.req.headers['x-signature-ed25519']
91
+ const timestamp = ctx.req.headers['x-signature-timestamp']
92
+ const rawBody = ctx.request.rawBody
93
+ const isValid = ntqqWebhook.validSign(timestamp, rawBody, String(sign))
94
+ if (!isValid) {
95
+ ctx.status = 400
96
+ ctx.body = { msg: 'invalid signature' }
97
+ return
98
+ }
99
+ const body = ctx.request.body
100
+ if (body.op == 13) {
101
+ ctx.status = 200
102
+ ctx.body = {
103
+ // 返回明文 token
104
+ plain_token: body.d.plain_token,
105
+ // 生成签名
106
+ signature: ntqqWebhook.getSign(body.d.event_ts, body.d.plain_token)
170
107
  }
171
- else {
172
- const reconnect = () => {
173
- // 使用了ws服务器
174
- this.#ws = new WebSocket(ws);
175
- this.#ws.on('open', () => {
176
- this.#count = 0;
177
- console.log('ws connected');
178
- });
179
- this.#ws.on('message', data => {
180
- try {
181
- // 拿到消息
182
- const body = JSON.parse(data.toString());
183
- const access_token = body['access_token'];
184
- if (access_token)
185
- config.set('access_token', access_token);
186
- for (const event of this.#events[body.t] || []) {
187
- event(body.d);
188
- }
189
- }
190
- catch (e) {
191
- this.#error(e);
192
- }
193
- });
194
- this.#ws.on('close', () => {
195
- console.log('ws closed');
196
- // 重连5次,超过5次不再重连
197
- if (this.#count > 5)
198
- return;
199
- // 1.3s 后重连
200
- setTimeout(() => {
201
- reconnect();
202
- }, 1300);
203
- });
204
- this.#ws.on('error', e => {
205
- this.#error(e);
206
- });
207
- };
208
- reconnect();
108
+ } else if (body.op == 0) {
109
+ ctx.status = 204
110
+ // 根据事件类型,处理事件
111
+ for (const event of this.#events[body.t] || []) {
112
+ event(body.d)
209
113
  }
210
- }
211
- catch (e) {
212
- this.#error(e);
213
- }
214
- }
215
- /**
216
- *
217
- * @param error
218
- */
219
- #error(error) {
220
- if (this.#events['ERROR']) {
221
- for (const event of this.#events['ERROR'] || []) {
222
- event(error);
114
+ const access_token = config.get('access_token')
115
+ // 也可以分法到客户端。 发送失败需要处理 或清理调
116
+ for (const client of this.#client) {
117
+ try {
118
+ if (access_token) body['access_token'] = access_token
119
+ client.ws.send(JSON.stringify(body))
120
+ } catch (e) {
121
+ this.#error(e)
122
+ }
123
+ }
124
+ }
125
+ })
126
+ this.#app.use(router.routes())
127
+ this.#app.use(router.allowedMethods())
128
+ // 启动服务
129
+ const server = this.#app.listen(cfg.port, () => {
130
+ console.log('Server running at http://localhost:' + cfg.port + route)
131
+ })
132
+ // 创建 WebSocketServer 并监听同一个端口
133
+ const wss = new WebSocketServer({ server: server })
134
+ console.log('Server running at wss://localhost:' + cfg.port + '/')
135
+ // 处理客户端连接
136
+ wss.on('connection', ws => {
137
+ const clientId = v4()
138
+ ws['clientId'] = clientId
139
+ console.log(clientId, 'connection')
140
+ this.#client.push({ id: clientId, ws })
141
+ // 处理消息事件
142
+ ws.on('message', message => {
143
+ // 拿到消息
144
+ try {
145
+ const body = JSON.parse(message.toString())
146
+ for (const event of this.#events[body.t] || []) {
147
+ event(body.d)
148
+ }
149
+ } catch (e) {
150
+ this.#error(e)
223
151
  }
152
+ })
153
+ // 处理关闭事件
154
+ ws.on('close', () => {
155
+ console.log(`${clientId} disconnected`)
156
+ this.#client = this.#client.filter(client => client.id !== clientId)
157
+ })
158
+ })
159
+ } else {
160
+ const reconnect = () => {
161
+ // 使用了ws服务器
162
+ this.#ws = new WebSocket(ws)
163
+ this.#ws.on('open', () => {
164
+ this.#count = 0
165
+ console.log('ws connected')
166
+ })
167
+ this.#ws.on('message', data => {
168
+ try {
169
+ // 拿到消息
170
+ const body = JSON.parse(data.toString())
171
+ const access_token = body['access_token']
172
+ if (access_token) config.set('access_token', access_token)
173
+ for (const event of this.#events[body.t] || []) {
174
+ event(body.d)
175
+ }
176
+ } catch (e) {
177
+ this.#error(e)
178
+ }
179
+ })
180
+ this.#ws.on('close', () => {
181
+ console.log('ws closed')
182
+ // 重连5次,超过5次不再重连
183
+ if (this.#count > 5) return
184
+ // 1.3s 后重连
185
+ setTimeout(() => {
186
+ reconnect()
187
+ }, 1300)
188
+ })
189
+ this.#ws.on('error', e => {
190
+ this.#error(e)
191
+ })
224
192
  }
193
+ reconnect()
194
+ }
195
+ } catch (e) {
196
+ this.#error(e)
197
+ }
198
+ }
199
+ /**
200
+ *
201
+ * @param error
202
+ */
203
+ #error(error) {
204
+ if (this.#events['ERROR']) {
205
+ for (const event of this.#events['ERROR'] || []) {
206
+ event(error)
207
+ }
225
208
  }
209
+ }
226
210
  }
227
211
 
228
- export { QQBotClient };
212
+ export { QQBotClient }
package/lib/config.js CHANGED
@@ -1,3 +1,3 @@
1
- const config = new Map();
1
+ const config = new Map()
2
2
 
3
- export { config };
3
+ export { config }
package/lib/desktop.js CHANGED
@@ -13,12 +13,14 @@ const activate = context => {
13
13
  context.onCommand('open.qq-bot', () => {
14
14
  const dir = join(__dirname, '../', 'dist', 'index.html');
15
15
  const scriptReg = /<script.*?src="(.+?)".*?>/;
16
- const styleReg = /<link.*?href="(.+?)".*?>/;
16
+ const styleReg = /<link.*?rel="stylesheet".*?href="(.+?)".*?>/;
17
+ const iconReg = /<link.*?rel="icon".*?href="(.+?)".*?>/g;
17
18
  // 创建 webview 路径
18
19
  const styleUri = context.createExtensionDir(join(__dirname, '../', 'dist', 'assets', 'index.css'));
19
20
  const scriptUri = context.createExtensionDir(join(__dirname, '../', 'dist', 'assets', 'index.js'));
20
21
  // 确保路径存在
21
22
  const html = readFileSync(dir, 'utf-8')
23
+ .replace(iconReg, ``)
22
24
  .replace(scriptReg, `<script type="module" crossorigin src="${scriptUri}"></script>`)
23
25
  .replace(styleReg, `<link rel="stylesheet" crossorigin href="${styleUri}">`);
24
26
  // 立即渲染 webview
package/lib/from.js CHANGED
@@ -1,7 +1,7 @@
1
- import { existsSync, createReadStream } from 'fs';
2
- import { Readable, isReadable } from 'stream';
3
- import { basename } from 'path';
4
- import { fileTypeFromBuffer, fileTypeFromStream } from 'file-type';
1
+ import { existsSync, createReadStream } from 'fs'
2
+ import { Readable, isReadable } from 'stream'
3
+ import { basename } from 'path'
4
+ import { fileTypeFromBuffer, fileTypeFromStream } from 'file-type'
5
5
 
6
6
  /**
7
7
  * 创建form
@@ -10,35 +10,28 @@ import { fileTypeFromBuffer, fileTypeFromStream } from 'file-type';
10
10
  * @returns
11
11
  */
12
12
  async function createPicFrom(image, name = 'image.jpg') {
13
- let picData;
14
- // 是 string
15
- if (typeof image === 'string') {
16
- if (!existsSync(image))
17
- return false;
18
- if (!name)
19
- name = basename(image);
20
- picData = createReadStream(image);
21
- // buffer
22
- }
23
- else if (Buffer.isBuffer(image)) {
24
- const file = await fileTypeFromBuffer(image);
25
- if (!name)
26
- name = 'file.' + file?.ext;
27
- picData = new Readable();
28
- picData.push(image);
29
- picData.push(null);
30
- // 文件流
31
- }
32
- else if (isReadable(image)) {
33
- const file = await fileTypeFromStream(image);
34
- if (!name)
35
- name = 'file.' + file?.ext;
36
- picData = image;
37
- }
38
- else {
39
- return false;
40
- }
41
- return { picData, image, name };
13
+ let picData
14
+ // 是 string
15
+ if (typeof image === 'string') {
16
+ if (!existsSync(image)) return false
17
+ if (!name) name = basename(image)
18
+ picData = createReadStream(image)
19
+ // buffer
20
+ } else if (Buffer.isBuffer(image)) {
21
+ const file = await fileTypeFromBuffer(image)
22
+ if (!name) name = 'file.' + file?.ext
23
+ picData = new Readable()
24
+ picData.push(image)
25
+ picData.push(null)
26
+ // 文件流
27
+ } else if (isReadable(image)) {
28
+ const file = await fileTypeFromStream(image)
29
+ if (!name) name = 'file.' + file?.ext
30
+ picData = image
31
+ } else {
32
+ return false
33
+ }
34
+ return { picData, image, name }
42
35
  }
43
36
 
44
- export { createPicFrom };
37
+ export { createPicFrom }
package/lib/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import * as alemonjs from 'alemonjs';
2
- import { QQBotClient } from './client.js';
2
+ import { QQBotAPI } from './sdk/api.js';
3
3
 
4
- type Client = typeof QQBotClient.prototype;
4
+ type Client = typeof QQBotAPI.prototype;
5
5
  declare const client: Client;
6
6
  declare const platform = "qq-bot";
7
- declare const _default: () => alemonjs.ClientAPI;
7
+ declare const _default: alemonjs.DefineBotValue;
8
8
 
9
9
  export { type Client, client, _default as default, platform };