@alemonjs/onebot 2.1.0-alpha.7 → 2.1.0-alpha.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -260,32 +260,86 @@ var index = () => {
260
260
  return Metions;
261
261
  }
262
262
  return [];
263
+ },
264
+ delete(message_id) {
265
+ return client.deleteMsg({ message_id: Number(message_id) });
266
+ },
267
+ file: {
268
+ channel: client.uploadGroupFile.bind(client),
269
+ user: client.uploadPrivateFile.bind(client)
270
+ },
271
+ forward: {
272
+ channel: client.sendGroupForward.bind(client),
273
+ user: client.sendPrivateForward.bind(client)
263
274
  }
264
275
  }
265
276
  };
266
277
  cbp.onactions(async (data, consume) => {
267
- if (data.action === 'message.send') {
268
- const event = data.payload.event;
269
- const paramFormat = data.payload.params.format;
270
- const res = await api.use.send(event, paramFormat);
271
- consume(res);
272
- }
273
- else if (data.action === 'message.send.channel') {
274
- const channel_id = data.payload.ChannelId;
275
- const val = data.payload.params.format;
276
- const res = await api.active.send.channel(channel_id, val);
277
- consume(res);
278
- }
279
- else if (data.action === 'message.send.user') {
280
- const user_id = data.payload.UserId;
281
- const val = data.payload.params.format;
282
- const res = await api.active.send.user(user_id, val);
283
- consume(res);
284
- }
285
- else if (data.action === 'mention.get') {
286
- const event = data.payload.event;
287
- const res = await api.use.mention(event);
288
- consume([createResult(ResultCode.Ok, '请求完成', res)]);
278
+ switch (data.action) {
279
+ case 'message.send': {
280
+ const event = data.payload.event;
281
+ const paramFormat = data.payload.params.format;
282
+ const res = await api.use.send(event, paramFormat);
283
+ return consume(res);
284
+ }
285
+ case 'message.send.channel': {
286
+ const channel_id = data.payload.ChannelId;
287
+ const val = data.payload.params.format;
288
+ const res = await api.active.send.channel(channel_id, val);
289
+ return consume(res);
290
+ }
291
+ case 'message.send.user': {
292
+ const user_id = data.payload.UserId;
293
+ const val = data.payload.params.format;
294
+ const res = await api.active.send.user(user_id, val);
295
+ return consume(res);
296
+ }
297
+ case 'mention.get': {
298
+ const event = data.payload.event;
299
+ const res = await api.use.mention(event);
300
+ return consume([createResult(ResultCode.Ok, '请求完成', res)]);
301
+ }
302
+ case 'message.delete': {
303
+ const res = await api.use
304
+ .delete(data.payload.MessageId)
305
+ .then(res => createResult(ResultCode.Ok, data.action, res))
306
+ .catch(err => createResult(ResultCode.Fail, data.action, err));
307
+ return consume([res]);
308
+ }
309
+ case 'file.send.channel': {
310
+ const res = await api.use.file
311
+ .channel(data.payload.ChannelId, data.payload.params)
312
+ .then(res => createResult(ResultCode.Ok, data.action, res))
313
+ .catch(err => createResult(ResultCode.Fail, data.action, err));
314
+ return consume([res]);
315
+ }
316
+ case 'file.send.user': {
317
+ const res = await api.use.file
318
+ .user(data.payload.UserId, data.payload.params)
319
+ .then(res => createResult(ResultCode.Ok, data.action, res))
320
+ .catch(err => createResult(ResultCode.Fail, data.action, err));
321
+ return consume([res]);
322
+ }
323
+ case 'message.forward.channel': {
324
+ const res = await api.use.forward
325
+ .channel(data.payload.ChannelId, data.payload.params.map(i => ({
326
+ ...i,
327
+ content: DataToMessage(i.content)
328
+ })))
329
+ .then(res => createResult(ResultCode.Ok, data.action, res))
330
+ .catch(err => createResult(ResultCode.Fail, data.action, err));
331
+ return consume([res]);
332
+ }
333
+ case 'message.forward.user': {
334
+ const res = await api.use.forward
335
+ .user(data.payload.UserId, data.payload.params.map(i => ({
336
+ ...i,
337
+ content: DataToMessage(i.content)
338
+ })))
339
+ .then(res => createResult(ResultCode.Ok, data.action, res))
340
+ .catch(err => createResult(ResultCode.Fail, data.action, err));
341
+ return consume([res]);
342
+ }
289
343
  }
290
344
  });
291
345
  // 处理 api 调用
package/lib/sdk/api.d.ts CHANGED
@@ -68,6 +68,43 @@ declare class OneBotAPI {
68
68
  approve: boolean;
69
69
  reason?: string;
70
70
  }): Promise<any>;
