@alemonjs/discord 2.1.0-alpha.0 → 2.1.0-alpha.12

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 (37) hide show
  1. package/README.md +16 -2
  2. package/dist/assets/index.js +6 -1
  3. package/lib/config.d.ts +35 -0
  4. package/lib/config.js +22 -0
  5. package/lib/hook.d.ts +39 -0
  6. package/lib/hook.js +24 -0
  7. package/lib/index.d.ts +5 -2
  8. package/lib/index.js +47 -85
  9. package/lib/sdk/api.d.ts +879 -965
  10. package/lib/sdk/api.js +30 -43
  11. package/lib/sdk/message/INTERACTION_CREATE.d.ts +113 -113
  12. package/lib/sdk/message/MESSAGE_CREATE.d.ts +53 -53
  13. package/lib/sdk/types.d.ts +4 -4
  14. package/lib/sdk/typings.d.ts +221 -0
  15. package/lib/sdk/wss.js +15 -38
  16. package/lib/send.js +167 -83
  17. package/package.json +9 -2
  18. package/lib/sdk/config.js +0 -10
  19. package/lib/sdk/core/config.js +0 -46
  20. package/lib/sdk/message/CHANNEL_TOPIC_UPDATE.d.ts +0 -11
  21. package/lib/sdk/message/CHANNEL_UPDATE.d.ts +0 -48
  22. package/lib/sdk/message/GUILD_MEMBER_ADD.d.ts +0 -28
  23. package/lib/sdk/message/GUILD_MEMBER_REMOVE.d.ts +0 -18
  24. package/lib/sdk/message/GUILD_MEMBER_UPDATE.d.ts +0 -33
  25. package/lib/sdk/message/MESSAGE_DELETE.d.ts +0 -11
  26. package/lib/sdk/message/MESSAGE_REACTION_ADD.d.ts +0 -42
  27. package/lib/sdk/message/MESSAGE_UPDATE.d.ts +0 -56
  28. package/lib/sdk/message/PRESENCE_UPDATE.d.ts +0 -69
  29. package/lib/sdk/message/READY.d.ts +0 -9
  30. package/lib/sdk/message/TYPING_START.d.ts +0 -35
  31. package/lib/sdk/message/VOICE_CHANNEL_STATUS_UPDATE.d.ts +0 -10
  32. package/lib/sdk/message/VOICE_STATE_UPDATE.d.ts +0 -41
  33. package/lib/sdk/message.d.ts +0 -101
  34. package/lib/sdk/types.js +0 -25
  35. package/lib/sdk/wss.d.ts +0 -27
  36. package/lib/sdk/wss.types.d.ts +0 -30
  37. /package/lib/sdk/{core/from.js → createPicFrom.js} +0 -0
