@alemonjs/onebot 2.1.0-alpha.1 → 2.1.0-alpha.3
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 +6 -0
- package/lib/config.js +13 -2
- package/lib/hook.d.ts +38 -0
- package/lib/hook.js +24 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +32 -39
- package/lib/sdk/types.d.ts +72 -0
- package/package.json +1 -1
package/README.md
CHANGED
package/lib/config.js
CHANGED
|
@@ -1,9 +1,20 @@
|
|
|
1
|
-
import { getConfigValue } from 'alemonjs';
|
|
1
|
+
import { getConfigValue, useUserHashKey } from 'alemonjs';
|
|
2
2
|
|
|
3
3
|
const platform = 'onebot';
|
|
4
4
|
const getOneBotConfig = () => {
|
|
5
5
|
const value = getConfigValue() || {};
|
|
6
6
|
return value[platform] || {};
|
|
7
7
|
};
|
|
8
|
+
const getMaster = (UserId) => {
|
|
9
|
+
const config = getOneBotConfig();
|
|
10
|
+
const master_key = config.master_key || [];
|
|
11
|
+
const master_id = config.master_id || [];
|
|
12
|
+
const UserKey = useUserHashKey({
|
|
13
|
+
Platform: platform,
|
|
14
|
+
UserId: UserId
|
|
15
|
+
});
|
|
16
|
+
const is = master_key.includes(UserKey) || master_id.includes(UserId);
|
|
17
|
+
return [is, UserKey];
|
|
18
|
+
};
|
|
8
19
|
|
|
9
|
-
export { getOneBotConfig, platform };
|
|
20
|
+
export { getMaster, getOneBotConfig, platform };
|
package/lib/hook.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
|
|
5
|
+
type MAP = {
|
|
6
|
+
'message.create': MESSAGES_TYPE;
|
|
7
|
+
'private.message.create': DIRECT_MESSAGE_TYPE;
|
|
8
|
+
'interaction.create': undefined;
|
|
9
|
+
'private.interaction.create': undefined;
|
|
10
|
+
'message.update': undefined;
|
|
11
|
+
'message.delete': undefined;
|
|
12
|
+
'message.reaction.add': undefined;
|
|
13
|
+
'message.reaction.remove': undefined;
|
|
14
|
+
'channal.create': undefined;
|
|
15
|
+
'channal.delete': undefined;
|
|
16
|
+
'guild.join': undefined;
|
|
17
|
+
'guild.exit': undefined;
|
|
18
|
+
'member.add': undefined;
|
|
19
|
+
'member.remove': undefined;
|
|
20
|
+
'private.message.update': undefined;
|
|
21
|
+
'private.message.delete': undefined;
|
|
22
|
+
'private.friend.add': undefined;
|
|
23
|
+
'private.guild.add': undefined;
|
|
24
|
+
};
|
|
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 };
|
package/lib/hook.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { createEventValue, useClient as useClient$1 } from 'alemonjs';
|
|
2
|
+
import { OneBotAPI } from './sdk/api.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* @param event
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
const useValue = (event) => {
|
|
10
|
+
const value = createEventValue(event);
|
|
11
|
+
return [value];
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
*
|
|
15
|
+
* @param event
|
|
16
|
+
* @returns
|
|
17
|
+
*/
|
|
18
|
+
const useClient = (event) => {
|
|
19
|
+
const [client] = useClient$1(event, OneBotAPI);
|
|
20
|
+
const value = createEventValue(event);
|
|
21
|
+
return [client, value];
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export { useClient, useValue };
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { cbpPlatform,
|
|
1
|
+
import { cbpPlatform, createResult, ResultCode } from 'alemonjs';
|
|
2
2
|
import { getBufferByURL } from 'alemonjs/utils';
|
|
3
3
|
import { OneBotClient } from './sdk/wss.js';
|
|
4
4
|
import { readFileSync } from 'fs';
|
|
5
|
-
import { getOneBotConfig, platform } from './config.js';
|
|
5
|
+
import { getOneBotConfig, getMaster, platform } from './config.js';
|
|
6
6
|
export { OneBotAPI as API } from './sdk/api.js';
|
|
7
|
+
export { useClient, useValue } from './hook.js';
|
|
7
8
|
|
|
8
9
|
const MyBot = {
|
|
9
10
|
id: ''};
|
|
@@ -40,14 +41,10 @@ var index = () => {
|
|
|
40
41
|
}
|
|
41
42
|
});
|
|
42
43
|
client.on('MESSAGES', event => {
|
|
43
|
-
const uis = config?.master_id ?? [];
|
|
44
44
|
const msg = getMessageText(event.message);
|
|
45
45
|
const UserId = String(event.user_id);
|
|
46
46
|
const UserAvatar = createUserAvatar(UserId);
|
|
47
|
-
const UserKey =
|
|
48
|
-
Platform: platform,
|
|
49
|
-
UserId
|
|
50
|
-
});
|
|
47
|
+
const [isMaster, UserKey] = getMaster(UserId);
|
|
51
48
|
const groupId = String(event.group_id);
|
|
52
49
|
// 定义消
|
|
53
50
|
const e = {
|
|
@@ -55,7 +52,7 @@ var index = () => {
|
|
|
55
52
|
Platform: platform,
|
|
56
53
|
GuildId: groupId,
|
|
57
54
|
ChannelId: groupId,
|
|
58
|
-
IsMaster:
|
|
55
|
+
IsMaster: isMaster,
|
|
59
56
|
SpaceId: groupId,
|
|
60
57
|
IsBot: false,
|
|
61
58
|
UserId: UserId,
|
|
@@ -72,19 +69,15 @@ var index = () => {
|
|
|
72
69
|
cbp.send(e);
|
|
73
70
|
});
|
|
74
71
|
client.on('DIRECT_MESSAGE', event => {
|
|
75
|
-
const uis = config?.master_id ?? [];
|
|
76
72
|
const msg = getMessageText(event.message);
|
|
77
73
|
const UserId = String(event.user_id);
|
|
78
74
|
const UserAvatar = createUserAvatar(UserId);
|
|
79
|
-
const UserKey =
|
|
80
|
-
Platform: platform,
|
|
81
|
-
UserId
|
|
82
|
-
});
|
|
75
|
+
const [isMaster, UserKey] = getMaster(UserId);
|
|
83
76
|
// 定义消
|
|
84
77
|
const e = {
|
|
85
78
|
name: 'private.message.create',
|
|
86
79
|
Platform: platform,
|
|
87
|
-
IsMaster:
|
|
80
|
+
IsMaster: isMaster,
|
|
88
81
|
IsBot: false,
|
|
89
82
|
UserId: UserId,
|
|
90
83
|
UserName: event.sender.nickname,
|
|
@@ -103,7 +96,7 @@ var index = () => {
|
|
|
103
96
|
client.on('ERROR', event => {
|
|
104
97
|
logger.error(event);
|
|
105
98
|
});
|
|
106
|
-
const sendGroup = async (
|
|
99
|
+
const sendGroup = async (ChannelId, val) => {
|
|
107
100
|
if (val.length < 0)
|
|
108
101
|
return [];
|
|
109
102
|
const content = val
|
|
@@ -113,7 +106,7 @@ var index = () => {
|
|
|
113
106
|
try {
|
|
114
107
|
if (content) {
|
|
115
108
|
const res = await client.sendGroupMessage({
|
|
116
|
-
group_id:
|
|
109
|
+
group_id: ChannelId,
|
|
117
110
|
message: [
|
|
118
111
|
{
|
|
119
112
|
type: 'text',
|
|
@@ -142,7 +135,7 @@ var index = () => {
|
|
|
142
135
|
data = Buffer.from(item.value, 'base64');
|
|
143
136
|
}
|
|
144
137
|
client.sendGroupMessage({
|
|
145
|
-
group_id:
|
|
138
|
+
group_id: ChannelId,
|
|
146
139
|
message: [
|
|
147
140
|
{
|
|
148
141
|
type: 'image',
|
|
@@ -165,11 +158,11 @@ var index = () => {
|
|
|
165
158
|
};
|
|
166
159
|
/**
|
|
167
160
|
*
|
|
168
|
-
* @param
|
|
161
|
+
* @param UserId
|
|
169
162
|
* @param val
|
|
170
163
|
* @returns
|
|
171
164
|
*/
|
|
172
|
-
const sendPrivate = async (
|
|
165
|
+
const sendPrivate = async (UserId, val) => {
|
|
173
166
|
if (val.length < 0)
|
|
174
167
|
return Promise.all([]);
|
|
175
168
|
const content = val
|
|
@@ -179,7 +172,7 @@ var index = () => {
|
|
|
179
172
|
try {
|
|
180
173
|
if (content) {
|
|
181
174
|
const res = await client.sendPrivateMessage({
|
|
182
|
-
user_id:
|
|
175
|
+
user_id: UserId,
|
|
183
176
|
message: [
|
|
184
177
|
{
|
|
185
178
|
type: 'text',
|
|
@@ -208,7 +201,7 @@ var index = () => {
|
|
|
208
201
|
data = Buffer.from(item.value, 'base64');
|
|
209
202
|
}
|
|
210
203
|
client.sendPrivateMessage({
|
|
211
|
-
user_id:
|
|
204
|
+
user_id: UserId,
|
|
212
205
|
message: [
|
|
213
206
|
{
|
|
214
207
|
type: 'image',
|
|
@@ -232,11 +225,11 @@ var index = () => {
|
|
|
232
225
|
const api = {
|
|
233
226
|
active: {
|
|
234
227
|
send: {
|
|
235
|
-
channel: (
|
|
236
|
-
return sendGroup(
|
|
228
|
+
channel: async (SpaceId, val) => {
|
|
229
|
+
return sendGroup(Number(SpaceId), val);
|
|
237
230
|
},
|
|
238
|
-
user: (
|
|
239
|
-
return sendPrivate(
|
|
231
|
+
user: async (OpenId, val) => {
|
|
232
|
+
return sendPrivate(Number(OpenId), val);
|
|
240
233
|
}
|
|
241
234
|
}
|
|
242
235
|
},
|
|
@@ -245,38 +238,38 @@ var index = () => {
|
|
|
245
238
|
if (val.length < 0)
|
|
246
239
|
return Promise.all([]);
|
|
247
240
|
if (event['name'] == 'private.message.create') {
|
|
248
|
-
|
|
241
|
+
const UserId = Number(event.UserId);
|
|
242
|
+
return sendPrivate(UserId, val);
|
|
249
243
|
}
|
|
250
244
|
else if (event['name'] == 'message.create') {
|
|
251
|
-
|
|
245
|
+
const GroupId = Number(event.ChannelId);
|
|
246
|
+
return sendGroup(GroupId, val);
|
|
252
247
|
}
|
|
253
248
|
return Promise.all([]);
|
|
254
249
|
},
|
|
255
250
|
mention: async (event) => {
|
|
256
|
-
const uis = config?.master_id ?? [];
|
|
257
251
|
const e = event.value;
|
|
258
|
-
|
|
252
|
+
const names = ['message.create', 'private.message.create'];
|
|
253
|
+
if (names.includes(event.name)) {
|
|
259
254
|
const Metions = [];
|
|
260
255
|
for (const item of e.message) {
|
|
261
256
|
if (item.type == 'at') {
|
|
262
257
|
let isBot = false;
|
|
263
|
-
const
|
|
264
|
-
if (
|
|
258
|
+
const UserId = String(item.data.qq);
|
|
259
|
+
if (UserId == 'all') {
|
|
265
260
|
continue;
|
|
266
261
|
}
|
|
267
|
-
if (
|
|
262
|
+
if (UserId == MyBot.id) {
|
|
268
263
|
isBot = true;
|
|
269
264
|
}
|
|
270
|
-
const
|
|
265
|
+
const [isMaster, UserKey] = getMaster(UserId);
|
|
266
|
+
const avatar = createUserAvatar(UserId);
|
|
271
267
|
Metions.push({
|
|
272
|
-
UserId:
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
UserId: uid
|
|
276
|
-
}),
|
|
268
|
+
UserId: UserId,
|
|
269
|
+
IsMaster: isMaster,
|
|
270
|
+
UserKey: UserKey,
|
|
277
271
|
UserName: item.data?.nickname,
|
|
278
272
|
UserAvatar: avatar,
|
|
279
|
-
IsMaster: uis.includes(uid),
|
|
280
273
|
IsBot: isBot
|
|
281
274
|
});
|
|
282
275
|
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
interface Sender {
|
|
2
|
+
user_id: number;
|
|
3
|
+
nickname: string;
|
|
4
|
+
card: string;
|
|
5
|
+
role: 'admin' | 'member';
|
|
6
|
+
}
|
|
7
|
+
type Message = {
|
|
8
|
+
type: 'image';
|
|
9
|
+
data: {
|
|
10
|
+
file: string;
|
|
11
|
+
sub_type: number;
|
|
12
|
+
file_id: string;
|
|
13
|
+
url: string;
|
|
14
|
+
file_size: string;
|
|
15
|
+
file_unique: string;
|
|
16
|
+
};
|
|
17
|
+
} | {
|
|
18
|
+
type: 'text';
|
|
19
|
+
data: {
|
|
20
|
+
text: string;
|
|
21
|
+
};
|
|
22
|
+
} | {
|
|
23
|
+
type: 'json';
|
|
24
|
+
data: string;
|
|
25
|
+
} | {
|
|
26
|
+
type: 'forward';
|
|
27
|
+
data: {
|
|
28
|
+
id: number;
|
|
29
|
+
content: any[];
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
interface MESSAGES_TYPE {
|
|
33
|
+
self_id: number;
|
|
34
|
+
user_id: number;
|
|
35
|
+
time: number;
|
|
36
|
+
message_id: number;
|
|
37
|
+
message_seq: number;
|
|
38
|
+
real_id: number;
|
|
39
|
+
message_type: 'group';
|
|
40
|
+
sender: Sender;
|
|
41
|
+
raw_message: string;
|
|
42
|
+
font: number;
|
|
43
|
+
sub_type: 'normal';
|
|
44
|
+
message: Message[];
|
|
45
|
+
message_format: 'array';
|
|
46
|
+
post_type: 'message';
|
|
47
|
+
group_id: number;
|
|
48
|
+
}
|
|
49
|
+
interface DIRECT_MESSAGE_TYPE {
|
|
50
|
+
self_id: number;
|
|
51
|
+
user_id: number;
|
|
52
|
+
time: number;
|
|
53
|
+
message_id: number;
|
|
54
|
+
message_seq: number;
|
|
55
|
+
real_id: number;
|
|
56
|
+
message_type: 'private';
|
|
57
|
+
sender: {
|
|
58
|
+
user_id: 1715713638;
|
|
59
|
+
nickname: string;
|
|
60
|
+
card: '';
|
|
61
|
+
};
|
|
62
|
+
raw_message: string;
|
|
63
|
+
font: 14;
|
|
64
|
+
sub_type: 'group' | 'friend';
|
|
65
|
+
message: Message[];
|
|
66
|
+
message_format: 'array';
|
|
67
|
+
post_type: 'message';
|
|
68
|
+
group_id: number;
|
|
69
|
+
temp_source: number;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type { DIRECT_MESSAGE_TYPE, MESSAGES_TYPE };
|