71
+ /** 撤回消息 */
72
+ deleteMsg(options: {
73
+ message_id: number;
74
+ }): Promise<any>;
75
+ /** 上传私聊文件 */
76
+ uploadPrivateFile(options: {
77
+ user_id: number;
78
+ file: string;
79
+ name?: string;
80
+ }): Promise<any>;
81
+ /** 上传群文件 */
82
+ uploadGroupFile(options: {
83
+ group_id: number;
84
+ file: string;
85
+ name?: string;
86
+ folder?: string;
87
+ }): Promise<any>;
88
+ /** 发送私聊转发 */
89
+ sendPrivateForward(options: {
90
+ user_id: number;
91
+ messages: {
92
+ time?: number;
93
+ content: any[];
94
+ user_id?: string;
95
+ nickname?: string;
96
+ }[];
97
+ }): Promise<any>;
98
+ /** 发送群转发 */
99
+ sendGroupForward(options: {
100
+ group_id: number;
101
+ messages: {
102
+ time?: number;
103
+ content: any[];
104
+ user_id?: string;
105
+ nickname?: string;
106
+ }[];
107
+ }): Promise<any>;
71
108
  }
72
109
 
73
110
  export { OneBotAPI };
package/lib/sdk/api.js CHANGED
@@ -153,6 +153,61 @@ class OneBotAPI {
153
153
  params: options
154
154
  });
155
155
  }
156
+ /** 撤回消息 */
157
+ deleteMsg(options) {
158
+ if (!this.ws)
159
+ return;
160
+ return send(this.ws, {
161
+ action: 'delete_msg',
162
+ params: options
163
+ });
164
+ }
165
+ /** 上传私聊文件 */
166
+ uploadPrivateFile(options) {
167
+ if (!this.ws)
168
+ return;
169
+ return send(this.ws, {
170
+ action: 'upload_private_file',
171
+ params: options
172
+ });
173
+ }
174
+ /** 上传群文件 */
175
+ uploadGroupFile(options) {
176
+ if (!this.ws)
177
+ return;
178
+ return send(this.ws, {
179
+ action: 'upload_group_file',
180
+ params: options
181
+ });
182
+ }
183
+ /** 发送私聊转发 */
184
+ sendPrivateForward(options) {
185
+ if (!this.ws)
186
+ return;
187
+ return send(this.ws, {
188
+ action: 'send_private_forward_msg',
189
+ params: {
190
+ user_id: 80000000,
191
+ nickname: '匿名消息',
192
+ ...options,
193
+ messages: options.messages.map(data => ({ data, type: 'node' }))
194
+ }
195
+ });
196
+ }
197
+ /** 发送群转发 */
198
+ sendGroupForward(options) {
199
+ if (!this.ws)
200
+ return;
201
+ return send(this.ws, {
202
+ action: 'send_group_forward_msg',
203
+ params: {
204
+ user_id: 80000000,
205
+ nickname: '匿名消息',
206
+ ...options,
207
+ messages: options.messages.map(data => ({ data, type: 'node' }))
208
+ }
209
+ });
210
+ }
156
211
  }
157
212
 
158
213
  export { OneBotAPI, consume };
package/lib/sdk/wss.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import WebSocket, { WebSocketServer } from 'ws';
2
2
  import { OneBotAPI, consume } from './api.js';
3
+ import dayjs from 'dayjs';
3
4
 
4
5
  /**
5
6
  * 连接
@@ -24,6 +25,15 @@ class OneBotClient extends OneBotAPI {
24
25
  }
25
26
  }
26
27
  #events = {};
28
+ // 重连次数
29
+ #count = 0;
30
+ #getReConnectTime() {
31
+ const time = this.#count > 3 ? 1000 * 6 : 1000 * 1;
32
+ const curTime = this.#count > 6 ? 1000 * this.#count * 2 : time;
33
+ logger.info(`[OneBot] 等待 ${dayjs(curTime).format('mm:ss')} 后重新连接`);
34
+ this.#count++;
35
+ return curTime;
36
+ }
27
37
  /**
28
38
  * 注册事件处理程序
29
39
  * @param key 事件名称
@@ -106,6 +116,12 @@ class OneBotClient extends OneBotAPI {
106
116
  };
107
117
  const onClose = (code, reason) => {
108
118
  logger.error(`[OneBot] WebSocket closed: ${code} - ${reason.toString('utf8')}`);
119
+ if (reverse_enable)
120
+ return;
121
+ const curTime = this.#getReConnectTime();
122
+ setTimeout(() => {
123
+ this.connect();
124
+ }, curTime);
109
125
  };
110
126
  if (!this.ws) {
111
127
  if (reverse_enable) {
@@ -125,6 +141,7 @@ class OneBotClient extends OneBotAPI {
125
141
  this.ws = new WebSocket(url, c);
126
142
  this.ws.on('open', () => {
127
143
  logger.info(`[OneBot] connected: ${url}`);
144
+ this.#count = 0;
128
145
  });
129
146
  // message
130
147
  this.ws.on('message', onMessage);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/onebot",
3
- "version": "2.1.0-alpha.7",
3
+ "version": "2.1.0-alpha.9",
4
4
  "description": "onebot v11",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",
@@ -21,6 +21,7 @@
21
21
  "alemonjs"
22
22
  ],
23
23
  "dependencies": {
24
+ "dayjs": "^1.11.13",
24
25
  "ws": "^8.18.1"
25
26
  },
26
27
  "peerDependencies": {