@@ -0,0 +1,221 @@
1
+ /**
2
+ * buttons start
3
+ */
4
+ type ComponentButton = {
5
+ type: number;
6
+ style: 2;
7
+ label?: string;
8
+ emoji?: any;
9
+ custom_id?: string;
10
+ sku_id?: any;
11
+ url?: string;
12
+ disabled?: boolean;
13
+ };
14
+ type ComponentButtons = {
15
+ type: 1;
16
+ components: ComponentButton[];
17
+ };
18
+ /**
19
+ * buttons end
20
+ */
21
+ /**
22
+ * select start
23
+ */
24
+ type ComponentSelectsOption = {
25
+ label: string;
26
+ value: string;
27
+ description?: string;
28
+ emoji?: {
29
+ name?: string;
30
+ id?: string;
31
+ };
32
+ };
33
+ type ComponentChannelSelect = {
34
+ type: 8;
35
+ custom_id: string;
36
+ placeholder?: string;
37
+ channel_types?: number[];
38
+ };
39
+ type ComponentMentionSelect = {
40
+ type: 7;
41
+ custom_id: string;
42
+ placeholder?: string;
43
+ };
44
+ type ComponentRuleSelect = {
45
+ type: 6;
46
+ custom_id: string;
47
+ placeholder?: string;
48
+ max_values?: number;
49
+ min_values?: number;
50
+ };
51
+ type ComponentUserSelect = {
52
+ type: 5;
53
+ custom_id: string;
54
+ placeholder?: string;
55
+ };
56
+ type ComponentSelect = {
57
+ type: 3;
58
+ custom_id: string;
59
+ max_values?: number;
60
+ min_values?: number;
61
+ options: ComponentSelectsOption[];
62
+ };
63
+ type ComponentSelects = {
64
+ type: 1;
65
+ components: (ComponentSelect | ComponentUserSelect | ComponentRuleSelect | ComponentMentionSelect | ComponentChannelSelect)[];
66
+ };
67
+ /**
68
+ * select end
69
+ */
70
+ /**
71
+ * input start
72
+ */
73
+ type ComponentInput = {
74
+ id?: number;
75
+ type: 4;
76
+ custom_id: string;
77
+ label: string;
78
+ style: number;
79
+ min_length?: number;
80
+ max_length?: number;
81
+ placeholder?: string;
82
+ required?: boolean;
83
+ };
84
+ type ComponentModal = {
85
+ title: string;
86
+ custom_id: string;
87
+ components: {
88
+ type: 1;
89
+ components: ComponentInput[];
90
+ }[];
91
+ };
92
+ /**
93
+ * input end
94
+ */
95
+ /**
96
+ * section start
97
+ */
98
+ type ComponentText = {
99
+ type: 10;
100
+ content: string;
101
+ };
102
+ type ComponentSection = {
103
+ type: 9;
104
+ components: ComponentText[];
105
+ accessory?: {
106
+ type: 11;
107
+ media: {
108
+ url: string;
109
+ };
110
+ };
111
+ };
112
+ /**
113
+ * section end
114
+ */
115
+ type ComponentMedia = {
116
+ type: 12;
117
+ items: {
118
+ media: {
119
+ url: string;
120
+ };
121
+ description?: string;
122
+ }[];
123
+ };
124
+ type ComponentFile = {
125
+ type: 13;
126
+ file: {
127
+ url: string;
128
+ };
129
+ };
130
+ type ComponentSeparator = {
131
+ type: 14;
132
+ divider: boolean;
133
+ spacing: number;
134
+ };
135
+ type ComponentContainer = {
136
+ type: 17;
137
+ accent_color: number;
138
+ components: (ComponentButtons | ComponentSelects | ComponentMedia | ComponentFile | ComponentText | ComponentSection | ComponentSeparator)[];
139
+ };
140
+ type MessageData = {
141
+ content?: string;
142
+ tts?: boolean;
143
+ /**
144
+ * 嵌入
145
+ */
146
+ embeds?: {
147
+ title?: string;
148
+ type?: string;
149
+ description?: string;
150
+ url?: string;
151
+ timestamp?: string;
152
+ color?: number;
153
+ footer?: {
154
+ text: string;
155
+ icon_url?: string;
156
+ proxy_icon_url?: string;
157
+ };
158
+ image?: {
159
+ url: string;
160
+ proxy_url?: string;
161
+ height?: number;
162
+ width?: number;
163
+ };
164
+ thumbnail?: {
165
+ url: string;
166
+ proxy_url?: string;
167
+ height?: number;
168
+ width?: number;
169
+ };
170
+ video?: {
171
+ url: string;
172
+ proxy_url?: string;
173
+ height?: number;
174
+ width?: number;
175
+ };
176
+ provider?: {
177
+ name: string;
178
+ url: string;
179
+ };
180
+ author?: {
181
+ name: string;
182
+ url?: string;
183
+ icon_url?: string;
184
+ proxy_icon_url?: string;
185
+ };
186
+ fields?: {
187
+ name: string;
188
+ value: string;
189
+ inline?: boolean;
190
+ }[];
191
+ }[];
192
+ allowed_mentions?: any;
193
+ message_reference?: any;
194
+ /**
195
+ * 组件
196
+ */
197
+ components?: (ComponentButtons | ComponentSelects | ComponentModal | ComponentMedia | ComponentFile | ComponentText | ComponentSection | ComponentSeparator | ComponentContainer)[];
198
+ sticker_ids?: any;
199
+ files?: any[];
200
+ payload_json?: string;
201
+ attachments?: {
202
+ id: string;
203
+ filename: string;
204
+ description?: string;
205
+ content_type?: string;
206
+ size: number;
207
+ url: string;
208
+ proxy_url?: string;
209
+ height?: number;
210
+ width?: number;
211
+ ephemeral?: boolean;
212
+ duration_secs?: number;
213
+ waveform?: string;
214
+ flags?: number;
215
+ }[];
216
+ flags?: number;
217
+ enforce_nonce?: boolean;
218
+ poll?: any;
219
+ };
220
+
221
+ export type { MessageData };
package/lib/sdk/wss.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import WebSocket from 'ws';
2
2
  import { DCAPI } from './api.js';
