@alemonjs/discord 0.2.7 → 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.
Files changed (29) hide show
  1. package/README.md +5 -0
  2. package/dist/assets/index.css +466 -1
  3. package/dist/assets/index.js +10879 -14
  4. package/dist/index.html +1 -1
  5. package/lib/desktop.d.ts +2 -2
  6. package/lib/desktop.js +54 -55
  7. package/lib/index.d.ts +7 -6
  8. package/lib/index.js +201 -86
  9. package/lib/sdk/platform/discord/sdk/api.d.ts +889 -907
  10. package/lib/sdk/platform/discord/sdk/api.js +5 -2
  11. package/lib/sdk/platform/discord/sdk/message/CHANNEL_TOPIC_UPDATE.d.ts +5 -5
  12. package/lib/sdk/platform/discord/sdk/message/CHANNEL_UPDATE.d.ts +42 -42
  13. package/lib/sdk/platform/discord/sdk/message/GUILD_MEMBER_ADD.d.ts +23 -23
  14. package/lib/sdk/platform/discord/sdk/message/GUILD_MEMBER_REMOVE.d.ts +12 -12
  15. package/lib/sdk/platform/discord/sdk/message/GUILD_MEMBER_UPDATE.d.ts +27 -27
  16. package/lib/sdk/platform/discord/sdk/message/MESSAGE_CREATE.d.ts +53 -53
  17. package/lib/sdk/platform/discord/sdk/message/MESSAGE_DELETE.d.ts +5 -5
  18. package/lib/sdk/platform/discord/sdk/message/MESSAGE_REACTION_ADD.d.ts +36 -36
  19. package/lib/sdk/platform/discord/sdk/message/MESSAGE_UPDATE.d.ts +50 -50
  20. package/lib/sdk/platform/discord/sdk/message/PRESENCE_UPDATE.d.ts +56 -62
  21. package/lib/sdk/platform/discord/sdk/message/READY.d.ts +7 -7
  22. package/lib/sdk/platform/discord/sdk/message/TYPING_START.d.ts +29 -29
  23. package/lib/sdk/platform/discord/sdk/message/VOICE_CHANNEL_STATUS_UPDATE.d.ts +5 -5
  24. package/lib/sdk/platform/discord/sdk/message/VOICE_STATE_UPDATE.d.ts +36 -36
  25. package/lib/sdk/platform/discord/sdk/message.d.ts +31 -31
  26. package/lib/sdk/platform/discord/sdk/types.d.ts +3 -23
  27. package/lib/sdk/platform/discord/sdk/wss.d.ts +23 -23
  28. package/lib/sdk/platform/discord/sdk/wss.types.d.ts +17 -17
  29. package/package.json +1 -1
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.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- declare const activate: (context: any) => void
1
+ declare const activate: (context: any) => void;
2
2
 
3
- export { activate }
3
+ export { activate };
package/lib/desktop.js CHANGED
@@ -1,61 +1,60 @@
1
- import { readFileSync } from 'fs'
2
- import { dirname, join } from 'path'
3
- import { fileURLToPath } from 'url'
4
- import { getConfig, getConfigValue } from 'alemonjs'
1
+ import { readFileSync } from 'fs';
2
+ import { dirname, join } from 'path';
3
+ import { fileURLToPath } from 'url';
4
+ import { getConfig, getConfigValue } from 'alemonjs';
5
5
 
6
6
  // 当前目录
7
- const __dirname = dirname(fileURLToPath(import.meta.url))
7
+ const __dirname = dirname(fileURLToPath(import.meta.url));
8
8
  // 被激活的时候。
