onebots 0.2.5 → 0.2.6

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/onebot.d.ts CHANGED
@@ -18,7 +18,7 @@ export declare class OneBot<V extends OneBot.Version> extends EventEmitter {
18
18
  client: Client;
19
19
  instances: (V11 | V12)[];
20
20
  constructor(app: App, uin: number, config: MayBeArray<OneBotConfig>);
21
- start(): Promise<void>;
21
+ start(): Promise<[boolean, any]>;
22
22
  startListen(): void;
23
23
  stop(force?: boolean): Promise<void>;
24
24
  dispatch(event: any, data: any): void;
package/lib/onebot.js CHANGED
@@ -73,7 +73,7 @@ class OneBot extends events_1.EventEmitter {
73
73
  }
74
74
  });
75
75
  }
76
- async start() {
76
+ start() {
77
77
  this.startListen();
78
78
  const disposeArr = [];
79
79
  const clean = () => {
@@ -93,7 +93,7 @@ class OneBot extends events_1.EventEmitter {
93
93
  this.client.trap('system.login.device', function deviceHelper(e) {
94
94
  console.log('请选择验证方式:1.短信验证 2.url验证');
95
95
  process.stdin.once('data', (buf) => {
96
- const input = e.toString().trim();
96
+ const input = buf.toString().trim();
97
97
  if (input === '1') {
98
98
  this.sendSmsCode();
99
99
  console.log('请输入短信验证码:');
@@ -133,7 +133,25 @@ class OneBot extends events_1.EventEmitter {
133
133
  });
134
134
  });
135
135
  this.client.trap('system.online', clean);
136
- await this.client.login(this.uin, this.password);
136
+ return new Promise(async (resolve) => {
137
+ const callback = (result) => {
138
+ if (timer) {
139
+ clearTimeout(timer);
140
+ timer = null;
141
+ }
142
+ resolve(result);
143
+ while (disposes.length) {
144
+ const dispose = disposes.shift();
145
+ dispose();
146
+ }
147
+ };
148
+ let timer = setTimeout(() => {
149
+ callback([false, '登录超时']);
150
+ }, this.app.config.timeout * 1000);
151
+ await this.client.login(this.uin, this.password);
152
+ const disposes = [this.client.trapOnce('system.online', () => { callback([true, null]); }),
153
+ this.client.trapOnce('system.login.error', (e) => callback([false, e.message]))];
154
+ });
137
155
  }
138
156
  startListen() {
139
157
  this.client.trap('system', this.dispatch.bind(this, 'system'));
@@ -33,7 +33,7 @@ export declare class App extends Koa {
33
33
  updateAccount(uin: number | `${number}`, config: MayBeArray<OneBot.Config<OneBot.Version>>): void;
34
34
  removeAccount(uin: number | `${number}`, force?: boolean): void;
35
35
  createOneBot(uin: number, config: MayBeArray<OneBot.Config<OneBot.Version>>): OneBot<OneBot.Version>;
36
- start(): void;
36
+ start(): Promise<void>;
37
37
  }
38
38
  export declare function createApp(config?: App.Config | string): App;
39
39
  export declare function defineConfig(config: App.Config): App.Config;
@@ -41,6 +41,7 @@ export declare namespace App {
41
41
  type Config = {
42
42
  port?: number;
43
43
  path?: string;
44
+ timeout?: number;
44
45
  log_level?: LogLevel;
45
46
  platform?: Platform;
46
47
  general?: {
package/lib/server/app.js CHANGED
@@ -119,16 +119,7 @@ class App extends koa_1.default {
119
119
  this.oneBots.push(oneBot);
120
120
  return oneBot;
121
121
  }
122
- start() {
123
- for (const oneBot of this.oneBots) {
124
- oneBot.start();
125
- }
126
- process.on('uncaughtException', (e) => {
127
- console.error('uncaughtException', e);
128
- });
129
- process.on('unhandledRejection', (e) => {
130
- console.error('unhandledRejection', e);
131
- });
122
+ async start() {
132
123
  this.httpServer.listen(this.config.port);
133
124
  this.router.get('/list', (ctx) => {
134
125
  ctx.body = this.oneBots.map(bot => {
@@ -197,7 +188,18 @@ class App extends koa_1.default {
197
188
  ctx.body = e.message;
198
189
  }
199
190
  });
191
+ process.on('uncaughtException', (e) => {
192
+ console.error('uncaughtException', e);
193
+ });
194
+ process.on('unhandledRejection', (e) => {
195
+ console.error('unhandledRejection', e);
196
+ });
200
197
  this.logger.mark(`server listen at http://0.0.0.0:${this.config.port}/${this.config.path ? this.config.path : ''}`);
198
+ for (const oneBot of this.oneBots) {
199
+ const [isSuccess, reason] = await oneBot.start();
200
+ if (!isSuccess)
201
+ this.logger.warn(`【${oneBot.uin}】: 登录失败,错误信息\n:`, reason);
202
+ }
201
203
  }
202
204
  }
203
205
  _a = App;
@@ -231,6 +233,7 @@ exports.defineConfig = defineConfig;
231
233
  (function (App) {
232
234
  App.defaultConfig = {
233
235
  port: 6727,
236
+ timeout: 30,
234
237
  platform: 5,
235
238
  general: {
236
239
  V11: V11_1.V11.defaultConfig,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "onebots",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "description": "基于icqq的多例oneBot实现",
5
5
  "engines": {
6
6
  "node": ">=16"