@alemonjs/bubble 2.1.0-alpha.1 → 2.1.0-alpha.10

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 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
- 'channal.create': undefined;
13
- 'channal.delete': undefined;
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 { sendchannel, senduser } from './send.js';
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?.bot) {
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.author.id);
33
+ const UserId = String(event.authorId);
34
34
  const [isMaster, UserKey] = getMaster(UserId);
35
- const UserAvatar = createUserAvatar(UserId, event.author.avatar);
36
- if (event.type === 0 && event.member) {
37
- const e = {
38
- name: 'message.create',
39
- Platform: platform,
40
- GuildId: String(event.guild_id || ''),
41
- ChannelId: String(event.channel_id || ''),
42
- SpaceId: String(event.channel_id || ''),
43
- UserId: UserId,
44
- UserKey,
45
- UserName: event.author.username,
46
- UserAvatar: UserAvatar,
47
- IsMaster: isMaster,
48
- IsBot: false,
49
- OpenId: UserId,
50
- MessageId: String(event.id),
51
- MessageText: msg,
52
- CreateAt: Date.now(),
53
- tag: 'message.create',
54
- value: event
55
- };
56
- cbp.send(e);
57
- }
58
- else if (event.type === 0 && !event.member) {
59
- const e = {
60
- name: 'private.message.create',
61
- Platform: platform,
62
- UserId: UserId,
63
- UserKey,
64
- UserName: event.author.username,
65
- UserAvatar: UserAvatar,
66
- IsMaster: isMaster,
67
- IsBot: false,
68
- OpenId: UserId,
69
- MessageId: String(event.id),
70
- MessageText: msg,
71
- CreateAt: Date.now(),
72
- tag: 'private.message.create',
73
- value: event
74
- };
75
- cbp.send(e);
76
- }
77
- else ;
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 sendchannel(client, { channel_id: UserId }, val);
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 senduser(client, { author_id: OpenId }, val);
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.channel_id || '');
100
- const res = await sendchannel(client, { channel_id: ChannelId }, val);
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.author.id || '');
105
- const ChannelId = String(event.value.channel_id || '');
106
- const res = await senduser(client, {
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/api.d.ts CHANGED
@@ -24,6 +24,8 @@ export declare class BubbleAPI {
24
24
  channelId?: string | number;
25
25
  threadId?: string | number;
26
26
  messageId?: string | number;
27
- }): Promise<FileUploadResponse>;
27
+ }): Promise<{
28
+ data: FileUploadResponse;
29
+ }>;
28
30
  filesQuota(): Promise<FileQuota>;
29
31
  }
