@alemonjs/onebot 2.1.0-alpha.11 → 2.1.0-alpha.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.
package/lib/config.d.ts CHANGED
@@ -1,3 +1,11 @@
1
- declare const platform = "onebot";
2
-
3
- export { platform };
1
+ export declare const platform = "onebot";
2
+ export type Options = {
3
+ url: string;
4
+ token?: string;
5
+ reverse_enable?: boolean;
6
+ reverse_port: number;
7
+ master_key?: string[];
8
+ master_id?: string[];
9
+ };
10
+ export declare const getOneBotConfig: () => Options;
11
+ export declare const getMaster: (UserId: string) => readonly [boolean, string];
package/lib/db.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export declare const BotMe: {
2
+ id: string;
3
+ name: string;
4
+ avatar: string;
5
+ };
package/lib/db.js CHANGED
@@ -1,4 +1,7 @@
1
1
  const BotMe = {
2
- id: ''};
2
+ id: '',
3
+ name: '',
4
+ avatar: ''
5
+ };
3
6
 
4
7
  export { BotMe };
package/lib/hook.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import { EventKeys, Events } from 'alemonjs';
2
- import { OneBotAPI } from './sdk/api.js';
3
- import { MESSAGES_TYPE, DIRECT_MESSAGE_TYPE } from './sdk/types.js';
4
-
2
+ import { OneBotAPI as API } from './sdk/api';
3
+ import { DIRECT_MESSAGE_TYPE, MESSAGES_TYPE } from './sdk/types';
5
4
  type MAP = {
6
5
  'message.create': MESSAGES_TYPE;
7
6
  'private.message.create': DIRECT_MESSAGE_TYPE;
@@ -11,8 +10,8 @@ type MAP = {
11
10
  'message.delete': undefined;
12
11
  'message.reaction.add': undefined;
13
12
  'message.reaction.remove': undefined;
14
- 'channal.create': undefined;
15
- 'channal.delete': undefined;
13
+ 'channel.create': undefined;
14
+ 'channel.delete': undefined;
16
15
  'guild.join': undefined;
17
16
  'guild.exit': undefined;
18
17
  'member.add': undefined;
@@ -22,17 +21,6 @@ type MAP = {
22
21
  'private.friend.add': undefined;
23
22
  'private.guild.add': undefined;
24
23
  };
25
- /**
26
- *
27
- * @param event
28
- * @returns
29
- */
30
- declare const useValue: <T extends EventKeys>(event: Events[T]) => readonly [MAP[T]];
31
- /**
32
- *
33
- * @param event
34
- * @returns
35
- */
36
- declare const useClient: <T extends EventKeys>(event: Events[T]) => readonly [OneBotAPI, MAP[T]];
37
-
38
- export { useClient, useValue };
24
+ export declare const useValue: <T extends EventKeys>(event: Events[T]) => readonly [MAP[T]];
25
+ export declare const useClient: <T extends EventKeys>(event: Events[T]) => readonly [API, MAP[T]];
26
+ export {};
package/lib/hook.js CHANGED
@@ -1,20 +1,10 @@
1
1
  import { createEventValue, useClient as useClient$1 } from 'alemonjs';
2
2
  import { OneBotAPI } from './sdk/api.js';
3
3
 
4
- /**
5
- *
6
- * @param event
7
- * @returns
8
- */
9
4
  const useValue = (event) => {
10
5
  const value = createEventValue(event);
11
6
  return [value];
12
7
  };
13
- /**
14
- *
15
- * @param event
16
- * @returns
17
- */
18
8
  const useClient = (event) => {
19
9
  const [client] = useClient$1(event, OneBotAPI);
20
10
  const value = createEventValue(event);
package/lib/index.d.ts CHANGED
@@ -1,7 +1,5 @@
1
- export { platform } from './config.js';
2
- export { OneBotAPI as API } from './sdk/api.js';
3
- export { useClient, useValue } from './hook.js';
4
-
1
+ export { platform } from './config';
2
+ export { OneBotAPI as API } from './sdk/api';
3
+ export * from './hook';
5
4
  declare const main: () => void;
6
-
7
- export { main as default };
5
+ export default main;
package/lib/index.js CHANGED
@@ -10,13 +10,9 @@ export { useClient, useValue } from './hook.js';
10
10
  const main = () => {
11
11
  const config = getOneBotConfig();
12
12
  const client = new OneBotClient({
13
- // url
14
13
  url: config?.url ?? '',
15
- // token
16
14
  access_token: config?.token ?? '',
17
- // 是否开启反向连接,正向连接失效
18
15
  reverse_enable: config?.reverse_enable ?? false,
19
- // 反向连接端口
20
16
  reverse_port: config?.reverse_port ?? 17158
21
17
  });
22
18
  void client.connect();
@@ -31,6 +27,7 @@ const main = () => {
31
27
  if (item.type === 'text') {
32
28
  msg += item.data.text;
33
29
  }
30
+ else if (item.type === 'at') ;
34
31
  }
35
32
  return msg.trim();
36
33
  };
@@ -45,7 +42,7 @@ const main = () => {
45
42
  const UserAvatar = createUserAvatar(UserId);
46
43
  const [isMaster, UserKey] = getMaster(UserId);
47
44
  const groupId = String(event.group_id);
48
- // 定义消
45
+ const replyId = event.message.find(item => item.type === 'reply')?.data?.id;
49
46
  const e = {
50
47
  name: 'message.create',
51
48
  Platform: platform,
@@ -61,6 +58,7 @@ const main = () => {
61
58
  MessageId: String(event.message_id),
62
59
  MessageText: msg.trim(),
63
60
  OpenId: UserId,
61
+ replyId,
64
62
  CreateAt: Date.now(),
65
63
  tag: 'message.create',
66
64
  value: event
@@ -72,7 +70,7 @@ const main = () => {
72
70
  const UserId = String(event.user_id);
73
71
  const UserAvatar = createUserAvatar(UserId);
74
72
  const [isMaster, UserKey] = getMaster(UserId);
75
- // 定义消
73
+ const replyId = event.message.find(item => item.type === 'reply')?.data?.id;
76
74
  const e = {
77
75
  name: 'private.message.create',
78
76
  Platform: platform,
@@ -86,17 +84,13 @@ const main = () => {
86
84
  MessageText: msg.trim(),
87
85
  OpenId: String(event.user_id),
88
86
  CreateAt: Date.now(),
87
+ replyId,
89
88
  tag: 'private.message.create',
90
89
  value: event
91
90
  };
92
91
  cbp.send(e);
93
92
  });
94
- /**
95
- * @param val
96
- * @returns
97
- */
98
93
  const DataToMessage = async (val = []) => {
99
- // 空元素
100
94
  const empty = {
101
95
  type: 'text',
102
96
  data: {
@@ -163,12 +157,6 @@ const main = () => {
163
157
  }));
164
158
  return message;
165
159
  };
166
- /**
167
- *
168
- * @param ChannelId
169
- * @param val
170
- * @returns
171
- */
172
160
  const sendGroup = async (ChannelId, val) => {
173
161
  if (val.length < 0) {
174
162
  return [];
@@ -185,12 +173,6 @@ const main = () => {
185
173
  return [createResult(ResultCode.Fail, 'client.groupOpenMessages', error)];
186
174
  }
187
175
  };
188
- /**
189
- *
190
- * @param UserId
191
- * @param val
192
- * @returns
193
- */
194
176
  const sendPrivate = async (UserId, val) => {
195
177
  if (val.length < 0) {
196
178
  return [];
@@ -279,6 +261,20 @@ const main = () => {
279
261
  };
280
262
  const onactions = async (data, consume) => {
281
263
  switch (data.action) {
264
+ case 'me.info': {
265
+ const res = await client.getLoginInfo();
266
+ const UserId = String(res?.user_id);
267
+ const [isMaster, UserKey] = getMaster(UserId);
268
+ const user = {
269
+ UserId: UserId,
270
+ UserName: res?.nickname,
271
+ IsBot: true,
272
+ IsMaster: isMaster,
273
+ UserAvatar: '',
274
+ UserKey: UserKey
275
+ };
276
+ return consume([createResult(ResultCode.Ok, '请求完成', user)]);
277
+ }
282
278
  case 'message.send': {
283
279
  const event = data.payload.event;
284
280
  const paramFormat = data.payload.params.format;
@@ -286,15 +282,15 @@ const main = () => {
286
282
  return consume(res);
287
283
  }
288
284
  case 'message.send.channel': {
289
- const channel_id = data.payload.ChannelId;
285
+ const ChannelId = data.payload.ChannelId;
290
286
  const val = data.payload.params.format;
291
- const res = await api.active.send.channel(channel_id, val);
287
+ const res = await api.active.send.channel(ChannelId, val);
292
288
  return consume(res);
293
289
  }
294
290
  case 'message.send.user': {
295
- const user_id = data.payload.UserId;
291
+ const UserId = data.payload.UserId;
296
292
  const val = data.payload.params.format;
297
- const res = await api.active.send.user(user_id, val);
293
+ const res = await api.active.send.user(UserId, val);
298
294
  return consume(res);
299
295
  }
300
296
  case 'mention.get': {
@@ -343,6 +339,9 @@ const main = () => {
343
339
  .catch(err => createResult(ResultCode.Fail, data.action, err));
344
340
  return consume([res]);
345
341
  }
342
+ default: {
343
+ return consume([createResult(ResultCode.Fail, '未知请求,请尝试升级版本', null)]);
344
+ }
346
345
  }
347
346
  };
348
347
  cbp.onactions((data, consume) => void onactions(data, consume));
@@ -353,21 +352,22 @@ const main = () => {
353
352
  const res = await client[key](...params);
354
353
  consume([createResult(ResultCode.Ok, '请求完成', res)]);
355
354
  }
355
+ else {
356
+ consume([createResult(ResultCode.Fail, '未知请求,请尝试升级版本', null)]);
357
+ }
356
358
  };
357
- // 处理 api 调用
358
359
  cbp.onapis((data, consume) => void onapis(data, consume));
359
360
  };
360
361
  const mainProcess = () => {
361
362
  ['SIGINT', 'SIGTERM', 'SIGQUIT', 'disconnect'].forEach(sig => {
362
363
  process?.on?.(sig, () => {
363
- logger?.info?.(`[@alemonjs/onebot][${sig}] 收到信号,正在关闭...`);
364
+ logger.info?.(`[@alemonjs/onebot][${sig}] 收到信号,正在关闭...`);
364
365
  setImmediate(() => process.exit(0));
365
366
  });
366
367
  });
367
368
  process?.on?.('exit', code => {
368
- logger?.info?.(`[@alemonjs/onebot][exit] 进程退出,code=${code}`);
369
+ logger.info?.(`[@alemonjs/onebot][exit] 进程退出,code=${code}`);
369
370
  });
370
- // 监听主进程消息
371
371
  process.on('message', msg => {
372
372
  try {
373
373
  const data = typeof msg === 'string' ? JSON.parse(msg) : msg;
@@ -380,7 +380,6 @@ const mainProcess = () => {
380
380
  }
381
381
  catch { }
382
382
  });
383
- // 主动发送 ready 消息
384
383
  if (process.send) {
385
384
  process.send(JSON.stringify({ type: 'ready' }));
386
385
  }
package/lib/sdk/api.d.ts CHANGED
@@ -1,91 +1,144 @@
1
1
  import WebSocket from 'ws';
2
-
3
- declare class OneBotAPI {
2
+ export declare const consume: (parsedMessage: any) => void;
3
+ export declare class OneBotAPI {
4
4
  ws: WebSocket | null;
5
- /**
6
- * 发送私聊消息
7
- * @param options
8
- * @returns
9
- */
10
5
  sendPrivateMessage(options: {
11
6
  user_id: number;
12
7
  message: any[];
13
8
  }): Promise<any>;
14
- /**
15
- * 发送群消息
16
- * @param options
17
- * @returns
18
- */
19
9
  sendGroupMessage(options: {
20
10
  group_id: number;
21
11
  message: any[];
22
12
  }): Promise<any>;
23
- /**
24
- * 发送消息
25
- * @param options
26
- * @returns
27
- */
28
13
  sendMessage(options: {
29
14
  message_type: 'private' | 'group';
30
15
  group_id?: number;
31
16
  user_id?: number;
32
17
  message: any[];
33
18
  }): Promise<any>;
34
- /**
35
- * 好友列表
36
- */
37
- getFriendList(): Promise<any>;
38
- /**
39
- * 群列表
40
- */
41
- getGroupList(): Promise<any>;
42
- /**
43
- * 群成员列表
44
- * @param options
45
- * @returns
46
- */
47
- getGroupMemberList(options: {
19
+ deleteMsg(options: {
20
+ message_id: number;
21
+ }): Promise<any>;
22
+ getMsg(options: {
23
+ message_id: number;
24
+ }): Promise<any>;
25
+ getForwardMsg(options: {
26
+ id: string;
27
+ }): Promise<any>;
28
+ sendLike(options: {
29
+ user_id: number;
30
+ times?: number;
31
+ }): Promise<any>;
32
+ setGroupKick(options: {
33
+ group_id: number;
34
+ user_id: number;
35
+ reject_add_request?: boolean;
36
+ }): Promise<any>;
37
+ setGroupBan(options: {
48
38
  group_id: number;
39
+ user_id: number;
40
+ duration: number;
41
+ }): Promise<any>;
42
+ setGroupAnonymousBan(options: {
43
+ group_id: number;
44
+ anonymous: {
45
+ id: string;
46
+ name: string;
47
+ };
48
+ duration: number;
49
+ }): Promise<any>;
50
+ setGroupWholeBan(options: {
51
+ group_id: number;
52
+ enable: boolean;
53
+ }): Promise<any>;
54
+ setGroupAdmin(options: {
55
+ group_id: number;
56
+ user_id: number;
57
+ enable: boolean;
58
+ }): Promise<any>;
59
+ setGroupAnonymous(options: {
60
+ group_id: number;
61
+ enable: boolean;
62
+ }): Promise<any>;
63
+ setGroupCard(options: {
64
+ group_id: number;
65
+ user_id: number;
66
+ card: string;
67
+ }): Promise<any>;
68
+ setGroupName(options: {
69
+ group_id: number;
70
+ group_name: string;
71
+ }): Promise<any>;
72
+ setGroupLeave(options: {
73
+ group_id: number;
74
+ is_dismiss?: boolean;
75
+ }): Promise<any>;
76
+ setGroupSpecialTitle(options: {
77
+ group_id: number;
78
+ user_id: number;
79
+ special_title: string;
80
+ duration: number;
49
81
  }): Promise<any>;
50
- /**
51
- * 处理好友请求
52
- * @param options
53
- * @returns
54
- */
55
82
  setFriendAddRequest(options: {
56
83
  flag: string;
57
84
  approve: boolean;
58
85
  remark?: string;
59
86
  }): Promise<any>;
60
- /**
61
- * 处理群请求
62
- * @param options
63
- * @returns
64
- */
65
87
  setGroupAddRequest(options: {
66
88
  flag: string;
67
89
  sub_type: string;
68
90
  approve: boolean;
69
91
  reason?: string;
70
92
  }): Promise<any>;
71
- /** 撤回消息 */
72
- deleteMsg(options: {
73
- message_id: number;
93
+ getLoginInfo(): Promise<any>;
94
+ getStrangerInfo(options: {
95
+ user_id: number;
96
+ no_cache?: boolean;
97
+ }): Promise<any>;
98
+ getFriendList(): Promise<any>;
99
+ getGroupInfo(params?: {
100
+ group_id: number;
101
+ no_cache?: boolean;
102
+ }): Promise<any>;
103
+ getGroupList(): Promise<any>;
104
+ getGroupMemberInfo(options: {
105
+ group_id: number;
106
+ user_id: number;
107
+ no_cache?: boolean;
108
+ }): Promise<any>;
109
+ getGroupMemberList(options: {
110
+ group_id: number;
111
+ }): Promise<any>;
112
+ getGroupHonorInfo(options: {
113
+ group_id: number;
114
+ type: 'talkative' | 'performer' | 'legend' | 'strong_newbie';
115
+ }): Promise<any>;
116
+ getCookies(): Promise<any>;
117
+ getCsrfToken(): Promise<any>;
118
+ getCredentials(): Promise<any>;
119
+ getRecord(options: {
120
+ file: string;
121
+ }): Promise<any>;
122
+ getImage(options: {
123
+ file: string;
74
124
  }): Promise<any>;
75
- /** 上传私聊文件 */
125
+ canSendImage(): Promise<any>;
126
+ canSendRecord(): Promise<any>;
127
+ getStatus(): Promise<any>;
128
+ getVersionInfo(): Promise<any>;
129
+ setRestart(): Promise<any>;
130
+ cleanCache(): Promise<any>;
76
131
  uploadPrivateFile(options: {
77
132
  user_id: number;
78
133
  file: string;
79
134
  name?: string;
80
135
  }): Promise<any>;
81
- /** 上传群文件 */
82
136
  uploadGroupFile(options: {
83
137
  group_id: number;
84
138
  file: string;
85
139
  name?: string;
86
140
  folder?: string;
87
141
  }): Promise<any>;
88
- /** 发送私聊转发 */
89
142
  sendPrivateForward(options: {
90
143
  user_id: number;
91
144
  messages: {
@@ -95,7 +148,6 @@ declare class OneBotAPI {
95
148
  nickname?: string;
96
149
  }[];
97
150
  }): Promise<any>;
98
- /** 发送群转发 */
99
151
  sendGroupForward(options: {
100
152
  group_id: number;
101
153
  messages: {
@@ -106,5 +158,3 @@ declare class OneBotAPI {
106
158
  }[];
107
159
  }): Promise<any>;
108
160
  }
109
-
110
- export { OneBotAPI };
package/lib/sdk/api.js CHANGED
@@ -1,42 +1,26 @@
1
1
  import { generateUniqueId, actionResolves, actionTimeouts, timeoutTime } from './config.js';
2
2
 
3
- /**
4
- *
5
- * @param ws
6
- * @param data
7
- * @returns
8
- */
9
3
  const send = (ws, data) => {
10
4
  const id = generateUniqueId();
11
- // 设置唯一标识符
12
5
  data.echo = id;
13
6
  return new Promise((resolve, reject) => {
14
7
  actionResolves.set(id, { resolve, reject });
15
- // 发送消息
16
8
  ws.send(JSON.stringify(data));
17
- // 30 秒后超时
18
9
  const timeout = setTimeout(() => {
19
- // 被清理了
20
10
  if (!actionResolves.has(id) || !actionTimeouts.has(id)) {
21
11
  return;
22
12
  }
23
- // 删除回调
24
13
  actionResolves.delete(id);
25
- // 删除超时器
26
14
  actionTimeouts.delete(id);
27
- // 不会当错误进行处理。而是传入错误码
28
15
  resolve(null);
29
16
  }, timeoutTime);
30
17
  actionTimeouts.set(id, timeout);
31
18
  });
32
19
  };
33
- // 消费
34
20
  const consume = (parsedMessage) => {
35
- // 读取 唯一标识
36
21
  const id = parsedMessage.echo;
37
22
  const { resolve, reject } = actionResolves.get(id) || {};
38
23
  actionResolves.delete(id);
39
- // 清除超时器
40
24
  const timeout = actionTimeouts.get(id);
41
25
  if (timeout) {
42
26
  actionTimeouts.delete(id);
@@ -53,11 +37,6 @@ const consume = (parsedMessage) => {
53
37
  };
54
38
  class OneBotAPI {
55
39
  ws = null;
56
- /**
57
- * 发送私聊消息
58
- * @param options
59
- * @returns
60
- */
61
40
  sendPrivateMessage(options) {
62
41
  if (!this.ws) {
63
42
  return;
@@ -67,11 +46,6 @@ class OneBotAPI {
67
46
  params: options
68
47
  });
69
48
  }
70
- /**
71
- * 发送群消息
72
- * @param options
73
- * @returns
74
- */
75
49
  sendGroupMessage(options) {
76
50
  if (!this.ws) {
77
51
  return;
@@ -81,11 +55,6 @@ class OneBotAPI {
81
55
  params: options
82
56
  });
83
57
  }
84
- /**
85
- * 发送消息
86
- * @param options
87
- * @returns
88
- */
89
58
  sendMessage(options) {
90
59
  if (!this.ws) {
91
60
  return;
@@ -95,9 +64,168 @@ class OneBotAPI {
95
64
  params: options
96
65
  });
97
66
  }
98
- /**
99
- * 好友列表
100
- */
67
+ deleteMsg(options) {
68
+ if (!this.ws) {
69
+ return;
70
+ }
71
+ return send(this.ws, {
72
+ action: 'delete_msg',
73
+ params: options
74
+ });
75
+ }
76
+ getMsg(options) {
77
+ if (!this.ws) {
78
+ return;
79
+ }
80
+ return send(this.ws, {
81
+ action: 'get_msg',
82
+ params: options
83
+ });
84
+ }
85
+ getForwardMsg(options) {
86
+ if (!this.ws) {
87
+ return;
88
+ }
89
+ return send(this.ws, {
90
+ action: 'get_forward_msg',
91
+ params: options
92
+ });
93
+ }
94
+ sendLike(options) {
95
+ if (!this.ws) {
96
+ return;
97
+ }
98
+ return send(this.ws, {
99
+ action: 'send_like',
100
+ params: options
101
+ });
102
+ }
103
+ setGroupKick(options) {
104
+ if (!this.ws) {
105
+ return;
106
+ }
107
+ return send(this.ws, {
108
+ action: 'set_group_kick',
109
+ params: options
110
+ });
111
+ }
112
+ setGroupBan(options) {
113
+ if (!this.ws) {
114
+ return;
115
+ }
116
+ return send(this.ws, {
117
+ action: 'set_group_ban',
118
+ params: options
119
+ });
120
+ }
121
+ setGroupAnonymousBan(options) {
122
+ if (!this.ws) {
123
+ return;
124
+ }
125
+ return send(this.ws, {
126
+ action: 'set_group_anonymous_ban',
127
+ params: options
128
+ });
129
+ }
130
+ setGroupWholeBan(options) {
131
+ if (!this.ws) {
132
+ return;
133
+ }
134
+ return send(this.ws, {
135
+ action: 'set_group_whole_ban',
136
+ params: options
137
+ });
138
+ }
139
+ setGroupAdmin(options) {
140
+ if (!this.ws) {
141
+ return;
142
+ }
143
+ return send(this.ws, {
144
+ action: 'set_group_admin',
145
+ params: options
146
+ });
147
+ }
148
+ setGroupAnonymous(options) {
149
+ if (!this.ws) {
150
+ return;
151
+ }
152
+ return send(this.ws, {
153
+ action: 'set_group_anonymous',
154
+ params: options
155
+ });
156
+ }
157
+ setGroupCard(options) {
158
+ if (!this.ws) {
159
+ return;
160
+ }
161
+ return send(this.ws, {
162
+ action: 'set_group_card',
163
+ params: options
164
+ });
165
+ }
166
+ setGroupName(options) {
167
+ if (!this.ws) {
168
+ return;
169
+ }
170
+ return send(this.ws, {
171
+ action: 'set_group_name',
172
+ params: options
173
+ });
174
+ }
175
+ setGroupLeave(options) {
176
+ if (!this.ws) {
177
+ return;
178
+ }
179
+ return send(this.ws, {
180
+ action: 'set_group_leave',
181
+ params: options
182
+ });
183
+ }
184
+ setGroupSpecialTitle(options) {
185
+ if (!this.ws) {
186
+ return;
187
+ }
188
+ return send(this.ws, {
189
+ action: 'set_group_special_title',
190
+ params: options
191
+ });
192
+ }
193
+ setFriendAddRequest(options) {
194
+ if (!this.ws) {
195
+ return;
196
+ }
197
+ return send(this.ws, {
198
+ action: 'set_friend_add_request',
199
+ params: options
200
+ });
201
+ }
202
+ setGroupAddRequest(options) {
203
+ if (!this.ws) {
204
+ return;
205
+ }
206
+ return send(this.ws, {
207
+ action: 'set_group_add_request',
208
+ params: options
209
+ });
210
+ }
211
+ getLoginInfo() {
212
+ if (!this.ws) {
213
+ return;
214
+ }
215
+ return send(this.ws, {
216
+ action: 'get_login_info',
217
+ params: {}
218
+ });
219
+ }
220
+ getStrangerInfo(options) {
221
+ if (!this.ws) {
222
+ return;
223
+ }
224
+ return send(this.ws, {
225
+ action: 'get_stranger_info',
226
+ params: options
227
+ });
228
+ }
101
229
  getFriendList() {
102
230
  if (!this.ws) {
103
231
  return;
@@ -107,9 +235,15 @@ class OneBotAPI {
107
235
  params: {}
108
236
  });
109
237
  }
110
- /**
111
- * 群列表
112
- */
238
+ getGroupInfo(params) {
239
+ if (!this.ws) {
240
+ return;
241
+ }
242
+ return send(this.ws, {
243
+ action: 'get_group_info',
244
+ params: params
245
+ });
246
+ }
113
247
  getGroupList() {
114
248
  if (!this.ws) {
115
249
  return;
@@ -119,11 +253,15 @@ class OneBotAPI {
119
253
  params: {}
120
254
  });
121
255
  }
122
- /**
123
- * 群成员列表
124
- * @param options
125
- * @returns
126
- */
256
+ getGroupMemberInfo(options) {
257
+ if (!this.ws) {
258
+ return;
259
+ }
260
+ return send(this.ws, {
261
+ action: 'get_group_member_info',
262
+ params: options
263
+ });
264
+ }
127
265
  getGroupMemberList(options) {
128
266
  if (!this.ws) {
129
267
  return;
@@ -133,45 +271,114 @@ class OneBotAPI {
133
271
  params: options
134
272
  });
135
273
  }
136
- /**
137
- * 处理好友请求
138
- * @param options
139
- * @returns
140
- */
141
- setFriendAddRequest(options) {
274
+ getGroupHonorInfo(options) {
142
275
  if (!this.ws) {
143
276
  return;
144
277
  }
145
278
  return send(this.ws, {
146
- action: 'set_friend_add_request',
279
+ action: 'get_group_honor_info',
147
280
  params: options
148
281
  });
149
282
  }
150
- /**
151
- * 处理群请求
152
- * @param options
153
- * @returns
154
- */
155
- setGroupAddRequest(options) {
283
+ getCookies() {
156
284
  if (!this.ws) {
157
285
  return;
158
286
  }
159
287
  return send(this.ws, {
160
- action: 'set_group_add_request',
288
+ action: 'get_cookies',
289
+ params: {}
290
+ });
291
+ }
292
+ getCsrfToken() {
293
+ if (!this.ws) {
294
+ return;
295
+ }
296
+ return send(this.ws, {
297
+ action: 'get_csrf_token',
298
+ params: {}
299
+ });
300
+ }
301
+ getCredentials() {
302
+ if (!this.ws) {
303
+ return;
304
+ }
305
+ return send(this.ws, {
306
+ action: 'get_credentials',
307
+ params: {}
308
+ });
309
+ }
310
+ getRecord(options) {
311
+ if (!this.ws) {
312
+ return;
313
+ }
314
+ return send(this.ws, {
315
+ action: 'get_record',
161
316
  params: options
162
317
  });
163
318
  }
164
- /** 撤回消息 */
165
- deleteMsg(options) {
319
+ getImage(options) {
166
320
  if (!this.ws) {
167
321
  return;
168
322
  }
169
323
  return send(this.ws, {
170
- action: 'delete_msg',
324
+ action: 'get_image',
171
325
  params: options
172
326
  });
173
327
  }
174
- /** 上传私聊文件 */
328
+ canSendImage() {
329
+ if (!this.ws) {
330
+ return;
331
+ }
332
+ return send(this.ws, {
333
+ action: 'can_send_image',
334
+ params: {}
335
+ });
336
+ }
337
+ canSendRecord() {
338
+ if (!this.ws) {
339
+ return;
340
+ }
341
+ return send(this.ws, {
342
+ action: 'can_send_record',
343
+ params: {}
344
+ });
345
+ }
346
+ getStatus() {
347
+ if (!this.ws) {
348
+ return;
349
+ }
350
+ return send(this.ws, {
351
+ action: 'get_status',
352
+ params: {}
353
+ });
354
+ }
355
+ getVersionInfo() {
356
+ if (!this.ws) {
357
+ return;
358
+ }
359
+ return send(this.ws, {
360
+ action: 'get_version_info',
361
+ params: {}
362
+ });
363
+ }
364
+ setRestart() {
365
+ if (!this.ws) {
366
+ return;
367
+ }
368
+ return send(this.ws, {
369
+ action: 'set_restart',
370
+ params: {}
371
+ });
372
+ }
373
+ cleanCache() {
374
+ if (!this.ws) {
375
+ return;
376
+ }
377
+ return send(this.ws, {
378
+ action: 'clean_cache',
379
+ params: {}
380
+ });
381
+ }
175
382
  uploadPrivateFile(options) {
176
383
  if (!this.ws) {
177
384
  return;
@@ -181,7 +388,6 @@ class OneBotAPI {
181
388
  params: options
182
389
  });
183
390
  }
184
- /** 上传群文件 */
185
391
  uploadGroupFile(options) {
186
392
  if (!this.ws) {
187
393
  return;
@@ -191,7 +397,6 @@ class OneBotAPI {
191
397
  params: options
192
398
  });
193
399
  }
194
- /** 发送私聊转发 */
195
400
  sendPrivateForward(options) {
196
401
  if (!this.ws) {
197
402
  return;
@@ -206,7 +411,6 @@ class OneBotAPI {
206
411
  }
207
412
  });
208
413
  }
209
- /** 发送群转发 */
210
414
  sendGroupForward(options) {
211
415
  if (!this.ws) {
212
416
  return;
@@ -0,0 +1,7 @@
1
+ export declare const actionResolves: Map<string, {
2
+ resolve: (value: any) => void;
3
+ reject: (reason?: any) => void;
4
+ }>;
5
+ export declare const actionTimeouts: Map<string, NodeJS.Timeout>;
6
+ export declare const generateUniqueId: () => string;
7
+ export declare const timeoutTime: number;
package/lib/sdk/config.js CHANGED
@@ -1,12 +1,8 @@
1
- // 行为回调
2
1
  const actionResolves = new Map();
3
- // 超时器
4
2
  const actionTimeouts = new Map();
5
- // 生成唯一标识符
6
3
  const generateUniqueId = () => {
7
4
  return Date.now().toString(36) + Math.random().toString(36).substring(2);
8
5
  };
9
- // 超时时间
10
- const timeoutTime = 1000 * 12; // 12秒
6
+ const timeoutTime = 1000 * 12;
11
7
 
12
8
  export { actionResolves, actionTimeouts, generateUniqueId, timeoutTime };
@@ -28,8 +28,13 @@ type Message = {
28
28
  id: number;
29
29
  content: any[];
30
30
  };
31
+ } | {
32
+ type: 'reply';
33
+ data: {
34
+ id: string;
35
+ };
31
36
  };
32
- interface MESSAGES_TYPE {
37
+ export interface MESSAGES_TYPE {
33
38
  self_id: number;
34
39
  user_id: number;
35
40
  time: number;
@@ -46,7 +51,7 @@ interface MESSAGES_TYPE {
46
51
  post_type: 'message';
47
52
  group_id: number;
48
53
  }
49
- interface DIRECT_MESSAGE_TYPE {
54
+ export interface DIRECT_MESSAGE_TYPE {
50
55
  self_id: number;
51
56
  user_id: number;
52
57
  time: number;
@@ -68,5 +73,41 @@ interface DIRECT_MESSAGE_TYPE {
68
73
  group_id: number;
69
74
  temp_source: number;
70
75
  }
71
-
72
- export type { DIRECT_MESSAGE_TYPE, MESSAGES_TYPE };
76
+ export interface meta_event_lifecycle {
77
+ time: number;
78
+ self_id: number;
79
+ post_type: 'meta_event';
80
+ meta_event_type: 'lifecycle';
81
+ sub_type: 'connect';
82
+ }
83
+ export interface meta_event_heartbeat {
84
+ time: number;
85
+ self_id: number;
86
+ post_type: 'meta_event';
87
+ meta_event_type: 'heartbeat';
88
+ status: {
89
+ online: boolean;
90
+ good: boolean;
91
+ };
92
+ interval: number;
93
+ }
94
+ export interface group_recall {
95
+ time: number;
96
+ self_id: number;
97
+ post_type: 'notice';
98
+ group_id: number;
99
+ user_id: number;
100
+ notice_type: 'group_recall';
101
+ operator_id: number;
102
+ message_id: number;
103
+ }
104
+ export interface request_friend {
105
+ time: number;
106
+ self_id: number;
107
+ post_type: 'request';
108
+ request_type: 'friend';
109
+ user_id: number;
110
+ comment: string;
111
+ flag: string;
112
+ }
113
+ export {};
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,10 @@
1
+ import { MESSAGES_TYPE, DIRECT_MESSAGE_TYPE, meta_event_lifecycle, meta_event_heartbeat } from './types';
2
+ export type OneBotEventMap = {
3
+ DIRECT_MESSAGE: DIRECT_MESSAGE_TYPE;
4
+ MESSAGES: MESSAGES_TYPE;
5
+ META: meta_event_lifecycle | meta_event_heartbeat;
6
+ REQUEST_ADD_FRIEND: any;
7
+ REQUEST_ADD_GROUP: any;
8
+ NOTICE_GROUP_MEMBER_INCREASE: any;
9
+ NOTICE_GROUP_MEMBER_REDUCE: any;
10
+ };
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,13 @@
1
+ import { OneBotAPI } from './api';
2
+ import { OneBotEventMap } from './typing';
3
+ export declare class OneBotClient extends OneBotAPI {
4
+ #private;
5
+ constructor(opstion: {
6
+ url: string;
7
+ access_token: string;
8
+ reverse_enable: boolean;
9
+ reverse_port: number;
10
+ });
11
+ on<T extends keyof OneBotEventMap>(key: T, val: (event: OneBotEventMap[T]) => any): this;
12
+ connect(): void;
13
+ }
package/lib/sdk/wss.js CHANGED
@@ -2,9 +2,6 @@ import WebSocket, { WebSocketServer } from 'ws';
2
2
  import { OneBotAPI, consume } from './api.js';
3
3
  import dayjs from 'dayjs';
4
4
 
5
- /**
6
- * 连接
7
- */
8
5
  class OneBotClient extends OneBotAPI {
9
6
  #options = {
10
7
  url: '',
@@ -12,10 +9,6 @@ class OneBotClient extends OneBotAPI {
12
9
  reverse_enable: false,
13
10
  reverse_port: 17158
14
11
  };
15
- /**
16
- * 设置配置
17
- * @param opstion
18
- */
19
12
  constructor(opstion) {
20
13
  super();
21
14
  for (const key in opstion) {
@@ -25,7 +18,6 @@ class OneBotClient extends OneBotAPI {
25
18
  }
26
19
  }
27
20
  #events = {};
28
- // 重连次数
29
21
  #count = 0;
30
22
  #getReConnectTime() {
31
23
  const time = this.#count > 3 ? 1000 * 6 : 1000 * 1;
@@ -34,20 +26,10 @@ class OneBotClient extends OneBotAPI {
34
26
  this.#count++;
35
27
  return curTime;
36
28
  }
37
- /**
38
- * 注册事件处理程序
39
- * @param key 事件名称
40
- * @param val 事件处理函数
41
- */
42
29
  on(key, val) {
43
30
  this.#events[key] = val;
44
31
  return this;
45
32
  }
46
- /**
47
- *
48
- * @param cfg
49
- * @param conversation
50
- */
51
33
  connect() {
52
34
  const { url, access_token: token, reverse_enable, reverse_port } = this.#options;
53
35
  const notToken = !token || token === '';
@@ -83,20 +65,17 @@ class OneBotClient extends OneBotAPI {
83
65
  }
84
66
  else if (event?.post_type === 'notice') {
85
67
  if (event?.notice_type === 'group_increase') {
86
- // 群成员增加
87
68
  if (this.#events['NOTICE_GROUP_MEMBER_INCREASE']) {
88
69
  this.#events['NOTICE_GROUP_MEMBER_INCREASE'](event);
89
70
  }
90
71
  }
91
72
  else if (event?.notice_type === 'group_decrease') {
92
- // 群成员减少
93
73
  if (this.#events['NOTICE_GROUP_MEMBER_REDUCE']) {
94
74
  this.#events['NOTICE_GROUP_MEMBER_REDUCE'](event);
95
75
  }
96
76
  }
97
77
  }
98
78
  else if (event?.post_type === 'request') {
99
- // 收到加群 或 加好友的请求。
100
79
  if (event?.request_type === 'friend') {
101
80
  if (this.#events['REQUEST_ADD_FRIEND']) {
102
81
  this.#events['REQUEST_ADD_FRIEND'](event);
@@ -109,7 +88,6 @@ class OneBotClient extends OneBotAPI {
109
88
  }
110
89
  }
111
90
  else if (event?.echo) {
112
- // 消费
113
91
  consume(event);
114
92
  }
115
93
  }
@@ -129,27 +107,21 @@ class OneBotClient extends OneBotAPI {
129
107
  };
130
108
  if (!this.ws) {
131
109
  if (reverse_enable) {
132
- // reverse_open
133
110
  const server = new WebSocketServer({ port: reverse_port ?? 17158 });
134
111
  server.on('connection', ws => {
135
112
  this.ws = ws;
136
- // message
137
113
  this.ws.on('message', onMessage);
138
- // close
139
114
  this.ws.on('close', onClose);
140
115
  logger.info(`[OneBot] connected: ws://127.0.0.1:${reverse_port}`);
141
116
  });
142
117
  }
143
118
  else {
144
- // forward_open
145
119
  this.ws = new WebSocket(url, c);
146
120
  this.ws.on('open', () => {
147
121
  logger.info(`[OneBot] connected: ${url}`);
148
122
  this.#count = 0;
149
123
  });
150
- // message
151
124
  this.ws.on('message', onMessage);
152
- // close
153
125
  this.ws.on('close', onClose);
154
126
  }
155
127
  }
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@alemonjs/onebot",
3
- "version": "2.1.0-alpha.11",
4
- "description": "onebot v11",
3
+ "version": "2.1.0-alpha.13",
4
+ "description": "oneBot v11",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",
7
7
  "type": "module",
8
8
  "main": "lib/index.js",
9
9
  "types": "lib",
10
10
  "scripts": {
11
- "build": "node bundle.js"
11
+ "build": "lvy build"
12
12
  },
13
13
  "exports": {
14
14
  ".": {
@@ -47,4 +47,4 @@
47
47
  "type": "git",
48
48
  "url": "https://github.com/lemonade-lab/alemonjs.git"
49
49
  }
50
- }
50
+ }