@alemonjs/bubble 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 +8 -8
- package/lib/hook.d.ts +2 -2
- package/lib/index.js +55 -68
- package/lib/sdk/types.d.ts +13 -26
- package/lib/send.d.ts +7 -3
- package/lib/send.js +39 -60
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,30 +18,30 @@ yarn add @alemonjs/bubble
|
|
|
18
18
|
bubble:
|
|
19
19
|
# Bot Token(必填)
|
|
20
20
|
token: 'YOUR_BOT_TOKEN'
|
|
21
|
-
|
|
21
|
+
|
|
22
22
|
# WebSocket Gateway URL(可选,默认: wss://bubble.alemonjs.com/api/bot/gateway)
|
|
23
23
|
GATEWAY_URL: 'wss://bubble.alemonjs.com/api/bot/gateway'
|
|
24
|
-
|
|
24
|
+
|
|
25
25
|
# API Base URL(可选,默认: https://bubble.alemonjs.com/api/bot/v1)
|
|
26
26
|
API_URL: 'https://bubble.alemonjs.com/api/bot/v1'
|
|
27
|
-
|
|
27
|
+
|
|
28
28
|
# CDN Base URL(可选,默认: https://bubble-oss-files.alemonjs.com)
|
|
29
29
|
CDN_URL: 'https://bubble-oss-files.alemonjs.com'
|
|
30
|
-
|
|
30
|
+
|
|
31
31
|
# 订阅的事件类型(可选,默认: 所有可用事件)
|
|
32
32
|
intent:
|
|
33
33
|
- MESSAGE_CREATE
|
|
34
34
|
- DM_MESSAGE_CREATE
|
|
35
35
|
- GUILD_MEMBER_ADD
|
|
36
|
-
|
|
36
|
+
|
|
37
37
|
# 主人配置(可选)
|
|
38
38
|
master_key: []
|
|
39
39
|
master_id: []
|
|
40
|
-
|
|
40
|
+
|
|
41
41
|
# 代理配置(可选)
|
|
42
42
|
websocket_proxy: ''
|
|
43
43
|
request_proxy: ''
|
|
44
|
-
|
|
44
|
+
|
|
45
45
|
# 客户端名称(可选,默认: alemonjs-bot)
|
|
46
46
|
clientName: 'alemonjs-bot'
|
|
47
47
|
```
|
|
@@ -75,4 +75,4 @@ bubble:
|
|
|
75
75
|
- `BOT_READY` - 机器人就绪
|
|
76
76
|
- `EVENTS_SUBSCRIBED` - 事件订阅成功
|
|
77
77
|
- `EVENTS_UNSUBSCRIBED` - 事件取消订阅成功
|
|
78
|
-
- `SUBSCRIBE_DENIED` - 订阅被拒绝
|
|
78
|
+
- `SUBSCRIBE_DENIED` - 订阅被拒绝
|
package/lib/hook.d.ts
CHANGED
|
@@ -9,8 +9,8 @@ type MAP = {
|
|
|
9
9
|
'message.delete': undefined;
|
|
10
10
|
'message.reaction.add': undefined;
|
|
11
11
|
'message.reaction.remove': undefined;
|
|
12
|
-
'
|
|
13
|
-
'
|
|
12
|
+
'channel.create': undefined;
|
|
13
|
+
'channel.delete': undefined;
|
|
14
14
|
'guild.join': undefined;
|
|
15
15
|
'guild.exit': undefined;
|
|
16
16
|
'member.add': undefined;
|
package/lib/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import { cbpPlatform, createResult, ResultCode } from 'alemonjs';
|
|
|
3
3
|
import { getMaster, platform } from './config.js';
|
|
4
4
|
import { CDN_URL } from './sdk/api.js';
|
|
5
5
|
export { BubbleAPI as API, API_URL, GATEWAY_URL } from './sdk/api.js';
|
|
6
|
-
import {
|
|
6
|
+
import { sendToRoom, sendToUser } from './send.js';
|
|
7
7
|
export { OpCode } from './sdk/wss.types.js';
|
|
8
8
|
export { useClient, useValue } from './hook.js';
|
|
9
9
|
|
|
@@ -17,7 +17,7 @@ const main = () => {
|
|
|
17
17
|
return `${CDN_URL}/${avatar}`;
|
|
18
18
|
};
|
|
19
19
|
client.on('MESSAGE_CREATE', event => {
|
|
20
|
-
if (event.author?.
|
|
20
|
+
if (event.author?.is_bot) {
|
|
21
21
|
return;
|
|
22
22
|
}
|
|
23
23
|
const atUsers = [];
|
|
@@ -30,61 +30,61 @@ const main = () => {
|
|
|
30
30
|
for (const item of atUsers) {
|
|
31
31
|
msg = msg.replace(`<@${item.id}>`, '').trim();
|
|
32
32
|
}
|
|
33
|
-
const UserId = String(event.
|
|
33
|
+
const UserId = String(event.authorId);
|
|
34
34
|
const [isMaster, UserKey] = getMaster(UserId);
|
|
35
|
-
const UserAvatar = createUserAvatar(UserId, event
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
35
|
+
const UserAvatar = createUserAvatar(UserId, event?.author?.avatar);
|
|
36
|
+
const e = {
|
|
37
|
+
name: 'message.create',
|
|
38
|
+
Platform: platform,
|
|
39
|
+
GuildId: String(event.channelId || ''),
|
|
40
|
+
ChannelId: String(event.channelId || ''),
|
|
41
|
+
SpaceId: String(event.channelId || ''),
|
|
42
|
+
UserId: UserId,
|
|
43
|
+
UserKey,
|
|
44
|
+
UserName: event?.author?.username,
|
|
45
|
+
UserAvatar: UserAvatar,
|
|
46
|
+
IsMaster: isMaster,
|
|
47
|
+
IsBot: false,
|
|
48
|
+
OpenId: UserId,
|
|
49
|
+
MessageId: String(event.id),
|
|
50
|
+
MessageText: msg,
|
|
51
|
+
CreateAt: Date.now(),
|
|
52
|
+
tag: 'message.create',
|
|
53
|
+
value: event
|
|
54
|
+
};
|
|
55
|
+
cbp.send(e);
|
|
56
|
+
});
|
|
57
|
+
client.on('DM_MESSAGE_CREATE', event => {
|
|
58
|
+
const UserId = String(event.authorId);
|
|
59
|
+
const [isMaster, UserKey] = getMaster(UserId);
|
|
60
|
+
const UserAvatar = createUserAvatar(UserId, event?.author?.avatar);
|
|
61
|
+
const e = {
|
|
62
|
+
name: 'private.message.create',
|
|
63
|
+
Platform: platform,
|
|
64
|
+
UserId: UserId,
|
|
65
|
+
UserKey,
|
|
66
|
+
UserName: event?.author?.username,
|
|
67
|
+
UserAvatar: UserAvatar,
|
|
68
|
+
IsMaster: isMaster,
|
|
69
|
+
IsBot: false,
|
|
70
|
+
OpenId: UserId,
|
|
71
|
+
MessageId: String(event.id),
|
|
72
|
+
MessageText: event.content,
|
|
73
|
+
CreateAt: Date.now(),
|
|
74
|
+
tag: 'private.message.create',
|
|
75
|
+
value: event
|
|
76
|
+
};
|
|
77
|
+
cbp.send(e);
|
|
78
78
|
});
|
|
79
79
|
const api = {
|
|
80
80
|
active: {
|
|
81
81
|
send: {
|
|
82
82
|
channel: async (UserId, val) => {
|
|
83
|
-
const res = await
|
|
83
|
+
const res = await sendToRoom(client, { channel_id: UserId }, val);
|
|
84
84
|
return [createResult(ResultCode.Ok, '请求完成', res)];
|
|
85
85
|
},
|
|
86
86
|
user: async (OpenId, val) => {
|
|
87
|
-
const res = await
|
|
87
|
+
const res = await sendToUser(client, { author_id: OpenId }, val);
|
|
88
88
|
return [createResult(ResultCode.Ok, '请求完成', res)];
|
|
89
89
|
}
|
|
90
90
|
}
|
|
@@ -96,30 +96,17 @@ const main = () => {
|
|
|
96
96
|
}
|
|
97
97
|
const tag = event.tag;
|
|
98
98
|
if (tag === 'message.create') {
|
|
99
|
-
const ChannelId = String(event.value.
|
|
100
|
-
const res = await
|
|
99
|
+
const ChannelId = String(event.value.channelId || '');
|
|
100
|
+
const res = await sendToRoom(client, { channel_id: ChannelId, message_id: String(event.value.id || '') }, val);
|
|
101
101
|
return [createResult(ResultCode.Ok, '请求完成', res)];
|
|
102
102
|
}
|
|
103
103
|
else if (tag === 'private.message.create') {
|
|
104
|
-
const UserId = String(event.value.
|
|
105
|
-
const ChannelId = String(event.value.
|
|
106
|
-
const res = await
|
|
107
|
-
channel_id: ChannelId,
|
|
108
|
-
author_id: UserId
|
|
109
|
-
}, val);
|
|
110
|
-
return [createResult(ResultCode.Ok, '请求完成', res)];
|
|
111
|
-
}
|
|
112
|
-
else if (tag === 'interaction.create') {
|
|
113
|
-
const ChannelId = String(event.value.channel_id || '');
|
|
114
|
-
const res = await sendchannel(client, { channel_id: ChannelId }, val);
|
|
115
|
-
return [createResult(ResultCode.Ok, '请求完成', res)];
|
|
116
|
-
}
|
|
117
|
-
else if (tag === 'private.interaction.create') {
|
|
118
|
-
const UserId = String(event.value.user.id || '');
|
|
119
|
-
const ChannelId = String(event.value.channel_id || '');
|
|
120
|
-
const res = await senduser(client, {
|
|
104
|
+
const UserId = String(event.value.authorId || '');
|
|
105
|
+
const ChannelId = String(event.value.channelId || '');
|
|
106
|
+
const res = await sendToUser(client, {
|
|
121
107
|
channel_id: ChannelId,
|
|
122
|
-
author_id: UserId
|
|
108
|
+
author_id: UserId,
|
|
109
|
+
message_id: String(event.value.id || '')
|
|
123
110
|
}, val);
|
|
124
111
|
return [createResult(ResultCode.Ok, '请求完成', res)];
|
|
125
112
|
}
|
package/lib/sdk/types.d.ts
CHANGED
|
@@ -30,23 +30,23 @@ export interface BaseMessage {
|
|
|
30
30
|
id: string | number;
|
|
31
31
|
content: string;
|
|
32
32
|
type?: string | number;
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
author
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
33
|
+
channelId?: string | number;
|
|
34
|
+
guildId?: string | number;
|
|
35
|
+
authorId?: string | number;
|
|
36
|
+
author: {
|
|
37
|
+
id: number;
|
|
38
|
+
is_bot?: boolean;
|
|
39
|
+
avatar?: string | null;
|
|
40
|
+
username: string;
|
|
41
|
+
};
|
|
42
|
+
attachment?: Attachment[];
|
|
43
|
+
createdAt?: string;
|
|
41
44
|
}
|
|
42
45
|
export interface MessageCreateEvent extends BaseMessage {
|
|
43
|
-
|
|
44
|
-
nickname?: string;
|
|
45
|
-
roles?: number[];
|
|
46
|
-
};
|
|
46
|
+
mentions?: any[];
|
|
47
47
|
}
|
|
48
48
|
export interface DmMessageCreateEvent extends BaseMessage {
|
|
49
|
-
|
|
49
|
+
threadId?: string | number;
|
|
50
50
|
}
|
|
51
51
|
export type MessageUpdateEvent = BaseMessage;
|
|
52
52
|
export interface MessageDeleteEvent {
|
|
@@ -110,18 +110,5 @@ export interface FileQuota {
|
|
|
110
110
|
}
|
|
111
111
|
export interface BubbleEventMap {
|
|
112
112
|
MESSAGE_CREATE: MessageCreateEvent;
|
|
113
|
-
MESSAGE_UPDATE: MessageUpdateEvent;
|
|
114
|
-
MESSAGE_DELETE: MessageDeleteEvent;
|
|
115
|
-
MESSAGE_UNPIN: MessageDeleteEvent;
|
|
116
113
|
DM_MESSAGE_CREATE: DmMessageCreateEvent;
|
|
117
|
-
DM_MESSAGE_UPDATE: DmMessageCreateEvent;
|
|
118
|
-
DM_MESSAGE_DELETE: MessageDeleteEvent;
|
|
119
|
-
DM_MESSAGE_UNPIN: MessageDeleteEvent;
|
|
120
|
-
GUILD_MEMBER_ADD: GuildMemberEvent;
|
|
121
|
-
GUILD_MEMBER_UPDATE: GuildMemberEvent;
|
|
122
|
-
GUILD_MEMBER_REMOVE: GuildMemberEvent;
|
|
123
|
-
BOT_READY: BotReadyEvent;
|
|
124
|
-
EVENTS_SUBSCRIBED: EventsSubscribedEvent;
|
|
125
|
-
EVENTS_UNSUBSCRIBED: EventsUnsubscribedEvent;
|
|
126
|
-
SUBSCRIBE_DENIED: SubscribeDeniedEvent;
|
|
127
114
|
}
|
package/lib/send.d.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import { DataEnums } from 'alemonjs';
|
|
2
2
|
import { BubbleClient } from './sdk/wss';
|
|
3
3
|
type Client = typeof BubbleClient.prototype;
|
|
4
|
-
export declare const
|
|
5
|
-
channel_id: string | number;
|
|
4
|
+
export declare const sendToRoom: (client: Client, param: {
|
|
5
|
+
channel_id: string | number | null;
|
|
6
|
+
thread_id?: string | number | null;
|
|
7
|
+
message_id?: string | number | null;
|
|
6
8
|
}, val: DataEnums[]) => Promise<import("alemonjs").Result<any>[]>;
|
|
7
|
-
export declare const
|
|
9
|
+
export declare const sendToUser: (client: Client, param: {
|
|
8
10
|
author_id?: string | number;
|
|
9
11
|
channel_id?: string | number;
|
|
12
|
+
thread_id?: string | number;
|
|
13
|
+
message_id?: string | number;
|
|
10
14
|
}, val: DataEnums[]) => Promise<import("alemonjs").Result<any>[]>;
|
|
11
15
|
declare const _default: {};
|
|
12
16
|
export default _default;
|
package/lib/send.js
CHANGED
|
@@ -5,38 +5,15 @@ const ImageURLToBuffer = async (url) => {
|
|
|
5
5
|
const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
|
|
6
6
|
return Buffer.from(arrayBuffer);
|
|
7
7
|
};
|
|
8
|
-
const
|
|
9
|
-
return rows.map(row => {
|
|
10
|
-
const val = row.value;
|
|
11
|
-
return {
|
|
12
|
-
type: 1,
|
|
13
|
-
components: val.map(button => {
|
|
14
|
-
const value = button.value;
|
|
15
|
-
let text = '';
|
|
16
|
-
if (typeof button.options?.data === 'object') {
|
|
17
|
-
text = button.options?.data.click;
|
|
18
|
-
}
|
|
19
|
-
else {
|
|
20
|
-
text = button.options.data;
|
|
21
|
-
}
|
|
22
|
-
return {
|
|
23
|
-
type: 2,
|
|
24
|
-
custom_id: text,
|
|
25
|
-
style: 1,
|
|
26
|
-
label: typeof value === 'object' ? value.title : value
|
|
27
|
-
};
|
|
28
|
-
})
|
|
29
|
-
};
|
|
30
|
-
});
|
|
31
|
-
};
|
|
32
|
-
const sendchannel = async (client, param, val) => {
|
|
8
|
+
const sendToRoom = async (client, param, val) => {
|
|
33
9
|
try {
|
|
34
10
|
if (!val || val.length <= 0) {
|
|
35
11
|
return [];
|
|
36
12
|
}
|
|
37
13
|
const channelId = String(param?.channel_id ?? '');
|
|
14
|
+
const threadId = String(param?.thread_id ?? '');
|
|
15
|
+
const messageId = param?.message_id ? String(param?.message_id) : undefined;
|
|
38
16
|
const images = val.filter(item => item.type === 'Image' || item.type === 'ImageURL' || item.type === 'ImageFile');
|
|
39
|
-
const buttons = val.filter(item => item.type === 'BT.group');
|
|
40
17
|
const mds = val.filter(item => item.type === 'Markdown');
|
|
41
18
|
const content = val
|
|
42
19
|
.filter(item => item.type === 'Mention' || item.type === 'Text' || item.type === 'Link')
|
|
@@ -92,19 +69,28 @@ const sendchannel = async (client, param, val) => {
|
|
|
92
69
|
bufferData = readFileSync(item.value);
|
|
93
70
|
}
|
|
94
71
|
}
|
|
95
|
-
const uploadRes = await client.uploadFile(bufferData, undefined, { channelId });
|
|
72
|
+
const uploadRes = await client.uploadFile(bufferData, undefined, { channelId, threadId, messageId: messageId });
|
|
96
73
|
const fileAttachment = uploadRes?.file;
|
|
97
74
|
if (!fileAttachment) {
|
|
98
75
|
throw new Error('文件上传失败:未返回文件信息');
|
|
99
76
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
77
|
+
if (channelId) {
|
|
78
|
+
const res = await client.sendMessage(channelId, {
|
|
79
|
+
content: content,
|
|
80
|
+
type: 'image',
|
|
81
|
+
attachments: [fileAttachment]
|
|
82
|
+
});
|
|
83
|
+
return [createResult(ResultCode.Ok, '完成', res)];
|
|
84
|
+
}
|
|
85
|
+
if (threadId) {
|
|
86
|
+
const res = await client.sendDm(threadId, {
|
|
87
|
+
content: content,
|
|
88
|
+
type: 'image',
|
|
89
|
+
attachments: [fileAttachment]
|
|
90
|
+
});
|
|
91
|
+
return [createResult(ResultCode.Ok, '完成', res)];
|
|
92
|
+
}
|
|
93
|
+
return [createResult(ResultCode.Ok, '完成', null)];
|
|
108
94
|
}
|
|
109
95
|
let contentMd = '';
|
|
110
96
|
if (mds && mds.length > 0) {
|
|
@@ -157,24 +143,16 @@ const sendchannel = async (client, param, val) => {
|
|
|
157
143
|
}
|
|
158
144
|
});
|
|
159
145
|
}
|
|
160
|
-
if (
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
content: contentMd || content,
|
|
171
|
-
components
|
|
172
|
-
});
|
|
173
|
-
return [createResult(ResultCode.Ok, '完成', res)];
|
|
174
|
-
}
|
|
175
|
-
if (content && content.length > 0) {
|
|
176
|
-
const res = await client.sendMessage(channelId, { content: contentMd || content });
|
|
177
|
-
return [createResult(ResultCode.Ok, '完成', res)];
|
|
146
|
+
if ((content && content.length > 0 || contentMd && contentMd.length > 0)) {
|
|
147
|
+
if (channelId) {
|
|
148
|
+
const res = await client.sendMessage(channelId, { content: content ?? contentMd, type: 'text' });
|
|
149
|
+
return [createResult(ResultCode.Ok, '完成', res)];
|
|
150
|
+
}
|
|
151
|
+
if (threadId) {
|
|
152
|
+
const res = await client.sendDm(threadId, { content: content ?? contentMd, type: 'text' });
|
|
153
|
+
return [createResult(ResultCode.Ok, '完成', res)];
|
|
154
|
+
}
|
|
155
|
+
return [createResult(ResultCode.Ok, '完成', null)];
|
|
178
156
|
}
|
|
179
157
|
return [];
|
|
180
158
|
}
|
|
@@ -182,20 +160,21 @@ const sendchannel = async (client, param, val) => {
|
|
|
182
160
|
return [createResult(ResultCode.Fail, err?.response?.data ?? err?.message ?? err, null)];
|
|
183
161
|
}
|
|
184
162
|
};
|
|
185
|
-
const
|
|
163
|
+
const sendToUser = async (client, param, val) => {
|
|
186
164
|
if (!val || val.length <= 0) {
|
|
187
165
|
return [];
|
|
188
166
|
}
|
|
189
|
-
let
|
|
190
|
-
|
|
167
|
+
let threadId = param?.channel_id || param?.thread_id;
|
|
168
|
+
const messageId = param?.message_id;
|
|
169
|
+
if (!threadId && param.author_id) {
|
|
191
170
|
const dm = await client.getOrCreateDm(param.author_id);
|
|
192
|
-
|
|
171
|
+
threadId = dm?.id;
|
|
193
172
|
}
|
|
194
|
-
if (!
|
|
173
|
+
if (!threadId) {
|
|
195
174
|
return [];
|
|
196
175
|
}
|
|
197
|
-
return
|
|
176
|
+
return sendToRoom(client, { channel_id: null, thread_id: threadId, message_id: messageId }, val);
|
|
198
177
|
};
|
|
199
178
|
var send = {};
|
|
200
179
|
|
|
201
|
-
export { send as default,
|
|
180
|
+
export { send as default, sendToRoom, sendToUser };
|