@alemonjs/discord 0.0.4 → 0.1.0

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 (28) hide show
  1. package/lib/index.d.ts +5 -1
  2. package/lib/index.js +24 -5
  3. package/lib/sdk/core/config.js +46 -0
  4. package/lib/sdk/core/utils/from.js +42 -0
  5. package/lib/sdk/platform/discord/sdk/api.d.ts +896 -0
  6. package/lib/sdk/platform/discord/sdk/api.js +1554 -0
  7. package/lib/sdk/platform/discord/sdk/config.js +29 -0
  8. package/lib/sdk/platform/discord/sdk/intents.js +36 -0
  9. package/lib/sdk/platform/discord/sdk/message/CHANNEL_TOPIC_UPDATE.d.ts +11 -0
  10. package/lib/sdk/platform/discord/sdk/message/CHANNEL_UPDATE.d.ts +48 -0
  11. package/lib/sdk/platform/discord/sdk/message/GUILD_MEMBER_ADD.d.ts +28 -0
  12. package/lib/sdk/platform/discord/sdk/message/GUILD_MEMBER_REMOVE.d.ts +18 -0
  13. package/lib/sdk/platform/discord/sdk/message/GUILD_MEMBER_UPDATE.d.ts +33 -0
  14. package/lib/sdk/platform/discord/sdk/message/MESSAGE_CREATE.d.ts +59 -0
  15. package/lib/sdk/platform/discord/sdk/message/MESSAGE_DELETE.d.ts +11 -0
  16. package/lib/sdk/platform/discord/sdk/message/MESSAGE_REACTION_ADD.d.ts +42 -0
  17. package/lib/sdk/platform/discord/sdk/message/MESSAGE_UPDATE.d.ts +56 -0
  18. package/lib/sdk/platform/discord/sdk/message/PRESENCE_UPDATE.d.ts +63 -0
  19. package/lib/sdk/platform/discord/sdk/message/READY.d.ts +9 -0
  20. package/lib/sdk/platform/discord/sdk/message/TYPING_START.d.ts +35 -0
  21. package/lib/sdk/platform/discord/sdk/message/VOICE_CHANNEL_STATUS_UPDATE.d.ts +10 -0
  22. package/lib/sdk/platform/discord/sdk/message/VOICE_STATE_UPDATE.d.ts +41 -0
  23. package/lib/sdk/platform/discord/sdk/message.d.ts +37 -0
  24. package/lib/sdk/platform/discord/sdk/types.d.ts +4 -0
  25. package/lib/sdk/platform/discord/sdk/wss.d.ts +27 -0
  26. package/lib/sdk/platform/discord/sdk/wss.js +201 -0
  27. package/lib/sdk/platform/discord/sdk/wss.types.d.ts +26 -0
  28. package/package.json +5 -7