package/lib/sdk/api.js CHANGED
@@ -71,9 +71,6 @@ class BubbleAPI {
71
71
  if (extra.threadId) {
72
72
  form.append('threadId', String(extra.threadId));
73
73
  }
74
- if (extra.messageId) {
75
- form.append('messageId', String(extra.messageId));
76
- }
77
74
  const value = getBubbleConfig();
78
75
  const token = value.token;
79
76
  const requestConfig = value.request_config ?? {};
@@ -30,23 +30,23 @@ export interface BaseMessage {
30
30
  id: string | number;
31
31
  content: string;
32
32
  type?: string | number;
33
- channel_id?: string | number;
34
- guild_id?: string | number;
35
- author_id?: string | number;
36
- author?: User;
37
- mentions?: User[];
38
- attachments?: Attachment[];
39
- created_at?: string;
40
- timestamp?: string;
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
- member?: {
44
- nickname?: string;
45
- roles?: number[];
46
- };
46
+ mentions?: any[];
47
47
  }
48
48
  export interface DmMessageCreateEvent extends BaseMessage {
49
- thread_id?: string | number;
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 sendchannel: (client: Client, param: {
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 senduser: (client: Client, param: {
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,39 +5,16 @@ 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 createButtonsData = (rows) => {
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
- const mds = val.filter(item => item.type === 'Markdown');
17
+ const mdAndButtons = val.filter(item => item.type === 'Markdown' || item.type === 'BT.group');
41
18
  const content = val
42
19
  .filter(item => item.type === 'Mention' || item.type === 'Text' || item.type === 'Link')
43
20
  .map(item => {
@@ -92,48 +69,55 @@ 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 });
96
- const fileAttachment = uploadRes?.file;
72
+ const uploadRes = await client.uploadFile(bufferData, undefined, { channelId, threadId, messageId: messageId });
73
+ const fileAttachment = uploadRes?.data?.file;
97
74
  if (!fileAttachment) {
98
- throw new Error('文件上传失败:未返回文件信息');
75
+ return [createResult(ResultCode.Ok, '文件上传失败:未返回文件信息', uploadRes)];
76
+ }
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)];
99
92
  }
100
- const attachments = [fileAttachment];
101
- const payload = {
102
- content: content,
103
- type: 'text',
104
- attachments
105
- };
106
- const res = await client.sendMessage(channelId, payload);
107
- return [createResult(ResultCode.Ok, '完成', res)];
93
+ return [createResult(ResultCode.Ok, '完成', null)];
108
94
  }
109
95
  let contentMd = '';
110
- if (mds && mds.length > 0) {
111
- mds.forEach(item => {
96
+ if (mdAndButtons && mdAndButtons.length > 0) {
97
+ mdAndButtons.forEach(item => {
112
98
  if (item.type === 'Markdown') {
113
99
  const md = item.value;
100
+ const map = {
101
+ title: (value) => `# ${value}`,
102
+ subtitle: (value) => `## ${value}`,
103
+ text: (value) => `${value} `,
104
+ bold: (value) => `**${value}** `,
105
+ divider: () => '\n————————\n',
106
+ italic: (value) => `_${value}_ `,
107
+ italicStar: (value) => `*${value}* `,
108
+ strikethrough: (value) => `~~${value}~~ `,
109
+ blockquote: (value) => `\n> ${value}`,
110
+ newline: () => '\n',
111
+ link: (value) => `[🔗${value.text}](${value.url}) `,
112
+ image: (value) => `\n![${value}](${value})\n`
113
+ };
114
114
  md.forEach(line => {
115
- if (line.type === 'MD.text') {
116
- contentMd += line.value;
117
- }
118
- else if (line.type === 'MD.blockquote') {
119
- contentMd += `> ${line.value}\n`;
120
- }
121
- else if (line.type === 'MD.bold') {
122
- contentMd += `**${line.value}**`;
123
- }
124
- else if (line.type === 'MD.italic') {
125
- contentMd += `*${line.value}*`;
126
- }
127
- else if (line.type === 'MD.divider') {
128
- contentMd += '---\n';
115
+ if (map[line.type]) {
116
+ const value = line?.value;
117
+ contentMd += map[line.type](value);
118
+ return;
129
119
  }
130
- else if (line.type === 'MD.image') {
131
- contentMd += `![${line.value}](${line.value})`;
132
- }
133
- else if (line.type === 'MD.link') {
134
- contentMd += `[${line.value}](${line.value})`;
135
- }
136
- else if (line.type === 'MD.list') {
120
+ if (line.type === 'MD.list') {
137
121
  const listStr = line.value.map(listItem => {
138
122
  if (typeof listItem.value === 'object') {
139
123
  return `\n${listItem.value.index}. ${listItem.value.text}`;
@@ -142,12 +126,9 @@ const sendchannel = async (client, param, val) => {
142
126
  });
143
127
  contentMd += `${listStr}\n`;
144
128
  }
145
- else if (line.type === 'MD.newline') {
146
- contentMd += '\n';
147
- }
148
129
  else if (line.type === 'MD.code') {
149
130
  const language = line?.options?.language || '';
150
- contentMd += `\`\`\`${language}\n${line.value}\n\`\`\`\n`;
131
+ contentMd += `\n\`\`\`${language}\n${line.value}\n\`\`\`\n`;
151
132
  }
152
133
  else {
153
134
  const value = line['value'] || '';
@@ -155,26 +136,38 @@ const sendchannel = async (client, param, val) => {
155
136
  }
156
137
  });
157
138
  }
158
- });
159
- }
160
- if (buttons && buttons.length > 0) {
161
- let components = null;
162
- buttons.forEach(item => {
163
- if (components) {
164
- return;
139
+ else if (item.type === 'BT.group' && item.value.length > 0) {
140
+ contentMd += `<box classWind="mt-2" variant="borderless" >${item.value
141
+ ?.map(row => {
142
+ const val = row.value;
143
+ if (val.length === 0) {
144
+ return '';
145
+ }
146
+ return `<flex>${val
147
+ .map(button => {
148
+ const value = button.value;
149
+ const options = button.options;
150
+ const autoEnter = options?.autoEnter ?? false;
151
+ const label = typeof value === 'object' ? value.title : value;
152
+ const command = options?.data || label;
153
+ return `<btn command="${command}" enter="${String(autoEnter)}" >${label}</btn>`;
154
+ })
155
+ .join('')}</flex>`;
156
+ })
157
+ .join('')}</box>`;
165
158
  }
166
- const rows = item.value;
167
- components = createButtonsData(rows);
168
- });
169
- const res = await client.sendMessage(channelId, {
170
- content: contentMd || content,
171
- components
172
159
  });
173
- return [createResult(ResultCode.Ok, '完成', res)];
174
160
  }
175
- if (content && content.length > 0) {
176
- const res = await client.sendMessage(channelId, { content: contentMd || content });
177
- return [createResult(ResultCode.Ok, '完成', res)];
161
+ if ((content && content.length > 0) || (contentMd && contentMd.length > 0)) {
162
+ if (channelId) {
163
+ const res = await client.sendMessage(channelId, { content: content !== '' ? content : contentMd, type: 'text' });
164
+ return [createResult(ResultCode.Ok, '完成', res)];
165
+ }
166
+ if (threadId) {
167
+ const res = await client.sendDm(threadId, { content: content !== '' ? content : contentMd, type: 'text' });
168
+ return [createResult(ResultCode.Ok, '完成', res)];
169
+ }
170
+ return [createResult(ResultCode.Ok, '完成', null)];
178
171
  }
179
172
  return [];
180
173
  }
@@ -182,20 +175,21 @@ const sendchannel = async (client, param, val) => {
182
175
  return [createResult(ResultCode.Fail, err?.response?.data ?? err?.message ?? err, null)];
183
176
  }
184
177
  };
185
- const senduser = async (client, param, val) => {
178
+ const sendToUser = async (client, param, val) => {
186
179
  if (!val || val.length <= 0) {
187
180
  return [];
188
181
  }
189
- let channelId = param?.channel_id;
190
- if (!channelId && param.author_id) {
182
+ let threadId = param?.channel_id || param?.thread_id;
183
+ const messageId = param?.message_id;
184
+ if (!threadId && param.author_id) {
191
185
  const dm = await client.getOrCreateDm(param.author_id);
192
- channelId = dm?.id;
186
+ threadId = dm?.id;
193
187
  }
194
- if (!channelId) {
188
+ if (!threadId) {
195
189
  return [];
196
190
  }
197
- return sendchannel(client, { channel_id: channelId }, val);
191
+ return sendToRoom(client, { channel_id: null, thread_id: threadId, message_id: messageId }, val);
198
192
  };
199
193
  var send = {};
200
194
 
201
- export { send as default, sendchannel, senduser };
195
+ export { send as default, sendToRoom, sendToUser };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/bubble",
3
- "version": "2.1.0-alpha.1",
3
+ "version": "2.1.0-alpha.10",
4
4
  "description": "bubble platform",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",
@@ -65,4 +65,4 @@
65
65
  "type": "git",
66
66
  "url": "https://github.com/lemonade-lab/alemonjs.git"
67
67
  }
68
- }
68
+ }