alemonjs 1.1.16 → 1.1.18
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/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 +4 -2
- package/lib/kook/sdk/api.js +2 -2
- package/lib/kook/sdk/config.js +11 -5
- package/lib/kook/sdk/ws.js +3 -3
- 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/wss.js +144 -101
- 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/index.js +1 -2
- package/lib/villa/sdk/api.js +5 -4
- package/lib/villa/sdk/config.js +12 -11
- package/lib/villa/sdk/index.js +2 -0
- package/lib/villa/sdk/wss.js +7 -3
- package/package.json +1 -1
- package/types/discord/index.d.ts +0 -4
- package/types/discord/sdk/config.d.ts +9 -5
- package/types/kook/sdk/config.d.ts +8 -2
- package/types/ntqq/sdk/config.d.ts +9 -12
- package/types/ntqq/sdk/wss.d.ts +5 -4
- package/types/qq/sdk/config.d.ts +7 -6
- package/types/villa/sdk/config.d.ts +7 -5
- package/types/villa/sdk/index.d.ts +2 -0
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,6 +1,6 @@
|
|
|
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
6
|
* @param conversation
|
|
@@ -27,7 +27,8 @@ export async function createClient(conversation, shard = [0, 1]) {
|
|
|
27
27
|
}));
|
|
28
28
|
setTimeout(call, heartbeat_interval);
|
|
29
29
|
};
|
|
30
|
-
const
|
|
30
|
+
const token = getBotConfig('token');
|
|
31
|
+
const intent = getBotConfig('intent');
|
|
31
32
|
const map = {
|
|
32
33
|
0: ({ d, t }) => {
|
|
33
34
|
conversation(t, d);
|
|
@@ -51,6 +52,7 @@ export async function createClient(conversation, shard = [0, 1]) {
|
|
|
51
52
|
},
|
|
52
53
|
9: message => {
|
|
53
54
|
// 6 或 2 失败
|
|
55
|
+
// 连接失败
|
|
54
56
|
console.info('[ws] parameter error', message);
|
|
55
57
|
},
|
|
56
58
|
/**
|
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,6 +1,6 @@
|
|
|
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
|
|
@@ -38,13 +38,13 @@ export async function getGatewayUrl(token, url = 'https://www.kookapp.cn/api/v3/
|
|
|
38
38
|
*/
|
|
39
39
|
export async function createClient(token, conversation) {
|
|
40
40
|
// 设置token
|
|
41
|
-
|
|
41
|
+
setBotConfig('token', token);
|
|
42
42
|
// 请求url
|
|
43
43
|
const gatewayUrl = await getGatewayUrl(token);
|
|
44
44
|
if (gatewayUrl) {
|
|
45
45
|
const ws = new WebSocket(gatewayUrl);
|
|
46
46
|
ws.on('open', () => {
|
|
47
|
-
console.info('
|
|
47
|
+
console.info('[ws] open');
|
|
48
48
|
});
|
|
49
49
|
// 标记是否已连接
|
|
50
50
|
let isConnected = false;
|
package/lib/ntqq/index.js
CHANGED
|
@@ -1,44 +1,25 @@
|
|
|
1
1
|
import { checkRobotByQQ } from './login.js';
|
|
2
2
|
import { getBotConfigByKey } from '../config/index.js';
|
|
3
3
|
import { conversation } from './alemon/conversation.js';
|
|
4
|
-
import { createClient,
|
|
4
|
+
import { createClient, getIntentsMask } from './sdk/index.js';
|
|
5
5
|
export async function createAlemonByNtqq() {
|
|
6
|
-
/**
|
|
7
|
-
* 登录
|
|
8
|
-
*/
|
|
9
6
|
if (await checkRobotByQQ().catch(err => {
|
|
10
7
|
console.error(err);
|
|
11
8
|
return false;
|
|
12
9
|
})) {
|
|
13
|
-
|
|
14
|
-
* 读取配置
|
|
15
|
-
*/
|
|
10
|
+
// 读取配置
|
|
16
11
|
const cfg = getBotConfigByKey('ntqq');
|
|
17
12
|
const intents = getIntentsMask(cfg.intents);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
await setTimeoutBotConfig({
|
|
30
|
-
appID: cfg.appID,
|
|
31
|
-
token: cfg.token,
|
|
32
|
-
secret: cfg.secret,
|
|
33
|
-
intents: intents,
|
|
34
|
-
isPrivate: cfg.isPrivate,
|
|
35
|
-
sandbox: cfg.sandbox
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* 创建客户端
|
|
40
|
-
*/
|
|
41
|
-
createClient(conversation, cfg?.shard ?? [0, 1]);
|
|
13
|
+
// 创建客户端
|
|
14
|
+
await createClient({
|
|
15
|
+
appID: cfg.appID,
|
|
16
|
+
token: cfg.token,
|
|
17
|
+
secret: cfg.secret,
|
|
18
|
+
intents: intents,
|
|
19
|
+
isPrivate: cfg.isPrivate,
|
|
20
|
+
sandbox: cfg.sandbox,
|
|
21
|
+
shard: cfg?.shard ?? [0, 1]
|
|
22
|
+
}, conversation);
|
|
42
23
|
return true;
|
|
43
24
|
}
|
|
44
25
|
return false;
|
|
@@ -8,7 +8,8 @@ import { ApiLog } from '../log.js';
|
|
|
8
8
|
* @returns
|
|
9
9
|
*/
|
|
10
10
|
export async function GroupService(config) {
|
|
11
|
-
const
|
|
11
|
+
const appID = getBotConfig('appID');
|
|
12
|
+
const token = getBotConfig('token');
|
|
12
13
|
const service = await axios.create({
|
|
13
14
|
baseURL: API_SGROUP,
|
|
14
15
|
timeout: 20000,
|
|
@@ -9,7 +9,9 @@ import { API_SGROUP_SANDBOX, API_SGROUP } from './config.js';
|
|
|
9
9
|
* @returns
|
|
10
10
|
*/
|
|
11
11
|
export async function GuildServer(config) {
|
|
12
|
-
const
|
|
12
|
+
const token = getBotConfig('token');
|
|
13
|
+
const sandbox = getBotConfig('sandbox');
|
|
14
|
+
const appID = getBotConfig('appID');
|
|
13
15
|
const service = await axios.create({
|
|
14
16
|
baseURL: sandbox ? API_SGROUP_SANDBOX : API_SGROUP,
|
|
15
17
|
timeout: 20000,
|
package/lib/ntqq/sdk/config.js
CHANGED
|
@@ -1,63 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* 机器人缓存配置
|
|
4
|
-
*/
|
|
5
|
-
let cfg = {
|
|
1
|
+
// 机器人缓存配置
|
|
2
|
+
const cfg = {
|
|
6
3
|
appID: '',
|
|
7
4
|
token: '',
|
|
8
5
|
secret: '',
|
|
9
6
|
intents: 0,
|
|
10
7
|
isPrivate: false,
|
|
11
|
-
sandbox: false
|
|
8
|
+
sandbox: false,
|
|
9
|
+
shard: [0, 1]
|
|
12
10
|
};
|
|
13
11
|
/**
|
|
14
|
-
*
|
|
15
|
-
* @
|
|
16
|
-
*/
|
|
17
|
-
export function getBotConfig() {
|
|
18
|
-
return cfg;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* 设置机器人配置
|
|
12
|
+
*
|
|
13
|
+
* @param key
|
|
22
14
|
* @param val
|
|
23
|
-
* @returns
|
|
24
15
|
*/
|
|
25
|
-
export function setBotConfig(val) {
|
|
26
|
-
cfg
|
|
27
|
-
|
|
16
|
+
export function setBotConfig(key, val) {
|
|
17
|
+
if (Object.prototype.hasOwnProperty.call(cfg, key)) {
|
|
18
|
+
cfg[key] = val;
|
|
19
|
+
}
|
|
28
20
|
}
|
|
29
21
|
/**
|
|
30
|
-
*
|
|
22
|
+
*
|
|
23
|
+
* @param key
|
|
24
|
+
* @returns
|
|
31
25
|
*/
|
|
32
|
-
export
|
|
33
|
-
|
|
34
|
-
* 发送请求
|
|
35
|
-
*/
|
|
36
|
-
const data = await getAuthentication(cfg.appID, cfg.secret).then(res => res.data);
|
|
37
|
-
const g = {
|
|
38
|
-
appID: cfg.appID,
|
|
39
|
-
token: data.access_token,
|
|
40
|
-
secret: cfg.secret,
|
|
41
|
-
intents: cfg.intents,
|
|
42
|
-
isPrivate: cfg.isPrivate,
|
|
43
|
-
sandbox: cfg.sandbox
|
|
44
|
-
};
|
|
45
|
-
/**
|
|
46
|
-
* 设置配置
|
|
47
|
-
*/
|
|
48
|
-
setBotConfig(g);
|
|
49
|
-
const bal = async () => {
|
|
50
|
-
/**
|
|
51
|
-
* 发送请求
|
|
52
|
-
*/
|
|
53
|
-
const data = await getAuthentication(cfg.appID, cfg.secret).then(res => res.data);
|
|
54
|
-
g.token = data.access_token;
|
|
55
|
-
/**
|
|
56
|
-
* 设置配置
|
|
57
|
-
*/
|
|
58
|
-
setBotConfig(g);
|
|
59
|
-
console.info('refresh', data.expires_in, 's');
|
|
60
|
-
setTimeout(bal, data.expires_in * 1000);
|
|
61
|
-
};
|
|
62
|
-
setTimeout(bal, data.expires_in * 1000);
|
|
26
|
+
export function getBotConfig(key) {
|
|
27
|
+
return cfg[key];
|
|
63
28
|
}
|
package/lib/ntqq/sdk/wss.js
CHANGED
|
@@ -1,14 +1,44 @@
|
|
|
1
1
|
import WebSocket from 'ws';
|
|
2
|
-
import { gateway } from './api/index.js';
|
|
3
|
-
import { getBotConfig } from './config.js';
|
|
2
|
+
import { gateway, getAuthentication } from './api/index.js';
|
|
3
|
+
import { getBotConfig, setBotConfig } from './config.js';
|
|
4
4
|
import { Counter } from './counter.js';
|
|
5
5
|
const counter = new Counter(1); // 初始值为1
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
* @param
|
|
9
|
-
* @
|
|
7
|
+
* 定时鉴权
|
|
8
|
+
* @param cfg
|
|
9
|
+
* @returns
|
|
10
10
|
*/
|
|
11
|
-
|
|
11
|
+
async function setTimeoutBotConfig() {
|
|
12
|
+
const callBack = async () => {
|
|
13
|
+
const appID = getBotConfig('appID');
|
|
14
|
+
const secret = getBotConfig('secret');
|
|
15
|
+
// 发送请求
|
|
16
|
+
const data = await getAuthentication(appID, secret).then(res => res.data);
|
|
17
|
+
setBotConfig('token', data.access_token);
|
|
18
|
+
console.info('refresh', data.expires_in, 's');
|
|
19
|
+
setTimeout(callBack, data.expires_in * 1000);
|
|
20
|
+
};
|
|
21
|
+
await callBack();
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
*
|
|
26
|
+
* @param cfg
|
|
27
|
+
* @param conversation
|
|
28
|
+
*/
|
|
29
|
+
export async function createClient(cfg, conversation) {
|
|
30
|
+
setBotConfig('appID', cfg.appID);
|
|
31
|
+
setBotConfig('token', cfg.token);
|
|
32
|
+
setBotConfig('intents', cfg.intents);
|
|
33
|
+
setBotConfig('shard', cfg.shard);
|
|
34
|
+
setBotConfig('isPrivate', cfg.isPrivate);
|
|
35
|
+
setBotConfig('sandbox', cfg.sandbox);
|
|
36
|
+
setBotConfig('secret', cfg.secret);
|
|
37
|
+
/**
|
|
38
|
+
* 定时模式
|
|
39
|
+
*/
|
|
40
|
+
if (cfg.secret != '')
|
|
41
|
+
await setTimeoutBotConfig();
|
|
12
42
|
// 请求url
|
|
13
43
|
const gatewayUrl = await gateway();
|
|
14
44
|
// 重新连接的逻辑
|
|
@@ -17,107 +47,120 @@ export async function createClient(conversation, shard = [0, 1]) {
|
|
|
17
47
|
console.info('The maximum number of reconnections has been reached, cancel reconnection');
|
|
18
48
|
return;
|
|
19
49
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
50
|
+
setTimeout(() => {
|
|
51
|
+
console.info('reconnecting...');
|
|
52
|
+
// 重新starrt
|
|
53
|
+
start();
|
|
54
|
+
// 记录
|
|
55
|
+
counter.getNextID();
|
|
56
|
+
}, 5000);
|
|
26
57
|
};
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
58
|
+
const start = () => {
|
|
59
|
+
if (gatewayUrl) {
|
|
60
|
+
const ws = new WebSocket(gatewayUrl);
|
|
61
|
+
ws.on('open', () => {
|
|
62
|
+
console.info('[ws] open');
|
|
63
|
+
});
|
|
64
|
+
// 标记是否已连接
|
|
65
|
+
let isConnected = false;
|
|
66
|
+
// 存储最新的消息序号
|
|
67
|
+
let heartbeat_interval = 30000;
|
|
68
|
+
// 鉴权
|
|
69
|
+
let power;
|
|
70
|
+
const map = {
|
|
71
|
+
0: async ({ t, d }) => {
|
|
72
|
+
// 存在,则执行t对应的函数
|
|
73
|
+
await conversation(t, d);
|
|
74
|
+
// Ready Event,鉴权成功
|
|
75
|
+
if (t === 'READY') {
|
|
76
|
+
power = setInterval(() => {
|
|
77
|
+
if (isConnected) {
|
|
78
|
+
ws.send(JSON.stringify({
|
|
79
|
+
op: 1, // op = 1
|
|
80
|
+
d: null // 如果是第一次连接,传null
|
|
81
|
+
}));
|
|
82
|
+
}
|
|
83
|
+
}, heartbeat_interval);
|
|
84
|
+
}
|
|
85
|
+
// Resumed Event,恢复连接成功
|
|
86
|
+
if (t === 'RESUMED') {
|
|
87
|
+
console.info('restore connection');
|
|
88
|
+
// 重制次数
|
|
89
|
+
counter.setID(0);
|
|
90
|
+
}
|
|
91
|
+
return;
|
|
92
|
+
},
|
|
93
|
+
6: ({ d }) => {
|
|
94
|
+
console.info('connection attempt', d);
|
|
95
|
+
return;
|
|
96
|
+
},
|
|
97
|
+
7: async ({ d }) => {
|
|
98
|
+
// 执行重新连接
|
|
99
|
+
console.info('reconnect', d);
|
|
100
|
+
// 取消鉴权发送
|
|
101
|
+
if (power)
|
|
102
|
+
clearInterval(power);
|
|
103
|
+
return;
|
|
104
|
+
},
|
|
105
|
+
9: ({ d, t }) => {
|
|
106
|
+
console.info('parameter error', d);
|
|
107
|
+
return;
|
|
108
|
+
},
|
|
109
|
+
10: ({ d }) => {
|
|
110
|
+
// 重制次数
|
|
111
|
+
isConnected = true;
|
|
112
|
+
// 记录新循环
|
|
113
|
+
heartbeat_interval = d.heartbeat_interval;
|
|
114
|
+
const token = getBotConfig('token');
|
|
115
|
+
const intents = getBotConfig('intents');
|
|
116
|
+
const shard = getBotConfig('shard');
|
|
117
|
+
// 发送鉴权
|
|
118
|
+
ws.send(JSON.stringify({
|
|
119
|
+
op: 2, // op = 2
|
|
120
|
+
d: {
|
|
121
|
+
token: `QQBot ${token}`,
|
|
122
|
+
intents: intents,
|
|
123
|
+
shard: shard,
|
|
124
|
+
properties: {
|
|
125
|
+
$os: process.platform,
|
|
126
|
+
$browser: 'alemonjs',
|
|
127
|
+
$device: 'alemonjs'
|
|
128
|
+
}
|
|
50
129
|
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
130
|
+
}));
|
|
131
|
+
return;
|
|
132
|
+
},
|
|
133
|
+
11: () => {
|
|
134
|
+
// OpCode 11 Heartbeat ACK 消息,心跳发送成功
|
|
135
|
+
console.info('heartbeat transmission');
|
|
56
136
|
// 重制次数
|
|
57
137
|
counter.setID(0);
|
|
138
|
+
return;
|
|
139
|
+
},
|
|
140
|
+
12: ({ d }) => {
|
|
141
|
+
console.info('platform data', d);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
// 监听消息
|
|
146
|
+
ws.on('message', async (msg) => {
|
|
147
|
+
const message = JSON.parse(msg.toString('utf8'));
|
|
148
|
+
const { op, s, d, t } = message;
|
|
149
|
+
if (process.env.NTQQ_WS == 'dev') {
|
|
150
|
+
console.log('data', d);
|
|
58
151
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
console.info('reconnect', message);
|
|
66
|
-
// 取消鉴权发送
|
|
67
|
-
if (power)
|
|
68
|
-
clearInterval(power);
|
|
152
|
+
// 根据 opcode 进行处理
|
|
153
|
+
if (Object.prototype.hasOwnProperty.call(map, op)) {
|
|
154
|
+
await map[op]({ d, t });
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
ws.on('close', async (err) => {
|
|
69
158
|
await reconnect();
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
// 重制次数
|
|
76
|
-
isConnected = true;
|
|
77
|
-
// 记录新循环
|
|
78
|
-
heartbeat_interval = d.heartbeat_interval;
|
|
79
|
-
const { token, intents } = getBotConfig();
|
|
80
|
-
// 发送鉴权
|
|
81
|
-
ws.send(JSON.stringify({
|
|
82
|
-
op: 2, // op = 2
|
|
83
|
-
d: {
|
|
84
|
-
token: `QQBot ${token}`,
|
|
85
|
-
intents: intents,
|
|
86
|
-
shard,
|
|
87
|
-
properties: {
|
|
88
|
-
$os: process.platform,
|
|
89
|
-
$browser: 'alemonjs',
|
|
90
|
-
$device: 'alemonjs'
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}));
|
|
94
|
-
// 重制次数
|
|
95
|
-
counter.setID(0);
|
|
96
|
-
},
|
|
97
|
-
11: () => {
|
|
98
|
-
// OpCode 11 Heartbeat ACK 消息,心跳发送成功
|
|
99
|
-
console.info('heartbeat transmission');
|
|
100
|
-
},
|
|
101
|
-
12: message => {
|
|
102
|
-
console.info('platform data', message);
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
// 监听消息
|
|
106
|
-
ws.on('message', async (msg) => {
|
|
107
|
-
const message = JSON.parse(msg.toString('utf8'));
|
|
108
|
-
const { op, s, d, t } = message;
|
|
109
|
-
if (process.env.NTQQ_WS == 'dev') {
|
|
110
|
-
console.log('data', d);
|
|
111
|
-
}
|
|
112
|
-
// 根据 opcode 进行处理
|
|
113
|
-
if (Object.prototype.hasOwnProperty.call(map, op)) {
|
|
114
|
-
map[op]({ d, t });
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
ws.on('close', err => {
|
|
118
|
-
console.info('[ws] close', err);
|
|
119
|
-
});
|
|
120
|
-
}
|
|
159
|
+
console.info('[ws] close', err);
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
start();
|
|
121
164
|
}
|
|
122
165
|
/**
|
|
123
166
|
|
package/lib/qq/index.js
CHANGED
|
@@ -23,16 +23,10 @@ export async function createAlemonByQQ() {
|
|
|
23
23
|
token: cfg.token,
|
|
24
24
|
sandbox: cfg.sandbox ?? false
|
|
25
25
|
});
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
setBotQQConfig(
|
|
30
|
-
token: cfg.token,
|
|
31
|
-
appID: cfg.appID,
|
|
32
|
-
intents: cfg.intents,
|
|
33
|
-
secret: '',
|
|
34
|
-
sandbox: cfg.sandbox
|
|
35
|
-
});
|
|
26
|
+
setBotQQConfig('appID', cfg.appID);
|
|
27
|
+
setBotQQConfig('token', cfg.token);
|
|
28
|
+
setBotQQConfig('intents', cfg.intents);
|
|
29
|
+
setBotQQConfig('sandbox', cfg.sandbox);
|
|
36
30
|
/**
|
|
37
31
|
* 创建 websocket
|
|
38
32
|
*/
|
package/lib/qq/sdk/api.js
CHANGED
|
@@ -8,7 +8,9 @@ import { createPicFrom } from '../../core/index.js';
|
|
|
8
8
|
* @returns
|
|
9
9
|
*/
|
|
10
10
|
export async function requestService(config) {
|
|
11
|
-
const
|
|
11
|
+
const appID = getBotConfig('appID');
|
|
12
|
+
const token = getBotConfig('token');
|
|
13
|
+
const sandbox = getBotConfig('sandbox');
|
|
12
14
|
const service = await axios.create({
|
|
13
15
|
baseURL: sandbox
|
|
14
16
|
? 'https://sandbox.api.sgroup.qq.com'
|
package/lib/qq/sdk/config.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
const cfg = {
|
|
2
2
|
appID: '',
|
|
3
3
|
token: '',
|
|
4
4
|
secret: '',
|
|
@@ -6,18 +6,20 @@ let cfg = {
|
|
|
6
6
|
sandbox: false
|
|
7
7
|
};
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
10
|
-
* @
|
|
9
|
+
*
|
|
10
|
+
* @param key
|
|
11
|
+
* @param val
|
|
11
12
|
*/
|
|
12
|
-
export function
|
|
13
|
-
|
|
13
|
+
export function setBotConfig(key, val) {
|
|
14
|
+
if (Object.prototype.hasOwnProperty.call(cfg, key)) {
|
|
15
|
+
cfg[key] = val;
|
|
16
|
+
}
|
|
14
17
|
}
|
|
15
18
|
/**
|
|
16
|
-
*
|
|
17
|
-
* @param
|
|
19
|
+
*
|
|
20
|
+
* @param key
|
|
18
21
|
* @returns
|
|
19
22
|
*/
|
|
20
|
-
export function
|
|
21
|
-
cfg
|
|
22
|
-
return;
|
|
23
|
+
export function getBotConfig(key) {
|
|
24
|
+
return cfg[key];
|
|
23
25
|
}
|
package/lib/qq/sdk/wss.js
CHANGED
|
@@ -35,7 +35,7 @@ export async function createClient(callBack, shard = [0, 4]) {
|
|
|
35
35
|
if (gatewayUrl) {
|
|
36
36
|
const ws = new WebSocket(gatewayUrl);
|
|
37
37
|
ws.on('open', () => {
|
|
38
|
-
console.info('
|
|
38
|
+
console.info('[ws] open');
|
|
39
39
|
});
|
|
40
40
|
/**
|
|
41
41
|
* 标记是否已连接
|
|
@@ -95,7 +95,9 @@ export async function createClient(callBack, shard = [0, 4]) {
|
|
|
95
95
|
// OpCode 10 Hello 消息,处理心跳周期
|
|
96
96
|
isConnected = true;
|
|
97
97
|
heartbeat_interval = data.heartbeat_interval;
|
|
98
|
-
const
|
|
98
|
+
const appID = getBotConfig('appID');
|
|
99
|
+
const token = getBotConfig('token');
|
|
100
|
+
const intents = getBotConfig('intents');
|
|
99
101
|
/**
|
|
100
102
|
* 发送鉴权
|
|
101
103
|
*/
|
package/lib/villa/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { checkRobotByVilla } from './login.js';
|
|
2
|
-
import { hmacSha256 } from './sdk/index.js';
|
|
2
|
+
import { hmacSha256, createClient } from './sdk/index.js';
|
|
3
3
|
import { getBotConfigByKey } from '../config/index.js';
|
|
4
|
-
import { createClient } from './sdk/wss.js';
|
|
5
4
|
import { conversation } from './alemon/conversation.js';
|
|
6
5
|
export async function createAlemonByVilla() {
|
|
7
6
|
// 登录
|
package/lib/villa/sdk/api.js
CHANGED
|
@@ -3,7 +3,7 @@ import FormData from 'form-data';
|
|
|
3
3
|
import { createHash } from 'crypto';
|
|
4
4
|
import { ApiEnum } from './types.js';
|
|
5
5
|
import { createPicFrom } from '../../core/index.js';
|
|
6
|
-
import {
|
|
6
|
+
import { getBotConfig } from './config.js';
|
|
7
7
|
import { ApiLog } from './log.js';
|
|
8
8
|
/**
|
|
9
9
|
* 别野服务
|
|
@@ -12,13 +12,14 @@ import { ApiLog } from './log.js';
|
|
|
12
12
|
* @returns
|
|
13
13
|
*/
|
|
14
14
|
export async function villaService(config) {
|
|
15
|
-
const
|
|
15
|
+
const bot_id = getBotConfig('bot_id');
|
|
16
|
+
const bot_secret = getBotConfig('bot_secret');
|
|
16
17
|
const service = axios.create({
|
|
17
18
|
baseURL: 'https://bbs-api.miyoushe.com', // 地址
|
|
18
19
|
timeout: 6000, // 响应
|
|
19
20
|
headers: {
|
|
20
|
-
'x-rpc-bot_id':
|
|
21
|
-
'x-rpc-bot_secret':
|
|
21
|
+
'x-rpc-bot_id': bot_id, // 账号
|
|
22
|
+
'x-rpc-bot_secret': bot_secret // 密码
|
|
22
23
|
}
|
|
23
24
|
});
|
|
24
25
|
return await service(config);
|
package/lib/villa/sdk/config.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* 配置
|
|
3
|
-
*/
|
|
4
|
-
let ClientCfg = {
|
|
1
|
+
const cfg = {
|
|
5
2
|
bot_id: '',
|
|
6
3
|
bot_secret: '',
|
|
7
4
|
pub_key: '',
|
|
@@ -9,16 +6,20 @@ let ClientCfg = {
|
|
|
9
6
|
token: ''
|
|
10
7
|
};
|
|
11
8
|
/**
|
|
12
|
-
*
|
|
13
|
-
* @param
|
|
9
|
+
*
|
|
10
|
+
* @param key
|
|
11
|
+
* @param val
|
|
14
12
|
*/
|
|
15
|
-
export function
|
|
16
|
-
|
|
13
|
+
export function setBotConfig(key, val) {
|
|
14
|
+
if (Object.prototype.hasOwnProperty.call(cfg, key)) {
|
|
15
|
+
cfg[key] = val;
|
|
16
|
+
}
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
|
-
*
|
|
19
|
+
*
|
|
20
|
+
* @param key
|
|
20
21
|
* @returns
|
|
21
22
|
*/
|
|
22
|
-
export function
|
|
23
|
-
return
|
|
23
|
+
export function getBotConfig(key) {
|
|
24
|
+
return cfg[key];
|
|
24
25
|
}
|
package/lib/villa/sdk/index.js
CHANGED
package/lib/villa/sdk/wss.js
CHANGED
|
@@ -3,7 +3,7 @@ import axios from 'axios';
|
|
|
3
3
|
import { Counter } from './counter.js';
|
|
4
4
|
import { createMessage, parseMessage } from './data.js';
|
|
5
5
|
import { ProtoCommand, ProtoModel } from './proto.js';
|
|
6
|
-
import {
|
|
6
|
+
import { setBotConfig } from './config.js';
|
|
7
7
|
const counter = new Counter(1); // 初始值为1
|
|
8
8
|
/**
|
|
9
9
|
* 别野服务
|
|
@@ -30,7 +30,11 @@ export async function getWebsocketInfo(bot_id, bot_secret) {
|
|
|
30
30
|
* @returns
|
|
31
31
|
*/
|
|
32
32
|
export async function createClient(options, conversation) {
|
|
33
|
-
|
|
33
|
+
setBotConfig('bot_id', options.bot_id);
|
|
34
|
+
setBotConfig('bot_secret', options.bot_secret);
|
|
35
|
+
setBotConfig('pub_key', options.pub_key);
|
|
36
|
+
setBotConfig('token', options.token);
|
|
37
|
+
setBotConfig('villa_id', options?.villa_id ?? 0);
|
|
34
38
|
const data = await getWebsocketInfo(options.bot_id, options.bot_secret).then(res => res.data);
|
|
35
39
|
if (!data?.websocket_url) {
|
|
36
40
|
console.log('鉴权失败');
|
|
@@ -38,7 +42,7 @@ export async function createClient(options, conversation) {
|
|
|
38
42
|
}
|
|
39
43
|
const ws = new WebSocket(data.websocket_url);
|
|
40
44
|
ws.on('open', async () => {
|
|
41
|
-
console.
|
|
45
|
+
console.info('[ws] open');
|
|
42
46
|
// login
|
|
43
47
|
ws.send(createMessage({
|
|
44
48
|
ID: counter.getNextID(),
|
package/package.json
CHANGED
package/types/discord/index.d.ts
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
|
+
interface ClientConfig {
|
|
2
|
+
token: string;
|
|
3
|
+
intent: number;
|
|
4
|
+
}
|
|
1
5
|
/**
|
|
2
6
|
*
|
|
7
|
+
* @param key
|
|
3
8
|
* @param val
|
|
4
9
|
*/
|
|
5
|
-
export declare function
|
|
10
|
+
export declare function setBotConfig<T extends keyof ClientConfig>(key: T, val: ClientConfig[T]): void;
|
|
6
11
|
/**
|
|
7
12
|
*
|
|
13
|
+
* @param key
|
|
8
14
|
* @returns
|
|
9
15
|
*/
|
|
10
|
-
export declare function
|
|
11
|
-
|
|
12
|
-
intent: number;
|
|
13
|
-
};
|
|
16
|
+
export declare function getBotConfig<T extends keyof ClientConfig>(key: T): ClientConfig[T] | undefined;
|
|
17
|
+
export {};
|
|
@@ -1,10 +1,16 @@
|
|
|
1
|
+
interface ClientConfig {
|
|
2
|
+
token: string;
|
|
3
|
+
}
|
|
1
4
|
/**
|
|
2
5
|
*
|
|
6
|
+
* @param key
|
|
3
7
|
* @param val
|
|
4
8
|
*/
|
|
5
|
-
export declare function
|
|
9
|
+
export declare function setBotConfig<T extends keyof ClientConfig>(key: T, val: ClientConfig[T]): void;
|
|
6
10
|
/**
|
|
7
11
|
*
|
|
12
|
+
* @param key
|
|
8
13
|
* @returns
|
|
9
14
|
*/
|
|
10
|
-
export declare function
|
|
15
|
+
export declare function getBotConfig<T extends keyof ClientConfig>(key: T): ClientConfig[T] | undefined;
|
|
16
|
+
export {};
|
|
@@ -1,24 +1,21 @@
|
|
|
1
|
-
interface BotCaCheType {
|
|
1
|
+
export interface BotCaCheType {
|
|
2
2
|
appID: string;
|
|
3
3
|
token: string;
|
|
4
4
|
secret: string;
|
|
5
5
|
intents: number;
|
|
6
6
|
isPrivate?: boolean;
|
|
7
7
|
sandbox?: boolean;
|
|
8
|
+
shard?: number[];
|
|
8
9
|
}
|
|
9
10
|
/**
|
|
10
|
-
*
|
|
11
|
-
* @
|
|
12
|
-
*/
|
|
13
|
-
export declare function getBotConfig(): BotCaCheType;
|
|
14
|
-
/**
|
|
15
|
-
* 设置机器人配置
|
|
11
|
+
*
|
|
12
|
+
* @param key
|
|
16
13
|
* @param val
|
|
17
|
-
* @returns
|
|
18
14
|
*/
|
|
19
|
-
export declare function setBotConfig(val: BotCaCheType): void;
|
|
15
|
+
export declare function setBotConfig<T extends keyof BotCaCheType>(key: T, val: BotCaCheType[T]): void;
|
|
20
16
|
/**
|
|
21
|
-
*
|
|
17
|
+
*
|
|
18
|
+
* @param key
|
|
19
|
+
* @returns
|
|
22
20
|
*/
|
|
23
|
-
export declare function
|
|
24
|
-
export {};
|
|
21
|
+
export declare function getBotConfig<T extends keyof BotCaCheType>(key: T): BotCaCheType[T] | undefined;
|
package/types/ntqq/sdk/wss.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import { type BotCaCheType } from './config.js';
|
|
1
2
|
/**
|
|
2
|
-
*
|
|
3
|
-
* @param
|
|
4
|
-
* @param
|
|
3
|
+
*
|
|
4
|
+
* @param cfg
|
|
5
|
+
* @param conversation
|
|
5
6
|
*/
|
|
6
|
-
export declare function createClient(conversation: (...args: any[]) => any
|
|
7
|
+
export declare function createClient(cfg: BotCaCheType, conversation: (...args: any[]) => any): Promise<void>;
|
|
7
8
|
/**
|
|
8
9
|
|
|
9
10
|
1 Heartbeat Send/Receive 客户端或服务端发送心跳
|
package/types/qq/sdk/config.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { BotConfig } from './typings.js';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
* @
|
|
3
|
+
*
|
|
4
|
+
* @param key
|
|
5
|
+
* @param val
|
|
5
6
|
*/
|
|
6
|
-
export declare function
|
|
7
|
+
export declare function setBotConfig<T extends keyof BotConfig>(key: T, val: BotConfig[T]): void;
|
|
7
8
|
/**
|
|
8
|
-
*
|
|
9
|
-
* @param
|
|
9
|
+
*
|
|
10
|
+
* @param key
|
|
10
11
|
* @returns
|
|
11
12
|
*/
|
|
12
|
-
export declare function
|
|
13
|
+
export declare function getBotConfig<T extends keyof BotConfig>(key: T): BotConfig[T] | undefined;
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { type ClientConfig } from './types.js';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
* @param
|
|
3
|
+
*
|
|
4
|
+
* @param key
|
|
5
|
+
* @param val
|
|
5
6
|
*/
|
|
6
|
-
export declare function
|
|
7
|
+
export declare function setBotConfig<T extends keyof ClientConfig>(key: T, val: ClientConfig[T]): void;
|
|
7
8
|
/**
|
|
8
|
-
*
|
|
9
|
+
*
|
|
10
|
+
* @param key
|
|
9
11
|
* @returns
|
|
10
12
|
*/
|
|
11
|
-
export declare function
|
|
13
|
+
export declare function getBotConfig<T extends keyof ClientConfig>(key: T): ClientConfig[T] | undefined;
|