9
9
  const activate = context => {
10
- // 创建一个 webview。
11
- const webView = context.createSidebarWebView(context)
12
- // 当命令被触发的时候。
13
- context.onCommand('open.discord', () => {
14
- const dir = join(__dirname, '../', 'dist', 'index.html')
15
- const scriptReg = /<script.*?src="(.+?)".*?>/
16
- const styleReg = /<link.*?rel="stylesheet".*?href="(.+?)".*?>/
17
- const iconReg = /<link.*?rel="icon".*?href="(.+?)".*?>/g
18
- // 创建 webview 路径
19
- const styleUri = context.createExtensionDir(
20
- join(__dirname, '../', 'dist', 'assets', 'index.css')
21
- )
22
- const scriptUri = context.createExtensionDir(
23
- join(__dirname, '../', 'dist', 'assets', 'index.js')
24
- )
25
- // 确保路径存在
26
- const html = readFileSync(dir, 'utf-8')
27
- .replace(iconReg, ``)
28
- .replace(scriptReg, `<script type="module" crossorigin src="${scriptUri}"></script>`)
29
- .replace(styleReg, `<link rel="stylesheet" crossorigin href="${styleUri}">`)
30
- // 立即渲染 webview
31
- webView.loadWebView(html)
32
- })
33
- // 监听 webview 的消息。
34
- webView.onMessage(data => {
35
- try {
36
- if (data.type === 'discord.form.save') {
37
- const db = data.data
38
- const config = getConfig()
39
- const value = config.value ?? {}
40
- value['discord'] = {
41
- token: db.token ?? '',
42
- master_key: db.master_key?.split(',') ?? null
10
+ // 创建一个 webview。
11
+ const webView = context.createSidebarWebView(context);
12
+ // 当命令被触发的时候。
13
+ context.onCommand('open.discord', () => {
14
+ const dir = join(__dirname, '../', 'dist', 'index.html');
15
+ const scriptReg = /<script.*?src="(.+?)".*?>/;
16
+ const styleReg = /<link.*?rel="stylesheet".*?href="(.+?)".*?>/;
17
+ const iconReg = /<link.*?rel="icon".*?href="(.+?)".*?>/g;
18
+ // 创建 webview 路径
19
+ const styleUri = context.createExtensionDir(join(__dirname, '../', 'dist', 'assets', 'index.css'));
20
+ const scriptUri = context.createExtensionDir(join(__dirname, '../', 'dist', 'assets', 'index.js'));
21
+ // 确保路径存在
22
+ const html = readFileSync(dir, 'utf-8')
23
+ .replace(iconReg, ``)
24
+ .replace(scriptReg, `<script type="module" crossorigin src="${scriptUri}"></script>`)
25
+ .replace(styleReg, `<link rel="stylesheet" crossorigin href="${styleUri}">`);
26
+ // 立即渲染 webview
27
+ webView.loadWebView(html);
28
+ });
29
+ // 监听 webview 的消息。
30
+ webView.onMessage(data => {
31
+ try {
32
+ if (data.type === 'discord.form.save') {
33
+ const db = data.data;
34
+ const config = getConfig();
35
+ const value = config.value ?? {};
36
+ value['discord'] = {
37
+ token: db.token ?? '',
38
+ master_key: db.master_key?.split(',') ?? null
39
+ };
40
+ config.saveValue(value);
41
+ context.notification('discord 配置保存成功~');
42
+ }
43
+ else if (data.type === 'discord.init') {
44
+ let config = getConfigValue();
45
+ if (!config)
46
+ config = {};
47
+ // 发送消息
48
+ webView.postMessage({
49
+ type: 'discord.init',
50
+ data: config.discord ?? {}
51
+ });
52
+ }
43
53
  }
44
- config.saveValue(value)
45
- context.notification('discord 配置保存成功~')
46
- } else if (data.type === 'discord.init') {
47
- let config = getConfigValue()
48
- if (!config) config = {}
49
- // 发送消息
50
- webView.postMessage({
51
- type: 'discord.init',
52
- data: config.discord ?? {}
53
- })
54
- }
55
- } catch (e) {
56
- console.error(e)
57
- }
58
- })
59
- }
54
+ catch (e) {
55
+ console.error(e);
56
+ }
57
+ });
58
+ };
60
59
 
