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.
@@ -1,34 +1,20 @@
1
- import { getIntents, setDISOCRD, createClient } from './sdk/index.js';
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';
@@ -1,5 +1,5 @@
1
1
  import axios from 'axios';
2
- import { getDISCORD } from './config.js';
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 { token } = getDISCORD();
10
+ const token = getBotConfig('token');
11
11
  const service = axios.create({
12
12
  baseURL: 'https://discord.com/api/v10',
13
13
  timeout: 6000,
@@ -1,9 +1,9 @@
1
1
  import axios from 'axios';
2
- import { getDISCORD } from './config.js';
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 { token } = getDISCORD();
6
+ const token = getBotConfig('token');
7
7
  const service = axios.create({
8
8
  baseURL: BaseUrl,
9
9
  timeout: 6000,
@@ -1,20 +1,22 @@
1
- let token = '';
2
- let intent = 0;
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 setDISOCRD(val, i) {
8
- token = val;
9
- intent = i;
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 getDISCORD() {
16
- return {
17
- token,
18
- intent
19
- };
20
+ export function getBotConfig(key) {
21
+ return cfg[key];
20
22
  }
@@ -1,6 +1,6 @@
1
1
  import WebSocket from 'ws';
2
2
  import { gateway } from './api.js';
3
- import { getDISCORD } from './config.js';
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 { token, intent } = getDISCORD();
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
  /**
@@ -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 { getKookToken } from './config.js';
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 = getKookToken();
23
+ const token = getBotConfig('token');
24
24
  const service = axios.create({
25
25
  baseURL: 'https://www.kookapp.cn',
26
26
  timeout: 30000,
@@ -1,15 +1,21 @@
1
- let token = '';
1
+ const cfg = {
2
+ token: ''
3
+ };
2
4
  /**
3
5
  *
6
+ * @param key
4
7
  * @param val
5
8
  */
6
- export function setKookToken(val) {
7
- token = val;
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 getKookToken() {
14
- return token;
19
+ export function getBotConfig(key) {
20
+ return cfg[key];
15
21
  }
@@ -1,6 +1,6 @@
1
1
  import WebSocket from 'ws';
2
2
  import axios from 'axios';
3
- import { setKookToken } from './config.js';
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
- setKookToken(token);
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('token ok');
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, setTimeoutBotConfig, getIntentsMask, setBotConfig } from './sdk/index.js';
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
- if (cfg.mode == 'qq-guild') {
19
- setBotConfig({
20
- appID: cfg.appID,
21
- token: cfg.token,
22
- secret: cfg.secret,
23
- intents: intents,
24
- isPrivate: cfg.isPrivate,
25
- sandbox: cfg.sandbox
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 { token, appID } = getBotConfig();
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 { appID, token, sandbox } = getBotConfig();
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,
@@ -1,63 +1,28 @@
1
- import { getAuthentication } from './api/auth.js';
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
- * @returns
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 = val;
27
- return;
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 async function setTimeoutBotConfig(cfg) {
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
  }
@@ -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
- * 使用获取到的网关连接地址建立 WebSocket 连接
8
- * @param token
9
- * @param callBack
7
+ * 定时鉴权
8
+ * @param cfg
9
+ * @returns
10
10
  */
11
- export async function createClient(conversation, shard = [0, 1]) {
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
- console.info('reconnecting...');
21
- // 延迟一段时间后发起重新连接
22
- await new Promise(resolve => setTimeout(resolve, 5000));
23
- // 调用createClient函数重新建立连接
24
- await createClient(conversation, shard);
25
- counter.getNextID();
50
+ setTimeout(() => {
51
+ console.info('reconnecting...');
52
+ // 重新starrt
53
+ start();
54
+ // 记录
55
+ counter.getNextID();
56
+ }, 5000);
26
57
  };
27
- if (gatewayUrl) {
28
- const ws = new WebSocket(gatewayUrl);
29
- ws.on('open', () => {
30
- console.info('TOKEN ok');
31
- });
32
- // 标记是否已连接
33
- let isConnected = false;
34
- // 存储最新的消息序号
35
- let heartbeat_interval = 30000;
36
- // 鉴权
37
- let power;
38
- const map = {
39
- 0: ({ t, d }) => {
40
- // 存在,则执行t对应的函数
41
- conversation(t, d);
42
- // Ready Event,鉴权成功
43
- if (t === 'READY') {
44
- power = setInterval(() => {
45
- if (isConnected) {
46
- ws.send(JSON.stringify({
47
- op: 1, // op = 1
48
- d: null // 如果是第一次连接,传null
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
- }, heartbeat_interval);
52
- }
53
- // Resumed Event,恢复连接成功
54
- if (t === 'RESUMED') {
55
- console.info('restore connection');
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
- 6: message => {
61
- console.info('connection attempt', message);
62
- },
63
- 7: async (message) => {
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
- 9: message => {
72
- console.info('parameter error', message);
73
- },
74
- 10: ({ d }) => {
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
- * 设置 qq-channal 配置
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 { appID, token, sandbox } = getBotConfig();
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'
@@ -1,4 +1,4 @@
1
- let cfg = {
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
- * @returns
9
+ *
10
+ * @param key
11
+ * @param val
11
12
  */
12
- export function getBotConfig() {
13
- return cfg;
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 val
19
+ *
20
+ * @param key
18
21
  * @returns
19
22
  */
20
- export function setBotConfig(val) {
21
- cfg = val;
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('token ok');
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 { appID, token, intents } = getBotConfig();
98
+ const appID = getBotConfig('appID');
99
+ const token = getBotConfig('token');
100
+ const intents = getBotConfig('intents');
99
101
  /**
100
102
  * 发送鉴权
101
103
  */
@@ -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
  // 登录
@@ -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 { getClientConfig } from './config.js';
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 ClientCfg = getClientConfig();
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': ClientCfg.bot_id, // 账号
21
- 'x-rpc-bot_secret': ClientCfg.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);
@@ -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 cfg
9
+ *
10
+ * @param key
11
+ * @param val
14
12
  */
15
- export function setClientConfig(cfg) {
16
- ClientCfg = cfg;
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 getClientConfig() {
23
- return ClientCfg;
23
+ export function getBotConfig(key) {
24
+ return cfg[key];
24
25
  }
@@ -1,3 +1,5 @@
1
1
  export * as ClientVILLA from './all.js';
2
2
  export * from './hs.js';
3
3
  export * from './types.js';
4
+ export * from './config.js';
5
+ export * from './wss.js';
@@ -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 { setClientConfig } from './config.js';
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
- setClientConfig(options);
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.log('open');
45
+ console.info('[ws] open');
42
46
  // login
43
47
  ws.send(createMessage({
44
48
  ID: counter.getNextID(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "alemonjs",
3
- "version": "1.1.16",
3
+ "version": "1.1.18",
4
4
  "description": "DOCS https://alemonjs.com/",
5
5
  "scripts": {
6
6
  "dev": "ts-node alemon.config.ts",
@@ -1,6 +1,2 @@
1
- /**
2
- * 创建实例
3
- * @returns
4
- */
5
1
  export declare function createAlemonByDISCORD(): Promise<boolean>;
6
2
  export { ClientDISOCRD } from './sdk/index.js';
@@ -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 setDISOCRD(val: string, i: number): void;
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 getDISCORD(): {
11
- token: string;
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 setKookToken(val: string): void;
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 getKookToken(): string;
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
- * @returns
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 setTimeoutBotConfig(cfg: BotCaCheType): Promise<void>;
24
- export {};
21
+ export declare function getBotConfig<T extends keyof BotCaCheType>(key: T): BotCaCheType[T] | undefined;
@@ -1,9 +1,10 @@
1
+ import { type BotCaCheType } from './config.js';
1
2
  /**
2
- * 使用获取到的网关连接地址建立 WebSocket 连接
3
- * @param token
4
- * @param callBack
3
+ *
4
+ * @param cfg
5
+ * @param conversation
5
6
  */
6
- export declare function createClient(conversation: (...args: any[]) => any, shard?: number[]): Promise<void>;
7
+ export declare function createClient(cfg: BotCaCheType, conversation: (...args: any[]) => any): Promise<void>;
7
8
  /**
8
9
 
9
10
  1 Heartbeat Send/Receive 客户端或服务端发送心跳
@@ -1,12 +1,13 @@
1
1
  import { BotConfig } from './typings.js';
2
2
  /**
3
- * 得到机器人配置
4
- * @returns
3
+ *
4
+ * @param key
5
+ * @param val
5
6
  */
6
- export declare function getBotConfig(): BotConfig;
7
+ export declare function setBotConfig<T extends keyof BotConfig>(key: T, val: BotConfig[T]): void;
7
8
  /**
8
- * 设置机器人配置
9
- * @param val
9
+ *
10
+ * @param key
10
11
  * @returns
11
12
  */
12
- export declare function setBotConfig(val: BotConfig): void;
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 cfg
3
+ *
4
+ * @param key
5
+ * @param val
5
6
  */
6
- export declare function setClientConfig(cfg: ClientConfig): void;
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 getClientConfig(): ClientConfig;
13
+ export declare function getBotConfig<T extends keyof ClientConfig>(key: T): ClientConfig[T] | undefined;
@@ -1,3 +1,5 @@
1
1
  export * as ClientVILLA from './all.js';
2
2
  export * from './hs.js';
3
3
  export * from './types.js';
4
+ export * from './config.js';
5
+ export * from './wss.js';