@@ -0,0 +1,201 @@
1
+ import WebSocket from 'ws';
2
+ import { DCAPI } from './api.js';
3
+ import { config } from './config.js';
4
+ import { getIntents } from './intents.js';
5
+
6
+ class DCClient extends DCAPI {
7
+ #heartbeat_interval = 0;
8
+ #session_id = '';
9
+ #gateway_url = '';
10
+ #timeout_id = null;
11
+ #seq = null;
12
+ #ws;
13
+ /**
14
+ * 设置配置
15
+ * @param opstion
16
+ */
17
+ constructor(opstion) {
18
+ super();
19
+ config.set('intent', opstion.intent);
20
+ config.set('shard', opstion.shard);
21
+ config.set('token', opstion.token);
22
+ return this;
23
+ }
24
+ /**
25
+ * 连接确认
26
+ * @returns
27
+ */
28
+ #aut() {
29
+ const token = config.get('token');
30
+ const intent = config.get('intent');
31
+ const shard = config.get('shard');
32
+ return {
33
+ op: 2,
34
+ d: {
35
+ shard: shard,
36
+ // 验证token
37
+ token: `Bot ${token}`,
38
+ intents: getIntents(intent),
39
+ properties: {
40
+ os: process.platform,
41
+ browser: 'alemonjs',
42
+ device: 'alemonjs'
43
+ }
44
+ }
45
+ };
46
+ }
47
+ /**
48
+ * 重新确认
49
+ */
50
+ // #reAut() {
51
+ // const token = config.get('token')
52
+ // const c = {
53
+ // op: 6,
54
+ // d: {
55
+ // // 会话token
56
+ // token: token,
57
+ // session_id: this.#session_id,
58
+ // // 收到的最后一个序列号
59
+ // seq: this.#seq
60
+ // }
61
+ // }
62
+ // console.log('[ws] c', c)
63
+ // return c
64
+ // }
65
+ #events = {};
66
+ /**
67
+ * 注册事件处理程序
68
+ * @param key 事件名称
69
+ * @param val 事件处理函数
70
+ */
71
+ on(key, val) {
72
+ this.#events[key] = val;
73
+ return this;
74
+ }
75
+ /**
76
+ * 创建ws监听
77
+ * @param conversation
78
+ * @param shard
79
+ * @returns
80
+ */
81
+ async connect() {
82
+ // 清除序列号
83
+ this.#seq = null;
84
+ // 清除心跳
85
+ clearTimeout(this.#timeout_id);
86
+ // 获取网关
87
+ const url = await this.gateway()
88
+ .then(res => res?.url)
89
+ .catch(err => {
90
+ if (this.#events['ERROR'])
91
+ this.#events['ERROR'](err);
92
+ });
93
+ // 没有网关
94
+ if (!url) {
95
+ console.error('[ws] 无法获取网关');
96
+ return;
97
+ }
98
+ /**
99
+ * 心跳恢复
100
+ */
101
+ const call = async () => {
102
+ this.#ws.send(JSON.stringify({
103
+ op: 1, // op = 1
104
+ d: this.#seq // 如果是第一次连接,传null
105
+ }));
106
+ // 确保清除
107
+ clearTimeout(this.#timeout_id);
108
+ // 开始心跳
109
+ this.#timeout_id = setTimeout(call, this.#heartbeat_interval);
110
+ };
111
+ const map = {
112
+ /**
113
+ * 事件接收到
114
+ * @param param0
115
+ */
116
+ 0: async ({ d, t, s }) => {
117
+ if (s) {
118
+ // 序列号
119
+ this.#seq = s;
120
+ }
121
+ // 准备
122
+ if (t == 'READY') {
123
+ if (d?.resume_gateway_url) {
124
+ this.#gateway_url = d?.resume_gateway_url;
125
+ console.log('[ws] gateway_url', this.#gateway_url);
126
+ }
127
+ if (d?.session_id) {
128
+ this.#session_id = d?.session_id;
129
+ console.log('[ws] session_id', this.#session_id);
130
+ }
131
+ }
132
+ // 事件处理
133
+ if (this.#events[t]) {
134
+ try {
135
+ await this.#events[t](d);
136
+ }
137
+ catch (err) {
138
+ if (this.#events['ERROR'])
139
+ this.#events['ERROR'](err);
140
+ }
141
+ }
142
+ //
143
+ },
144
+ /**
145
+ * 重新连接
146
+ */
147
+ 7: () => {
148
+ console.info('[ws] 重新连接');
149
+ // this.#ws.send(JSON.stringify(this.#reAut()))
150
+ },
151
+ /**
152
+ * 无效会话
153
+ * @param message
154
+ */
155
+ 9: ({ d }) => {
156
+ console.error('[ws] 无效会话 ', d);
157
+ },
158
+ /**
159
+ * 你好
160
+ * @param param0
161
+ */
162
+ 10: ({ d }) => {
163
+ // 得到心跳间隔
164
+ this.#heartbeat_interval = d.heartbeat_interval;
165
+ // 开始心跳
166
+ call();
167
+ // 开启会话
168
+ this.#ws.send(JSON.stringify(this.#aut()));
169
+ },
170
+ /**
171
+ * 心跳确认
172
+ */
173
+ 11: () => {
174
+ console.info('[ws] 心跳确认');
175
+ }
176
+ };
177
+ this.#ws = new WebSocket(`${url}?v=10&encoding=json`);
178
+ this.#ws.on('open', async () => {
179
+ console.info('[ws] open');
180
+ });
181
+ // 消息
182
+ this.#ws.on('message', data => {
183
+ const message = JSON.parse(data.toString());
184
+ if (map[message.op])
185
+ map[message.op](message);
186
+ });
187
+ // 关闭
188
+ this.#ws.on('close', err => {
189
+ console.error('[ws] 连接已关闭', err);
190
+ console.log('[ws] 等待重连');
191
+ this.connect();
192
+ });
193
+ // 出错
194
+ this.#ws.on('error', err => {
195
+ console.error('[ws] error:', err);
196
+ });
197
+ ///
198
+ }
199
+ }
200
+
201
+ export { DCClient };
@@ -0,0 +1,26 @@
1
+ import { DCIntentsEnum } from './types.js';
2
+
3
+ /**
4
+ * ****
5
+ * discord
6
+ * ***
7
+ */
8
+ interface DISOCRDOptions {
9
+ /**
10
+ * 钥匙
11
+ */
12
+ token: string;
13
+ /**
14
+ * 订阅(有默认值)
15
+ * ******
16
+ */
17
+ intent?: DCIntentsEnum[];
18
+ /**
19
+ * 分片(有默认值)
20
+ * ******
21
+ * [0, 1]
22
+ */
23
+ shard?: number[];
24
+ }
25
+
26
+ export type { DISOCRDOptions };
package/package.json CHANGED
@@ -1,14 +1,11 @@
1
1
  {
2
2
  "name": "@alemonjs/discord",
3
- "version": "0.0.4",
3
+ "version": "0.1.0",
4
4
  "description": "discord-bot",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",
7
7
  "type": "module",
8
8
  "main": "lib/index.js",
9
- "dependencies": {
10
- "chat-space": "^0.0.6"
11
- },
12
9
  "types": "lib",
13
10
  "exports": {
14
11
  ".": {
@@ -23,11 +20,12 @@
23
20
  "chat-bot"
24
21
  ],
25
22
  "publishConfig": {
26
- "registry": "https://registry.npmjs.org"
23
+ "registry": "https://registry.npmjs.org",
24
+ "access": "public"
27
25
  },
28
- "bugs": "https://github.com/ningmengchongshui/alemonjs/issues",
26
+ "bugs": "https://github.com/lemonade-lab/alemonjs/issues",
29
27
  "repository": {
30
28
  "type": "git",
31
- "url": "https://github.com/ningmengchongshui/alemonjs.git"
29
+ "url": "https://github.com/lemonade-lab/alemonjs.git"
32
30
  }
33
31
  }