@alemonjs/discord 2.1.0-alpha.5 → 2.1.0-alpha.6

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.
@@ -0,0 +1,3 @@
1
+ declare const platform = "discord";
2
+
3
+ export { platform };
package/lib/config.js ADDED
@@ -0,0 +1,11 @@
1
+ import { getConfigValue } from 'alemonjs';
2
+
3
+ // 平台
4
+ const platform = 'discord';
5
+ const getDiscordConfigValue = () => {
6
+ const value = getConfigValue() || {};
7
+ const config = value[platform] || {};
8
+ return config;
9
+ };
10
+
11
+ export { getDiscordConfigValue, platform };
package/lib/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
- declare const platform = "discord";
1
+ export { platform } from './config.js';
2
+ export { DCAPI as API } from './sdk/api.js';
3
+
2
4
  declare const _default: () => void;
3
5
 
4
- export { _default as default, platform };
6
+ export { _default as default };
package/lib/index.js CHANGED
@@ -1,30 +1,24 @@
1
1
  import './env.js';
2
- import { getConfigValue, cbpPlatform, useUserHashKey, createResult, ResultCode } from 'alemonjs';
2
+ import { cbpPlatform, useUserHashKey, createResult, ResultCode } from 'alemonjs';
3
3
  import { sendchannel, senduser } from './send.js';
4
4
  import { DCClient } from './sdk/wss.js';
5
- import { AvailableIntentsEventsEnum } from './sdk/types.js';
5
+ import { getDiscordConfigValue, platform } from './config.js';
6
+ export { DCAPI as API } from './sdk/api.js';
6
7
 
7
- const platform = 'discord';
8
+ // main
8
9
  var index = () => {
9
- let value = getConfigValue();
10
- if (!value)
11
- value = {};
12
- const config = value[platform];
13
- // 创建客户端
14
- const client = new DCClient({
15
- gatewayURL: config?.gatewayURL,
16
- token: config.token,
17
- shard: config?.shard ?? [0, 1],
18
- intent: config?.intent ?? AvailableIntentsEventsEnum
19
- });
10
+ const value = getDiscordConfigValue();
11
+ const port = process.env?.port || value?.port || 17117;
20
12
  /**
21
13
  * 连接 alemonjs 服务器。
22
14
  * 向 alemonjs 推送标准信息
23
15
  */
24
- const url = `ws://127.0.0.1:${process.env?.port || config?.port || 17117}`;
16
+ const url = `ws://127.0.0.1:${port}`;
25
17
  const cbp = cbpPlatform(url);
18
+ // 创建客户端
19
+ const client = new DCClient();
26
20
  // 连接
27
- client.connect(config?.gatewayURL);
21
+ client.connect();
28
22
  /**
29
23
  * 创建用户头像
30
24
  * @param UserId
@@ -57,7 +51,8 @@ var index = () => {
57
51
  Platform: platform,
58
52
  UserId: UserId
59
53
  });
60
- const master_key = config?.master_key ?? [];
54
+ const value = getDiscordConfigValue();
55
+ const master_key = value?.master_key ?? [];
61
56
  const isMaster = master_key.includes(UserKey);
62
57
  const UserAvatar = createUserAvatar(UserId, event.author.avatar);
63
58
  if (event.type == 0 && event.member) {
@@ -126,7 +121,8 @@ var index = () => {
126
121
  });
127
122
  const UserAvatar = createUserAvatar(UserId, user.avatar);
128
123
  const UserName = user.username;
129
- const master_key = config?.master_key ?? [];
124
+ const value = getDiscordConfigValue();
125
+ const master_key = value?.master_key ?? [];
130
126
  const isMaster = master_key.includes(UserKey);
131
127
  const MessageText = event.data.custom_id;
132
128
  if (isPrivate) {
@@ -240,9 +236,8 @@ var index = () => {
240
236
  const MessageMention = event.mentions.map(item => {
241
237
  const UserId = item.id;
242
238
  const avatar = event.author.avatar;
243
- const value = getConfigValue();
244
- const config = value?.discord;
245
- const master_key = config?.master_key ?? [];
239
+ const value = getDiscordConfigValue();
240
+ const master_key = value?.master_key ?? [];
246
241
  const UserAvatar = createUserAvatar(UserId, avatar);
247
242
  const UserKey = useUserHashKey({
248
243
  Platform: platform,
@@ -285,6 +280,16 @@ var index = () => {
285
280
  consume([createResult(ResultCode.Ok, '请求完成', res)]);
286
281
  }
287
282
  });
283
+ // 处理 api 调用
284
+ cbp?.onapis(async (data, consume) => {
285
+ const key = data.payload?.key;
286
+ if (client[key]) {
287
+ // 如果 client 上有对应的 key,直接调用。
288
+ const params = data.payload.params;
289
+ const res = await client[key](...params);
290
+ consume([createResult(ResultCode.Ok, '请求完成', res)]);
291
+ }
292
+ });
288
293
  };
289
294
 
290
295
  export { index as default, platform };