@alemonjs/bubble 2.1.0-alpha.8 → 2.1.0

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/index.d.ts CHANGED
@@ -7,5 +7,5 @@ export { OpCode } from './sdk/wss.types';
7
7
  export type { BUBBLEOptions, HelloPayload, SubscribePayload } from './sdk/wss.types';
8
8
  export * from './hook';
9
9
  export { type Options } from './config';
10
- declare const main: () => void;
11
- export default main;
10
+ declare const _default: () => any;
11
+ export default _default;
package/lib/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { BubbleClient } from './sdk/wss.js';
2
- import { cbpPlatform, createResult, ResultCode } from 'alemonjs';
2
+ import { definePlatform, 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';
@@ -13,11 +13,18 @@ const main = () => {
13
13
  const cbp = cbpPlatform(url);
14
14
  const client = new BubbleClient();
15
15
  void client.connect();
16
+ let botId = '';
17
+ client.getMe().then(res => {
18
+ botId = String(res?.id ?? '');
19
+ }).catch(() => { });
16
20
  const createUserAvatar = (_UserId, avatar) => {
21
+ if (!avatar) {
22
+ return '';
23
+ }
17
24
  return `${CDN_URL}/${avatar}`;
18
25
  };
19
26
  client.on('MESSAGE_CREATE', event => {
20
- if (event.author?.is_bot) {
27
+ if (event.author?.is_bot || event.author?.isBot) {
21
28
  return;
22
29
  }
23
30
  const atUsers = [];
@@ -48,8 +55,8 @@ const main = () => {
48
55
  OpenId: UserId,
49
56
  MessageId: String(event.id),
50
57
  MessageText: msg,
51
- CreateAt: Date.now(),
52
- tag: 'message.create',
58
+ BotId: botId,
59
+ _tag: 'message.create',
53
60
  value: event
54
61
  };
55
62
  cbp.send(e);
@@ -70,8 +77,118 @@ const main = () => {
70
77
  OpenId: UserId,
71
78
  MessageId: String(event.id),
72
79
  MessageText: event.content,
73
- CreateAt: Date.now(),
74
- tag: 'private.message.create',
80
+ BotId: botId,
81
+ _tag: 'private.message.create',
82
+ value: event
83
+ };
84
+ cbp.send(e);
85
+ });
86
+ client.on('MESSAGE_UPDATE', event => {
87
+ const UserId = String(event.authorId);
88
+ const [isMaster, UserKey] = getMaster(UserId);
89
+ const UserAvatar = createUserAvatar(UserId, event?.author?.avatar);
90
+ const e = {
91
+ name: 'message.update',
92
+ Platform: platform,
93
+ GuildId: String(event.channelId || ''),
94
+ ChannelId: String(event.channelId || ''),
95
+ SpaceId: String(event.channelId || ''),
96
+ UserId: UserId,
97
+ UserKey,
98
+ UserName: event?.author?.username,
99
+ UserAvatar: UserAvatar,
100
+ IsMaster: isMaster,
101
+ IsBot: false,
102
+ MessageId: String(event.id),
103
+ BotId: botId,
104
+ _tag: 'MESSAGE_UPDATE',
105
+ value: event
106
+ };
107
+ cbp.send(e);
108
+ });
109
+ client.on('MESSAGE_DELETE', event => {
110
+ const e = {
111
+ name: 'message.delete',
112
+ Platform: platform,
113
+ GuildId: String(event.guild_id || ''),
114
+ ChannelId: String(event.channel_id || ''),
115
+ SpaceId: String(event.channel_id || ''),
116
+ MessageId: String(event.id),
117
+ BotId: botId,
118
+ _tag: 'MESSAGE_DELETE',
119
+ value: event
120
+ };
121
+ cbp.send(e);
122
+ });
123
+ client.on('DM_MESSAGE_UPDATE', event => {
124
+ const UserId = String(event.authorId);
125
+ const [isMaster, UserKey] = getMaster(UserId);
126
+ const UserAvatar = createUserAvatar(UserId, event?.author?.avatar);
127
+ const e = {
128
+ name: 'private.message.update',
129
+ Platform: platform,
130
+ UserId: UserId,
131
+ UserKey,
132
+ UserName: event?.author?.username,
133
+ UserAvatar: UserAvatar,
134
+ IsMaster: isMaster,
135
+ IsBot: false,
136
+ MessageId: String(event.id),
137
+ BotId: botId,
138
+ _tag: 'DM_MESSAGE_UPDATE',
139
+ value: event
140
+ };
141
+ cbp.send(e);
142
+ });
143
+ client.on('DM_MESSAGE_DELETE', event => {
144
+ const e = {
145
+ name: 'private.message.delete',
146
+ Platform: platform,
147
+ MessageId: String(event.id),
148
+ BotId: botId,
149
+ _tag: 'DM_MESSAGE_DELETE',
150
+ value: event
151
+ };
152
+ cbp.send(e);
153
+ });
154
+ client.on('GUILD_MEMBER_ADD', event => {
155
+ const UserId = String(event.user_id || event.user?.id || '');
156
+ const [isMaster, UserKey] = getMaster(UserId);
157
+ const e = {
158
+ name: 'member.add',
159
+ Platform: platform,
160
+ GuildId: String(event.guild_id || ''),
161
+ ChannelId: '',
162
+ SpaceId: String(event.guild_id || ''),
163
+ UserId: UserId,
164
+ UserKey,
165
+ UserName: event.user?.username ?? event.nickname,
166
+ IsMaster: isMaster,
167
+ IsBot: false,
168
+ MessageId: '',
169
+ BotId: botId,
170
+ _tag: 'GUILD_MEMBER_ADD',
171
+ value: event
172
+ };
173
+ cbp.send(e);
174
+ });
175
+ client.on('GUILD_MEMBER_REMOVE', event => {
176
+ const UserId = String(event.user_id || event.user?.id || '');
177
+ const [isMaster, UserKey] = getMaster(UserId);
178
+ const e = {
179
+ name: 'member.remove',
180
+ Platform: platform,
181
+ GuildId: String(event.guild_id || ''),
182
+ ChannelId: '',
183
+ SpaceId: String(event.guild_id || ''),
184
+ UserId: UserId,
185
+ UserKey,
186
+ UserName: event.user?.username ?? event.nickname,
187
+ IsMaster: isMaster,
188
+ IsBot: false,
189
+ MessageId: '',
190
+ BotId: botId,
191
+ _tag: 'GUILD_MEMBER_REMOVE',
75
192
  value: event
76
193
  };
77
194
  cbp.send(e);
@@ -91,10 +208,10 @@ const main = () => {
91
208
  },
92
209
  use: {
93
210
  send: async (event, val) => {
94
- if (val.length < 0) {
211
+ if (!val || val.length <= 0) {
95
212
  return [];
96
213
  }
97
- const tag = event.tag;
214
+ const tag = event._tag;
98
215
  if (tag === 'message.create') {
99
216
  const ChannelId = String(event.value.channelId || '');
100
217
  const res = await sendToRoom(client, { channel_id: ChannelId, message_id: String(event.value.id || '') }, val);
@@ -114,9 +231,9 @@ const main = () => {
114
231
  },
115
232
  mention: e => {
116
233
  const event = e.value;
117
- const MessageMention = event.mentions.map(item => {
234
+ const MessageMention = (event.mentions || []).map(item => {
118
235
  const UserId = item.id;
119
- const avatar = event.author.avatar;
236
+ const avatar = item.avatar;
120
237
  const UserAvatar = createUserAvatar(UserId, avatar);
121
238
  const [isMaster, UserKey] = getMaster(UserId);
122
239
  return {
@@ -135,7 +252,21 @@ const main = () => {
135
252
  };
136
253
  const onactions = async (data, consume) => {
137
254
  try {
138
- if (data.action === 'message.send') {
255
+ if (data.action === 'me.info') {
256
+ const res = await client.getMe();
257
+ const UserId = String(res.id);
258
+ const [isMaster, UserKey] = getMaster(UserId);
259
+ const user = {
260
+ UserId: UserId,
261
+ UserName: res.name,
262
+ IsBot: true,
263
+ IsMaster: isMaster,
264
+ UserAvatar: '',
265
+ UserKey: UserKey
266
+ };
267
+ consume([createResult(ResultCode.Ok, '请求完成', user)]);
268
+ }
269
+ else if (data.action === 'message.send') {
139
270
  const event = data.payload.event;
140
271
  const paramFormat = data.payload.params.format;
141
272
  const res = await api.use.send(event, paramFormat);
@@ -158,6 +289,9 @@ const main = () => {
158
289
  const res = await api.use.mention(event);
159
290
  consume([createResult(ResultCode.Ok, '请求完成', res)]);
160
291
  }
292
+ else {
293
+ consume([createResult(ResultCode.Fail, '未知请求,请尝试升级版本', null)]);
294
+ }
161
295
  }
162
296
  catch (error) {
163
297
  consume([createResult(ResultCode.Fail, '请求失败', error)]);
@@ -176,35 +310,12 @@ const main = () => {
176
310
  consume([createResult(ResultCode.Fail, '请求失败', error)]);
177
311
  }
178
312
  }
313
+ else {
314
+ consume([createResult(ResultCode.Fail, '未知请求,请尝试升级版本', null)]);
315
+ }
179
316
  };
180
317
  cbp.onapis((data, consume) => void onapis(data, consume));
181
318
  };
182
- const mainProcess = () => {
183
- ['SIGINT', 'SIGTERM', 'SIGQUIT', 'disconnect'].forEach(sig => {
184
- process?.on?.(sig, () => {
185
- logger.info?.(`[alemonjs][${sig}] 收到信号,正在关闭...`);
186
- setImmediate(() => process.exit(0));
187
- });
188
- });
189
- process?.on?.('exit', code => {
190
- logger.info?.(`[alemonjs][exit] 进程退出,code=${code}`);
191
- });
192
- process.on('message', msg => {
193
- try {
194
- const data = typeof msg === 'string' ? JSON.parse(msg) : msg;
195
- if (data?.type === 'start') {
196
- main();
197
- }
198
- else if (data?.type === 'stop') {
199
- process.exit(0);
200
- }
201
- }
202
- catch { }
203
- });
204
- if (process.send) {
205
- process.send(JSON.stringify({ type: 'ready' }));
206
- }
207
- };
208
- mainProcess();
319
+ var index = definePlatform({ main });
209
320
 
210
- export { BubbleClient, CDN_URL, main as default, platform };
321
+ export { BubbleClient, CDN_URL, index as default, platform };
@@ -79,15 +79,15 @@ const loggerError = err => {
79
79
  message: err?.message
80
80
  });
81
81
  };
82
- const createAxiosInstance = (service, options) => {
83
- return new Promise((resolve, reject) => {
84
- service(options)
85
- .then(res => resolve(res?.data ?? {}))
86
- .catch(err => {
87
- loggerError(err);
88
- reject(err?.response?.data);
89
- });
90
- });
82
+ const createAxiosInstance = async (service, options) => {
83
+ try {
84
+ const res = await service(options);
85
+ return res?.data ?? {};
86
+ }
87
+ catch (err) {
88
+ loggerError(err);
89
+ throw err?.response?.data ?? err;
90
+ }
91
91
  };
92
92
 
93
93
  export { createAxiosInstance };
@@ -35,6 +35,7 @@ export interface BaseMessage {
35
35
  authorId?: string | number;
36
36
  author: {
37
37
  id: number;
38
+ isBot?: boolean;
38
39
  is_bot?: boolean;
39
40
  avatar?: string | null;
40
41
  username: string;
@@ -110,5 +111,11 @@ export interface FileQuota {
110
111
  }
111
112
  export interface BubbleEventMap {
112
113
  MESSAGE_CREATE: MessageCreateEvent;
114
+ MESSAGE_UPDATE: MessageUpdateEvent;
115
+ MESSAGE_DELETE: MessageDeleteEvent;
113
116
  DM_MESSAGE_CREATE: DmMessageCreateEvent;
117
+ DM_MESSAGE_UPDATE: MessageUpdateEvent;
118
+ DM_MESSAGE_DELETE: MessageDeleteEvent;
119
+ GUILD_MEMBER_ADD: GuildMemberEvent;
120
+ GUILD_MEMBER_REMOVE: GuildMemberEvent;
114
121
  }
package/lib/send.js CHANGED
@@ -59,7 +59,20 @@ const sendToRoom = async (client, param, val) => {
59
59
  }
60
60
  const item = images[i];
61
61
  if (item.type === 'Image') {
62
- bufferData = Buffer.from(item.value, 'base64');
62
+ if (Buffer.isBuffer(item.value)) {
63
+ bufferData = item.value;
64
+ }
65
+ else if (item.value.startsWith('http://') || item.value.startsWith('https://')) {
66
+ const res = await ImageURLToBuffer(item.value);
67
+ bufferData = res;
68
+ }
69
+ else if (item.value.startsWith('base64://')) {
70
+ const base64Str = item.value.slice(9);
71
+ bufferData = Buffer.from(base64Str, 'base64');
72
+ }
73
+ else if (item.value.startsWith('file://')) {
74
+ bufferData = readFileSync(item.value.slice(7));
75
+ }
63
76
  }
64
77
  else if (item.type === 'ImageURL') {
65
78
  const res = await ImageURLToBuffer(item.value);
@@ -95,41 +108,57 @@ const sendToRoom = async (client, param, val) => {
95
108
  let contentMd = '';
96
109
  if (mdAndButtons && mdAndButtons.length > 0) {
97
110
  mdAndButtons.forEach(item => {
98
- if (item.type === 'Markdown') {
111
+ if (item.type === 'Markdown' && typeof item.value !== 'string') {
99
112
  const md = item.value;
113
+ const map = {
114
+ 'MD.title': value => `# ${value}`,
115
+ 'MD.subtitle': value => `## ${value}`,
116
+ 'MD.text': value => `${value} `,
117
+ 'MD.bold': value => `**${value}** `,
118
+ 'MD.divider': () => '\n————————\n',
119
+ 'MD.italic': value => `_${value}_ `,
120
+ 'MD.italicStar': value => `*${value}* `,
121
+ 'MD.strikethrough': value => `~~${value}~~ `,
122
+ 'MD.blockquote': value => `\n> ${value}`,
123
+ 'MD.newline': () => '\n',
124
+ 'MD.link': value => `[🔗${value.text}](${value.url}) `,
125
+ 'MD.image': value => `\n![${value}](${value})\n`,
126
+ 'MD.mention': (value, options) => {
127
+ const { belong } = options || {};
128
+ if (value === 'everyone' || value === 'all' || value === '' || typeof value !== 'string') {
129
+ return '<@everyone> ';
130
+ }
131
+ if (belong === 'user') {
132
+ return `<@${value}> `;
133
+ }
134
+ else if (belong === 'channel') {
135
+ return `<#${value}> `;
136
+ }
137
+ return '';
138
+ },
139
+ 'MD.button': (value, options) => {
140
+ const autoEnter = options?.autoEnter ?? false;
141
+ const label = typeof value === 'object' ? value.title : value;
142
+ const command = options?.data || label;
143
+ return `<btn variant="borderless" command="${command}" enter="${String(autoEnter)}" >${label}</btn> `;
144
+ },
145
+ 'MD.content': value => `${value}`
146
+ };
100
147
  md.forEach(line => {
101
- if (line.type === 'MD.text') {
102
- contentMd += line.value;
103
- }
104
- else if (line.type === 'MD.blockquote') {
105
- contentMd += `\n> ${line.value}\n`;
106
- }
107
- else if (line.type === 'MD.bold') {
108
- contentMd += `**${line.value}**`;
148
+ if (map[line.type]) {
149
+ const value = line?.value;
150
+ const options = line?.options;
151
+ contentMd += map[line.type](value, options);
152
+ return;
109
153
  }
110
- else if (line.type === 'MD.italic') {
111
- contentMd += `*${line.value}*`;
112
- }
113
- else if (line.type === 'MD.divider') {
114
- contentMd += '\n---\n';
115
- }
116
- else if (line.type === 'MD.image') {
117
- contentMd += `\n![${line.value}](${line.value})\n`;
118
- }
119
- else if (line.type === 'MD.link') {
120
- contentMd += `[${line.value}](${line.value})`;
121
- }
122
- else if (line.type === 'MD.list') {
154
+ if (line.type === 'MD.list') {
123
155
  const listStr = line.value.map(listItem => {
124
156
  if (typeof listItem.value === 'object') {
125
157
  return `\n${listItem.value.index}. ${listItem.value.text}`;
126
158
  }
127
159
  return `\n- ${listItem.value}`;
128
160
  });
129
- contentMd += `${listStr}\n`;
130
- }
131
- else if (line.type === 'MD.newline') {
132
- contentMd += '\n';
161
+ contentMd += `${listStr.join('')}\n`;
133
162
  }
134
163
  else if (line.type === 'MD.code') {
135
164
  const language = line?.options?.language || '';
@@ -141,7 +170,7 @@ const sendToRoom = async (client, param, val) => {
141
170
  }
142
171
  });
143
172
  }
144
- else if (item.type === 'BT.group' && item.value.length > 0) {
173
+ else if (item.type === 'BT.group' && item.value.length > 0 && typeof item.value !== 'string') {
145
174
  contentMd += `<box classWind="mt-2" variant="borderless" >${item.value
146
175
  ?.map(row => {
147
176
  const val = row.value;
@@ -150,10 +179,10 @@ const sendToRoom = async (client, param, val) => {
150
179
  }
151
180
  return `<flex>${val
152
181
  .map(button => {
153
- const value = button.value;
182
+ const value = button?.value || {};
154
183
  const options = button.options;
155
184
  const autoEnter = options?.autoEnter ?? false;
156
- const label = typeof value === 'object' ? value.title : value;
185
+ const label = value;
157
186
  const command = options?.data || label;
158
187
  return `<btn command="${command}" enter="${String(autoEnter)}" >${label}</btn>`;
159
188
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/bubble",
3
- "version": "2.1.0-alpha.8",
3
+ "version": "2.1.0",
4
4
  "description": "bubble platform",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",