61
- export { activate }
60
+ export { activate };
package/lib/index.d.ts CHANGED
@@ -1,8 +1,9 @@
1
- import { DCClient } from './sdk/platform/discord/sdk/wss.js'
1
+ import * as alemonjs from 'alemonjs';
2
+ import { DCClient } from './sdk/platform/discord/sdk/wss.js';
2
3
 
3
- type Client = typeof DCClient.prototype
4
- declare const client: Client
5
- declare const platform = 'discord'
6
- declare const _default: any
4
+ type Client = typeof DCClient.prototype;
5
+ declare const client: Client;
6
+ declare const platform = "discord";
7
+ declare const _default: alemonjs.DefineBotValue;
7
8
 
8
- export { type Client, client, _default as default, platform }
9
+ export { type Client, client, _default as default, platform };
package/lib/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { defineBot, getConfigValue, useUserHashKey, onProcessor } from 'alemonjs';
2
- import 'fs';
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,56 +91,66 @@ 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
- onProcessor('message.create', e, event);
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 ;
108
152
  });
153
+ // client.on('')
109
154
  // 发送错误时
110
155
  client.on('ERROR', console.error);
111
156
  global.client = client;
@@ -160,21 +205,101 @@ var index = defineBot(() => {
160
205
  content: item
161
206
  })));
162
207
  }
163
- const images = val.filter(item => item.type == 'Image').map(item => item.value);
164
- if (images) {
165
- return Promise.all(images.map(item => client.channelsMessagesImage(channel_id, item)));
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
+ }));
166
222
  }
167
223
  return Promise.all([]);
168
224
  },
169
- user: (user_id, data) => {
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
+ }
170
288
  return Promise.all([]);
171
289
  }
172
290
  }
173
291
  },
174
292
  use: {
175
- send: (event, val) => {
293
+ send: async (event, val) => {
176
294
  if (val.length < 0)
177
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
+ }
178
303
  const content = val
179
304
  .filter(item => item.type == 'Mention' || item.type == 'Text')
180
305
  .map(item => {
@@ -214,56 +339,46 @@ var index = defineBot(() => {
214
339
  })
215
340
  .join('');
216
341
  if (content) {
217
- return Promise.all([content].map(item => client.channelsMessages(event.ChannelId, {
342
+ return Promise.all([content].map(item => client.channelsMessages(channel_id, {
218
343
  content: item
219
344
  })));
220
345
  }
221
- const images = val.filter(item => item.type == 'Image').map(item => item.value);
222
- if (images) {
223
- 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
+ }));
224
360
  }
225
361
  return Promise.all([]);
226
362
  },
227
363
  mention: async (e) => {
228
364
  const event = e.value;
229
365
  const MessageMention = event.mentions.map(item => {
230
- let url = null;
231
366
  const UserId = item.id;
232
367
  const avatar = event.author.avatar;
233
368
  const value = getConfigValue();
234
369
  const config = value?.discord;
235
370
  const master_key = config?.master_key ?? [];
236
- const UserAvatar = {
237
- toBuffer: async () => {
238
- if (!url) {
239
- url = client.userAvatar(UserId, avatar);
240
- }
241
- const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
242
- return Buffer.from(arrayBuffer);
243
- },
244
- toBase64: async () => {
245
- if (!url) {
246
- url = client.userAvatar(UserId, avatar);
247
- }
248
- const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
249
- return Buffer.from(arrayBuffer).toString('base64');
250
- },
251
- toURL: async () => {
252
- if (!url) {
253
- url = client.userAvatar(UserId, avatar);
254
- }
255
- return url;
256
- }
257
- };
371
+ const UserAvatar = createUserAvatar(UserId, avatar);
372
+ const UserKey = useUserHashKey({
373
+ Platform: platform,
374
+ UserId: UserId
375
+ });
258
376
  return {
259
377
  UserId: item.id,
260
378
  IsMaster: master_key.includes(UserId),
261
379
  IsBot: item.bot,
262
380
  UserAvatar,
263
- UserKey: useUserHashKey({
264
- Platform: platform,
265
- UserId: UserId
266
- })
381
+ UserKey
267
382
  };
268
383
  });
269
384
  return MessageMention;