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

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,12 +18,16 @@ yarn add @alemonjs/discord
18
18
  discord:
19
19
  # 令牌
20
20
  token: ''
21
- # 主人
22
- master_key: null
23
21
  # 前缀(非必填)
24
22
  intent: null
25
23
  # 活动 非必填)
26
24
  shard: null
25
+ # 使用 user_key
26
+ master_key:
27
+ - 'xxx'
28
+ # 使用 user_id
29
+ master_id:
30
+ - 'yyy'
27
31
  ```
28
32
 
29
33
  ```sh
package/lib/config.js CHANGED
@@ -1,11 +1,22 @@
1
- import { getConfigValue } from 'alemonjs';
1
+ import { getConfigValue, useUserHashKey } from 'alemonjs';
2
2
 
3
3
  // 平台
4
4
  const platform = 'discord';
5
- const getDiscordConfigValue = () => {
5
+ const getDiscordConfig = () => {
6
6
  const value = getConfigValue() || {};
7
7
  const config = value[platform] || {};
8
8
  return config;
9
9
  };
10
+ const getMaster = (UserId) => {
11
+ const config = getDiscordConfig();
12
+ const master_key = config.master_key || [];
13
+ const master_id = config.master_id || [];
14
+ const UserKey = useUserHashKey({
15
+ Platform: platform,
16
+ UserId: UserId
17
+ });
18
+ const is = master_key.includes(UserKey) || master_id.includes(UserId);
19
+ return [is, UserKey];
20
+ };
10
21
 
11
- export { getDiscordConfigValue, platform };
22
+ export { getDiscordConfig, getMaster, platform };
package/lib/hook.d.ts ADDED
@@ -0,0 +1,39 @@
1
+ import { EventKeys, Events } from 'alemonjs';
2
+ import { DCAPI } from './sdk/api.js';
3
+ import { MESSAGE_CREATE_TYPE } from './sdk/message/MESSAGE_CREATE.js';
4
+ import { INTERACTION_CREATE_TYPE } from './sdk/message/INTERACTION_CREATE.js';
5
+
6
+ type MAP = {
7
+ 'message.create': MESSAGE_CREATE_TYPE;
8
+ 'private.message.create': MESSAGE_CREATE_TYPE;
9
+ 'interaction.create': INTERACTION_CREATE_TYPE;
10
+ 'private.interaction.create': INTERACTION_CREATE_TYPE;
11
+ 'message.update': undefined;
12
+ 'message.delete': undefined;
13
+ 'message.reaction.add': undefined;
14
+ 'message.reaction.remove': undefined;
15
+ 'channal.create': undefined;
16
+ 'channal.delete': undefined;
17
+ 'guild.join': undefined;
18
+ 'guild.exit': undefined;
19
+ 'member.add': undefined;
20
+ 'member.remove': undefined;
21
+ 'private.message.update': undefined;
22
+ 'private.message.delete': undefined;
23
+ 'private.friend.add': undefined;
24
+ 'private.guild.add': undefined;
25
+ };
26
+ /**
27
+ *
28
+ * @param event
29
+ * @returns
30
+ */
31
+ declare const useValue: <T extends EventKeys>(event: Events[T]) => readonly [MAP[T]];
32
+ /**
33
+ *
34
+ * @param event
35
+ * @returns
36
+ */
37
+ declare const useClient: <T extends EventKeys>(event: Events[T]) => readonly [DCAPI, MAP[T]];
38
+
39
+ export { useClient, useValue };
package/lib/hook.js ADDED
@@ -0,0 +1,24 @@
1
+ import { createEventValue, useClient as useClient$1 } from 'alemonjs';
2
+ import { DCAPI } from './sdk/api.js';
3
+
4
+ /**
5
+ *
6
+ * @param event
7
+ * @returns
8
+ */
9
+ const useValue = (event) => {
10
+ const value = createEventValue(event);
11
+ return [value];
12
+ };
13
+ /**
14
+ *
15
+ * @param event
16
+ * @returns
17
+ */
18
+ const useClient = (event) => {
19
+ const [client] = useClient$1(event, DCAPI);
20
+ const value = createEventValue(event);
21
+ return [client, value];
22
+ };
23
+
24
+ export { useClient, useValue };
package/lib/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { platform } from './config.js';
2
2
  export { DCAPI as API } from './sdk/api.js';
3
+ export { useClient, useValue } from './hook.js';
3
4
 
4
5
  declare const _default: () => void;
5
6
 
package/lib/index.js CHANGED
@@ -1,13 +1,14 @@
1
1
  import './env.js';
2
- import { cbpPlatform, useUserHashKey, createResult, ResultCode } from 'alemonjs';
2
+ import { cbpPlatform, createResult, ResultCode } from 'alemonjs';
3
3
  import { sendchannel, senduser } from './send.js';
4
4
  import { DCClient } from './sdk/wss.js';
5
- import { getDiscordConfigValue, platform } from './config.js';
5
+ import { getDiscordConfig, getMaster, platform } from './config.js';
6
6
  export { DCAPI as API } from './sdk/api.js';
7
+ export { useClient, useValue } from './hook.js';
7
8
 
8
9
  // main
9
10
  var index = () => {
10
- const value = getDiscordConfigValue();
11
+ const value = getDiscordConfig();
11
12
  const port = process.env?.port || value?.port || 17117;
12
13
  /**
13
14
  * 连接 alemonjs 服务器。
@@ -47,13 +48,7 @@ var index = () => {
47
48
  msg = msg.replace(`<@${item.id}>`, '').trim();
48
49
  }
49
50
  const UserId = event.author.id;
50
- const UserKey = useUserHashKey({
51
- Platform: platform,
52
- UserId: UserId
53
- });
54
- const value = getDiscordConfigValue();
55
- const master_key = value?.master_key ?? [];
56
- const isMaster = master_key.includes(UserKey);
51
+ const [isMaster, UserKey] = getMaster(UserId);
57
52
  const UserAvatar = createUserAvatar(UserId, event.author.avatar);
58
53
  if (event.type == 0 && event.member) {
59
54
  const e = {
@@ -63,6 +58,7 @@ var index = () => {
63
58
  // guild
64
59
  GuildId: event.guild_id,
65
60
  ChannelId: event.channel_id,
61
+ SpaceId: event.channel_id,
66
62
  // user
67
63
  UserId: UserId,
68
64
  UserKey,
@@ -87,9 +83,6 @@ var index = () => {
87
83
  name: 'private.message.create',
88
84
  // 事件类型
89
85
  Platform: platform,
90
- // guild
91
- // GuildId: event.guild_id,
92
- // ChannelId: event.channel_id,
93
86
  // user
94
87
  UserId: UserId,
95
88
  UserKey,
@@ -111,19 +104,12 @@ var index = () => {
111
104
  else ;
112
105
  });
113
106
  client.on('INTERACTION_CREATE', event => {
114
- console.log('event', event);
115
107
  const isPrivate = typeof event['user'] === 'object' ? true : false;
116
108
  const user = isPrivate ? event['user'] : event['member'].user;
117
109
  const UserId = user.id;
118
- const UserKey = useUserHashKey({
119
- Platform: platform,
120
- UserId: UserId
121
- });
122
110
  const UserAvatar = createUserAvatar(UserId, user.avatar);
123
111
  const UserName = user.username;
124
- const value = getDiscordConfigValue();
125
- const master_key = value?.master_key ?? [];
126
- const isMaster = master_key.includes(UserKey);
112
+ const [isMaster, UserKey] = getMaster(UserId);
127
113
  const MessageText = event.data.custom_id;
128
114
  if (isPrivate) {
129
115
  // 处理消息
@@ -160,6 +146,7 @@ var index = () => {
160
146
  // guild
161
147
  GuildId: event['guild_id'],
162
148
  ChannelId: event.channel_id,
149
+ SpaceId: event.channel_id,
163
150
  // user
164
151
  UserId: UserId,
165
152
  UserKey,
@@ -236,16 +223,11 @@ var index = () => {
236
223
  const MessageMention = event.mentions.map(item => {
237
224
  const UserId = item.id;
238
225
  const avatar = event.author.avatar;
239
- const value = getDiscordConfigValue();
240
- const master_key = value?.master_key ?? [];
241
226
  const UserAvatar = createUserAvatar(UserId, avatar);
242
- const UserKey = useUserHashKey({
243
- Platform: platform,
244
- UserId: UserId
245
- });
227
+ const [isMaster, UserKey] = getMaster(UserId);
246
228
  return {
247
229
  UserId: item.id,
248
- IsMaster: master_key.includes(UserId),
230
+ IsMaster: isMaster,
249
231
  IsBot: item.bot,
250
232
  UserAvatar,
251
233
  UserKey
@@ -280,8 +262,7 @@ var index = () => {
280
262
  consume([createResult(ResultCode.Ok, '请求完成', res)]);
281
263
  }
282
264
  });
283
- // 处理 api 调用
284
- cbp?.onapis(async (data, consume) => {
265
+ cbp.onapis(async (data, consume) => {
285
266
  const key = data.payload?.key;
286
267
  if (client[key]) {
287
268
  // 如果 client 上有对应的 key,直接调用。
package/lib/sdk/api.js CHANGED
@@ -4,7 +4,7 @@ import { existsSync, createReadStream } from 'fs';
4
4
  import { Readable, isReadable } from 'stream';
5
5
  import { basename } from 'path';
6
6
  import { fileTypeFromBuffer, fileTypeFromStream } from 'file-type';
7
- import { getDiscordConfigValue } from '../config.js';
7
+ import { getDiscordConfig } from '../config.js';
8
8
 
9
9
  /**
10
10
  * 创建form
@@ -53,7 +53,7 @@ class DCAPI {
53
53
  * @returns
54
54
  */
55
55
  request(options) {
56
- const value = getDiscordConfigValue();
56
+ const value = getDiscordConfig();
57
57
  const token = value.token;
58
58
  const service = axios.create({
59
59
  baseURL: API_URL,
@@ -71,7 +71,7 @@ class DCAPI {
71
71
  * @returns
72
72
  */
73
73
  requestCDN(options) {
74
- const value = getDiscordConfigValue();
74
+ const value = getDiscordConfig();
75
75
  const token = value.token;
76
76
  const service = axios.create({
77
77
  baseURL: CDB_URL,
@@ -0,0 +1,126 @@
1
+ interface User {
2
+ username: string;
3
+ public_flags: number;
4
+ primary_guild: null;
5
+ id: string;
6
+ global_name: string | null;
7
+ discriminator: string;
8
+ collectibles: null;
9
+ clan: null;
10
+ avatar_decoration_data: null;
11
+ avatar: string;
12
+ }
13
+ type Message = {
14
+ type: number;
15
+ tts: boolean;
16
+ timestamp: string;
17
+ pinned: boolean;
18
+ mentions: unknown[];
19
+ mention_roles: unknown[];
20
+ mention_everyone: boolean;
21
+ id: string;
22
+ flags: number;
23
+ embeds: unknown[];
24
+ edited_timestamp: string | null;
25
+ content: string;
26
+ components: unknown[];
27
+ channel_id: string;
28
+ author: User;
29
+ attachments: unknown[];
30
+ };
31
+ type Member = {
32
+ user: User;
33
+ unusual_dm_activity_until: string | null;
34
+ roles: string[];
35
+ premium_since: string | null;
36
+ permissions: string;
37
+ pending: boolean;
38
+ nick: string | null;
39
+ mute: boolean;
40
+ joined_at: string;
41
+ flags: number;
42
+ deaf: boolean;
43
+ communication_disabled_until: string | null;
44
+ banner: string | null;
45
+ avatar: string | null;
46
+ };
47
+ type DiscordGuild = {
48
+ locale: string;
49
+ id: string;
50
+ features: string[];
51
+ };
52
+ type PublicChannel = {
53
+ type: number;
54
+ topic: string | null;
55
+ theme_color: number | null;
56
+ rate_limit_per_user: number;
57
+ position: number;
58
+ permissions: string;
59
+ parent_id: string;
60
+ nsfw: boolean;
61
+ name: string;
62
+ last_message_id: string;
63
+ id: string;
64
+ icon_emoji: {
65
+ name: string;
66
+ id: string | null;
67
+ };
68
+ guild_id: string;
69
+ flags: number;
70
+ };
71
+ interface Channel {
72
+ type: number;
73
+ recipients: any[];
74
+ recipient_flags: number;
75
+ last_message_id: string;
76
+ id: string;
77
+ flags: number;
78
+ }
79
+ interface Data {
80
+ id: number;
81
+ custom_id: string;
82
+ component_type: number;
83
+ }
84
+ type Public = {
85
+ version: number;
86
+ type: number;
87
+ token: string;
88
+ member: Member;
89
+ message: Message;
90
+ locale: string;
91
+ id: string;
92
+ guild_locale: string;
93
+ guild_id: string;
94
+ guild: DiscordGuild;
95
+ entitlement_sku_ids: any[];
96
+ entitlements: any[];
97
+ data: Data;
98
+ context: number;
99
+ channel_id: string;
100
+ channel: PublicChannel;
101
+ authorizing_integration_owners: Record<string, string>;
102
+ attachment_size_limit: number;
103
+ application_id: string;
104
+ app_permissions: string;
105
+ };
106
+ type Private = {
107
+ version: number;
108
+ type: number;
109
+ token: string;
110
+ user: User;
111
+ message: Message;
112
+ locale: string;
113
+ id: string;
114
+ entitlements: any[];
115
+ data: Data;
116
+ context: number;
117
+ channel_id: string;
118
+ channel: Channel;
119
+ authorizing_integration_owners: Record<string, string>;
120
+ attachment_size_limit: number;
121
+ application_id: string;
122
+ app_permissions: string;
123
+ };
124
+ type INTERACTION_CREATE_TYPE = Public | Private;
125
+
126
+ export type { INTERACTION_CREATE_TYPE };
@@ -0,0 +1,59 @@
1
+ /**
2
+ * 基础消息
3
+ * @param event
4
+ */
5
+ type MESSAGE_CREATE_TYPE = {
6
+ type: number;
7
+ tts: boolean;
8
+ timestamp: string;
9
+ referenced_message: null;
10
+ pinned: boolean;
11
+ nonce: string;
12
+ mentions: {
13
+ username: string;
14
+ public_flags: number;
15
+ member: any;
16
+ id: string;
17
+ global_name: null;
18
+ discriminator: string;
19
+ bot: boolean;
20
+ avatar_decoration_data: null;
21
+ avatar: string;
22
+ }[];
23
+ mention_roles: any[];
24
+ mention_everyone: boolean;
25
+ member: {
26
+ roles: any[];
27
+ premium_since: null;
28
+ pending: boolean;
29
+ nick: null;
30
+ mute: boolean;
31
+ joined_at: string;
32
+ flags: number;
33
+ deaf: boolean;
34
+ communication_disabled_until: null;
35
+ avatar: null;
36
+ };
37
+ id: string;
38
+ flags: number;
39
+ embeds: any[];
40
+ edited_timestamp: null;
41
+ content: string;
42
+ components: any[];
43
+ channel_id: string;
44
+ author: {
45
+ username: string;
46
+ public_flags: number;
47
+ premium_type: number;
48
+ id: string;
49
+ global_name: string;
50
+ discriminator: string;
51
+ avatar_decoration_data: null;
52
+ avatar: string;
53
+ bot?: boolean;
54
+ };
55
+ attachments: any[];
56
+ guild_id: string;
57
+ };
58
+
59
+ export type { MESSAGE_CREATE_TYPE };
package/lib/sdk/wss.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import WebSocket from 'ws';
2
2
  import { DCAPI } from './api.js';
3
3
  import { getIntents } from './intents.js';
4
- import { getDiscordConfigValue } from '../config.js';
4
+ import { getDiscordConfig } from '../config.js';
5
5
 
6
6
  class DCClient extends DCAPI {
7
7
  #heartbeat_interval = 0;
@@ -19,7 +19,7 @@ class DCClient extends DCAPI {
19
19
  * @returns
20
20
  */
21
21
  #aut() {
22
- const value = getDiscordConfigValue();
22
+ const value = getDiscordConfig();
23
23
  const token = value.token;
24
24
  const intent = value.intent || [];
25
25
  const shard = value.shard || [0, 1];
@@ -55,7 +55,7 @@ class DCClient extends DCAPI {
55
55
  * @returns
56
56
  */
57
57
  async connect() {
58
- const value = getDiscordConfigValue();
58
+ const value = getDiscordConfig();
59
59
  const gatewayURL = value.gatewayURL;
60
60
  // 清除序列号
61
61
  this.#seq = null;
package/lib/send.js CHANGED
@@ -95,7 +95,7 @@ const sendchannel = async (client, param, val) => {
95
95
  }
96
96
  }
97
97
  const res = await client.channelsMessagesForm(channel_id, {
98
- content: content,
98
+ content: content
99
99
  }, bufferData);
100
100
  return [createResult(ResultCode.Ok, '完成', {})];
101
101
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@alemonjs/discord",
3
- "version": "2.1.0-alpha.6",
4
- "description": "阿柠檬discord平台连接",
3
+ "version": "2.1.0-alpha.9",
4
+ "description": "discord platform connection",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",
7
7
  "type": "module",