onebots 0.0.8 → 0.0.10

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/README.md CHANGED
@@ -49,14 +49,14 @@ onebots
49
49
  ```
50
50
  # 使用接口管理oneBot
51
51
 
52
- | url | method | params | desc |
53
- |:--------| :--- |:-----------------|:----------------------|
54
- | /list | GET | | 获取当前运行的机器人列表 |
55
- | /detail | GET | uin | 获取指定机器人配置 |
56
- | /qrcode | GET | uin | 获取指定机器人登录二维码 |
57
- | /add | POST | {uin,...config} | 添加机器人 config 为机器人配置 |
58
- | /edit | POST | {uin,...config} | 修改机器人配置 config 为机器人配置 |
59
- | /remove | get | uin | 移除机器人 |
52
+ | url | method | params | desc |
53
+ |:--------| :--- |:----------------|:-------------------------------|
54
+ | /list | GET | | 获取当前运行的机器人列表 |
55
+ | /detail | GET | uin | 获取指定机器人配置 |
56
+ | /qrcode | GET | uin | 获取指定机器人登录二维码 |
57
+ | /add | POST | {uin,...config} | 添加机器人 config 为机器人配置 |
58
+ | /edit | POST | {uin,...config} | 修改机器人配置 config 为机器人配置 |
59
+ | /remove | get | uin,force | 移除机器人,force为true时,将删除机器人data目录 |
60
60
 
61
61
  # 鸣谢
62
62
  1. [takayama-lily/oicq](https://github.com/takayama-lily/oicq) 底层服务支持
package/lib/onebot.d.ts CHANGED
@@ -18,9 +18,9 @@ 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(): void;
21
+ start(): Promise<void>;
22
22
  startListen(): void;
23
- stop(): void;
23
+ stop(force?: boolean): Promise<void>;
24
24
  dispatch(data: any): void;
25
25
  }
26
26
  export declare enum OneBotStatus {
package/lib/onebot.js CHANGED
@@ -49,23 +49,23 @@ class OneBot extends events_1.EventEmitter {
49
49
  }
50
50
  });
51
51
  }
52
- start() {
52
+ async start() {
53
53
  this.startListen();
54
- this.client.login(this.password);
54
+ await this.client.login(this.password);
55
55
  }
56
56
  startListen() {
57
57
  this.client.on('system', this.dispatch.bind(this));
58
58
  this.client.on('notice', this.dispatch.bind(this));
59
59
  this.client.on('request', this.dispatch.bind(this));
60
60
  this.client.on('message', this.dispatch.bind(this));
61
- this.instances.forEach(instance => {
61
+ for (const instance of this.instances) {
62
62
  instance.start(this.instances.length > 1 ? '/' + instance.version : undefined);
63
- });
63
+ }
64
64
  }
65
- stop() {
66
- this.instances.forEach(instance => {
67
- instance.stop();
68
- });
65
+ async stop(force) {
66
+ for (const instance of this.instances) {
67
+ await instance.stop(force);
68
+ }
69
69
  this.client.off('system', this.dispatch.bind(this));
70
70
  this.client.off('notice', this.dispatch.bind(this));
71
71
  this.client.off('request', this.dispatch.bind(this));
@@ -31,7 +31,7 @@ export declare class App extends Koa {
31
31
  private createOneBots;
32
32
  addAccount(uin: number | `${number}`, config: MayBeArray<OneBot.Config<OneBot.Version>>): void;
33
33
  updateAccount(uin: number | `${number}`, config: MayBeArray<OneBot.Config<OneBot.Version>>): void;
34
- removeAccount(uin: number | `${number}`): void;
34
+ removeAccount(uin: number | `${number}`, force?: boolean): void;
35
35
  createOneBot(uin: number, config: MayBeArray<OneBot.Config<OneBot.Version>>): OneBot<OneBot.Version>;
36
36
  start(): void;
37
37
  }
package/lib/server/app.js CHANGED
@@ -100,7 +100,7 @@ class App extends koa_1.default {
100
100
  this.removeAccount(uin);
101
101
  this.addAccount(uin, newConfig);
102
102
  }
103
- removeAccount(uin) {
103
+ removeAccount(uin, force) {
104
104
  if (typeof uin !== "number")
105
105
  uin = Number(uin);
106
106
  if (Number.isNaN(uin))
@@ -109,7 +109,7 @@ class App extends koa_1.default {
109
109
  if (currentIdx < 0)
110
110
  throw new Error('账户不存在');
111
111
  const oneBot = this.oneBots[currentIdx];
112
- oneBot.stop();
112
+ oneBot.stop(force);
113
113
  delete this.config[uin];
114
114
  this.oneBots.splice(currentIdx, 1);
115
115
  (0, fs_1.writeFileSync)(App.configPath, js_yaml_1.default.dump(this.config));
@@ -181,9 +181,9 @@ class App extends koa_1.default {
181
181
  }
182
182
  });
183
183
  this.router.get('/remove', (ctx, next) => {
184
- const { uin } = ctx.request.query;
184
+ const { uin, force } = ctx.request.query;
185
185
  try {
186
- this.removeAccount(Number(uin));
186
+ this.removeAccount(Number(uin), Boolean(force));
187
187
  ctx.body = `移除成功`;
188
188
  }
189
189
  catch (e) {
@@ -61,5 +61,5 @@ export declare class CommonAction {
61
61
  submitSlider(this: V11, ticket: string): Promise<any>;
62
62
  login(this: V11, password?: string): any;
63
63
  submitSmsCode(this: V11, code: string): Promise<any>;
64
- sendSmsCode(this: V11): void;
64
+ sendSmsCode(this: V11): Promise<any>;
65
65
  }
@@ -112,7 +112,21 @@ class CommonAction {
112
112
  return this.action.callLogin.apply(this, ['submitSmsCode', code]);
113
113
  }
114
114
  sendSmsCode() {
115
- return this.client.sendSmsCode();
115
+ return new Promise(resolve => {
116
+ const receiveResult = (e) => {
117
+ const callback = (data) => {
118
+ this.client.off('internal.verbose', receiveResult);
119
+ this.client.off('system.login.error', receiveResult);
120
+ resolve(data);
121
+ };
122
+ if ((typeof e === 'string' && e.includes('已发送')) || typeof e !== 'string') {
123
+ callback(e);
124
+ }
125
+ };
126
+ this.client.on('internal.verbose', receiveResult);
127
+ this.client.on('system.login.error', receiveResult);
128
+ this.client.sendSmsCode();
129
+ });
116
130
  }
117
131
  }
118
132
  exports.CommonAction = CommonAction;
@@ -32,7 +32,7 @@ export declare class V11 extends EventEmitter implements OneBot.Base {
32
32
  private startHttpReverse;
33
33
  private startWs;
34
34
  private startWsReverse;
35
- stop(): void;
35
+ stop(force?: boolean): Promise<void>;
36
36
  dispatch(data: any): void;
37
37
  private _httpRequestHandler;
38
38
  /**
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.V11 = void 0;
7
+ const oicq_1 = require("oicq");
7
8
  const action_1 = require("./action");
8
9
  const ws_1 = require("ws");
9
10
  const url_1 = require("url");
@@ -13,6 +14,7 @@ const onebot_1 = require("../../onebot");
13
14
  const http_1 = __importDefault(require("http"));
14
15
  const https_1 = __importDefault(require("https"));
15
16
  const events_1 = require("events");
17
+ const fs_1 = require("fs");
16
18
  class V11 extends events_1.EventEmitter {
17
19
  constructor(oneBot, client, config) {
18
20
  super();
@@ -161,8 +163,13 @@ class V11 extends events_1.EventEmitter {
161
163
  }
162
164
  });
163
165
  }
164
- stop() {
165
- this.client.logout();
166
+ async stop(force) {
167
+ if (this.client.status === oicq_1.OnlineStatus.Online) {
168
+ await this.client.logout();
169
+ }
170
+ if (force) {
171
+ (0, fs_1.rmSync)(this.client.dir, { force: true, recursive: true });
172
+ }
166
173
  }
167
174
  dispatch(data) {
168
175
  if (!data.post_type)
@@ -28,6 +28,6 @@ export declare class CommonAction {
28
28
  submitSlider(this: V11, ticket: string): Promise<any>;
29
29
  login(this: V11, password?: string): any;
30
30
  submitSmsCode(this: V11, code: string): Promise<any>;
31
- sendSmsCode(this: V12): void;
31
+ sendSmsCode(this: V12): Promise<any>;
32
32
  getSupportedActions(this: V12): string[];
33
33
  }
@@ -73,7 +73,21 @@ class CommonAction {
73
73
  return this.action.callLogin.apply(this, ['submitSmsCode', code]);
74
74
  }
75
75
  sendSmsCode() {
76
- return this.client.sendSmsCode();
76
+ return new Promise(resolve => {
77
+ const receiveResult = (e) => {
78
+ const callback = (data) => {
79
+ this.client.off('internal.verbose', receiveResult);
80
+ this.client.off('system.login.error', receiveResult);
81
+ resolve(data);
82
+ };
83
+ if ((typeof e === 'string' && e.includes('已发送')) || typeof e !== 'string') {
84
+ callback(e);
85
+ }
86
+ };
87
+ this.client.on('internal.verbose', receiveResult);
88
+ this.client.on('system.login.error', receiveResult);
89
+ this.client.sendSmsCode();
90
+ });
77
91
  }
78
92
  getSupportedActions() {
79
93
  return [...new Set((0, utils_1.getProperties)(this.action))].filter(key => {
@@ -28,7 +28,7 @@ export declare class V12 extends EventEmitter implements OneBot.Base {
28
28
  private startWebhook;
29
29
  private startWs;
30
30
  private startWsReverse;
31
- stop(): Promise<void>;
31
+ stop(force?: boolean): Promise<void>;
32
32
  dispatch<T extends Record<string, any>>(data?: T): void;
33
33
  apply(req: any): Promise<string>;
34
34
  private _httpRequestHandler;
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.V12 = void 0;
7
+ const oicq_1 = require("oicq");
7
8
  const path_1 = require("path");
8
9
  const onebot_1 = require("../../onebot");
9
10
  const action_1 = require("./action");
@@ -16,6 +17,7 @@ const oicq2_cq_enable_1 = require("oicq2-cq-enable");
16
17
  const utils_1 = require("../../utils");
17
18
  const db_1 = require("../../db");
18
19
  const app_1 = require("../../server/app");
20
+ const fs_1 = require("fs");
19
21
  class V12 extends events_1.EventEmitter {
20
22
  constructor(oneBot, client, config) {
21
23
  super();
@@ -197,8 +199,13 @@ class V12 extends events_1.EventEmitter {
197
199
  }
198
200
  });
199
201
  }
200
- stop() {
201
- return this.client.logout();
202
+ async stop(force) {
203
+ if (this.client.status === oicq_1.OnlineStatus.Online) {
204
+ await this.client.logout();
205
+ }
206
+ if (force) {
207
+ (0, fs_1.rmSync)(this.client.dir, { force: true, recursive: true });
208
+ }
202
209
  }
203
210
  dispatch(data = {}) {
204
211
  if (!data)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "onebots",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "description": "基于oicq的多例oneBot实现",
5
5
  "main": "lib/index.js",
6
6
  "bin": {