@alemonjs/onebot 0.2.2 → 0.2.4
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/README.md +0 -2
- package/lib/index.d.ts +3 -2
- package/lib/index.js +212 -58
- package/lib/sdk/wss.d.ts +57 -4
- package/lib/sdk/wss.js +111 -13
- package/lib/server/index.js +73 -0
- package/package.json +14 -8
package/README.md
CHANGED
package/lib/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { OneBotClient } from './sdk/wss.js';
|
|
|
4
4
|
type Client = typeof OneBotClient.prototype;
|
|
5
5
|
declare const client: Client;
|
|
6
6
|
declare const platform = "onebot";
|
|
7
|
-
declare const _default:
|
|
7
|
+
declare const _default: alemonjs.DefinePlatformValue;
|
|
8
8
|
|
|
9
|
-
export {
|
|
9
|
+
export { client, _default as default, platform };
|
|
10
|
+
export type { Client };
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getConfigValue, useUserHashKey, onProcessor, createResult, ResultCode } from 'alemonjs';
|
|
2
2
|
import { OneBotClient } from './sdk/wss.js';
|
|
3
|
+
import { createServer } from './server/index.js';
|
|
4
|
+
import { readFileSync } from 'fs';
|
|
3
5
|
|
|
6
|
+
const ImageURLToBuffer = async (url) => {
|
|
7
|
+
const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
|
|
8
|
+
return Buffer.from(arrayBuffer);
|
|
9
|
+
};
|
|
4
10
|
const client = new Proxy({}, {
|
|
5
11
|
get: (_, prop) => {
|
|
6
12
|
if (prop in global.client) {
|
|
@@ -12,12 +18,11 @@ const client = new Proxy({}, {
|
|
|
12
18
|
}
|
|
13
19
|
});
|
|
14
20
|
const platform = 'onebot';
|
|
15
|
-
var index =
|
|
21
|
+
var index = definePlatform(() => {
|
|
16
22
|
let value = getConfigValue();
|
|
17
23
|
if (!value)
|
|
18
24
|
value = {};
|
|
19
25
|
const config = value[platform];
|
|
20
|
-
//
|
|
21
26
|
const client = new OneBotClient({
|
|
22
27
|
// url
|
|
23
28
|
url: config?.url ?? '',
|
|
@@ -28,20 +33,16 @@ var index = defineBot(() => {
|
|
|
28
33
|
// 反向连接端口
|
|
29
34
|
reverse_port: config?.reverse_port ?? 17158
|
|
30
35
|
});
|
|
31
|
-
//
|
|
32
36
|
client.connect();
|
|
33
|
-
//
|
|
34
37
|
client.on('META', event => {
|
|
35
38
|
if (event?.self_id) {
|
|
36
39
|
String(event.self_id);
|
|
37
40
|
}
|
|
38
41
|
});
|
|
39
|
-
//
|
|
40
42
|
client.on('MESSAGES', event => {
|
|
41
43
|
const uis = config.master_uids ?? [];
|
|
42
44
|
let msg = '';
|
|
43
45
|
const arr = [];
|
|
44
|
-
// let at_users = []
|
|
45
46
|
for (const item of event.message) {
|
|
46
47
|
if (item.type == 'text') {
|
|
47
48
|
msg = item.data.text;
|
|
@@ -76,7 +77,6 @@ var index = defineBot(() => {
|
|
|
76
77
|
Platform: platform,
|
|
77
78
|
// 频道
|
|
78
79
|
GuildId: String(event.group_id),
|
|
79
|
-
// guild_name: event.group_name,
|
|
80
80
|
// 子频道
|
|
81
81
|
ChannelId: String(event.group_id),
|
|
82
82
|
IsMaster: uis.includes(String(event.user_id)),
|
|
@@ -87,80 +87,234 @@ var index = defineBot(() => {
|
|
|
87
87
|
UserAvatar: UserAvatar,
|
|
88
88
|
// message
|
|
89
89
|
MessageId: String(event.message_id),
|
|
90
|
-
MessageText: msg,
|
|
90
|
+
MessageText: msg.trim(),
|
|
91
91
|
// 用户openId
|
|
92
92
|
OpenId: String(event.user_id),
|
|
93
93
|
// 创建时间
|
|
94
94
|
CreateAt: Date.now(),
|
|
95
95
|
// 标签
|
|
96
|
-
tag: '
|
|
96
|
+
tag: 'message.create',
|
|
97
97
|
//
|
|
98
98
|
value: null
|
|
99
99
|
};
|
|
100
|
-
//
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
100
|
+
// 处理消息
|
|
101
|
+
onProcessor('message.create', e, event);
|
|
102
|
+
});
|
|
103
|
+
client.on('DIRECT_MESSAGE', (event) => {
|
|
104
|
+
const uis = config.master_uids ?? [];
|
|
105
|
+
let msg = '';
|
|
106
|
+
const arr = [];
|
|
107
|
+
for (const item of event.message) {
|
|
108
|
+
if (item.type == 'text') {
|
|
109
|
+
msg = item.data.text;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
for (const item of arr) {
|
|
113
|
+
msg = msg.replace(item.text, '').trim();
|
|
114
|
+
}
|
|
115
|
+
const url = `https://q1.qlogo.cn/g?b=qq&s=0&nk=${event.user_id}`;
|
|
116
|
+
const UserAvatar = {
|
|
117
|
+
toBuffer: async () => {
|
|
118
|
+
const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
|
|
119
|
+
return Buffer.from(arrayBuffer);
|
|
120
|
+
},
|
|
121
|
+
toBase64: async () => {
|
|
122
|
+
const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
|
|
123
|
+
return Buffer.from(arrayBuffer).toString('base64');
|
|
124
|
+
},
|
|
125
|
+
toURL: async () => {
|
|
126
|
+
return url;
|
|
104
127
|
}
|
|
128
|
+
};
|
|
129
|
+
const UserId = String(event.user_id);
|
|
130
|
+
const UserKey = useUserHashKey({
|
|
131
|
+
Platform: platform,
|
|
132
|
+
UserId
|
|
105
133
|
});
|
|
134
|
+
// 定义消
|
|
135
|
+
const e = {
|
|
136
|
+
name: 'private.message.create',
|
|
137
|
+
// 平台类型
|
|
138
|
+
Platform: platform,
|
|
139
|
+
// 频道
|
|
140
|
+
// GuildId: String(event.group_id),
|
|
141
|
+
// 子频道
|
|
142
|
+
// ChannelId: String(event.group_id),
|
|
143
|
+
IsMaster: uis.includes(String(event.user_id)),
|
|
144
|
+
IsBot: false,
|
|
145
|
+
UserId: UserId,
|
|
146
|
+
UserName: event.sender.nickname,
|
|
147
|
+
UserKey,
|
|
148
|
+
UserAvatar: UserAvatar,
|
|
149
|
+
// message
|
|
150
|
+
MessageId: String(event.message_id),
|
|
151
|
+
MessageText: msg.trim(),
|
|
152
|
+
// 用户openId
|
|
153
|
+
OpenId: String(event.user_id),
|
|
154
|
+
// 创建时间
|
|
155
|
+
CreateAt: Date.now(),
|
|
156
|
+
// 标签
|
|
157
|
+
tag: 'private.message.create',
|
|
158
|
+
//
|
|
159
|
+
value: null
|
|
160
|
+
};
|
|
106
161
|
// 处理消息
|
|
107
|
-
|
|
108
|
-
});
|
|
109
|
-
client.on('DIRECT_MESSAGE', () => {
|
|
110
|
-
// const e = {
|
|
111
|
-
// isMaster: event.user_id == masterId,
|
|
112
|
-
// msg_txt: event.raw_message,
|
|
113
|
-
// msg: event.raw_message.trim(),
|
|
114
|
-
// msg_id: event.message_id,
|
|
115
|
-
// open_id: event.user_id,
|
|
116
|
-
// user_id: event.user_id,
|
|
117
|
-
// user_avatar:
|
|
118
|
-
// event.platform == 'qq'
|
|
119
|
-
// ? `https://q1.qlogo.cn/g?b=qq&s=0&nk=${event.user_id}`
|
|
120
|
-
// : '',
|
|
121
|
-
// user_name: event.sender.nickname,
|
|
162
|
+
onProcessor('private.message.create', e, event);
|
|
122
163
|
});
|
|
123
164
|
// 错误处理
|
|
124
165
|
client.on('ERROR', event => {
|
|
125
166
|
console.error('ERROR', event);
|
|
126
167
|
});
|
|
127
168
|
global.client = client;
|
|
169
|
+
createServer(client);
|
|
170
|
+
const sendGroup = async (event, val) => {
|
|
171
|
+
if (val.length < 0)
|
|
172
|
+
return Promise.all([]);
|
|
173
|
+
const content = val
|
|
174
|
+
.filter(item => item.type == 'Mention' || item.type == 'Text')
|
|
175
|
+
.map(item => item.value)
|
|
176
|
+
.join('');
|
|
177
|
+
if (content) {
|
|
178
|
+
return Promise.all([content].map(item => {
|
|
179
|
+
client.sendGroupMessage({
|
|
180
|
+
group_id: event.ChannelId,
|
|
181
|
+
message: [
|
|
182
|
+
{
|
|
183
|
+
type: 'text',
|
|
184
|
+
data: {
|
|
185
|
+
text: item
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
]
|
|
189
|
+
});
|
|
190
|
+
return createResult(ResultCode.Ok, 'client.groupOpenMessages', {
|
|
191
|
+
id: null
|
|
192
|
+
});
|
|
193
|
+
}));
|
|
194
|
+
}
|
|
195
|
+
const images = val.filter(item => item.type == 'Image' || item.type == 'ImageFile' || item.type == 'ImageURL');
|
|
196
|
+
if (images) {
|
|
197
|
+
return Promise.all(images.map(async (item) => {
|
|
198
|
+
let data = null;
|
|
199
|
+
if (item.type === 'ImageFile') {
|
|
200
|
+
const db = readFileSync(item.value);
|
|
201
|
+
data = db;
|
|
202
|
+
}
|
|
203
|
+
else if (item.type === 'ImageURL') {
|
|
204
|
+
const db = await ImageURLToBuffer(item.value);
|
|
205
|
+
data = db;
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
data = item.value;
|
|
209
|
+
}
|
|
210
|
+
client.sendGroupMessage({
|
|
211
|
+
group_id: event.ChannelId,
|
|
212
|
+
message: [
|
|
213
|
+
{
|
|
214
|
+
type: 'image',
|
|
215
|
+
data: {
|
|
216
|
+
file: `base64://${data.toString('base64')}`
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
]
|
|
220
|
+
});
|
|
221
|
+
return createResult(ResultCode.Ok, 'client.groupOpenMessages', {
|
|
222
|
+
id: null
|
|
223
|
+
});
|
|
224
|
+
}));
|
|
225
|
+
}
|
|
226
|
+
return Promise.all([]);
|
|
227
|
+
};
|
|
228
|
+
/**
|
|
229
|
+
*
|
|
230
|
+
* @param event
|
|
231
|
+
* @param val
|
|
232
|
+
* @returns
|
|
233
|
+
*/
|
|
234
|
+
const sendPrivate = async (event, val) => {
|
|
235
|
+
if (val.length < 0)
|
|
236
|
+
return Promise.all([]);
|
|
237
|
+
const content = val
|
|
238
|
+
.filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
|
|
239
|
+
.map(item => item.value)
|
|
240
|
+
.join('');
|
|
241
|
+
if (content) {
|
|
242
|
+
return Promise.all([content].map(item => {
|
|
243
|
+
client.sendGroupMessage({
|
|
244
|
+
group_id: event.ChannelId,
|
|
245
|
+
message: [
|
|
246
|
+
{
|
|
247
|
+
type: 'text',
|
|
248
|
+
data: {
|
|
249
|
+
text: item
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
]
|
|
253
|
+
});
|
|
254
|
+
return createResult(ResultCode.Ok, 'client.groupOpenMessages', {
|
|
255
|
+
id: null
|
|
256
|
+
});
|
|
257
|
+
})).catch(err => [
|
|
258
|
+
createResult(ResultCode.Fail, err?.response?.data ?? err?.message ?? err, null)
|
|
259
|
+
]);
|
|
260
|
+
}
|
|
261
|
+
const images = val.filter(item => item.type == 'Image' || item.type == 'ImageFile' || item.type == 'ImageURL');
|
|
262
|
+
if (images) {
|
|
263
|
+
return Promise.all(images.map(async (item) => {
|
|
264
|
+
let data = null;
|
|
265
|
+
if (item.type === 'ImageFile') {
|
|
266
|
+
const db = readFileSync(item.value);
|
|
267
|
+
data = db;
|
|
268
|
+
}
|
|
269
|
+
else if (item.type === 'ImageURL') {
|
|
270
|
+
const db = await ImageURLToBuffer(item.value);
|
|
271
|
+
data = db;
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
data = item.value;
|
|
275
|
+
}
|
|
276
|
+
client.sendGroupMessage({
|
|
277
|
+
group_id: event.ChannelId,
|
|
278
|
+
message: [
|
|
279
|
+
{
|
|
280
|
+
type: 'image',
|
|
281
|
+
data: {
|
|
282
|
+
file: `base64://${data.toString('base64')}`
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
]
|
|
286
|
+
});
|
|
287
|
+
return createResult(ResultCode.Ok, 'client.groupOpenMessages', {
|
|
288
|
+
id: null
|
|
289
|
+
});
|
|
290
|
+
})).catch(err => [
|
|
291
|
+
createResult(ResultCode.Fail, err?.response?.data ?? err?.message ?? err, null)
|
|
292
|
+
]);
|
|
293
|
+
}
|
|
294
|
+
return Promise.all([]);
|
|
295
|
+
};
|
|
128
296
|
return {
|
|
297
|
+
platform,
|
|
129
298
|
api: {
|
|
299
|
+
active: {
|
|
300
|
+
send: {
|
|
301
|
+
channel: (event, val) => {
|
|
302
|
+
return sendGroup(event, val);
|
|
303
|
+
},
|
|
304
|
+
user: (event, val) => {
|
|
305
|
+
return sendPrivate(event, val);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
},
|
|
130
309
|
use: {
|
|
131
310
|
send: (event, val) => {
|
|
132
311
|
if (val.length < 0)
|
|
133
312
|
return Promise.all([]);
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
.map(item => item.value)
|
|
137
|
-
.join('');
|
|
138
|
-
if (content) {
|
|
139
|
-
return Promise.all([content].map(item => client.sendGroupMessage({
|
|
140
|
-
group_id: event.ChannelId,
|
|
141
|
-
message: [
|
|
142
|
-
{
|
|
143
|
-
type: 'text',
|
|
144
|
-
data: {
|
|
145
|
-
text: item
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
]
|
|
149
|
-
})));
|
|
313
|
+
if (event['name'] == 'private.message.create') {
|
|
314
|
+
return sendPrivate(event, val);
|
|
150
315
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
return Promise.all(images.map(item => client.sendGroupMessage({
|
|
154
|
-
group_id: event.ChannelId,
|
|
155
|
-
message: [
|
|
156
|
-
{
|
|
157
|
-
type: 'image',
|
|
158
|
-
data: {
|
|
159
|
-
file: `base64://${item.toString('base64')}`
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
]
|
|
163
|
-
})));
|
|
316
|
+
else if (event['name'] == 'message.create') {
|
|
317
|
+
return sendGroup(event, val);
|
|
164
318
|
}
|
|
165
319
|
return Promise.all([]);
|
|
166
320
|
},
|
package/lib/sdk/wss.d.ts
CHANGED
|
@@ -4,6 +4,10 @@ type OneBotEventMap = {
|
|
|
4
4
|
DIRECT_MESSAGE: DIRECT_MESSAGE_TYPE;
|
|
5
5
|
MESSAGES: MESSAGES_TYPE;
|
|
6
6
|
META: meta_event_lifecycle | meta_event_heartbeat;
|
|
7
|
+
REQUEST_ADD_FRIEND: any;
|
|
8
|
+
REQUEST_ADD_GROUP: any;
|
|
9
|
+
NOTICE_GROUP_MEMBER_INCREASE: any;
|
|
10
|
+
NOTICE_GROUP_MEMBER_REDUCE: any;
|
|
7
11
|
ERROR: any;
|
|
8
12
|
};
|
|
9
13
|
/**
|
|
@@ -35,7 +39,16 @@ declare class OneBotClient {
|
|
|
35
39
|
*/
|
|
36
40
|
connect(): Promise<void>;
|
|
37
41
|
/**
|
|
38
|
-
*
|
|
42
|
+
* 发送私聊消息
|
|
43
|
+
* @param options
|
|
44
|
+
* @returns
|
|
45
|
+
*/
|
|
46
|
+
sendPrivateMessage(options: {
|
|
47
|
+
user_id: number;
|
|
48
|
+
message: any[];
|
|
49
|
+
}): void;
|
|
50
|
+
/**
|
|
51
|
+
* 发送群消息
|
|
39
52
|
* @param options
|
|
40
53
|
* @returns
|
|
41
54
|
*/
|
|
@@ -44,13 +57,53 @@ declare class OneBotClient {
|
|
|
44
57
|
message: any[];
|
|
45
58
|
}): void;
|
|
46
59
|
/**
|
|
60
|
+
* 发送消息
|
|
47
61
|
* @param options
|
|
48
62
|
* @returns
|
|
49
63
|
*/
|
|
50
|
-
|
|
51
|
-
|
|
64
|
+
sendMessage(options: {
|
|
65
|
+
message_type: 'private' | 'group';
|
|
66
|
+
group_id?: number;
|
|
67
|
+
user_id?: number;
|
|
52
68
|
message: any[];
|
|
53
69
|
}): void;
|
|
70
|
+
/**
|
|
71
|
+
* 好友列表
|
|
72
|
+
*/
|
|
73
|
+
getFriendList(): void;
|
|
74
|
+
/**
|
|
75
|
+
* 群列表
|
|
76
|
+
*/
|
|
77
|
+
getGroupList(): void;
|
|
78
|
+
/**
|
|
79
|
+
* 群成员列表
|
|
80
|
+
* @param options
|
|
81
|
+
* @returns
|
|
82
|
+
*/
|
|
83
|
+
getGroupMemberList(options: {
|
|
84
|
+
group_id: number;
|
|
85
|
+
}): void;
|
|
86
|
+
/**
|
|
87
|
+
* 处理好友请求
|
|
88
|
+
* @param options
|
|
89
|
+
* @returns
|
|
90
|
+
*/
|
|
91
|
+
setFriendAddRequest(options: {
|
|
92
|
+
flag: string;
|
|
93
|
+
approve: boolean;
|
|
94
|
+
remark?: string;
|
|
95
|
+
}): void;
|
|
96
|
+
/**
|
|
97
|
+
* 处理群请求
|
|
98
|
+
* @param options
|
|
99
|
+
* @returns
|
|
100
|
+
*/
|
|
101
|
+
setGroupAddRequest(options: {
|
|
102
|
+
flag: string;
|
|
103
|
+
sub_type: string;
|
|
104
|
+
approve: boolean;
|
|
105
|
+
reason?: string;
|
|
106
|
+
}): void;
|
|
54
107
|
/**
|
|
55
108
|
* @param options
|
|
56
109
|
* @returns
|
|
@@ -61,7 +114,7 @@ declare class OneBotClient {
|
|
|
61
114
|
[key: string]: any;
|
|
62
115
|
};
|
|
63
116
|
echo?: string;
|
|
64
|
-
}): Promise<unknown
|
|
117
|
+
}): Promise<unknown>;
|
|
65
118
|
}
|
|
66
119
|
|
|
67
120
|
export { OneBotClient };
|
package/lib/sdk/wss.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import WebSocket, { WebSocketServer } from 'ws';
|
|
2
2
|
import { randomUUID } from 'crypto';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -71,21 +71,38 @@ class OneBotClient {
|
|
|
71
71
|
if (this.#events['DIRECT_MESSAGE'])
|
|
72
72
|
this.#events['DIRECT_MESSAGE'](event);
|
|
73
73
|
}
|
|
74
|
-
else {
|
|
75
|
-
console.info('未知消息类型', event);
|
|
76
|
-
}
|
|
77
74
|
return;
|
|
78
75
|
}
|
|
79
76
|
else if (event?.post_type == 'notice') {
|
|
80
|
-
|
|
77
|
+
if (event?.notice_type == 'group_increase') {
|
|
78
|
+
// 群成员增加
|
|
79
|
+
if (this.#events['NOTICE_GROUP_MEMBER_INCREASE'])
|
|
80
|
+
this.#events['NOTICE_GROUP_MEMBER_INCREASE'](event);
|
|
81
|
+
}
|
|
82
|
+
else if (event?.notice_type == 'group_decrease') {
|
|
83
|
+
// 群成员减少
|
|
84
|
+
if (this.#events['NOTICE_GROUP_MEMBER_REDUCE'])
|
|
85
|
+
this.#events['NOTICE_GROUP_MEMBER_REDUCE'](event);
|
|
86
|
+
}
|
|
81
87
|
return;
|
|
82
88
|
}
|
|
83
89
|
else if (event?.post_type == 'request') {
|
|
84
|
-
|
|
90
|
+
// 收到加群 或 加好友的请求。
|
|
91
|
+
if (event?.request_type == 'friend') {
|
|
92
|
+
if (this.#events['REQUEST_ADD_FRIEND'])
|
|
93
|
+
this.#events['REQUEST_ADD_FRIEND'](event);
|
|
94
|
+
}
|
|
95
|
+
else if (event?.request_type == 'group') {
|
|
96
|
+
if (this.#events['REQUEST_ADD_GROUP'])
|
|
97
|
+
this.#events['REQUEST_ADD_GROUP'](event);
|
|
98
|
+
}
|
|
85
99
|
return;
|
|
86
100
|
}
|
|
87
|
-
else {
|
|
88
|
-
|
|
101
|
+
else if (event?.echo === 'get_friend_list' || event?.echo === 'get_group_list' || event?.echo === 'get_group_member_list') {
|
|
102
|
+
// 处理获取好友列表和群列表的响应
|
|
103
|
+
// if (this.#events['META']) this.#events['META'](event)
|
|
104
|
+
// console.debug('响应', event)
|
|
105
|
+
return;
|
|
89
106
|
}
|
|
90
107
|
if (!event?.post_type && event?.echo && this.#echo[event?.echo]) {
|
|
91
108
|
if (![0, 1].includes(event?.retcode))
|
|
@@ -115,7 +132,7 @@ class OneBotClient {
|
|
|
115
132
|
if (!this.#ws) {
|
|
116
133
|
if (reverse_enable) {
|
|
117
134
|
// reverse_open
|
|
118
|
-
const server = new
|
|
135
|
+
const server = new WebSocketServer({ port: reverse_port ?? 17158 });
|
|
119
136
|
server.on('connection', ws => {
|
|
120
137
|
this.#ws = ws;
|
|
121
138
|
// message
|
|
@@ -127,7 +144,7 @@ class OneBotClient {
|
|
|
127
144
|
}
|
|
128
145
|
else {
|
|
129
146
|
// forward_open
|
|
130
|
-
this.#ws = new
|
|
147
|
+
this.#ws = new WebSocket(url, c);
|
|
131
148
|
this.#ws.on('open', () => {
|
|
132
149
|
console.debug(`open:${url}`);
|
|
133
150
|
});
|
|
@@ -139,7 +156,21 @@ class OneBotClient {
|
|
|
139
156
|
}
|
|
140
157
|
}
|
|
141
158
|
/**
|
|
142
|
-
*
|
|
159
|
+
* 发送私聊消息
|
|
160
|
+
* @param options
|
|
161
|
+
* @returns
|
|
162
|
+
*/
|
|
163
|
+
sendPrivateMessage(options) {
|
|
164
|
+
if (!this.#ws)
|
|
165
|
+
return;
|
|
166
|
+
return this.#ws.send(JSON.stringify({
|
|
167
|
+
action: 'send_private_msg',
|
|
168
|
+
params: options,
|
|
169
|
+
echo: randomUUID()
|
|
170
|
+
}));
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* 发送群消息
|
|
143
174
|
* @param options
|
|
144
175
|
* @returns
|
|
145
176
|
*/
|
|
@@ -153,14 +184,81 @@ class OneBotClient {
|
|
|
153
184
|
}));
|
|
154
185
|
}
|
|
155
186
|
/**
|
|
187
|
+
* 发送消息
|
|
156
188
|
* @param options
|
|
157
189
|
* @returns
|
|
158
190
|
*/
|
|
159
|
-
|
|
191
|
+
sendMessage(options) {
|
|
160
192
|
if (!this.#ws)
|
|
161
193
|
return;
|
|
162
194
|
return this.#ws.send(JSON.stringify({
|
|
163
|
-
action: '
|
|
195
|
+
action: 'send_msg',
|
|
196
|
+
params: options,
|
|
197
|
+
echo: randomUUID()
|
|
198
|
+
}));
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* 好友列表
|
|
202
|
+
*/
|
|
203
|
+
getFriendList() {
|
|
204
|
+
if (!this.#ws)
|
|
205
|
+
return;
|
|
206
|
+
return this.#ws.send(JSON.stringify({
|
|
207
|
+
action: 'get_friend_list',
|
|
208
|
+
params: {},
|
|
209
|
+
echo: 'get_friend_list'
|
|
210
|
+
}));
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* 群列表
|
|
214
|
+
*/
|
|
215
|
+
getGroupList() {
|
|
216
|
+
if (!this.#ws)
|
|
217
|
+
return;
|
|
218
|
+
return this.#ws.send(JSON.stringify({
|
|
219
|
+
action: 'get_group_list',
|
|
220
|
+
params: {},
|
|
221
|
+
echo: 'get_group_list'
|
|
222
|
+
}));
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* 群成员列表
|
|
226
|
+
* @param options
|
|
227
|
+
* @returns
|
|
228
|
+
*/
|
|
229
|
+
getGroupMemberList(options) {
|
|
230
|
+
if (!this.#ws)
|
|
231
|
+
return;
|
|
232
|
+
return this.#ws.send(JSON.stringify({
|
|
233
|
+
action: 'get_group_member_list',
|
|
234
|
+
params: options,
|
|
235
|
+
echo: 'get_group_member_list'
|
|
236
|
+
}));
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* 处理好友请求
|
|
240
|
+
* @param options
|
|
241
|
+
* @returns
|
|
242
|
+
*/
|
|
243
|
+
setFriendAddRequest(options) {
|
|
244
|
+
if (!this.#ws)
|
|
245
|
+
return;
|
|
246
|
+
return this.#ws.send(JSON.stringify({
|
|
247
|
+
action: 'set_friend_add_request',
|
|
248
|
+
params: options,
|
|
249
|
+
echo: randomUUID()
|
|
250
|
+
}));
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* 处理群请求
|
|
254
|
+
* @param options
|
|
255
|
+
* @returns
|
|
256
|
+
*/
|
|
257
|
+
setGroupAddRequest(options) {
|
|
258
|
+
if (!this.#ws)
|
|
259
|
+
return;
|
|
260
|
+
return this.#ws.send(JSON.stringify({
|
|
261
|
+
action: 'set_group_add_request',
|
|
164
262
|
params: options,
|
|
165
263
|
echo: randomUUID()
|
|
166
264
|
}));
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import Koa from 'koa';
|
|
2
|
+
import Router from 'koa-router';
|
|
3
|
+
|
|
4
|
+
// 使用koa + router 写 hello world
|
|
5
|
+
const createServer = (client) => {
|
|
6
|
+
const app = new Koa();
|
|
7
|
+
const router = new Router();
|
|
8
|
+
const port = 8080; // 端口号
|
|
9
|
+
// 检测
|
|
10
|
+
router.get('/', (ctx) => {
|
|
11
|
+
ctx.body = 'Hello, World!';
|
|
12
|
+
});
|
|
13
|
+
// 获取群列表
|
|
14
|
+
router.get('/get_group_list', async (ctx) => {
|
|
15
|
+
client.getGroupList();
|
|
16
|
+
ctx.body = 'Hello, World!';
|
|
17
|
+
});
|
|
18
|
+
// 获取好友列表
|
|
19
|
+
router.get('/get_friend_list', async (ctx) => {
|
|
20
|
+
client.getFriendList();
|
|
21
|
+
ctx.body = 'Hello, World!';
|
|
22
|
+
});
|
|
23
|
+
// 获取群成员列表
|
|
24
|
+
router.get('/get_group_member_list', async (ctx) => {
|
|
25
|
+
const group_id = ctx.query.group_id;
|
|
26
|
+
if (!group_id) {
|
|
27
|
+
ctx.status = 400;
|
|
28
|
+
ctx.body = 'group_id is required';
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
client.getGroupMemberList({ group_id: Number(group_id) });
|
|
32
|
+
ctx.body = 'Hello, World!';
|
|
33
|
+
});
|
|
34
|
+
// 同意加群
|
|
35
|
+
router.get('/set_group_add_request', async (ctx) => {
|
|
36
|
+
const flag = ctx.query.flag;
|
|
37
|
+
const sub_type = ctx.query.sub_type;
|
|
38
|
+
const approve = ctx.query.approve;
|
|
39
|
+
if (!flag || !sub_type || !approve) {
|
|
40
|
+
ctx.status = 400;
|
|
41
|
+
ctx.body = 'flag, sub_type and approve are required';
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
client.setGroupAddRequest({
|
|
45
|
+
flag: String(flag),
|
|
46
|
+
sub_type: String(sub_type),
|
|
47
|
+
approve: Boolean(approve),
|
|
48
|
+
});
|
|
49
|
+
ctx.body = 'Hello, World!';
|
|
50
|
+
});
|
|
51
|
+
// 同意加好友
|
|
52
|
+
router.get('/set_friend_add_request', async (ctx) => {
|
|
53
|
+
const flag = ctx.query.flag;
|
|
54
|
+
const approve = ctx.query.approve;
|
|
55
|
+
if (!flag || !approve) {
|
|
56
|
+
ctx.status = 400;
|
|
57
|
+
ctx.body = 'flag and approve are required';
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
client.setFriendAddRequest({
|
|
61
|
+
flag: String(flag),
|
|
62
|
+
approve: Boolean(approve),
|
|
63
|
+
});
|
|
64
|
+
ctx.body = 'Hello, World!';
|
|
65
|
+
});
|
|
66
|
+
app.use(router.routes());
|
|
67
|
+
app.use(router.allowedMethods());
|
|
68
|
+
app.listen(port, () => {
|
|
69
|
+
console.log(`Server is running at http://localhost:${port}`);
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export { createServer };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alemonjs/onebot",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"description": "onebot",
|
|
5
5
|
"author": "lemonade",
|
|
6
6
|
"license": "MIT",
|
|
@@ -17,16 +17,22 @@
|
|
|
17
17
|
},
|
|
18
18
|
"./package": "./package.json"
|
|
19
19
|
},
|
|
20
|
-
"devDependencies": {
|
|
21
|
-
"@rollup/plugin-typescript": "^11.1.6",
|
|
22
|
-
"lvyjs": "^0.2.13",
|
|
23
|
-
"rollup": "^4.18.1",
|
|
24
|
-
"rollup-plugin-dts": "^6.1.1",
|
|
25
|
-
"tsx": "^4.19.1"
|
|
26
|
-
},
|
|
27
20
|
"keywords": [
|
|
28
21
|
"alemonjs"
|
|
29
22
|
],
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"ws": "^8.18.1"
|
|
25
|
+
},
|
|
26
|
+
"alemonjs": {
|
|
27
|
+
"desktop": {
|
|
28
|
+
"platform": [
|
|
29
|
+
{
|
|
30
|
+
"name": "onebot"
|
|
31
|
+
}
|
|
32
|
+
],
|
|
33
|
+
"logo": "antd.RobotOutlined"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
30
36
|
"publishConfig": {
|
|
31
37
|
"registry": "https://registry.npmjs.org",
|
|
32
38
|
"access": "public"
|