3
- import { config } from './config.js';
4
3
  import { getIntents } from './intents.js';
4
+ import { getDiscordConfig } from '../config.js';
5
+ import { HttpsProxyAgent } from 'https-proxy-agent';
5
6
 
6
7
  class DCClient extends DCAPI {
7
8
  #heartbeat_interval = 0;
@@ -10,16 +11,8 @@ class DCClient extends DCAPI {
10
11
  #timeout_id = null;
11
12
  #seq = null;
12
13
  #ws;
13
- /**
14
- * 设置配置
15
- * @param opstion
16
- */
17
- constructor(opstion) {
14
+ constructor() {
18
15
  super();
19
- config.set('intent', opstion.intent);
20
- config.set('shard', opstion.shard);
21
- config.set('token', opstion.token);
22
- config.set('gatewayURL', opstion.gatewayURL);
23
16
  return this;
24
17
  }
25
18
  /**
@@ -27,9 +20,10 @@ class DCClient extends DCAPI {
27
20
  * @returns
28
21
  */
29
22
  #aut() {
30
- const token = config.get('token');
31
- const intent = config.get('intent');
32
- const shard = config.get('shard');
23
+ const value = getDiscordConfig();
24
+ const token = value.token;
25
+ const intent = value.intent || [];
26
+ const shard = value.shard || [0, 1];
33
27
  return {
34
28
  op: 2,
35
29
  d: {
@@ -45,24 +39,6 @@ class DCClient extends DCAPI {
45
39
  }
46
40
  };
47
41
  }
48
- /**
49
- * 重新确认
50
- */
51
- // #reAut() {
52
- // const token = config.get('token')
53
- // const c = {
54
- // op: 6,
55
- // d: {
56
- // // 会话token
57
- // token: token,
58
- // session_id: this.#session_id,
59
- // // 收到的最后一个序列号
60
- // seq: this.#seq
61
- // }
62
- // }
63
- // console.log('[ws] c', c)
64
- // return c
65
- // }
66
42
  #events = {};
67
43
  /**
68
44
  * 注册事件处理程序
@@ -79,7 +55,9 @@ class DCClient extends DCAPI {
79
55
  * @param shard
80
56
  * @returns
81
57
  */
82
- async connect(gatewayURL) {
58
+ async connect() {
59
+ const value = getDiscordConfig();
60
+ const gatewayURL = value.gatewayURL;
83
61
  // 清除序列号
84
62
  this.#seq = null;
85
63
  // 清除心跳
@@ -131,11 +109,6 @@ class DCClient extends DCAPI {
131
109
  console.log('[ws] session_id', this.#session_id);
132
110
  }
133
111
  }
134
- // const events = ['PRESENCE_UPDATE','TYPING_START']
135
- // if (!events.includes(t)) {
136
- // console.log("t", t)
137
- // console.log("d", d)
138
- // }
139
112
  // 事件处理
140
113
  if (this.#events[t]) {
141
114
  try {
@@ -181,7 +154,11 @@ class DCClient extends DCAPI {
181
154
  console.info('[ws] 心跳确认');
182
155
  }
183
156
  };
184
- this.#ws = new WebSocket(`${url}?v=10&encoding=json`);
157
+ const ClientOptions = value.websocket_options || {};
158
+ if (value.websocket_proxy) {
159
+ ClientOptions.agent = new HttpsProxyAgent(value.websocket_proxy);
160
+ }
161
+ this.#ws = new WebSocket(`${url}?v=10&encoding=json`, ClientOptions);
185
162
  this.#ws.on('open', async () => {
186
163
  console.info('[ws] open');
187
164
  });
package/lib/send.js CHANGED
@@ -29,98 +29,182 @@ const createButtonsData = (rows) => {
29
29
  };
30
30
  });
31
31
  };
32
- const sendchannel = (client, param, val) => {
33
- if (val.length < 0)
34
- return Promise.all([]);
35
- const channel_id = param?.channel_id ?? '';
36
- // images
37
- const images = val.filter(item => item.type == 'Image' || item.type == 'ImageURL' || item.type == 'ImageFile');
38
- // buttons
39
- const buttons = val.filter(item => item.type == 'BT.group');
40
- // text
41
- const content = val
42
- .filter(item => item.type == 'Mention' || item.type == 'Text' || item.type == 'Link')
43
- .map(item => {
44
- if (item.type == 'Link') {
45
- return `[${item.value}](${item?.options?.link ?? item.value})`;
46
- }
47
- else if (item.type == 'Mention') {
48
- if (item.value == 'everyone' ||
49
- item.value == 'all' ||
50
- item.value == '' ||
51
- typeof item.value != 'string') {
52
- return `<@everyone>`;
53
- }
54
- if (item.options?.belong == 'user') {
55
- return `<@${item.value}>`;
56
- }
57
- else if (item.options?.belong == 'channel') {
58
- return `<#${item.value}>`;
59
- }
60
- return '';
61
- }
62
- else if (item.type == 'Text') {
63
- if (item.options?.style == 'block') {
64
- return `\`${item.value}\``;
32
+ const sendchannel = async (client, param, val) => {
33
+ try {
34
+ if (val.length < 0)
35
+ return [];
36
+ const channel_id = param?.channel_id ?? '';
37
+ // images
38
+ const images = val.filter(item => item.type == 'Image' || item.type == 'ImageURL' || item.type == 'ImageFile');
39
+ // buttons
40
+ const buttons = val.filter(item => item.type == 'BT.group');
41
+ // markdown
42
+ const mds = val.filter(item => item.type == 'Markdown');
43
+ // text
44
+ const content = val
45
+ .filter(item => item.type == 'Mention' || item.type == 'Text' || item.type == 'Link')
46
+ .map(item => {
47
+ if (item.type == 'Link') {
48
+ return `[${item.value}](${item?.options?.link ?? item.value})`;
65
49
  }
66
- else if (item.options?.style == 'italic') {
67
- return `*${item.value}*`;
50
+ else if (item.type == 'Mention') {
51
+ if (item.value == 'everyone' ||
52
+ item.value == 'all' ||
53
+ item.value == '' ||
54
+ typeof item.value != 'string') {
55
+ return `<@everyone>`;
56
+ }
57
+ if (item.options?.belong == 'user') {
58
+ return `<@${item.value}>`;
59
+ }
60
+ else if (item.options?.belong == 'channel') {
61
+ return `<#${item.value}>`;
62
+ }
63
+ return '';
68
64
  }
69
- else if (item.options?.style == 'bold') {
70
- return `**${item.value}**`;
65
+ else if (item.type == 'Text') {
66
+ if (item.options?.style == 'block') {
67
+ return `\`${item.value}\``;
68
+ }
69
+ else if (item.options?.style == 'italic') {
70
+ return `*${item.value}*`;
71
+ }
72
+ else if (item.options?.style == 'bold') {
73
+ return `**${item.value}**`;
74
+ }
75
+ else if (item.options?.style == 'strikethrough') {
76
+ return `~~${item.value}~~`;
77
+ }
78
+ return item.value;
71
79
  }
72
- else if (item.options?.style == 'strikethrough') {
73
- return `~~${item.value}~~`;
80
+ })
81
+ .join('');
82
+ if (images.length > 0) {
83
+ let bufferData = null;
84
+ for (let i = 0; i < images.length; i++) {
85
+ if (bufferData)
86
+ break;
87
+ const item = images[i];
88
+ if (item.type == 'Image') {
89
+ bufferData = Buffer.from(item.value, 'base64');
90
+ }
91
+ else if (item.type == 'ImageURL') {
92
+ const res = await ImageURLToBuffer(item.value);
93
+ bufferData = res;
94
+ }
95
+ else if (item.type == 'ImageFile') {
96
+ bufferData = readFileSync(item.value);
97
+ }
74
98
  }
75
- return item.value;
99
+ const res = await client.channelsMessagesForm(channel_id, {
100
+ content: content
101
+ }, bufferData);
102
+ return [createResult(ResultCode.Ok, '完成', res)];
76
103
  }
77
- })
78
- .join('');
79
- if (images.length > 0) {
80
- let isText = false;
81
- return Promise.all(images.map(async (item) => {
82
- // content
83
- let text = null;
84
- if (!isText) {
85
- text = content;
86
- }
87
- isText = true;
88
- if (item.type == 'Image') {
89
- return client.channelsMessagesImage(channel_id, item.value, { content: text });
90
- }
91
- else if (item.type == 'ImageURL') {
92
- return client.channelsMessagesImage(channel_id, await ImageURLToBuffer(item.value), {
93
- content: text
94
- });
95
- }
96
- else if (item.type == 'ImageFile') {
97
- const data = readFileSync(item.value);
98
- return client.channelsMessagesImage(channel_id, data, { content: text });
99
- }
100
- }));
101
- }
102
- if (buttons && buttons.length > 0) {
103
- return Promise.all(buttons.map(async (item) => {
104
- const rows = item.value;
105
- // 构造成按钮
106
- const data = createButtonsData(rows);
107
- const res = await client.channelsMessages(channel_id, {
108
- content: content,
109
- components: data
104
+ // discord 的 md。会替代掉 content
105
+ let contentMd = '';
106
+ if (mds && mds.length > 0) {
107
+ mds.forEach(item => {
108
+ if (item.type == 'Markdown') {
109
+ const md = item.value;
110
+ md.forEach(line => {
111
+ if (line.type == 'MD.text') {
112
+ // 普通文本
113
+ contentMd += line.value;
114
+ }
115
+ else if (line.type === 'MD.blockquote') {
116
+ // 引用
117
+ contentMd += `> ${line.value}\n`;
118
+ }
119
+ else if (line.type === 'MD.bold') {
120
+ // 粗体
121
+ contentMd += `**${line.value}**`;
122
+ }
123
+ else if (line.type === 'MD.italic') {
124
+ // 斜体
125
+ contentMd += `*${line.value}*`;
126
+ }
127
+ else if (line.type === 'MD.divider') {
128
+ // 分割线
129
+ contentMd += `---\n`;
130
+ }
131
+ else if (line.type === 'MD.image') {
132
+ // 图片
133
+ contentMd += `![${line.value}](${line.value})`;
134
+ }
135
+ else if (line.type === 'MD.italicStar') {
136
+ // 星号斜体
137
+ contentMd += `*${line.value}*`;
138
+ }
139
+ else if (line.type === 'MD.link') {
140
+ // 链接
141
+ contentMd += `[${line.value}](${line.value})`;
142
+ }
143
+ else if (line.type === 'MD.list') {
144
+ const listStr = line.value.map(listItem => {
145
+ // 有序
146
+ if (typeof listItem.value === 'object') {
147
+ return `\n${listItem.value.index}. ${listItem.value.text}`;
148
+ }
149
+ return `\n- ${listItem.value}`;
150
+ });
151
+ contentMd += `${listStr}\n`;
152
+ }
153
+ else if (line.type === 'MD.newline') {
154
+ // 换行
155
+ contentMd += `\n`;
156
+ }
157
+ else if (line.type === 'MD.strikethrough') {
158
+ // 删除线
159
+ contentMd += `~~${line.value}~~`;
160
+ }
161
+ else if (line.type === 'MD.subtitle') {
162
+ // 副标题
163
+ contentMd += `## ${line.value}\n`;
164
+ }
165
+ else if (line.type === 'MD.title') {
166
+ // 标题
167
+ contentMd += `# ${line.value}\n`;
168
+ }
169
+ else if (line.type === 'MD.code') {
170
+ // 代码块
171
+ const language = line?.options?.language || '';
172
+ contentMd += `\`\`\`${language}\n${line.value}\n\`\`\`\n`;
173
+ }
174
+ else {
175
+ const value = line['value'] || '';
176
+ contentMd += String(value);
177
+ }
178
+ });
179
+ }
180
+ });
181
+ }
182
+ if (buttons && buttons.length > 0) {
183
+ let components = null;
184
+ buttons.forEach(item => {
185
+ if (components)
186
+ return;
187
+ const rows = item.value;
188
+ // 构造成按钮
189
+ components = createButtonsData(rows);
110
190
  });
111
- return createResult(ResultCode.Ok, 'client.groupOpenMessages', {
112
- id: res.id
191
+ const res = await client.channelsMessagesForm(channel_id, {
192
+ content: contentMd || content,
193
+ components: components
113
194
  });
114
- })).catch(err => [
115
- createResult(ResultCode.Fail, err?.response?.data ?? err?.message ?? err, null)
116
- ]);
195
+ return [createResult(ResultCode.Ok, '完成', res)];
196
+ }
197
+ if (content) {
198
+ const res = await client.channelsMessagesForm(channel_id, {
199
+ content: contentMd || content
200
+ });
201
+ return [createResult(ResultCode.Ok, '完成', res)];
202
+ }
203
+ return [];
117
204
  }
118
- if (content) {
119
- return Promise.all([content].map(item => client.channelsMessages(channel_id, {
120
- content: item
121
- })));
205
+ catch (err) {
206
+ return [createResult(ResultCode.Fail, err?.response?.data ?? err?.message ?? err, null)];
122
207
  }
123
- return Promise.all([]);
124
208
  };
125
209
  const senduser = async (client, param, val) => {
126
210
  if (val.length < 0)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@alemonjs/discord",
3
- "version": "2.1.0-alpha.0",
4
- "description": "阿柠檬discord平台连接",
3
+ "version": "2.1.0-alpha.12",
4
+ "description": "discord platform connection",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",
7
7
  "type": "module",
@@ -18,9 +18,16 @@
18
18
  "./package": "./package.json",
19
19
  "./desktop": "./lib/desktop.js"
20
20
  },
21
+ "dependencies": {
22
+ "http-proxy-agent": "^7.0.2",
23
+ "https-proxy-agent": "^7"
24
+ },
21
25
  "devDependencies": {
22
26
  "@types/qrcode": "^1.5.5"
23
27
  },
28
+ "peerDependencies": {
29
+ "alemonjs": "^2.1.0-alpha.15"
30
+ },
24
31
  "alemonjs": {
25
32
  "desktop": {
26
33
  "platform": [
package/lib/sdk/config.js DELETED
@@ -1,10 +0,0 @@
1
- import { BaseConfig } from './core/config.js';
2
- import { AvailableIntentsEventsEnum } from './types.js';
3
-
4
- const config = new BaseConfig({
5
- token: '',
6
- intent: AvailableIntentsEventsEnum,
7
- shard: [0, 1]
8
- });
9
-
10
- export { config };
@@ -1,46 +0,0 @@
1
- /**
2
- * 基础配置结构
3
- */
4
- class BaseConfig {
5
- #data = null;
6
- constructor(val) {
7
- this.#data = val;
8
- }
9
- /**
10
- * 设置配置
11
- * @param key
12
- * @param val
13
- */
14
- set(key, val) {
15
- if (val !== undefined)
16
- this.#data[key] = val;
17
- return this;
18
- }
19
- /**
20
- *
21
- * @param key
22
- * @returns
23
- */
24
- has(key) {
25
- if (Object.prototype.hasOwnProperty.call(this.#data, key)) ;
26
- return false;
27
- }
28
- /**
29
- * 读取配置
30
- * @param key
31
- * @returns
32
- */
33
- all() {
34
- return this.#data;
35
- }
36
- /**
37
- * 读取配置
38
- * @param key
39
- * @returns
40
- */
41
- get(key) {
42
- return this.#data[key];
43
- }
44
- }
45
-
46
- export { BaseConfig };
@@ -1,11 +0,0 @@
1
- /**
2
- * 子频道
3
- * @param event
4
- */
5
- type CHANNEL_TOPIC_UPDATE_TYPE = {
6
- topic: null
7
- id: string
8
- guild_id: string
9
- }
10
-
11
- export type { CHANNEL_TOPIC_UPDATE_TYPE }