alemonjs 1.1.14 → 1.1.16
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/lib/discord/alemon/controller.js +2 -1
- package/lib/discord/alemon/reply.js +4 -16
- package/lib/discord/sdk/wss.js +18 -20
- package/lib/kook/alemon/controller.js +19 -16
- package/lib/kook/alemon/conversation.js +1 -1
- package/lib/kook/alemon/direct.js +15 -21
- package/lib/kook/alemon/reply.js +12 -18
- package/lib/kook/index.js +2 -2
- package/lib/kook/sdk/ws.js +32 -46
- package/lib/ntqq/alemon/group/controller.js +13 -8
- package/lib/ntqq/alemon/group/direct.js +37 -33
- package/lib/ntqq/alemon/group/reply.js +39 -45
- package/lib/ntqq/sdk/counter.js +15 -0
- package/lib/ntqq/sdk/wss.js +19 -26
- package/lib/one/index.js +1 -4
- package/lib/qq/alemon/controller.js +53 -23
- package/lib/qq/alemon/direct.js +18 -28
- package/lib/qq/alemon/reply.js +12 -24
- package/lib/villa/alemon/controller.js +11 -8
- package/lib/villa/alemon/conversation.js +1 -1
- package/lib/villa/alemon/reply.js +51 -56
- package/lib/villa/index.js +4 -6
- package/lib/villa/sdk/api.js +1 -14
- package/lib/villa/sdk/reply.js +38 -213
- package/lib/villa/sdk/wss.js +22 -20
- package/lib/villa/villa.js +0 -2
- package/package.json +3 -2
- package/types/config/login.d.ts +1 -1
- package/types/core/dealmsg.d.ts +1 -1
- package/types/discord/alemon/controller.d.ts +1 -1
- package/types/discord/sdk/wss.d.ts +3 -0
- package/types/kook/alemon/controller.d.ts +15 -119
- package/types/kook/alemon/conversation.d.ts +1 -1
- package/types/kook/alemon/direct.d.ts +1 -1
- package/types/kook/alemon/message/DIRECT_MESSAGE.d.ts +1 -1
- package/types/kook/alemon/message/GUILD_MESSAGE_REACTIONS.d.ts +1 -1
- package/types/kook/alemon/message/PUBLIC_GUILD_MESSAGES.d.ts +1 -1
- package/types/kook/sdk/ws.d.ts +3 -3
- package/types/ntqq/alemon/group/controller.d.ts +5 -17
- package/types/ntqq/alemon/group/direct.d.ts +1 -1
- package/types/ntqq/sdk/counter.d.ts +7 -0
- package/types/one/alemon/message/DIRECT_MESSAGE.d.ts +1 -1
- package/types/one/alemon/message/MESSAGES.d.ts +1 -1
- package/types/qq/alemon/controller.d.ts +11 -11
- package/types/qq/alemon/direct.d.ts +1 -1
- package/types/villa/alemon/conversation.d.ts +1 -1
- package/types/villa/sdk/api.d.ts +2 -15
- package/types/villa/sdk/reply.d.ts +15 -58
- package/types/villa/sdk/types.d.ts +39 -10
- package/types/villa/sdk/utils.d.ts +1 -1
- package/types/villa/sdk/wss.d.ts +24 -1
- package/types/villa/villa.d.ts +0 -10
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { everyoneError } from '../../log/index.js';
|
|
1
2
|
import { replyController } from './reply.js';
|
|
2
3
|
export const Controller = {
|
|
3
4
|
Member: ({ guild_id, user_id, channel_id }) => {
|
|
@@ -37,7 +38,7 @@ export const Controller = {
|
|
|
37
38
|
Message: ({ guild_id, channel_id, msg_id, open_id, user_id }) => {
|
|
38
39
|
return {
|
|
39
40
|
reply: async (content) => {
|
|
40
|
-
return await replyController(content, channel_id);
|
|
41
|
+
return await replyController(content, channel_id).catch(everyoneError);
|
|
41
42
|
},
|
|
42
43
|
quote: async (content) => {
|
|
43
44
|
return false;
|
|
@@ -10,13 +10,7 @@ import { everyoneError } from '../../log/index.js';
|
|
|
10
10
|
*/
|
|
11
11
|
export async function replyController(msg, channel_id, select) {
|
|
12
12
|
if (Buffer.isBuffer(msg)) {
|
|
13
|
-
|
|
14
|
-
return await Client.channelsMessagesImage(channel_id, msg);
|
|
15
|
-
}
|
|
16
|
-
catch (err) {
|
|
17
|
-
console.error(err);
|
|
18
|
-
return err;
|
|
19
|
-
}
|
|
13
|
+
return await Client.channelsMessagesImage(channel_id, msg).catch(everyoneError);
|
|
20
14
|
}
|
|
21
15
|
// arr & find buffer
|
|
22
16
|
if (Array.isArray(msg) && msg.find(item => Buffer.isBuffer(item))) {
|
|
@@ -29,13 +23,7 @@ export async function replyController(msg, channel_id, select) {
|
|
|
29
23
|
})
|
|
30
24
|
.filter(element => typeof element === 'string')
|
|
31
25
|
.join('');
|
|
32
|
-
|
|
33
|
-
return await Client.channelsMessagesImage(channel_id, msg[isBuffer], cont);
|
|
34
|
-
}
|
|
35
|
-
catch (err) {
|
|
36
|
-
console.error(err);
|
|
37
|
-
return err;
|
|
38
|
-
}
|
|
26
|
+
return await Client.channelsMessagesImage(channel_id, msg[isBuffer], cont).catch(everyoneError);
|
|
39
27
|
}
|
|
40
28
|
const content = Array.isArray(msg)
|
|
41
29
|
? msg.join('')
|
|
@@ -49,9 +37,9 @@ export async function replyController(msg, channel_id, select) {
|
|
|
49
37
|
const match = content.match(/<http>(.*?)<\/http>/);
|
|
50
38
|
if (match) {
|
|
51
39
|
const getUrl = match[1];
|
|
52
|
-
const msg = await getUrlbuffer(getUrl);
|
|
40
|
+
const msg = await getUrlbuffer(getUrl).catch(everyoneError);
|
|
53
41
|
if (msg) {
|
|
54
|
-
return await Client.channelsMessagesImage(channel_id, msg);
|
|
42
|
+
return await Client.channelsMessagesImage(channel_id, msg).catch(everyoneError);
|
|
55
43
|
}
|
|
56
44
|
}
|
|
57
45
|
return Client.channelsMessages(channel_id, {
|
package/lib/discord/sdk/wss.js
CHANGED
|
@@ -3,6 +3,9 @@ import { gateway } from './api.js';
|
|
|
3
3
|
import { getDISCORD } from './config.js';
|
|
4
4
|
/**
|
|
5
5
|
* 创建ws监听
|
|
6
|
+
* @param conversation
|
|
7
|
+
* @param shard
|
|
8
|
+
* @returns
|
|
6
9
|
*/
|
|
7
10
|
export async function createClient(conversation, shard = [0, 1]) {
|
|
8
11
|
const { url } = await gateway();
|
|
@@ -15,7 +18,7 @@ export async function createClient(conversation, shard = [0, 1]) {
|
|
|
15
18
|
let session_id = '';
|
|
16
19
|
let resume_gateway_url = '';
|
|
17
20
|
wsConn.on('open', async () => {
|
|
18
|
-
|
|
21
|
+
console.log('[ws] open');
|
|
19
22
|
});
|
|
20
23
|
const call = async () => {
|
|
21
24
|
wsConn.send(JSON.stringify({
|
|
@@ -36,7 +39,7 @@ export async function createClient(conversation, shard = [0, 1]) {
|
|
|
36
39
|
}
|
|
37
40
|
},
|
|
38
41
|
7: () => {
|
|
39
|
-
console.info('重新请求');
|
|
42
|
+
console.info('[ws] 重新请求');
|
|
40
43
|
wsConn.send(JSON.stringify({
|
|
41
44
|
op: 6,
|
|
42
45
|
d: {
|
|
@@ -48,7 +51,7 @@ export async function createClient(conversation, shard = [0, 1]) {
|
|
|
48
51
|
},
|
|
49
52
|
9: message => {
|
|
50
53
|
// 6 或 2 失败
|
|
51
|
-
console.info('parameter error', message);
|
|
54
|
+
console.info('[ws] parameter error', message);
|
|
52
55
|
},
|
|
53
56
|
/**
|
|
54
57
|
* 打招呼
|
|
@@ -57,12 +60,14 @@ export async function createClient(conversation, shard = [0, 1]) {
|
|
|
57
60
|
10: ({ d }) => {
|
|
58
61
|
const { heartbeat_interval: ih } = d;
|
|
59
62
|
heartbeat_interval = ih;
|
|
63
|
+
//
|
|
60
64
|
wsConn.send(JSON.stringify({
|
|
61
65
|
op: 1,
|
|
62
66
|
d: null
|
|
63
67
|
}));
|
|
64
68
|
setTimeout(call, heartbeat_interval);
|
|
65
|
-
|
|
69
|
+
// 在初次握手期间启动新会话
|
|
70
|
+
wsConn.send(JSON.stringify({
|
|
66
71
|
op: 2,
|
|
67
72
|
d: {
|
|
68
73
|
shard: shard,
|
|
@@ -74,36 +79,29 @@ export async function createClient(conversation, shard = [0, 1]) {
|
|
|
74
79
|
device: 'alemonjs'
|
|
75
80
|
}
|
|
76
81
|
}
|
|
77
|
-
};
|
|
78
|
-
if (process.argv.includes('dev')) {
|
|
79
|
-
console.info('data', data);
|
|
80
|
-
}
|
|
81
|
-
// 在初次握手期间启动新会话
|
|
82
|
-
wsConn.send(JSON.stringify(data));
|
|
82
|
+
}));
|
|
83
83
|
},
|
|
84
84
|
11: ({ d }) => {
|
|
85
|
-
console.info('heartbeat transmission');
|
|
85
|
+
console.info('[ws] heartbeat transmission');
|
|
86
86
|
}
|
|
87
87
|
};
|
|
88
88
|
wsConn.on('message', data => {
|
|
89
|
-
const
|
|
90
|
-
const
|
|
91
|
-
if (process.
|
|
92
|
-
console.info('
|
|
89
|
+
const parsedData = JSON.parse(data.toString());
|
|
90
|
+
const { op, d } = parsedData;
|
|
91
|
+
if (process.env?.DISCORD_WS == 'dev') {
|
|
92
|
+
console.info('data', d);
|
|
93
93
|
}
|
|
94
|
-
const { op } = parsedData;
|
|
95
94
|
if (Object.prototype.hasOwnProperty.call(map, op)) {
|
|
96
95
|
map[op](parsedData);
|
|
97
96
|
}
|
|
98
97
|
});
|
|
99
98
|
// 关闭
|
|
100
|
-
wsConn.on('close',
|
|
101
|
-
console.error('ws
|
|
102
|
-
console.error('登录失败,TOKEN存在风险');
|
|
99
|
+
wsConn.on('close', err => {
|
|
100
|
+
console.error('[ws] 登录失败,TOKEN存在风险');
|
|
103
101
|
});
|
|
104
102
|
// 出错
|
|
105
103
|
wsConn.on('error', error => {
|
|
106
|
-
console.error('ws error:', error);
|
|
104
|
+
console.error('[ws] error:', error);
|
|
107
105
|
});
|
|
108
106
|
}
|
|
109
107
|
/**
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { ClientKOOK } from '../sdk/index.js';
|
|
2
2
|
import { replyController } from './reply.js';
|
|
3
3
|
import { getBotConfigByKey } from '../../config/index.js';
|
|
4
|
+
import { everyoneError } from '../../log/index.js';
|
|
4
5
|
export const Controller = {
|
|
5
6
|
Member: ({ guild_id, user_id }) => {
|
|
6
7
|
return {
|
|
7
8
|
information: async () => {
|
|
8
|
-
const data = await ClientKOOK.userView(guild_id, user_id)
|
|
9
|
+
const data = await ClientKOOK.userView(guild_id, user_id)
|
|
10
|
+
.then(res => res.data)
|
|
11
|
+
.catch(everyoneError);
|
|
9
12
|
if (data) {
|
|
10
13
|
const cfg = getBotConfigByKey('qq');
|
|
11
14
|
const masterID = cfg.masterID;
|
|
@@ -26,7 +29,7 @@ export const Controller = {
|
|
|
26
29
|
return false;
|
|
27
30
|
},
|
|
28
31
|
remove: async () => {
|
|
29
|
-
return await ClientKOOK.guildKickout(guild_id, user_id);
|
|
32
|
+
return await ClientKOOK.guildKickout(guild_id, user_id).catch(everyoneError);
|
|
30
33
|
},
|
|
31
34
|
operate: async (role_id, add = true) => {
|
|
32
35
|
return false;
|
|
@@ -36,16 +39,16 @@ export const Controller = {
|
|
|
36
39
|
Message: ({ channel_id, user_id, msg_id }) => {
|
|
37
40
|
return {
|
|
38
41
|
reply: async (content) => {
|
|
39
|
-
return await replyController(content, channel_id);
|
|
42
|
+
return await replyController(content, channel_id).catch(everyoneError);
|
|
40
43
|
},
|
|
41
44
|
quote: async (content) => {
|
|
42
|
-
return await replyController(content, channel_id);
|
|
45
|
+
return await replyController(content, channel_id).catch(everyoneError);
|
|
43
46
|
},
|
|
44
47
|
update: async (content) => {
|
|
45
|
-
return await ClientKOOK.messageUpdate({ msg_id, content });
|
|
48
|
+
return await ClientKOOK.messageUpdate({ msg_id, content }).catch(everyoneError);
|
|
46
49
|
},
|
|
47
50
|
withdraw: async (hideTip) => {
|
|
48
|
-
return await ClientKOOK.messageDelete(msg_id);
|
|
51
|
+
return await ClientKOOK.messageDelete(msg_id).catch(everyoneError);
|
|
49
52
|
},
|
|
50
53
|
pinning: async (cancel) => {
|
|
51
54
|
return false;
|
|
@@ -64,12 +67,12 @@ export const Controller = {
|
|
|
64
67
|
msg_id,
|
|
65
68
|
emoji: item,
|
|
66
69
|
user_id
|
|
67
|
-
}));
|
|
70
|
+
}).catch(everyoneError));
|
|
68
71
|
}
|
|
69
72
|
return arr;
|
|
70
73
|
}
|
|
71
74
|
for (const item of msg) {
|
|
72
|
-
arr.push(await ClientKOOK.messageAddReaction({ msg_id, emoji: item }));
|
|
75
|
+
arr.push(await ClientKOOK.messageAddReaction({ msg_id, emoji: item }).catch(everyoneError));
|
|
73
76
|
}
|
|
74
77
|
return arr;
|
|
75
78
|
},
|
|
@@ -79,16 +82,16 @@ export const Controller = {
|
|
|
79
82
|
type: 3,
|
|
80
83
|
target_id: channel_id,
|
|
81
84
|
content: file
|
|
82
|
-
});
|
|
85
|
+
}).catch(everyoneError);
|
|
83
86
|
}
|
|
84
|
-
const ret = await ClientKOOK.postFile(file, name);
|
|
87
|
+
const ret = await ClientKOOK.postFile(file, name).catch(everyoneError);
|
|
85
88
|
if (!ret)
|
|
86
89
|
return false;
|
|
87
90
|
return await ClientKOOK.createMessage({
|
|
88
91
|
type: 3,
|
|
89
92
|
target_id: channel_id,
|
|
90
93
|
content: ret.data.url
|
|
91
|
-
});
|
|
94
|
+
}).catch(everyoneError);
|
|
92
95
|
},
|
|
93
96
|
video: async (file, name) => {
|
|
94
97
|
if (typeof file == 'string') {
|
|
@@ -96,16 +99,16 @@ export const Controller = {
|
|
|
96
99
|
type: 3,
|
|
97
100
|
target_id: channel_id,
|
|
98
101
|
content: file
|
|
99
|
-
});
|
|
102
|
+
}).catch(everyoneError);
|
|
100
103
|
}
|
|
101
|
-
const ret = await ClientKOOK.postFile(file, name);
|
|
104
|
+
const ret = await ClientKOOK.postFile(file, name).catch(everyoneError);
|
|
102
105
|
if (!ret)
|
|
103
106
|
return false;
|
|
104
107
|
return await ClientKOOK.createMessage({
|
|
105
108
|
type: 3,
|
|
106
109
|
target_id: channel_id,
|
|
107
110
|
content: ret.data.url
|
|
108
|
-
});
|
|
111
|
+
}).catch(everyoneError);
|
|
109
112
|
},
|
|
110
113
|
card: async (msg) => {
|
|
111
114
|
return [
|
|
@@ -113,7 +116,7 @@ export const Controller = {
|
|
|
113
116
|
type: 10,
|
|
114
117
|
target_id: channel_id,
|
|
115
118
|
content: JSON.stringify(msg)
|
|
116
|
-
})
|
|
119
|
+
}).catch(everyoneError)
|
|
117
120
|
];
|
|
118
121
|
},
|
|
119
122
|
allUsers: async (emoji, options = {
|
|
@@ -121,7 +124,7 @@ export const Controller = {
|
|
|
121
124
|
limit: 20
|
|
122
125
|
}) => {
|
|
123
126
|
// 该消息下 指定emoji的所有用户
|
|
124
|
-
return await ClientKOOK.messageReactionList({ msg_id, emoji });
|
|
127
|
+
return await ClientKOOK.messageReactionList({ msg_id, emoji }).catch(everyoneError);
|
|
125
128
|
},
|
|
126
129
|
article: async (msg) => {
|
|
127
130
|
return false;
|
|
@@ -128,7 +128,7 @@ const ConversationMap = {
|
|
|
128
128
|
* @param req
|
|
129
129
|
* @param res
|
|
130
130
|
*/
|
|
131
|
-
export async function
|
|
131
|
+
export async function conversation(event) {
|
|
132
132
|
if (Object.prototype.hasOwnProperty.call(ConversationMap, event.type)) {
|
|
133
133
|
if (event.channel_type == 'GROUP') {
|
|
134
134
|
return await ConversationMap[event.type]['public'](event);
|
|
@@ -43,10 +43,10 @@ const Controller = {
|
|
|
43
43
|
Message: ({ open_id, channel_id }) => {
|
|
44
44
|
return {
|
|
45
45
|
reply: async (content) => {
|
|
46
|
-
return await directController(content, channel_id, open_id);
|
|
46
|
+
return await directController(content, channel_id, open_id).catch(everyoneError);
|
|
47
47
|
},
|
|
48
48
|
quote: async (content) => {
|
|
49
|
-
return await directController(content, channel_id, open_id);
|
|
49
|
+
return await directController(content, channel_id, open_id).catch(everyoneError);
|
|
50
50
|
},
|
|
51
51
|
/**
|
|
52
52
|
* 更新信息
|
|
@@ -138,22 +138,16 @@ export async function directController(msg, channel_id, open_id) {
|
|
|
138
138
|
* isbuffer
|
|
139
139
|
*/
|
|
140
140
|
if (Buffer.isBuffer(msg)) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}).catch(everyoneError);
|
|
150
|
-
}
|
|
151
|
-
return false;
|
|
152
|
-
}
|
|
153
|
-
catch (err) {
|
|
154
|
-
console.error(err);
|
|
155
|
-
return err;
|
|
141
|
+
const ret = await ClientKOOK.postImage(msg).catch(everyoneError);
|
|
142
|
+
if (ret && ret.data) {
|
|
143
|
+
return await ClientKOOK.createDirectMessage({
|
|
144
|
+
type: 2,
|
|
145
|
+
target_id: channel_id,
|
|
146
|
+
chat_code: open_id,
|
|
147
|
+
content: ret.data.url
|
|
148
|
+
}).catch(everyoneError);
|
|
156
149
|
}
|
|
150
|
+
return false;
|
|
157
151
|
}
|
|
158
152
|
/**
|
|
159
153
|
* string[] arr and find buffer
|
|
@@ -171,7 +165,7 @@ export async function directController(msg, channel_id, open_id) {
|
|
|
171
165
|
.filter(element => typeof element === 'string')
|
|
172
166
|
.join('');
|
|
173
167
|
// 转存
|
|
174
|
-
const ret = await ClientKOOK.postImage(msg[isBuffer]);
|
|
168
|
+
const ret = await ClientKOOK.postImage(msg[isBuffer]).catch(everyoneError);
|
|
175
169
|
if (!ret)
|
|
176
170
|
return false;
|
|
177
171
|
// 私聊
|
|
@@ -180,7 +174,7 @@ export async function directController(msg, channel_id, open_id) {
|
|
|
180
174
|
target_id: channel_id,
|
|
181
175
|
chat_code: open_id,
|
|
182
176
|
content: content
|
|
183
|
-
});
|
|
177
|
+
}).catch(everyoneError);
|
|
184
178
|
return await ClientKOOK.createDirectMessage({
|
|
185
179
|
type: 2,
|
|
186
180
|
target_id: channel_id,
|
|
@@ -200,10 +194,10 @@ export async function directController(msg, channel_id, open_id) {
|
|
|
200
194
|
const match = content.match(/<http>(.*?)<\/http>/);
|
|
201
195
|
if (match) {
|
|
202
196
|
const getUrl = match[1];
|
|
203
|
-
const msg = await getUrlbuffer(getUrl);
|
|
197
|
+
const msg = await getUrlbuffer(getUrl).catch(everyoneError);
|
|
204
198
|
if (!msg)
|
|
205
199
|
return false;
|
|
206
|
-
const ret = await ClientKOOK.postImage(msg);
|
|
200
|
+
const ret = await ClientKOOK.postImage(msg).catch(everyoneError);
|
|
207
201
|
if (!ret)
|
|
208
202
|
return false;
|
|
209
203
|
if (msg && ret) {
|
package/lib/kook/alemon/reply.js
CHANGED
|
@@ -13,21 +13,15 @@ export async function replyController(msg, channel_id) {
|
|
|
13
13
|
* isbuffer
|
|
14
14
|
*/
|
|
15
15
|
if (Buffer.isBuffer(msg)) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}).catch(everyoneError);
|
|
24
|
-
}
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
catch (err) {
|
|
28
|
-
console.error(err);
|
|
29
|
-
return err;
|
|
16
|
+
const ret = await ClientKOOK.postImage(msg).catch(everyoneError);
|
|
17
|
+
if (ret && ret.data) {
|
|
18
|
+
return await ClientKOOK.createMessage({
|
|
19
|
+
type: 2,
|
|
20
|
+
target_id: channel_id,
|
|
21
|
+
content: ret.data.url
|
|
22
|
+
}).catch(everyoneError);
|
|
30
23
|
}
|
|
24
|
+
return false;
|
|
31
25
|
}
|
|
32
26
|
/**
|
|
33
27
|
* string[] arr and find buffer
|
|
@@ -45,7 +39,7 @@ export async function replyController(msg, channel_id) {
|
|
|
45
39
|
.filter(element => typeof element === 'string')
|
|
46
40
|
.join('');
|
|
47
41
|
// 转存
|
|
48
|
-
const ret = await ClientKOOK.postImage(msg[isBuffer]);
|
|
42
|
+
const ret = await ClientKOOK.postImage(msg[isBuffer]).catch(everyoneError);
|
|
49
43
|
if (!ret)
|
|
50
44
|
return false;
|
|
51
45
|
if (ret?.data) {
|
|
@@ -53,7 +47,7 @@ export async function replyController(msg, channel_id) {
|
|
|
53
47
|
type: 9,
|
|
54
48
|
target_id: channel_id,
|
|
55
49
|
content: content
|
|
56
|
-
}).catch(
|
|
50
|
+
}).catch(everyoneError);
|
|
57
51
|
return await ClientKOOK.createMessage({
|
|
58
52
|
type: 2,
|
|
59
53
|
target_id: channel_id,
|
|
@@ -73,10 +67,10 @@ export async function replyController(msg, channel_id) {
|
|
|
73
67
|
const match = content.match(/<http>(.*?)<\/http>/);
|
|
74
68
|
if (match) {
|
|
75
69
|
const getUrl = match[1];
|
|
76
|
-
const msg = await getUrlbuffer(getUrl);
|
|
70
|
+
const msg = await getUrlbuffer(getUrl).catch(everyoneError);
|
|
77
71
|
if (!msg)
|
|
78
72
|
return false;
|
|
79
|
-
const ret = await ClientKOOK.postImage(msg);
|
|
73
|
+
const ret = await ClientKOOK.postImage(msg).catch(everyoneError);
|
|
80
74
|
if (!ret)
|
|
81
75
|
return false;
|
|
82
76
|
if (msg && ret) {
|
package/lib/kook/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { checkRobotByKOOK } from './login.js';
|
|
2
|
-
import {
|
|
2
|
+
import { conversation } from './alemon/conversation.js';
|
|
3
3
|
import { createClient, ClientKOOK } from './sdk/index.js';
|
|
4
4
|
import { setBotMsgByKOOK } from './alemon/bot.js';
|
|
5
5
|
import { getBotConfigByKey } from '../config/index.js';
|
|
@@ -22,7 +22,7 @@ export async function createAlemonByKOOK() {
|
|
|
22
22
|
/**
|
|
23
23
|
* 创建连接
|
|
24
24
|
*/
|
|
25
|
-
await createClient(cfg.token,
|
|
25
|
+
await createClient(cfg.token, conversation).then(async (res) => {
|
|
26
26
|
const data = await ClientKOOK.userMe().then(res => res?.data);
|
|
27
27
|
if (data) {
|
|
28
28
|
setBotMsgByKOOK({
|
package/lib/kook/sdk/ws.js
CHANGED
|
@@ -2,16 +2,14 @@ import WebSocket from 'ws';
|
|
|
2
2
|
import axios from 'axios';
|
|
3
3
|
import { setKookToken } from './config.js';
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* 获取鉴权
|
|
6
6
|
* @param token token
|
|
7
7
|
* @param url 请求地址
|
|
8
8
|
* @param compress 下发数据是否压缩,默认为1,代表压缩
|
|
9
9
|
* @returns
|
|
10
10
|
*/
|
|
11
11
|
export async function getGatewayUrl(token, url = 'https://www.kookapp.cn/api/v3/gateway/index', compress = 0) {
|
|
12
|
-
|
|
13
|
-
* 替换为实际的接口地址
|
|
14
|
-
*/
|
|
12
|
+
// 替换为实际的接口地址
|
|
15
13
|
const Authorization = `Bot ${token}`;
|
|
16
14
|
try {
|
|
17
15
|
const response = await axios.get(url, {
|
|
@@ -36,43 +34,30 @@ export async function getGatewayUrl(token, url = 'https://www.kookapp.cn/api/v3/
|
|
|
36
34
|
/**
|
|
37
35
|
* 使用获取到的网关连接地址建立 WebSocket 连接
|
|
38
36
|
* @param token
|
|
39
|
-
* @param
|
|
37
|
+
* @param conversation
|
|
40
38
|
*/
|
|
41
|
-
export async function createClient(token,
|
|
42
|
-
|
|
43
|
-
* 设置token
|
|
44
|
-
*/
|
|
39
|
+
export async function createClient(token, conversation) {
|
|
40
|
+
// 设置token
|
|
45
41
|
setKookToken(token);
|
|
46
|
-
|
|
47
|
-
* 请求url
|
|
48
|
-
*/
|
|
42
|
+
// 请求url
|
|
49
43
|
const gatewayUrl = await getGatewayUrl(token);
|
|
50
44
|
if (gatewayUrl) {
|
|
51
45
|
const ws = new WebSocket(gatewayUrl);
|
|
52
46
|
ws.on('open', () => {
|
|
53
47
|
console.info('token ok');
|
|
54
48
|
});
|
|
55
|
-
|
|
56
|
-
* 标记是否已连接
|
|
57
|
-
*/
|
|
49
|
+
// 标记是否已连接
|
|
58
50
|
let isConnected = false;
|
|
59
|
-
|
|
60
|
-
* 存储 session ID
|
|
61
|
-
*/
|
|
51
|
+
// 存储 session ID
|
|
62
52
|
let sessionID = '';
|
|
63
|
-
|
|
64
|
-
* 存储最新的消息序号
|
|
65
|
-
*/
|
|
53
|
+
// 存储最新的消息序号
|
|
66
54
|
let lastMessageSN = 0;
|
|
67
|
-
ws.on('message', msg => {
|
|
68
|
-
/**
|
|
69
|
-
*
|
|
70
|
-
*/
|
|
55
|
+
ws.on('message', async (msg) => {
|
|
71
56
|
const message = JSON.parse(msg.toString('utf8'));
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
57
|
+
const { s, d, sn } = message;
|
|
58
|
+
if (process.env.KOOK_WS == 'dev') {
|
|
59
|
+
console.log('data', d);
|
|
60
|
+
}
|
|
76
61
|
switch (s) {
|
|
77
62
|
/**
|
|
78
63
|
* 消息(包含聊天和通知消息)
|
|
@@ -82,7 +67,7 @@ export async function createClient(token, callBack) {
|
|
|
82
67
|
* 处理 EVENT 信令
|
|
83
68
|
* 包括按序处理消息和记录最新的消息序号
|
|
84
69
|
*/
|
|
85
|
-
if (
|
|
70
|
+
if (d && sn) {
|
|
86
71
|
if (sn === lastMessageSN + 1) {
|
|
87
72
|
/**
|
|
88
73
|
* 消息序号正确
|
|
@@ -92,8 +77,8 @@ export async function createClient(token, callBack) {
|
|
|
92
77
|
/**
|
|
93
78
|
* 处理消息并传递给almeon
|
|
94
79
|
*/
|
|
95
|
-
const event =
|
|
96
|
-
|
|
80
|
+
const event = d;
|
|
81
|
+
await conversation(event);
|
|
97
82
|
}
|
|
98
83
|
else if (sn > lastMessageSN + 1) {
|
|
99
84
|
/**
|
|
@@ -114,13 +99,13 @@ export async function createClient(token, callBack) {
|
|
|
114
99
|
* 服务端返回握手结果
|
|
115
100
|
*/
|
|
116
101
|
case 1: {
|
|
117
|
-
if (
|
|
118
|
-
console.info('ws ok');
|
|
119
|
-
sessionID =
|
|
102
|
+
if (d && d.code === 0) {
|
|
103
|
+
console.info('[ws] ok');
|
|
104
|
+
sessionID = d.session_id;
|
|
120
105
|
isConnected = true;
|
|
121
106
|
}
|
|
122
107
|
else {
|
|
123
|
-
console.info('ws err');
|
|
108
|
+
console.info('[ws] err');
|
|
124
109
|
}
|
|
125
110
|
break;
|
|
126
111
|
}
|
|
@@ -128,7 +113,7 @@ export async function createClient(token, callBack) {
|
|
|
128
113
|
* 心跳,ping
|
|
129
114
|
*/
|
|
130
115
|
case 2: {
|
|
131
|
-
console.info('ws ping');
|
|
116
|
+
console.info('[ws] ping');
|
|
132
117
|
ws.send(JSON.stringify({
|
|
133
118
|
s: 3
|
|
134
119
|
}));
|
|
@@ -138,21 +123,21 @@ export async function createClient(token, callBack) {
|
|
|
138
123
|
* 心跳,pong
|
|
139
124
|
*/
|
|
140
125
|
case 3: {
|
|
141
|
-
console.info('ws pong');
|
|
126
|
+
console.info('[ws] pong');
|
|
142
127
|
break;
|
|
143
128
|
}
|
|
144
129
|
/**
|
|
145
130
|
* resume, 恢复会话
|
|
146
131
|
*/
|
|
147
132
|
case 4: {
|
|
148
|
-
console.info('ws resume');
|
|
133
|
+
console.info('[ws] resume');
|
|
149
134
|
break;
|
|
150
135
|
}
|
|
151
136
|
/**
|
|
152
137
|
* reconnect, 要求客户端断开当前连接重新连接
|
|
153
138
|
*/
|
|
154
139
|
case 5: {
|
|
155
|
-
console.info('ws Connection failed, reconnect');
|
|
140
|
+
console.info('[ws] Connection failed, reconnect');
|
|
156
141
|
/**
|
|
157
142
|
* 处理 RECONNECT 信令
|
|
158
143
|
* 断开当前连接并进行重新连接
|
|
@@ -171,18 +156,16 @@ export async function createClient(token, callBack) {
|
|
|
171
156
|
* resume ack
|
|
172
157
|
*/
|
|
173
158
|
case 6: {
|
|
174
|
-
console.info('ws resume ack');
|
|
159
|
+
console.info('[ws] resume ack');
|
|
175
160
|
break;
|
|
176
161
|
}
|
|
177
162
|
default: {
|
|
178
|
-
console.info('ws define');
|
|
163
|
+
console.info('[ws] define');
|
|
179
164
|
break;
|
|
180
165
|
}
|
|
181
166
|
}
|
|
182
167
|
});
|
|
183
|
-
|
|
184
|
-
* 心跳定时发送
|
|
185
|
-
*/
|
|
168
|
+
// 心跳定时发送
|
|
186
169
|
setInterval(() => {
|
|
187
170
|
if (isConnected) {
|
|
188
171
|
ws.send(JSON.stringify({
|
|
@@ -192,7 +175,10 @@ export async function createClient(token, callBack) {
|
|
|
192
175
|
}
|
|
193
176
|
}, 30000);
|
|
194
177
|
ws.on('close', () => {
|
|
195
|
-
console.error('ws close');
|
|
178
|
+
console.error('[ws] close');
|
|
179
|
+
});
|
|
180
|
+
ws.on('error', err => {
|
|
181
|
+
console.error('[ws] error', err);
|
|
196
182
|
});
|
|
197
183
|
}
|
|
198
184
|
}
|