@alemonjs/discord 0.2.6 → 0.2.8

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/dist/index.html CHANGED
@@ -6,7 +6,7 @@
6
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
7
  <title>AlemonJS</title>
8
8
  <script type="module" crossorigin src="/assets/index.js"></script>
9
- <link rel="stylesheet" crossorigin href="/assets/index.css">
9
+ <link rel="stylesheet" crossorigin href="/assets/index.css" />
10
10
  </head>
11
11
 
12
12
  <body>
package/lib/desktop.js CHANGED
@@ -13,12 +13,14 @@ const activate = context => {
13
13
  context.onCommand('open.discord', () => {
14
14
  const dir = join(__dirname, '../', 'dist', 'index.html');
15
15
  const scriptReg = /<script.*?src="(.+?)".*?>/;
16
- const styleReg = /<link.*?href="(.+?)".*?>/;
16
+ const styleReg = /<link.*?rel="stylesheet".*?href="(.+?)".*?>/;
17
+ const iconReg = /<link.*?rel="icon".*?href="(.+?)".*?>/g;
17
18
  // 创建 webview 路径
18
19
  const styleUri = context.createExtensionDir(join(__dirname, '../', 'dist', 'assets', 'index.css'));
19
20
  const scriptUri = context.createExtensionDir(join(__dirname, '../', 'dist', 'assets', 'index.js'));
20
21
  // 确保路径存在
21
22
  const html = readFileSync(dir, 'utf-8')
23
+ .replace(iconReg, ``)
22
24
  .replace(scriptReg, `<script type="module" crossorigin src="${scriptUri}"></script>`)
23
25
  .replace(styleReg, `<link rel="stylesheet" crossorigin href="${styleUri}">`);
24
26
  // 立即渲染 webview
package/lib/index.d.ts CHANGED
@@ -4,6 +4,6 @@ import { DCClient } from './sdk/platform/discord/sdk/wss.js';
4
4
  type Client = typeof DCClient.prototype;
5
5
  declare const client: Client;
6
6
  declare const platform = "discord";
7
- declare const _default: () => alemonjs.ClientAPI;
7
+ declare const _default: alemonjs.DefineBotValue;
8
8
 
9
9
  export { type Client, client, _default as default, platform };
package/lib/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import { defineBot, getConfigValue, useUserHashKey, OnProcessor } from 'alemonjs';
2
- import 'fs';
1
+ import { defineBot, getConfigValue, useUserHashKey, onProcessor } from 'alemonjs';
2
+ import { readFileSync } from 'fs';
3
3
  import 'axios';
4
4
  import 'path';
5
5
  import 'qrcode';
@@ -31,11 +31,46 @@ var index = defineBot(() => {
31
31
  });
32
32
  // 连接
33
33
  client.connect();
34
+ const ImageURLToBuffer = async (url) => {
35
+ const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
36
+ return Buffer.from(arrayBuffer);
37
+ };
38
+ /**
39
+ * 创建用户头像
40
+ * @param UserId
41
+ * @param avatar
42
+ * @returns
43
+ */
44
+ const createUserAvatar = (UserId, avatar) => {
45
+ let url = null;
46
+ return {
47
+ toBuffer: async () => {
48
+ if (!url) {
49
+ url = client.userAvatar(UserId, avatar);
50
+ }
51
+ return ImageURLToBuffer(url);
52
+ },
53
+ toBase64: async () => {
54
+ if (!url) {
55
+ url = client.userAvatar(UserId, avatar);
56
+ }
57
+ const buffer = await ImageURLToBuffer(url);
58
+ return buffer?.toString('base64');
59
+ },
60
+ toURL: async () => {
61
+ if (!url) {
62
+ url = client.userAvatar(UserId, avatar);
63
+ }
64
+ return url;
65
+ }
66
+ };
67
+ };
34
68
  // 监听消息
35
69
  client.on('MESSAGE_CREATE', async (event) => {
36
70
  // 消除bot消息
37
71
  if (event.author?.bot)
38
72
  return;
73
+ console.log("event", event);
39
74
  const master_key = config?.master_key ?? [];
40
75
  const isMaster = master_key.includes(event.author.id);
41
76
  // 艾特消息处理
@@ -56,78 +91,222 @@ var index = defineBot(() => {
56
91
  Platform: platform,
57
92
  UserId: UserId
58
93
  });
59
- let url = null;
60
- const UserAvatar = {
61
- toBuffer: async () => {
62
- if (!url) {
63
- url = client.userAvatar(UserId, event.author.avatar);
64
- }
65
- const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
66
- return Buffer.from(arrayBuffer);
67
- },
68
- toBase64: async () => {
69
- if (!url) {
70
- url = client.userAvatar(UserId, event.author.avatar);
71
- }
72
- const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
73
- return Buffer.from(arrayBuffer).toString('base64');
74
- },
75
- toURL: async () => {
76
- if (!url) {
77
- url = client.userAvatar(UserId, event.author.avatar);
78
- }
79
- return url;
80
- }
81
- };
82
- // 定义消
83
- const e = {
84
- name: 'message.create',
85
- // 事件类型
86
- Platform: platform,
87
- // guild
88
- GuildId: event.guild_id,
89
- ChannelId: event.channel_id,
90
- // user
91
- UserId: UserId,
92
- UserKey,
93
- UserName: event.author.username,
94
- UserAvatar: UserAvatar,
95
- IsMaster: isMaster,
96
- IsBot: false,
97
- // message
98
- MessageId: event.id,
99
- MessageText: msg,
100
- OpenId: '',
101
- CreateAt: Date.now(),
102
- // other
103
- tag: 'MESSAGE_CREATE',
104
- value: null
105
- };
106
- // 当访问的时候获取
107
- Object.defineProperty(e, 'value', {
108
- get() {
109
- return event;
110
- }
111
- });
112
- // 处理消息
113
- OnProcessor(e, 'message.create');
94
+ const UserAvatar = createUserAvatar(UserId, event.author.avatar);
95
+ if (event.type == 0) {
96
+ // 私聊
97
+ const e = {
98
+ name: 'private.message.create',
99
+ // 事件类型
100
+ Platform: platform,
101
+ // guild
102
+ // GuildId: event.guild_id,
103
+ // ChannelId: event.channel_id,
104
+ // user
105
+ UserId: UserId,
106
+ UserKey,
107
+ UserName: event.author.username,
108
+ UserAvatar: UserAvatar,
109
+ IsMaster: isMaster,
110
+ IsBot: false,
111
+ // message
112
+ MessageId: event.id,
113
+ MessageText: msg,
114
+ OpenId: '',
115
+ CreateAt: Date.now(),
116
+ // other
117
+ tag: 'private.message.create',
118
+ value: null
119
+ };
120
+ // 处理消息
121
+ onProcessor('private.message.create', e, event);
122
+ }
123
+ else if (event.type == 8) {
124
+ // 群聊
125
+ const e = {
126
+ name: 'message.create',
127
+ // 事件类型
128
+ Platform: platform,
129
+ // guild
130
+ GuildId: event.guild_id,
131
+ ChannelId: event.channel_id,
132
+ // user
133
+ UserId: UserId,
134
+ UserKey,
135
+ UserName: event.author.username,
136
+ UserAvatar: UserAvatar,
137
+ IsMaster: isMaster,
138
+ IsBot: false,
139
+ // message
140
+ MessageId: event.id,
141
+ MessageText: msg,
142
+ OpenId: '',
143
+ CreateAt: Date.now(),
144
+ // other
145
+ tag: 'message.create',
146
+ value: null
147
+ };
148
+ // 处理消息
149
+ onProcessor('message.create', e, event);
150
+ }
151
+ else ;
114
152
  });
153
+ // client.on('')
115
154
  // 发送错误时
116
155
  client.on('ERROR', console.error);
117
156
  global.client = client;
118
157
  return {
158
+ platform,
119
159
  api: {
160
+ active: {
161
+ send: {
162
+ channel: (channel_id, val) => {
163
+ if (val.length < 0)
164
+ return Promise.all([]);
165
+ const content = val
166
+ .filter(item => item.type == 'Mention' || item.type == 'Text')
167
+ .map(item => {
168
+ // if (item.type == 'Link') {
169
+ // return `[${item.options?.title ?? item.value}](${item.value})`
170
+ // } else
171
+ if (item.type == 'Mention') {
172
+ if (item.value == 'everyone' ||
173
+ item.value == 'all' ||
174
+ item.value == '' ||
175
+ typeof item.value != 'string') {
176
+ return `<@everyone>`;
177
+ }
178
+ if (item.options?.belong == 'user') {
179
+ return `<@${item.value}>`;
180
+ }
181
+ else if (item.options?.belong == 'channel') {
182
+ return `<#${item.value}>`;
183
+ }
184
+ return '';
185
+ }
186
+ else if (item.type == 'Text') {
187
+ if (item.options?.style == 'block') {
188
+ return `\`${item.value}\``;
189
+ }
190
+ else if (item.options?.style == 'italic') {
191
+ return `*${item.value}*`;
192
+ }
193
+ else if (item.options?.style == 'bold') {
194
+ return `**${item.value}**`;
195
+ }
196
+ else if (item.options?.style == 'strikethrough') {
197
+ return `~~${item.value}~~`;
198
+ }
199
+ return item.value;
200
+ }
201
+ })
202
+ .join('');
203
+ if (content) {
204
+ return Promise.all([content].map(item => client.channelsMessages(channel_id, {
205
+ content: item
206
+ })));
207
+ }
208
+ const images = val.filter(item => item.type == 'Image' || item.type == 'ImageURL' || item.type == 'ImageFile');
209
+ if (images.length > 0) {
210
+ return Promise.all(images.map(async (item) => {
211
+ if (item.type == 'Image') {
212
+ return client.channelsMessagesImage(channel_id, item.value);
213
+ }
214
+ else if (item.type == 'ImageURL') {
215
+ return client.channelsMessagesImage(channel_id, ImageURLToBuffer(item.value));
216
+ }
217
+ else if (item.type == 'ImageFile') {
218
+ const data = readFileSync(item.value);
219
+ return client.channelsMessagesImage(channel_id, data);
220
+ }
221
+ }));
222
+ }
223
+ return Promise.all([]);
224
+ },
225
+ user: async (author_id, val) => {
226
+ if (val.length < 0)
227
+ return Promise.all([]);
228
+ const res = await client.userMeChannels(author_id);
229
+ const channel_id = res.id;
230
+ const content = val
231
+ .filter(item => item.type == 'Mention' || item.type == 'Text')
232
+ .map(item => {
233
+ // if (item.type == 'Link') {
234
+ // return `[${item.options?.title ?? item.value}](${item.value})`
235
+ // } else
236
+ if (item.type == 'Mention') {
237
+ if (item.value == 'everyone' ||
238
+ item.value == 'all' ||
239
+ item.value == '' ||
240
+ typeof item.value != 'string') {
241
+ return `<@everyone>`;
242
+ }
243
+ if (item.options?.belong == 'user') {
244
+ return `<@${item.value}>`;
245
+ }
246
+ else if (item.options?.belong == 'channel') {
247
+ return `<#${item.value}>`;
248
+ }
249
+ return '';
250
+ }
251
+ else if (item.type == 'Text') {
252
+ if (item.options?.style == 'block') {
253
+ return `\`${item.value}\``;
254
+ }
255
+ else if (item.options?.style == 'italic') {
256
+ return `*${item.value}*`;
257
+ }
258
+ else if (item.options?.style == 'bold') {
259
+ return `**${item.value}**`;
260
+ }
261
+ else if (item.options?.style == 'strikethrough') {
262
+ return `~~${item.value}~~`;
263
+ }
264
+ return item.value;
265
+ }
266
+ })
267
+ .join('');
268
+ if (content) {
269
+ return Promise.all([content].map(item => client.channelsMessages(channel_id, {
270
+ content: item
271
+ })));
272
+ }
273
+ const images = val.filter(item => item.type == 'Image' || item.type == 'ImageURL' || item.type == 'ImageFile');
274
+ if (images.length > 0) {
275
+ return Promise.all(images.map(async (item) => {
276
+ if (item.type == 'Image') {
277
+ return client.channelsMessagesImage(channel_id, item.value);
278
+ }
279
+ else if (item.type == 'ImageURL') {
280
+ return client.channelsMessagesImage(channel_id, ImageURLToBuffer(item.value));
281
+ }
282
+ else if (item.type == 'ImageFile') {
283
+ const data = readFileSync(item.value);
284
+ return client.channelsMessagesImage(channel_id, data);
285
+ }
286
+ }));
287
+ }
288
+ return Promise.all([]);
289
+ }
290
+ }
291
+ },
120
292
  use: {
121
- send: (event, val) => {
293
+ send: async (event, val) => {
122
294
  if (val.length < 0)
123
295
  return Promise.all([]);
296
+ let channel_id = event.ChannelId;
297
+ if (/private/.test(event.name)) {
298
+ const data = event.value;
299
+ channel_id = data?.channel_id;
300
+ if (!channel_id)
301
+ return Promise.all([]);
302
+ }
124
303
  const content = val
125
- .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
304
+ .filter(item => item.type == 'Mention' || item.type == 'Text')
126
305
  .map(item => {
127
- if (item.type == 'Link') {
128
- return `[${item.options?.title ?? item.value}](${item.value})`;
129
- }
130
- else if (item.type == 'Mention') {
306
+ // if (item.type == 'Link') {
307
+ // return `[${item.options?.title ?? item.value}](${item.value})`
308
+ // } else
309
+ if (item.type == 'Mention') {
131
310
  if (item.value == 'everyone' ||
132
311
  item.value == 'all' ||
133
312
  item.value == '' ||
@@ -160,56 +339,46 @@ var index = defineBot(() => {
160
339
  })
161
340
  .join('');
162
341
  if (content) {
163
- return Promise.all([content].map(item => client.channelsMessages(event.ChannelId, {
342
+ return Promise.all([content].map(item => client.channelsMessages(channel_id, {
164
343
  content: item
165
344
  })));
166
345
  }
167
- const images = val.filter(item => item.type == 'Image').map(item => item.value);
168
- if (images) {
169
- return Promise.all(images.map(item => client.channelsMessagesImage(event.ChannelId, item)));
346
+ const images = val.filter(item => item.type == 'Image' || item.type == 'ImageURL' || item.type == 'ImageFile');
347
+ if (images.length > 0) {
348
+ return Promise.all(images.map(async (item) => {
349
+ if (item.type == 'Image') {
350
+ return client.channelsMessagesImage(channel_id, item.value);
351
+ }
352
+ else if (item.type == 'ImageURL') {
353
+ return client.channelsMessagesImage(channel_id, ImageURLToBuffer(item.value));
354
+ }
355
+ else if (item.type == 'ImageFile') {
356
+ const data = readFileSync(item.value);
357
+ return client.channelsMessagesImage(channel_id, data);
358
+ }
359
+ }));
170
360
  }
171
361
  return Promise.all([]);
172
362
  },
173
363
  mention: async (e) => {
174
364
  const event = e.value;
175
365
  const MessageMention = event.mentions.map(item => {
176
- let url = null;
177
366
  const UserId = item.id;
178
367
  const avatar = event.author.avatar;
179
368
  const value = getConfigValue();
180
369
  const config = value?.discord;
181
370
  const master_key = config?.master_key ?? [];
182
- const UserAvatar = {
183
- toBuffer: async () => {
184
- if (!url) {
185
- url = client.userAvatar(UserId, avatar);
186
- }
187
- const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
188
- return Buffer.from(arrayBuffer);
189
- },
190
- toBase64: async () => {
191
- if (!url) {
192
- url = client.userAvatar(UserId, avatar);
193
- }
194
- const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
195
- return Buffer.from(arrayBuffer).toString('base64');
196
- },
197
- toURL: async () => {
198
- if (!url) {
199
- url = client.userAvatar(UserId, avatar);
200
- }
201
- return url;
202
- }
203
- };
371
+ const UserAvatar = createUserAvatar(UserId, avatar);
372
+ const UserKey = useUserHashKey({
373
+ Platform: platform,
374
+ UserId: UserId
375
+ });
204
376
  return {
205
377
  UserId: item.id,
206
378
  IsMaster: master_key.includes(UserId),
207
379
  IsBot: item.bot,
208
380
  UserAvatar,
209
- UserKey: useUserHashKey({
210
- Platform: platform,
211
- UserId: UserId
212
- })
381
+ UserKey
213
382
  };
214
383
  });
215
384
  return MessageMention;
@@ -148,7 +148,7 @@ declare class DCAPI {
148
148
  * 创建DM
149
149
  * *********
150
150
  */
151
- userMeChannels(): Promise<any>;
151
+ userMeChannels(recipient_id: string): Promise<any>;
152
152
  /**
153
153
  * ********
154
154
  * 应用api
@@ -253,10 +253,13 @@ class DCAPI {
253
253
  * 创建DM
254
254
  * *********
255
255
  */
256
- async userMeChannels() {
256
+ async userMeChannels(recipient_id) {
257
257
  return this.request({
258
258
  method: 'post',
259
- url: `/user/@me/channels`
259
+ url: `/user/@me/channels`,
260
+ data: {
261
+ recipient_id
262
+ }
260
263
  }).then(res => res?.data);
261
264
  }
262
265
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/discord",
3
- "version": "0.2.6",
3
+ "version": "0.2.8",
4
4
  "description": "阿柠檬discord平台连接",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",
@@ -25,15 +25,18 @@
25
25
  "name": "discord"
26
26
  }
27
27
  ],
28
+ "logo": "antd.DiscordOutlined",
28
29
  "commond": [
29
30
  {
30
31
  "name": "打开discord",
32
+ "icon": "antd.DiscordOutlined",
31
33
  "commond": "open.discord"
32
34
  }
33
35
  ],
34
36
  "sidebars": [
35
37
  {
36
38
  "name": "dc",
39
+ "icon": "antd.DiscordOutlined",
37
40
  "commond": "open.discord"
38
41
  }
39
42
  ]