alemonjs 1.1.15 → 1.1.17
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 +1 -1
- package/lib/discord/index.js +5 -19
- package/lib/discord/sdk/base.js +2 -2
- package/lib/discord/sdk/cdn.js +2 -2
- package/lib/discord/sdk/config.js +12 -10
- package/lib/discord/sdk/wss.js +22 -22
- package/lib/kook/alemon/controller.js +10 -10
- package/lib/kook/alemon/conversation.js +1 -1
- package/lib/kook/alemon/direct.js +5 -5
- package/lib/kook/alemon/reply.js +2 -2
- package/lib/kook/index.js +2 -2
- package/lib/kook/sdk/api.js +2 -2
- package/lib/kook/sdk/config.js +11 -5
- package/lib/kook/sdk/ws.js +35 -49
- package/lib/ntqq/alemon/group/controller.js +9 -5
- package/lib/ntqq/alemon/group/direct.js +20 -12
- package/lib/ntqq/alemon/group/reply.js +2 -2
- package/lib/ntqq/index.js +12 -31
- package/lib/ntqq/sdk/api/group.js +2 -1
- package/lib/ntqq/sdk/api/guild.js +3 -1
- package/lib/ntqq/sdk/config.js +15 -50
- package/lib/ntqq/sdk/counter.js +15 -0
- package/lib/ntqq/sdk/wss.js +147 -111
- package/lib/one/index.js +1 -4
- package/lib/qq/alemon/direct.js +1 -1
- package/lib/qq/alemon/reply.js +1 -1
- package/lib/qq/index.js +4 -10
- package/lib/qq/sdk/api.js +3 -1
- package/lib/qq/sdk/config.js +12 -10
- package/lib/qq/sdk/wss.js +4 -2
- package/lib/villa/alemon/controller.js +1 -1
- package/lib/villa/alemon/conversation.js +1 -1
- package/lib/villa/alemon/reply.js +10 -4
- package/lib/villa/index.js +4 -7
- package/lib/villa/sdk/api.js +5 -17
- package/lib/villa/sdk/config.js +12 -11
- package/lib/villa/sdk/index.js +2 -0
- package/lib/villa/sdk/wss.js +27 -21
- package/package.json +1 -1
- package/types/discord/index.d.ts +0 -4
- package/types/discord/sdk/config.d.ts +9 -5
- package/types/discord/sdk/wss.d.ts +3 -0
- package/types/kook/alemon/controller.d.ts +8 -52
- package/types/kook/alemon/conversation.d.ts +1 -1
- package/types/kook/sdk/config.d.ts +8 -2
- package/types/kook/sdk/ws.d.ts +3 -3
- package/types/ntqq/sdk/config.d.ts +9 -12
- package/types/ntqq/sdk/counter.d.ts +7 -0
- package/types/ntqq/sdk/wss.d.ts +5 -4
- package/types/qq/sdk/config.d.ts +7 -6
- package/types/villa/alemon/conversation.d.ts +1 -1
- package/types/villa/sdk/api.d.ts +0 -13
- package/types/villa/sdk/config.d.ts +7 -5
- package/types/villa/sdk/index.d.ts +2 -0
- package/types/villa/sdk/wss.d.ts +24 -1
|
@@ -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;
|
|
@@ -37,7 +37,7 @@ export async function replyController(msg, channel_id, select) {
|
|
|
37
37
|
const match = content.match(/<http>(.*?)<\/http>/);
|
|
38
38
|
if (match) {
|
|
39
39
|
const getUrl = match[1];
|
|
40
|
-
const msg = await getUrlbuffer(getUrl);
|
|
40
|
+
const msg = await getUrlbuffer(getUrl).catch(everyoneError);
|
|
41
41
|
if (msg) {
|
|
42
42
|
return await Client.channelsMessagesImage(channel_id, msg).catch(everyoneError);
|
|
43
43
|
}
|
package/lib/discord/index.js
CHANGED
|
@@ -1,34 +1,20 @@
|
|
|
1
|
-
import { getIntents,
|
|
1
|
+
import { getIntents, createClient, setBotConfig } from './sdk/index.js';
|
|
2
2
|
import { conversation } from './alemon/conversation.js';
|
|
3
3
|
import { checkRobotByDISCORD } from './login.js';
|
|
4
4
|
import { getBotConfigByKey } from '../config/index.js';
|
|
5
|
-
/**
|
|
6
|
-
* 创建实例
|
|
7
|
-
* @returns
|
|
8
|
-
*/
|
|
9
5
|
export async function createAlemonByDISCORD() {
|
|
10
|
-
/**
|
|
11
|
-
* 创建登录配置
|
|
12
|
-
*/
|
|
13
6
|
if (await checkRobotByDISCORD().catch(err => {
|
|
14
7
|
console.error(err);
|
|
15
8
|
return false;
|
|
16
9
|
})) {
|
|
17
|
-
|
|
18
|
-
* 读取配置
|
|
19
|
-
*/
|
|
10
|
+
// 读取配置
|
|
20
11
|
const cfg = getBotConfigByKey('discord');
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
setDISOCRD(cfg.token, getIntents(cfg.intent));
|
|
25
|
-
/**
|
|
26
|
-
* 启动监听
|
|
27
|
-
*/
|
|
12
|
+
setBotConfig('token', cfg.token);
|
|
13
|
+
setBotConfig('intent', getIntents(cfg.intent));
|
|
14
|
+
// 启动监听
|
|
28
15
|
createClient(conversation, cfg?.shard);
|
|
29
16
|
return true;
|
|
30
17
|
}
|
|
31
18
|
return false;
|
|
32
19
|
}
|
|
33
|
-
// 客户端
|
|
34
20
|
export { ClientDISOCRD } from './sdk/index.js';
|
package/lib/discord/sdk/base.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import axios from 'axios';
|
|
2
|
-
import {
|
|
2
|
+
import { getBotConfig } from './config.js';
|
|
3
3
|
import { ApiLog } from './log.js';
|
|
4
4
|
/**
|
|
5
5
|
* KOOK服务
|
|
@@ -7,7 +7,7 @@ import { ApiLog } from './log.js';
|
|
|
7
7
|
* @returns
|
|
8
8
|
*/
|
|
9
9
|
export function Service(config) {
|
|
10
|
-
const
|
|
10
|
+
const token = getBotConfig('token');
|
|
11
11
|
const service = axios.create({
|
|
12
12
|
baseURL: 'https://discord.com/api/v10',
|
|
13
13
|
timeout: 6000,
|
package/lib/discord/sdk/cdn.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import axios from 'axios';
|
|
2
|
-
import {
|
|
2
|
+
import { getBotConfig } from './config.js';
|
|
3
3
|
import { ApiLog } from './log.js';
|
|
4
4
|
const BaseUrl = 'https://cdn.discordapp.com';
|
|
5
5
|
export function ServiceApp(config) {
|
|
6
|
-
const
|
|
6
|
+
const token = getBotConfig('token');
|
|
7
7
|
const service = axios.create({
|
|
8
8
|
baseURL: BaseUrl,
|
|
9
9
|
timeout: 6000,
|
|
@@ -1,20 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
const cfg = {
|
|
2
|
+
token: '',
|
|
3
|
+
intent: 0
|
|
4
|
+
};
|
|
3
5
|
/**
|
|
4
6
|
*
|
|
7
|
+
* @param key
|
|
5
8
|
* @param val
|
|
6
9
|
*/
|
|
7
|
-
export function
|
|
8
|
-
|
|
9
|
-
|
|
10
|
+
export function setBotConfig(key, val) {
|
|
11
|
+
if (Object.prototype.hasOwnProperty.call(cfg, key)) {
|
|
12
|
+
cfg[key] = val;
|
|
13
|
+
}
|
|
10
14
|
}
|
|
11
15
|
/**
|
|
12
16
|
*
|
|
17
|
+
* @param key
|
|
13
18
|
* @returns
|
|
14
19
|
*/
|
|
15
|
-
export function
|
|
16
|
-
return
|
|
17
|
-
token,
|
|
18
|
-
intent
|
|
19
|
-
};
|
|
20
|
+
export function getBotConfig(key) {
|
|
21
|
+
return cfg[key];
|
|
20
22
|
}
|
package/lib/discord/sdk/wss.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import WebSocket from 'ws';
|
|
2
2
|
import { gateway } from './api.js';
|
|
3
|
-
import {
|
|
3
|
+
import { getBotConfig } 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({
|
|
@@ -24,7 +27,8 @@ export async function createClient(conversation, shard = [0, 1]) {
|
|
|
24
27
|
}));
|
|
25
28
|
setTimeout(call, heartbeat_interval);
|
|
26
29
|
};
|
|
27
|
-
const
|
|
30
|
+
const token = getBotConfig('token');
|
|
31
|
+
const intent = getBotConfig('intent');
|
|
28
32
|
const map = {
|
|
29
33
|
0: ({ d, t }) => {
|
|
30
34
|
conversation(t, d);
|
|
@@ -36,7 +40,7 @@ export async function createClient(conversation, shard = [0, 1]) {
|
|
|
36
40
|
}
|
|
37
41
|
},
|
|
38
42
|
7: () => {
|
|
39
|
-
console.info('重新请求');
|
|
43
|
+
console.info('[ws] 重新请求');
|
|
40
44
|
wsConn.send(JSON.stringify({
|
|
41
45
|
op: 6,
|
|
42
46
|
d: {
|
|
@@ -48,7 +52,8 @@ export async function createClient(conversation, shard = [0, 1]) {
|
|
|
48
52
|
},
|
|
49
53
|
9: message => {
|
|
50
54
|
// 6 或 2 失败
|
|
51
|
-
|
|
55
|
+
// 连接失败
|
|
56
|
+
console.info('[ws] parameter error', message);
|
|
52
57
|
},
|
|
53
58
|
/**
|
|
54
59
|
* 打招呼
|
|
@@ -57,12 +62,14 @@ export async function createClient(conversation, shard = [0, 1]) {
|
|
|
57
62
|
10: ({ d }) => {
|
|
58
63
|
const { heartbeat_interval: ih } = d;
|
|
59
64
|
heartbeat_interval = ih;
|
|
65
|
+
//
|
|
60
66
|
wsConn.send(JSON.stringify({
|
|
61
67
|
op: 1,
|
|
62
68
|
d: null
|
|
63
69
|
}));
|
|
64
70
|
setTimeout(call, heartbeat_interval);
|
|
65
|
-
|
|
71
|
+
// 在初次握手期间启动新会话
|
|
72
|
+
wsConn.send(JSON.stringify({
|
|
66
73
|
op: 2,
|
|
67
74
|
d: {
|
|
68
75
|
shard: shard,
|
|
@@ -74,36 +81,29 @@ export async function createClient(conversation, shard = [0, 1]) {
|
|
|
74
81
|
device: 'alemonjs'
|
|
75
82
|
}
|
|
76
83
|
}
|
|
77
|
-
};
|
|
78
|
-
if (process.argv.includes('dev')) {
|
|
79
|
-
console.info('data', data);
|
|
80
|
-
}
|
|
81
|
-
// 在初次握手期间启动新会话
|
|
82
|
-
wsConn.send(JSON.stringify(data));
|
|
84
|
+
}));
|
|
83
85
|
},
|
|
84
86
|
11: ({ d }) => {
|
|
85
|
-
console.info('heartbeat transmission');
|
|
87
|
+
console.info('[ws] heartbeat transmission');
|
|
86
88
|
}
|
|
87
89
|
};
|
|
88
90
|
wsConn.on('message', data => {
|
|
89
|
-
const
|
|
90
|
-
const
|
|
91
|
-
if (process.
|
|
92
|
-
console.info('
|
|
91
|
+
const parsedData = JSON.parse(data.toString());
|
|
92
|
+
const { op, d } = parsedData;
|
|
93
|
+
if (process.env?.DISCORD_WS == 'dev') {
|
|
94
|
+
console.info('data', d);
|
|
93
95
|
}
|
|
94
|
-
const { op } = parsedData;
|
|
95
96
|
if (Object.prototype.hasOwnProperty.call(map, op)) {
|
|
96
97
|
map[op](parsedData);
|
|
97
98
|
}
|
|
98
99
|
});
|
|
99
100
|
// 关闭
|
|
100
|
-
wsConn.on('close',
|
|
101
|
-
console.error('ws
|
|
102
|
-
console.error('登录失败,TOKEN存在风险');
|
|
101
|
+
wsConn.on('close', err => {
|
|
102
|
+
console.error('[ws] 登录失败,TOKEN存在风险');
|
|
103
103
|
});
|
|
104
104
|
// 出错
|
|
105
105
|
wsConn.on('error', error => {
|
|
106
|
-
console.error('ws error:', error);
|
|
106
|
+
console.error('[ws] error:', error);
|
|
107
107
|
});
|
|
108
108
|
}
|
|
109
109
|
/**
|
|
@@ -29,7 +29,7 @@ export const Controller = {
|
|
|
29
29
|
return false;
|
|
30
30
|
},
|
|
31
31
|
remove: async () => {
|
|
32
|
-
return await ClientKOOK.guildKickout(guild_id, user_id);
|
|
32
|
+
return await ClientKOOK.guildKickout(guild_id, user_id).catch(everyoneError);
|
|
33
33
|
},
|
|
34
34
|
operate: async (role_id, add = true) => {
|
|
35
35
|
return false;
|
|
@@ -39,16 +39,16 @@ export const Controller = {
|
|
|
39
39
|
Message: ({ channel_id, user_id, msg_id }) => {
|
|
40
40
|
return {
|
|
41
41
|
reply: async (content) => {
|
|
42
|
-
return await replyController(content, channel_id);
|
|
42
|
+
return await replyController(content, channel_id).catch(everyoneError);
|
|
43
43
|
},
|
|
44
44
|
quote: async (content) => {
|
|
45
|
-
return await replyController(content, channel_id);
|
|
45
|
+
return await replyController(content, channel_id).catch(everyoneError);
|
|
46
46
|
},
|
|
47
47
|
update: async (content) => {
|
|
48
|
-
return await ClientKOOK.messageUpdate({ msg_id, content });
|
|
48
|
+
return await ClientKOOK.messageUpdate({ msg_id, content }).catch(everyoneError);
|
|
49
49
|
},
|
|
50
50
|
withdraw: async (hideTip) => {
|
|
51
|
-
return await ClientKOOK.messageDelete(msg_id);
|
|
51
|
+
return await ClientKOOK.messageDelete(msg_id).catch(everyoneError);
|
|
52
52
|
},
|
|
53
53
|
pinning: async (cancel) => {
|
|
54
54
|
return false;
|
|
@@ -67,12 +67,12 @@ export const Controller = {
|
|
|
67
67
|
msg_id,
|
|
68
68
|
emoji: item,
|
|
69
69
|
user_id
|
|
70
|
-
}));
|
|
70
|
+
}).catch(everyoneError));
|
|
71
71
|
}
|
|
72
72
|
return arr;
|
|
73
73
|
}
|
|
74
74
|
for (const item of msg) {
|
|
75
|
-
arr.push(await ClientKOOK.messageAddReaction({ msg_id, emoji: item }));
|
|
75
|
+
arr.push(await ClientKOOK.messageAddReaction({ msg_id, emoji: item }).catch(everyoneError));
|
|
76
76
|
}
|
|
77
77
|
return arr;
|
|
78
78
|
},
|
|
@@ -84,7 +84,7 @@ export const Controller = {
|
|
|
84
84
|
content: file
|
|
85
85
|
}).catch(everyoneError);
|
|
86
86
|
}
|
|
87
|
-
const ret = await ClientKOOK.postFile(file, name);
|
|
87
|
+
const ret = await ClientKOOK.postFile(file, name).catch(everyoneError);
|
|
88
88
|
if (!ret)
|
|
89
89
|
return false;
|
|
90
90
|
return await ClientKOOK.createMessage({
|
|
@@ -101,7 +101,7 @@ export const Controller = {
|
|
|
101
101
|
content: file
|
|
102
102
|
}).catch(everyoneError);
|
|
103
103
|
}
|
|
104
|
-
const ret = await ClientKOOK.postFile(file, name);
|
|
104
|
+
const ret = await ClientKOOK.postFile(file, name).catch(everyoneError);
|
|
105
105
|
if (!ret)
|
|
106
106
|
return false;
|
|
107
107
|
return await ClientKOOK.createMessage({
|
|
@@ -116,7 +116,7 @@ export const Controller = {
|
|
|
116
116
|
type: 10,
|
|
117
117
|
target_id: channel_id,
|
|
118
118
|
content: JSON.stringify(msg)
|
|
119
|
-
})
|
|
119
|
+
}).catch(everyoneError)
|
|
120
120
|
];
|
|
121
121
|
},
|
|
122
122
|
allUsers: async (emoji, options = {
|
|
@@ -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,7 +138,7 @@ export async function directController(msg, channel_id, open_id) {
|
|
|
138
138
|
* isbuffer
|
|
139
139
|
*/
|
|
140
140
|
if (Buffer.isBuffer(msg)) {
|
|
141
|
-
const ret = await ClientKOOK.postImage(msg);
|
|
141
|
+
const ret = await ClientKOOK.postImage(msg).catch(everyoneError);
|
|
142
142
|
if (ret && ret.data) {
|
|
143
143
|
return await ClientKOOK.createDirectMessage({
|
|
144
144
|
type: 2,
|
|
@@ -194,10 +194,10 @@ export async function directController(msg, channel_id, open_id) {
|
|
|
194
194
|
const match = content.match(/<http>(.*?)<\/http>/);
|
|
195
195
|
if (match) {
|
|
196
196
|
const getUrl = match[1];
|
|
197
|
-
const msg = await getUrlbuffer(getUrl);
|
|
197
|
+
const msg = await getUrlbuffer(getUrl).catch(everyoneError);
|
|
198
198
|
if (!msg)
|
|
199
199
|
return false;
|
|
200
|
-
const ret = await ClientKOOK.postImage(msg);
|
|
200
|
+
const ret = await ClientKOOK.postImage(msg).catch(everyoneError);
|
|
201
201
|
if (!ret)
|
|
202
202
|
return false;
|
|
203
203
|
if (msg && ret) {
|
package/lib/kook/alemon/reply.js
CHANGED
|
@@ -13,7 +13,7 @@ export async function replyController(msg, channel_id) {
|
|
|
13
13
|
* isbuffer
|
|
14
14
|
*/
|
|
15
15
|
if (Buffer.isBuffer(msg)) {
|
|
16
|
-
const ret = await ClientKOOK.postImage(msg);
|
|
16
|
+
const ret = await ClientKOOK.postImage(msg).catch(everyoneError);
|
|
17
17
|
if (ret && ret.data) {
|
|
18
18
|
return await ClientKOOK.createMessage({
|
|
19
19
|
type: 2,
|
|
@@ -67,7 +67,7 @@ export async function replyController(msg, channel_id) {
|
|
|
67
67
|
const match = content.match(/<http>(.*?)<\/http>/);
|
|
68
68
|
if (match) {
|
|
69
69
|
const getUrl = match[1];
|
|
70
|
-
const msg = await getUrlbuffer(getUrl);
|
|
70
|
+
const msg = await getUrlbuffer(getUrl).catch(everyoneError);
|
|
71
71
|
if (!msg)
|
|
72
72
|
return false;
|
|
73
73
|
const ret = await ClientKOOK.postImage(msg).catch(everyoneError);
|
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/api.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import axios from 'axios';
|
|
2
2
|
import FormData from 'form-data';
|
|
3
3
|
import { ApiEnum } from './typings.js';
|
|
4
|
-
import {
|
|
4
|
+
import { getBotConfig } from './config.js';
|
|
5
5
|
import { createPicFrom } from '../../core/index.js';
|
|
6
6
|
export function ApiLog(res) {
|
|
7
7
|
if (process.env?.KOOK_API_REQUEST == 'dev')
|
|
@@ -20,7 +20,7 @@ export function ApiLog(res) {
|
|
|
20
20
|
* @returns
|
|
21
21
|
*/
|
|
22
22
|
export function kookService(config) {
|
|
23
|
-
const token =
|
|
23
|
+
const token = getBotConfig('token');
|
|
24
24
|
const service = axios.create({
|
|
25
25
|
baseURL: 'https://www.kookapp.cn',
|
|
26
26
|
timeout: 30000,
|
package/lib/kook/sdk/config.js
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
const cfg = {
|
|
2
|
+
token: ''
|
|
3
|
+
};
|
|
2
4
|
/**
|
|
3
5
|
*
|
|
6
|
+
* @param key
|
|
4
7
|
* @param val
|
|
5
8
|
*/
|
|
6
|
-
export function
|
|
7
|
-
|
|
9
|
+
export function setBotConfig(key, val) {
|
|
10
|
+
if (Object.prototype.hasOwnProperty.call(cfg, key)) {
|
|
11
|
+
cfg[key] = val;
|
|
12
|
+
}
|
|
8
13
|
}
|
|
9
14
|
/**
|
|
10
15
|
*
|
|
16
|
+
* @param key
|
|
11
17
|
* @returns
|
|
12
18
|
*/
|
|
13
|
-
export function
|
|
14
|
-
return
|
|
19
|
+
export function getBotConfig(key) {
|
|
20
|
+
return cfg[key];
|
|
15
21
|
}
|
package/lib/kook/sdk/ws.js
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
import WebSocket from 'ws';
|
|
2
2
|
import axios from 'axios';
|
|
3
|
-
import {
|
|
3
|
+
import { setBotConfig } 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
|
-
|
|
44
|
-
|
|
45
|
-
setKookToken(token);
|
|
46
|
-
/**
|
|
47
|
-
* 请求url
|
|
48
|
-
*/
|
|
39
|
+
export async function createClient(token, conversation) {
|
|
40
|
+
// 设置token
|
|
41
|
+
setBotConfig('token', token);
|
|
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
|
-
console.info('
|
|
47
|
+
console.info('[ws] open');
|
|
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
|
}
|
|
@@ -21,10 +21,10 @@ export const Controller = {
|
|
|
21
21
|
Message: ({ guild_id, msg_id }) => {
|
|
22
22
|
return {
|
|
23
23
|
reply: async (content) => {
|
|
24
|
-
return await replyController(content, guild_id, msg_id);
|
|
24
|
+
return await replyController(content, guild_id, msg_id).catch(everyoneError);
|
|
25
25
|
},
|
|
26
26
|
quote: async (content) => {
|
|
27
|
-
return await replyController(content, guild_id, msg_id);
|
|
27
|
+
return await replyController(content, guild_id, msg_id).catch(everyoneError);
|
|
28
28
|
},
|
|
29
29
|
update: async (content) => {
|
|
30
30
|
return false;
|
|
@@ -50,7 +50,9 @@ export const Controller = {
|
|
|
50
50
|
srv_send_msg: false,
|
|
51
51
|
file_type: 3,
|
|
52
52
|
url: file
|
|
53
|
-
})
|
|
53
|
+
})
|
|
54
|
+
.then(res => res.file_info)
|
|
55
|
+
.catch(everyoneError)
|
|
54
56
|
},
|
|
55
57
|
msg_id,
|
|
56
58
|
msg_type: 7,
|
|
@@ -68,7 +70,9 @@ export const Controller = {
|
|
|
68
70
|
srv_send_msg: false,
|
|
69
71
|
file_type: 2,
|
|
70
72
|
url: file
|
|
71
|
-
})
|
|
73
|
+
})
|
|
74
|
+
.then(res => res.file_info)
|
|
75
|
+
.catch(everyoneError)
|
|
72
76
|
},
|
|
73
77
|
msg_id,
|
|
74
78
|
msg_type: 7,
|
|
@@ -83,7 +87,7 @@ export const Controller = {
|
|
|
83
87
|
card: async (msg) => {
|
|
84
88
|
const arr = [];
|
|
85
89
|
for (const item of msg) {
|
|
86
|
-
arr.push(ClientNTQQ.groupOpenMessages(guild_id, {
|
|
90
|
+
arr.push(await ClientNTQQ.groupOpenMessages(guild_id, {
|
|
87
91
|
msg_id,
|
|
88
92
|
...item,
|
|
89
93
|
msg_seq: ClientNTQQ.getMsgSeq(msg_id)
|