@alemonjs/kook 0.0.3 → 0.1.1

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,168 @@
1
+ /**
2
+ * api枚举
3
+ */
4
+ var ApiEnum;
5
+ (function (ApiEnum) {
6
+ /**
7
+ * ********
8
+ * 频道相关
9
+ * ********
10
+ */
11
+ ApiEnum["GuildList"] = "/api/v3/guild/list";
12
+ ApiEnum["GuildView"] = "/api/v3/guild/view";
13
+ ApiEnum["GuildUserList"] = "/api/v3/guild/user-list";
14
+ ApiEnum["GuildNickname"] = "/api/v3/guild/nickname";
15
+ ApiEnum["GuildLeave"] = "/api/v3/guild/leave";
16
+ ApiEnum["GuildKickout"] = "/api/v3/guild/kickout";
17
+ ApiEnum["GuildMuteList"] = "/api/v3/guild-mute/list";
18
+ ApiEnum["GuildMuteCreate"] = "/api/v3/guild-mute/create";
19
+ ApiEnum["GuildMuteDelete"] = "/api/v3/guild-mute/delete";
20
+ ApiEnum["GuildBoostHistory"] = "/api/v3/guild-boost/history";
21
+ /**
22
+ * *******
23
+ * 子频道接口
24
+ * ******
25
+ */
26
+ ApiEnum["ChannelMessage"] = "/api/v3/channel/message";
27
+ ApiEnum["ChannelList"] = "/api/v3/channel/list";
28
+ ApiEnum["ChannelView"] = "/api/v3/channel/view";
29
+ ApiEnum["ChannelCreate"] = "/api/v3/channel/create";
30
+ ApiEnum["ChannelUpdate"] = "/api/v3/channel/update";
31
+ ApiEnum["ChannelDelete"] = "/api/v3/channel/delete";
32
+ ApiEnum["ChannelUserList"] = "/api/v3/channel/user-list";
33
+ ApiEnum["ChannelMoveUser"] = "/api/v3/channel/move-user";
34
+ ApiEnum["ChannelRoleIndex"] = "/api/v3/channel-role/index";
35
+ ApiEnum["ChannelRoleCreate"] = "/api/v3/channel-role/create";
36
+ ApiEnum["ChannelRoleUpdate"] = "/api/v3/channel-role/update";
37
+ ApiEnum["ChannelRoleSync"] = "/api/v3/channel-role/sync";
38
+ ApiEnum["ChannelRoleDelete"] = "/api/v3/channel-role/delete";
39
+ /**
40
+ * ******
41
+ * 消息接口
42
+ * ****
43
+ */
44
+ ApiEnum["MessageList"] = "/api/v3/message/list";
45
+ ApiEnum["MessageView"] = "/api/v3/message/view";
46
+ ApiEnum["MessageCreate"] = "/api/v3/message/create";
47
+ ApiEnum["MessageUpdate"] = "/api/v3/message/update";
48
+ ApiEnum["MessageDelete"] = "/api/v3/message/delete";
49
+ ApiEnum["MessageReactionList"] = "/api/v3/message/reaction-list";
50
+ ApiEnum["MessageAddReaction"] = "/api/v3/message/add-reaction";
51
+ ApiEnum["MessageDeleteReaction"] = "/api/v3/message/delete-reaction";
52
+ /**
53
+ * *******
54
+ * 频道用户
55
+ * *******
56
+ */
57
+ ApiEnum["GetJoinedChannel"] = "/api/v3/channel-user/get-joined-channel";
58
+ /**
59
+ * *******
60
+ * 私聊会话
61
+ * *******
62
+ */
63
+ ApiEnum["UserChatList"] = "/api/v3/user-chat/list";
64
+ ApiEnum["UserChatView"] = "/api/v3/user-chat/view";
65
+ ApiEnum["UserChatCreate"] = "/api/v3/user-chat/create";
66
+ ApiEnum["UserChatDelete"] = "/api/v3/user-chat/delete";
67
+ /**
68
+ * *******
69
+ * 用户私聊
70
+ * *******
71
+ */
72
+ ApiEnum["DirectMessageList"] = "/api/v3/direct-message/list";
73
+ ApiEnum["DirectMessageView"] = "/api/v3/direct-message/view";
74
+ ApiEnum["DirectMessageCreate"] = "/api/v3/direct-message/create";
75
+ ApiEnum["DirectMessageUpdate"] = "/api/v3/direct-message/update";
76
+ ApiEnum["DirectMessageDelete"] = "/api/v3/direct-message/delete";
77
+ ApiEnum["DirectMessageReactionList"] = "/api/v3/direct-message/reaction-list";
78
+ ApiEnum["DirectMessageAddReaction"] = "/api/v3/direct-message/add-reaction";
79
+ ApiEnum["DirectMessageDeleteReaction"] = "/api/v3/direct-message/delete-reaction";
80
+ /**
81
+ * ******
82
+ * 用户接口
83
+ * ******
84
+ */
85
+ ApiEnum["UserMe"] = "/api/v3/user/me";
86
+ ApiEnum["UserView"] = "/api/v3/user/view";
87
+ ApiEnum["UserOffline"] = "/api/v3/user/offline";
88
+ /**
89
+ * *******
90
+ * 媒体接口
91
+ * *******
92
+ */
93
+ ApiEnum["AssetCreate"] = "/api/v3/asset/create";
94
+ /**
95
+ * *******
96
+ * 服务器角色权限相关接口列表
97
+ * *******
98
+ */
99
+ ApiEnum["GuildRoleList"] = "/api/v3/guild-role/list";
100
+ ApiEnum["GuildRoleCreate"] = "/api/v3/guild-role/create";
101
+ ApiEnum["GuildRoleUpdate"] = "/api/v3/guild-role/update";
102
+ ApiEnum["GuildRoleDelete"] = "/api/v3/guild-role/delete";
103
+ ApiEnum["GuildRoleGrant"] = "/api/v3/guild-role/grant";
104
+ ApiEnum["GuildRoleRevoke"] = "/api/v3/guild-role/revoke";
105
+ /**
106
+ * *******
107
+ * 亲密度相关接口列表
108
+ * *******
109
+ */
110
+ ApiEnum["IntimacyIndex"] = "/api/v3/intimacy/index";
111
+ ApiEnum["IntimacyUpdate"] = "/api/v3/intimacy/update";
112
+ /**
113
+ * *******
114
+ * 服务器表情相关接口
115
+ * *******
116
+ */
117
+ ApiEnum["GuildEmojiList"] = "/api/v3/guild-emoji/list";
118
+ ApiEnum["GuildEmojiCreate"] = "/api/v3/guild-emoji/create";
119
+ ApiEnum["GuildEmojiUpdate"] = "/api/v3/guild-emoji/update";
120
+ ApiEnum["GuildEmojiDelete"] = "/api/v3/guild-emoji/delete";
121
+ /**
122
+ * *******
123
+ * 邀请相关接口
124
+ * *******
125
+ */
126
+ ApiEnum["InviteList"] = "/api/v3/invite/list";
127
+ ApiEnum["InviteCreate"] = "/api/v3/invite/create";
128
+ ApiEnum["InviteDelete"] = "/api/v3/invite/delete";
129
+ /**
130
+ * *******
131
+ * 黑名单相关接口
132
+ * *******
133
+ */
134
+ ApiEnum["BlacklistList"] = "/api/v3/blacklist/list";
135
+ ApiEnum["BlacklistCreate"] = "/api/v3/blacklist/create";
136
+ ApiEnum["BlacklistDelete"] = "/api/v3/blacklist/delete";
137
+ /**
138
+ * *******
139
+ * Badge 相关文档
140
+ * *******
141
+ */
142
+ ApiEnum["BadgeGuild"] = "/api/v3/badge/guild";
143
+ /**
144
+ * *******
145
+ * 用户动态相关接口-游戏/进程/音乐
146
+ * *******
147
+ */
148
+ ApiEnum["GameList"] = "/api/v3/game";
149
+ ApiEnum["GameCreate"] = "/api/v3/game/create";
150
+ ApiEnum["GameUpdate"] = "/api/v3/game/update";
151
+ ApiEnum["GameDelete"] = "/api/v3/game/delete";
152
+ ApiEnum["GameActivity"] = "/api/v3/game/activity";
153
+ ApiEnum["GameDeleteActivity"] = "/api/v3/game/delete-activity";
154
+ /**
155
+ * *******
156
+ * Gateway
157
+ * *******
158
+ */
159
+ ApiEnum["OAuth2Token"] = "/api/oauth2/token";
160
+ /**
161
+ * *******
162
+ * OAuth2.0相关接口
163
+ * *******
164
+ */
165
+ ApiEnum["GatewayIndex"] = "/api/v3/gateway/index";
166
+ })(ApiEnum || (ApiEnum = {}));
167
+
168
+ export { ApiEnum };
@@ -0,0 +1,26 @@
1
+ import { KOOKOptions } from './wss.types.js';
2
+ import { KOOKAPI } from './api.js';
3
+ import { KOOKEventMap } from './message.js';
4
+
5
+ declare class KOOKClient extends KOOKAPI {
6
+ #private;
7
+ /**
8
+ *
9
+ * @param opstion
10
+ */
11
+ constructor(opstion: KOOKOptions);
12
+ /**
13
+ * 注册事件处理程序
14
+ * @param key 事件名称
15
+ * @param val 事件处理函数
16
+ */
17
+ on<T extends keyof KOOKEventMap>(key: T, val: (event: KOOKEventMap[T]) => any): this;
18
+ /**
19
+ * 使用获取到的网关连接地址建立 WebSocket 连接
20
+ * @param token
21
+ * @param conversation
22
+ */
23
+ connect(): Promise<void>;
24
+ }
25
+
26
+ export { KOOKClient };
@@ -0,0 +1,151 @@
1
+ import WebSocket from 'ws';
2
+ import { config } from './config.js';
3
+ import { KOOKAPI } from './api.js';
4
+ import { ConversationMap } from './conversation.js';
5
+
6
+ class KOOKClient extends KOOKAPI {
7
+ // 标记是否已连接
8
+ #isConnected = false;
9
+ // 存储 session Id
10
+ #sessionId = null;
11
+ // 存储最新的消息序号
12
+ #lastMessageSN = 0;
13
+ /**
14
+ *
15
+ * @param opstion
16
+ */
17
+ constructor(opstion) {
18
+ super();
19
+ config.set('token', opstion.token);
20
+ }
21
+ #ws;
22
+ #events = {};
23
+ /**
24
+ * 注册事件处理程序
25
+ * @param key 事件名称
26
+ * @param val 事件处理函数
27
+ */
28
+ on(key, val) {
29
+ this.#events[key] = val;
30
+ return this;
31
+ }
32
+ /**
33
+ * 使用获取到的网关连接地址建立 WebSocket 连接
34
+ * @param token
35
+ * @param conversation
36
+ */
37
+ async connect() {
38
+ // 请求url
39
+ const gatewayUrl = await this.gateway()
40
+ .then(res => res?.data?.url)
41
+ .catch(err => {
42
+ if (this.#events['ERROR'])
43
+ this.#events['ERROR'](err);
44
+ });
45
+ if (!gatewayUrl && gatewayUrl == '')
46
+ return;
47
+ // 建立连接
48
+ const map = {
49
+ 0: async ({ d, sn }) => {
50
+ /**
51
+ * 处理 EVENT 信令
52
+ * 包括按序处理消息和记录最新的消息序号
53
+ */
54
+ if (d && sn) {
55
+ if (sn === this.#lastMessageSN + 1) {
56
+ /**
57
+ * 消息序号正确
58
+ * 按序处理消息
59
+ */
60
+ this.#lastMessageSN = sn;
61
+ try {
62
+ if (d.channel_type == 'GROUP') {
63
+ const t = ConversationMap[d.type]['public'](d);
64
+ if (this.#events[t])
65
+ await this.#events[t](d);
66
+ }
67
+ else {
68
+ const t = ConversationMap[d.type]['direct'](d);
69
+ if (this.#events[t])
70
+ await this.#events[t](d);
71
+ }
72
+ }
73
+ catch (err) {
74
+ if (this.#events['ERROR'])
75
+ this.#events['ERROR'](err);
76
+ }
77
+ //
78
+ }
79
+ else if (sn > this.#lastMessageSN + 1) ;
80
+ /**
81
+ * 如果收到已处理过的消息序号
82
+ * 则直接丢弃
83
+ */
84
+ }
85
+ },
86
+ 1: ({ d }) => {
87
+ if (d && d.code === 0) {
88
+ console.info('[ws] ok');
89
+ this.#sessionId = d.session_id;
90
+ this.#isConnected = true;
91
+ }
92
+ else {
93
+ console.info('[ws] err');
94
+ }
95
+ },
96
+ 2: () => {
97
+ console.info('[ws] ping');
98
+ this.#ws.send(JSON.stringify({
99
+ s: 3
100
+ }));
101
+ },
102
+ 3: () => {
103
+ console.info('[ws] pong');
104
+ },
105
+ 4: () => {
106
+ console.info('[ws] resume');
107
+ },
108
+ 5: () => {
109
+ console.info('[ws] Connection failed, reconnect');
110
+ /**
111
+ * 处理 RECONNECT 信令
112
+ * 断开当前连接并进行重新连接
113
+ */
114
+ this.#isConnected = false;
115
+ this.#sessionId = null;
116
+ console.info('[ws] sessionId', this.#sessionId);
117
+ },
118
+ 6: () => {
119
+ console.info('[ws] resume ack');
120
+ }
121
+ };
122
+ this.#ws = new WebSocket(gatewayUrl);
123
+ this.#ws.on('open', () => {
124
+ console.info('[ws] open');
125
+ });
126
+ this.#ws.on('message', async (msg) => {
127
+ const message = JSON.parse(msg.toString('utf8'));
128
+ if (process.env.KOOK_WS == 'dev')
129
+ console.info('message', message);
130
+ if (map[message.s])
131
+ map[message.s](message);
132
+ });
133
+ // 心跳定时发送
134
+ setInterval(() => {
135
+ if (this.#isConnected) {
136
+ this.#ws.send(JSON.stringify({
137
+ s: 2,
138
+ sn: this.#lastMessageSN
139
+ }));
140
+ }
141
+ }, 30000);
142
+ this.#ws.on('close', () => {
143
+ console.error('[ws] close');
144
+ });
145
+ this.#ws.on('error', err => {
146
+ console.error('[ws] error', err);
147
+ });
148
+ }
149
+ }
150
+
151
+ export { KOOKClient };
@@ -0,0 +1,12 @@
1
+ /**
2
+ * ****
3
+ * ***
4
+ */
5
+ interface KOOKOptions {
6
+ /**
7
+ * 钥匙
8
+ */
9
+ token: string;
10
+ }
11
+
12
+ export type { KOOKOptions };
package/package.json CHANGED
@@ -1,14 +1,11 @@
1
1
  {
2
2
  "name": "@alemonjs/kook",
3
- "version": "0.0.3",
3
+ "version": "0.1.1",
4
4
  "description": "kook-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
  }