onebots 0.4.47 → 0.4.49
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/dist/assets/{index-yG7HG4Oa.css → index-2tNhvoZy.css} +1 -1
- package/dist/assets/index-_Kz1yWe8.js +19 -0
- package/dist/index.html +2 -2
- package/lib/adapter.js +6 -8
- package/lib/adapters/dingtalk/index.d.ts +4 -4
- package/lib/adapters/dingtalk/index.js +56 -49
- package/lib/adapters/dingtalk/utils.js +5 -20
- package/lib/adapters/icqq/index.d.ts +5 -3
- package/lib/adapters/icqq/index.js +134 -101
- package/lib/adapters/icqq/utils.js +2 -2
- package/lib/adapters/qq/index.d.ts +4 -4
- package/lib/adapters/qq/index.js +63 -52
- package/lib/adapters/qq/utils.js +5 -20
- package/lib/adapters/wechat/index.d.ts +3 -3
- package/lib/adapters/wechat/index.js +63 -52
- package/lib/adapters/wechat/utils.js +5 -20
- package/lib/bin.js +6 -6
- package/lib/db.js +10 -10
- package/lib/onebot.d.ts +12 -12
- package/lib/onebot.js +25 -16
- package/lib/server/app.d.ts +2 -2
- package/lib/server/app.js +71 -70
- package/lib/server/router.d.ts +1 -1
- package/lib/server/router.js +5 -5
- package/lib/service/V11/action/common.d.ts +1 -0
- package/lib/service/V11/action/common.js +24 -24
- package/lib/service/V11/action/friend.js +16 -8
- package/lib/service/V11/action/group.js +52 -22
- package/lib/service/V11/index.js +86 -50
- package/lib/service/V12/action/common.d.ts +3 -3
- package/lib/service/V12/action/common.js +33 -31
- package/lib/service/V12/action/friend.js +8 -4
- package/lib/service/V12/action/group.js +50 -20
- package/lib/service/V12/action/guild.js +110 -35
- package/lib/service/V12/index.d.ts +18 -18
- package/lib/service/V12/index.js +189 -147
- package/lib/service/shareMusicCustom.js +14 -9
- package/lib/service.js +29 -27
- package/lib/utils.d.ts +1 -1
- package/lib/utils.js +44 -32
- package/package.json +1 -1
- package/dist/assets/index-YN4djyjB.js +0 -19
package/lib/db.js
CHANGED
|
@@ -34,8 +34,8 @@ class JsonDB {
|
|
|
34
34
|
const dir = path.dirname(this.filePath);
|
|
35
35
|
if (fs.existsSync(dir))
|
|
36
36
|
fs.mkdirSync(dir, { recursive: true });
|
|
37
|
-
if (!this.filePath.endsWith(
|
|
38
|
-
this.filePath = this.filePath +
|
|
37
|
+
if (!this.filePath.endsWith(".jsondb"))
|
|
38
|
+
this.filePath = this.filePath + ".jsondb";
|
|
39
39
|
if (!fs.existsSync(this.filePath))
|
|
40
40
|
this.write();
|
|
41
41
|
this.init();
|
|
@@ -44,21 +44,21 @@ class JsonDB {
|
|
|
44
44
|
this.read();
|
|
45
45
|
}
|
|
46
46
|
write() {
|
|
47
|
-
fs.writeFileSync(this.filePath, (0, utils_1.stringifyObj)(this.data),
|
|
47
|
+
fs.writeFileSync(this.filePath, (0, utils_1.stringifyObj)(this.data), "utf8");
|
|
48
48
|
}
|
|
49
49
|
read() {
|
|
50
|
-
this.data = (0, utils_1.parseObjFromStr)(fs.readFileSync(this.filePath,
|
|
50
|
+
this.data = (0, utils_1.parseObjFromStr)(fs.readFileSync(this.filePath, "utf8"));
|
|
51
51
|
}
|
|
52
52
|
findIndex(route, predicate) {
|
|
53
53
|
const arr = this.getArray(route);
|
|
54
54
|
return arr.findIndex(predicate);
|
|
55
55
|
}
|
|
56
56
|
indexOf(route, item) {
|
|
57
|
-
return this.findIndex(route,
|
|
57
|
+
return this.findIndex(route, value => value === item);
|
|
58
58
|
}
|
|
59
59
|
get(route, initialValue) {
|
|
60
60
|
this.read();
|
|
61
|
-
const parentPath = route.split(
|
|
61
|
+
const parentPath = route.split(".");
|
|
62
62
|
const key = parentPath.pop();
|
|
63
63
|
if (!key)
|
|
64
64
|
return this.data;
|
|
@@ -76,11 +76,11 @@ class JsonDB {
|
|
|
76
76
|
return initialValue;
|
|
77
77
|
}
|
|
78
78
|
set(route, data) {
|
|
79
|
-
const parentPath = route.split(
|
|
79
|
+
const parentPath = route.split(".").filter(c => c.length);
|
|
80
80
|
const key = parentPath.pop();
|
|
81
81
|
if (!key)
|
|
82
82
|
throw new SyntaxError(`route can't empty`);
|
|
83
|
-
const parentObj = this.get(parentPath.join(
|
|
83
|
+
const parentObj = this.get(parentPath.join("."), {});
|
|
84
84
|
if (!parentObj)
|
|
85
85
|
throw new SyntaxError(`can't set property ${key} of undefined`);
|
|
86
86
|
parentObj[key] = data;
|
|
@@ -88,11 +88,11 @@ class JsonDB {
|
|
|
88
88
|
return data;
|
|
89
89
|
}
|
|
90
90
|
delete(route) {
|
|
91
|
-
const parentPath = route.split(
|
|
91
|
+
const parentPath = route.split(".");
|
|
92
92
|
const key = parentPath.pop();
|
|
93
93
|
if (!key)
|
|
94
94
|
throw new SyntaxError(`route can't empty`);
|
|
95
|
-
const parentObj = this.get(parentPath.join(
|
|
95
|
+
const parentObj = this.get(parentPath.join("."), {});
|
|
96
96
|
if (!parentObj)
|
|
97
97
|
throw new SyntaxError(`property ${key} is not exist of undefined`);
|
|
98
98
|
const result = delete parentObj[key];
|
package/lib/onebot.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { EventEmitter } from
|
|
2
|
+
import { EventEmitter } from "events";
|
|
3
3
|
import { V11 } from "./service/V11";
|
|
4
4
|
import { V12 } from "./service/V12";
|
|
5
5
|
import { Adapter } from "./adapter";
|
|
@@ -48,22 +48,22 @@ export declare enum OneBotStatus {
|
|
|
48
48
|
}
|
|
49
49
|
export declare namespace OneBot {
|
|
50
50
|
type Filters = {};
|
|
51
|
-
type Version =
|
|
51
|
+
type Version = "V11" | "V12";
|
|
52
52
|
type Config<V extends Version = Version> = {
|
|
53
53
|
version?: V;
|
|
54
54
|
filters?: Service.Filters;
|
|
55
|
-
} & (V extends
|
|
55
|
+
} & (V extends "V11" ? V11.Config : V12.Config);
|
|
56
56
|
const UnsupportedMethodError: Error;
|
|
57
57
|
const UnsupportedVersionError: Error;
|
|
58
|
-
type Payload<V extends Version = Version> = V extends
|
|
59
|
-
type Segment<V extends Version = Version> = V extends
|
|
60
|
-
type SelfInfo<V extends Version = Version> = V extends
|
|
61
|
-
type GroupInfo<V extends Version> = V extends
|
|
62
|
-
type UserInfo<V extends Version> = V extends
|
|
63
|
-
type Message<V extends Version> = V extends
|
|
64
|
-
type MessageElement<V extends Version> = V extends
|
|
65
|
-
type GroupMemberInfo<V extends Version> = V extends
|
|
66
|
-
type MessageRet<V extends Version> = V extends
|
|
58
|
+
type Payload<V extends Version = Version> = V extends "V11" ? V11.Payload : V12.Payload;
|
|
59
|
+
type Segment<V extends Version = Version> = V extends "V11" ? V11.Segment : V12.Segment;
|
|
60
|
+
type SelfInfo<V extends Version = Version> = V extends "V11" ? V11.SelfInfo : V12.SelfInfo;
|
|
61
|
+
type GroupInfo<V extends Version> = V extends "V11" ? V11.GroupInfo : V12.GroupInfo;
|
|
62
|
+
type UserInfo<V extends Version> = V extends "V11" ? V11.UserInfo : V12.UserInfo;
|
|
63
|
+
type Message<V extends Version> = V extends "V11" ? V11.Message : V12.Message;
|
|
64
|
+
type MessageElement<V extends Version> = V extends "V11" ? V11.MessageElement : V12.MessageElement;
|
|
65
|
+
type GroupMemberInfo<V extends Version> = V extends "V11" ? V11.GroupMemberInfo : V12.GroupMemberInfo;
|
|
66
|
+
type MessageRet<V extends Version> = V extends "V11" ? V11.MessageRet : V12.MessageRet;
|
|
67
67
|
interface Base {
|
|
68
68
|
start(path?: string): any;
|
|
69
69
|
stop(): any;
|
package/lib/onebot.js
CHANGED
|
@@ -20,7 +20,7 @@ const V12_1 = require("./service/V12");
|
|
|
20
20
|
class NotFoundError extends Error {
|
|
21
21
|
constructor() {
|
|
22
22
|
super(...arguments);
|
|
23
|
-
this.message =
|
|
23
|
+
this.message = "不支持的API";
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
exports.NotFoundError = NotFoundError;
|
|
@@ -29,16 +29,16 @@ class OneBot extends events_1.EventEmitter {
|
|
|
29
29
|
return this.adapter.app;
|
|
30
30
|
}
|
|
31
31
|
get V11() {
|
|
32
|
-
return this.instances.find(i => i.version ===
|
|
32
|
+
return this.instances.find(i => i.version === "V11");
|
|
33
33
|
}
|
|
34
34
|
get V12() {
|
|
35
|
-
return this.instances.find(i => i.version ===
|
|
35
|
+
return this.instances.find(i => i.version === "V12");
|
|
36
36
|
}
|
|
37
37
|
get platform() {
|
|
38
38
|
return this.adapter.platform;
|
|
39
39
|
}
|
|
40
40
|
get logger() {
|
|
41
|
-
return __classPrivateFieldSet(this, _OneBot_logger, __classPrivateFieldGet(this, _OneBot_logger, "f") || this.adapter.getLogger(this.uin), "f");
|
|
41
|
+
return (__classPrivateFieldSet(this, _OneBot_logger, __classPrivateFieldGet(this, _OneBot_logger, "f") || this.adapter.getLogger(this.uin), "f"));
|
|
42
42
|
}
|
|
43
43
|
get info() {
|
|
44
44
|
return {
|
|
@@ -50,7 +50,7 @@ class OneBot extends events_1.EventEmitter {
|
|
|
50
50
|
dependency: this.dependency,
|
|
51
51
|
urls: this.instances.map(ins => {
|
|
52
52
|
return `/${this.platform}/${this.uin}/${ins.version}`;
|
|
53
|
-
})
|
|
53
|
+
}),
|
|
54
54
|
};
|
|
55
55
|
}
|
|
56
56
|
constructor(adapter, uin, version_configs) {
|
|
@@ -60,24 +60,24 @@ class OneBot extends events_1.EventEmitter {
|
|
|
60
60
|
_OneBot_logger.set(this, void 0);
|
|
61
61
|
this.config = version_configs.map(c => {
|
|
62
62
|
if (!c.version)
|
|
63
|
-
c.version =
|
|
63
|
+
c.version = "V11";
|
|
64
64
|
switch (c.version) {
|
|
65
|
-
case
|
|
65
|
+
case "V11":
|
|
66
66
|
return (0, utils_1.deepMerge)((0, utils_1.deepClone)(this.adapter.app.config.general.V11), c);
|
|
67
|
-
case
|
|
67
|
+
case "V12":
|
|
68
68
|
return (0, utils_1.deepMerge)((0, utils_1.deepClone)(this.adapter.app.config.general.V12), c);
|
|
69
69
|
default:
|
|
70
|
-
throw new Error(
|
|
70
|
+
throw new Error("不支持的oneBot版本:" + c.version);
|
|
71
71
|
}
|
|
72
72
|
});
|
|
73
73
|
this.instances = this.config.map(c => {
|
|
74
74
|
switch (c.version) {
|
|
75
|
-
case
|
|
75
|
+
case "V11":
|
|
76
76
|
return new V11_1.V11(this, c);
|
|
77
|
-
case
|
|
77
|
+
case "V12":
|
|
78
78
|
return new V12_1.V12(this, c);
|
|
79
79
|
default:
|
|
80
|
-
throw new Error(
|
|
80
|
+
throw new Error("不支持的oneBot版本:" + c.version);
|
|
81
81
|
}
|
|
82
82
|
});
|
|
83
83
|
this.status = OneBotStatus.Good;
|
|
@@ -91,7 +91,7 @@ class OneBot extends events_1.EventEmitter {
|
|
|
91
91
|
for (const instance of this.instances) {
|
|
92
92
|
await instance.stop(force);
|
|
93
93
|
}
|
|
94
|
-
this.emit(
|
|
94
|
+
this.emit("stop");
|
|
95
95
|
}
|
|
96
96
|
getGroupList(version) {
|
|
97
97
|
return this.adapter.getGroupList(this.uin, version);
|
|
@@ -114,7 +114,16 @@ var OneBotStatus;
|
|
|
114
114
|
OneBotStatus["Online"] = "online";
|
|
115
115
|
})(OneBotStatus || (exports.OneBotStatus = OneBotStatus = {}));
|
|
116
116
|
(function (OneBot) {
|
|
117
|
-
OneBot.UnsupportedMethodError = new Error(
|
|
118
|
-
OneBot.UnsupportedVersionError = new Error(
|
|
117
|
+
OneBot.UnsupportedMethodError = new Error("不支持的方法");
|
|
118
|
+
OneBot.UnsupportedVersionError = new Error("不支持的oneBot版本");
|
|
119
119
|
})(OneBot || (exports.OneBot = OneBot = {}));
|
|
120
|
-
exports.BOOLS = [
|
|
120
|
+
exports.BOOLS = [
|
|
121
|
+
"no_cache",
|
|
122
|
+
"auto_escape",
|
|
123
|
+
"as_long",
|
|
124
|
+
"enable",
|
|
125
|
+
"reject_add_request",
|
|
126
|
+
"is_dismiss",
|
|
127
|
+
"approve",
|
|
128
|
+
"block",
|
|
129
|
+
];
|
package/lib/server/app.d.ts
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
/// <reference types="node" />
|
|
4
4
|
/// <reference types="koa-bodyparser" />
|
|
5
5
|
/// <reference types="node" />
|
|
6
|
-
import Koa from
|
|
7
|
-
import * as os from
|
|
6
|
+
import Koa from "koa";
|
|
7
|
+
import * as os from "os";
|
|
8
8
|
import "reflect-metadata";
|
|
9
9
|
import { Logger } from "log4js";
|
|
10
10
|
import { Server } from "http";
|
package/lib/server/app.js
CHANGED
|
@@ -47,16 +47,16 @@ const process_1 = __importDefault(require("process"));
|
|
|
47
47
|
const fs = __importStar(require("fs"));
|
|
48
48
|
class App extends koa_1.default {
|
|
49
49
|
static get configPath() {
|
|
50
|
-
return path.join(App.configDir,
|
|
50
|
+
return path.join(App.configDir, "config.yaml");
|
|
51
51
|
}
|
|
52
52
|
static get dataDir() {
|
|
53
|
-
return path.join(App.configDir,
|
|
53
|
+
return path.join(App.configDir, "data");
|
|
54
54
|
}
|
|
55
55
|
static get logFile() {
|
|
56
|
-
return path.join(App.configDir,
|
|
56
|
+
return path.join(App.configDir, "onebots.log");
|
|
57
57
|
}
|
|
58
58
|
get info() {
|
|
59
|
-
const pkg = require(path.resolve(__dirname,
|
|
59
|
+
const pkg = require(path.resolve(__dirname, "../../package.json"));
|
|
60
60
|
const free_memory = os.freemem();
|
|
61
61
|
const total_memory = os.totalmem();
|
|
62
62
|
return {
|
|
@@ -74,7 +74,7 @@ class App extends koa_1.default {
|
|
|
74
74
|
process_use_memory: process_1.default.memoryUsage.rss(),
|
|
75
75
|
node_version: process_1.default.version,
|
|
76
76
|
sdk_version: pkg.version,
|
|
77
|
-
uptime: process_1.default.uptime() * 1000
|
|
77
|
+
uptime: process_1.default.uptime() * 1000,
|
|
78
78
|
};
|
|
79
79
|
}
|
|
80
80
|
constructor(config = {}) {
|
|
@@ -84,20 +84,19 @@ class App extends koa_1.default {
|
|
|
84
84
|
this.init();
|
|
85
85
|
}
|
|
86
86
|
init() {
|
|
87
|
-
this.logger = (0, log4js_1.getLogger)(
|
|
87
|
+
this.logger = (0, log4js_1.getLogger)("[OneBots]");
|
|
88
88
|
this.logger.level = this.config.log_level;
|
|
89
89
|
this.router = new router_1.Router({ prefix: this.config.path });
|
|
90
|
-
this
|
|
91
|
-
.use((0, koa_bodyparser_1.default)())
|
|
90
|
+
this.use((0, koa_bodyparser_1.default)())
|
|
92
91
|
.use(this.router.routes())
|
|
93
92
|
.use(this.router.allowedMethods())
|
|
94
93
|
.use((0, koa_basic_auth_1.default)({
|
|
95
94
|
name: this.config.username,
|
|
96
|
-
pass: this.config.password
|
|
95
|
+
pass: this.config.password,
|
|
97
96
|
}))
|
|
98
|
-
.use((0, koa_static_1.default)(path.resolve(__dirname,
|
|
97
|
+
.use((0, koa_static_1.default)(path.resolve(__dirname, "../../dist")));
|
|
99
98
|
this.httpServer = (0, http_1.createServer)(this.callback());
|
|
100
|
-
this.ws = this.router.ws(
|
|
99
|
+
this.ws = this.router.ws("/", this.httpServer);
|
|
101
100
|
this.createOneBots();
|
|
102
101
|
}
|
|
103
102
|
getLogger(patform) {
|
|
@@ -108,8 +107,8 @@ class App extends koa_1.default {
|
|
|
108
107
|
getConfigMaps() {
|
|
109
108
|
const result = [];
|
|
110
109
|
for (const [key, value] of Object.entries(this.config)) {
|
|
111
|
-
const [adapter, ...uinArr] = key.split(
|
|
112
|
-
const uin = uinArr.join(
|
|
110
|
+
const [adapter, ...uinArr] = key.split(".");
|
|
111
|
+
const uin = uinArr.join(".");
|
|
113
112
|
if (!uin)
|
|
114
113
|
continue;
|
|
115
114
|
result.push([adapter, uin, value]);
|
|
@@ -157,9 +156,11 @@ class App extends koa_1.default {
|
|
|
157
156
|
return adapter.createOneBot(uin, config.protocol, config.versions);
|
|
158
157
|
}
|
|
159
158
|
get oneBots() {
|
|
160
|
-
return [...this.adapters.values()]
|
|
159
|
+
return [...this.adapters.values()]
|
|
160
|
+
.map(adapter => {
|
|
161
161
|
return [...adapter.oneBots.values()];
|
|
162
|
-
})
|
|
162
|
+
})
|
|
163
|
+
.flat();
|
|
163
164
|
}
|
|
164
165
|
findOrCreateAdapter(platform, config) {
|
|
165
166
|
if (this.adapters.has(platform))
|
|
@@ -173,35 +174,35 @@ class App extends koa_1.default {
|
|
|
173
174
|
}
|
|
174
175
|
async start() {
|
|
175
176
|
this.httpServer.listen(this.config.port);
|
|
176
|
-
const fileListener =
|
|
177
|
-
if (e ===
|
|
177
|
+
const fileListener = e => {
|
|
178
|
+
if (e === "change")
|
|
178
179
|
this.ws.clients.forEach(async (client) => {
|
|
179
180
|
client.send(JSON.stringify({
|
|
180
|
-
event:
|
|
181
|
-
data: await (0, utils_1.readLine)(1, App.logFile)
|
|
181
|
+
event: "system.log",
|
|
182
|
+
data: await (0, utils_1.readLine)(1, App.logFile),
|
|
182
183
|
}));
|
|
183
184
|
});
|
|
184
185
|
};
|
|
185
186
|
fs.watch(App.logFile, fileListener);
|
|
186
|
-
this.on(
|
|
187
|
+
this.on("close", () => {
|
|
187
188
|
fs.unwatchFile(App.logFile, fileListener);
|
|
188
189
|
});
|
|
189
|
-
process_1.default.on(
|
|
190
|
+
process_1.default.on("disconnect", () => {
|
|
190
191
|
fs.unwatchFile(App.logFile, fileListener);
|
|
191
192
|
});
|
|
192
|
-
this.ws.on(
|
|
193
|
+
this.ws.on("connection", async (client) => {
|
|
193
194
|
client.send(JSON.stringify({
|
|
194
|
-
event:
|
|
195
|
+
event: "system.sync",
|
|
195
196
|
data: {
|
|
196
|
-
config: fs.readFileSync(App.configPath,
|
|
197
|
+
config: fs.readFileSync(App.configPath, "utf8"),
|
|
197
198
|
adapters: [...this.adapters.values()].map(adapter => {
|
|
198
199
|
return adapter.info;
|
|
199
200
|
}),
|
|
200
201
|
app: this.info,
|
|
201
|
-
logs: fs.existsSync(App.logFile) ? await (0, utils_1.readLine)(100, App.logFile) :
|
|
202
|
-
}
|
|
202
|
+
logs: fs.existsSync(App.logFile) ? await (0, utils_1.readLine)(100, App.logFile) : "",
|
|
203
|
+
},
|
|
203
204
|
}));
|
|
204
|
-
client.on(
|
|
205
|
+
client.on("message", async (raw) => {
|
|
205
206
|
let payload = {};
|
|
206
207
|
try {
|
|
207
208
|
payload = JSON.parse(raw.toString());
|
|
@@ -210,38 +211,38 @@ class App extends koa_1.default {
|
|
|
210
211
|
return;
|
|
211
212
|
}
|
|
212
213
|
switch (payload.action) {
|
|
213
|
-
case
|
|
214
|
+
case "system.input":
|
|
214
215
|
return process_1.default.stdin.write(`${payload.data}\n`);
|
|
215
|
-
case
|
|
216
|
-
return fs.writeFileSync(App.configPath, payload.data,
|
|
217
|
-
case
|
|
218
|
-
const config = js_yaml_1.default.load(fs.readFileSync(App.configPath,
|
|
216
|
+
case "system.saveConfig":
|
|
217
|
+
return fs.writeFileSync(App.configPath, payload.data, "utf8");
|
|
218
|
+
case "system.reload":
|
|
219
|
+
const config = js_yaml_1.default.load(fs.readFileSync(App.configPath, "utf8"));
|
|
219
220
|
return this.reload(config);
|
|
220
|
-
case
|
|
221
|
+
case "bot.start": {
|
|
221
222
|
const { platform, uin } = JSON.parse(payload.data);
|
|
222
223
|
await this.adapters.get(platform)?.setOnline(uin);
|
|
223
224
|
return client.send(JSON.stringify({
|
|
224
|
-
event:
|
|
225
|
-
data: this.adapters.get(platform).getOneBot(uin).info
|
|
225
|
+
event: "bot.change",
|
|
226
|
+
data: this.adapters.get(platform).getOneBot(uin).info,
|
|
226
227
|
}));
|
|
227
228
|
}
|
|
228
|
-
case
|
|
229
|
+
case "bot.stop": {
|
|
229
230
|
const { platform, uin } = JSON.parse(payload.data);
|
|
230
231
|
await this.adapters.get(platform)?.setOffline(uin);
|
|
231
232
|
return client.send(JSON.stringify({
|
|
232
|
-
event:
|
|
233
|
-
data: this.adapters.get(platform).getOneBot(uin).info
|
|
233
|
+
event: "bot.change",
|
|
234
|
+
data: this.adapters.get(platform).getOneBot(uin).info,
|
|
234
235
|
}));
|
|
235
236
|
}
|
|
236
237
|
}
|
|
237
238
|
});
|
|
238
239
|
});
|
|
239
|
-
this.router.get(
|
|
240
|
+
this.router.get("/list", ctx => {
|
|
240
241
|
ctx.body = this.oneBots.map(bot => {
|
|
241
242
|
return bot.info;
|
|
242
243
|
});
|
|
243
244
|
});
|
|
244
|
-
this.router.post(
|
|
245
|
+
this.router.post("/add", (ctx, next) => {
|
|
245
246
|
const { uin, ...config } = (ctx.request.body || {});
|
|
246
247
|
try {
|
|
247
248
|
this.addAccount(config.platform, uin, config);
|
|
@@ -251,7 +252,7 @@ class App extends koa_1.default {
|
|
|
251
252
|
ctx.body = e.message;
|
|
252
253
|
}
|
|
253
254
|
});
|
|
254
|
-
this.router.post(
|
|
255
|
+
this.router.post("/edit", (ctx, next) => {
|
|
255
256
|
const { uin, ...config } = (ctx.request.body || {});
|
|
256
257
|
try {
|
|
257
258
|
this.updateAccount(config.platform, uin, config);
|
|
@@ -261,7 +262,7 @@ class App extends koa_1.default {
|
|
|
261
262
|
ctx.body = e.message;
|
|
262
263
|
}
|
|
263
264
|
});
|
|
264
|
-
this.router.get(
|
|
265
|
+
this.router.get("/remove", (ctx, next) => {
|
|
265
266
|
const { uin, platform, force } = ctx.request.query;
|
|
266
267
|
try {
|
|
267
268
|
this.removeAccount(String(platform), String(uin), Boolean(force));
|
|
@@ -272,13 +273,13 @@ class App extends koa_1.default {
|
|
|
272
273
|
ctx.body = e.message;
|
|
273
274
|
}
|
|
274
275
|
});
|
|
275
|
-
process_1.default.on(
|
|
276
|
-
console.error(
|
|
276
|
+
process_1.default.on("uncaughtException", e => {
|
|
277
|
+
console.error("uncaughtException", e);
|
|
277
278
|
});
|
|
278
|
-
process_1.default.on(
|
|
279
|
-
console.error(
|
|
279
|
+
process_1.default.on("unhandledRejection", e => {
|
|
280
|
+
console.error("unhandledRejection", e);
|
|
280
281
|
});
|
|
281
|
-
this.logger.mark(`server listen at http://0.0.0.0:${this.config.port}/${this.config.path ? this.config.path :
|
|
282
|
+
this.logger.mark(`server listen at http://0.0.0.0:${this.config.port}/${this.config.path ? this.config.path : ""}`);
|
|
282
283
|
for (const [_, adapter] of this.adapters) {
|
|
283
284
|
await adapter.start();
|
|
284
285
|
}
|
|
@@ -296,13 +297,13 @@ class App extends koa_1.default {
|
|
|
296
297
|
this.adapters.clear();
|
|
297
298
|
// this.ws.close()
|
|
298
299
|
this.httpServer.close();
|
|
299
|
-
this.emit(
|
|
300
|
+
this.emit("close");
|
|
300
301
|
}
|
|
301
302
|
}
|
|
302
303
|
exports.App = App;
|
|
303
|
-
App.configDir = path.join(os.homedir(),
|
|
304
|
-
function createOnebots(config =
|
|
305
|
-
if (typeof config ===
|
|
304
|
+
App.configDir = path.join(os.homedir(), ".onebots");
|
|
305
|
+
function createOnebots(config = "config.yaml", cp) {
|
|
306
|
+
if (typeof config === "string") {
|
|
306
307
|
config = path.resolve(process_1.default.cwd(), config);
|
|
307
308
|
App.configDir = path.dirname(config);
|
|
308
309
|
if (!(0, fs_1.existsSync)(App.configDir)) {
|
|
@@ -310,37 +311,37 @@ function createOnebots(config = 'config.yaml', cp) {
|
|
|
310
311
|
}
|
|
311
312
|
if (!(0, fs_1.existsSync)(App.dataDir)) {
|
|
312
313
|
(0, fs_1.mkdirSync)(App.dataDir);
|
|
313
|
-
console.log(
|
|
314
|
+
console.log("以为你创建数据存储目录", App.dataDir);
|
|
314
315
|
}
|
|
315
316
|
if (!(0, fs_1.existsSync)(App.configPath)) {
|
|
316
|
-
(0, fs_1.copyFileSync)(path.resolve(__dirname,
|
|
317
|
-
console.log(
|
|
317
|
+
(0, fs_1.copyFileSync)(path.resolve(__dirname, "../config.sample.yaml"), App.configPath);
|
|
318
|
+
console.log("未找到对应配置文件,已自动生成默认配置文件,请修改配置文件后重新启动");
|
|
318
319
|
console.log(`配置文件在: ${App.configPath}`);
|
|
319
320
|
process_1.default.exit();
|
|
320
321
|
}
|
|
321
|
-
config = js_yaml_1.default.load((0, fs_2.readFileSync)(App.configPath,
|
|
322
|
+
config = js_yaml_1.default.load((0, fs_2.readFileSync)(App.configPath, "utf8"));
|
|
322
323
|
}
|
|
323
324
|
(0, log4js_1.configure)({
|
|
324
325
|
appenders: {
|
|
325
326
|
out: {
|
|
326
|
-
type:
|
|
327
|
-
layout: { type:
|
|
327
|
+
type: "stdout",
|
|
328
|
+
layout: { type: "colored" },
|
|
328
329
|
},
|
|
329
330
|
files: {
|
|
330
|
-
type:
|
|
331
|
-
filename: path.join(process_1.default.cwd(),
|
|
332
|
-
}
|
|
331
|
+
type: "file",
|
|
332
|
+
filename: path.join(process_1.default.cwd(), "onebots.log"),
|
|
333
|
+
},
|
|
333
334
|
},
|
|
334
335
|
categories: {
|
|
335
336
|
default: {
|
|
336
|
-
appenders: [
|
|
337
|
-
level: config.log_level || "info"
|
|
338
|
-
}
|
|
337
|
+
appenders: ["out", "files"],
|
|
338
|
+
level: config.log_level || "info",
|
|
339
|
+
},
|
|
339
340
|
},
|
|
340
|
-
disableClustering: true
|
|
341
|
+
disableClustering: true,
|
|
341
342
|
});
|
|
342
343
|
if (cp)
|
|
343
|
-
process_1.default.on(
|
|
344
|
+
process_1.default.on("disconnect", () => cp.kill());
|
|
344
345
|
return new App(config);
|
|
345
346
|
}
|
|
346
347
|
exports.createOnebots = createOnebots;
|
|
@@ -352,14 +353,14 @@ exports.defineConfig = defineConfig;
|
|
|
352
353
|
App.ADAPTERS = new Map();
|
|
353
354
|
App.defaultConfig = {
|
|
354
355
|
port: 6727,
|
|
355
|
-
username:
|
|
356
|
-
password:
|
|
356
|
+
username: "admin",
|
|
357
|
+
password: "123456",
|
|
357
358
|
timeout: 30,
|
|
358
359
|
general: {
|
|
359
360
|
V11: V11_1.V11.defaultConfig,
|
|
360
361
|
V12: V12_1.V12.defaultConfig,
|
|
361
362
|
},
|
|
362
|
-
log_level:
|
|
363
|
+
log_level: "info",
|
|
363
364
|
};
|
|
364
365
|
function registerAdapter(platform, adapter) {
|
|
365
366
|
if (!adapter)
|
|
@@ -373,10 +374,10 @@ exports.defineConfig = defineConfig;
|
|
|
373
374
|
App.registerAdapter = registerAdapter;
|
|
374
375
|
function loadAdapter(platform) {
|
|
375
376
|
const maybeNames = [
|
|
376
|
-
path.join(__dirname,
|
|
377
|
+
path.join(__dirname, "../adapters", platform), // 内置的
|
|
377
378
|
`@onebots/adapter-${platform}`, // 我写的
|
|
378
379
|
`onebots-adapter-${platform}`, // 别人按照规范写的
|
|
379
|
-
platform // 别人写的
|
|
380
|
+
platform, // 别人写的
|
|
380
381
|
];
|
|
381
382
|
let adapter = null;
|
|
382
383
|
for (const adapterName of maybeNames) {
|
package/lib/server/router.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="koa__router" />
|
|
3
|
-
import KoaRouter from
|
|
3
|
+
import KoaRouter from "@koa/router";
|
|
4
4
|
import { WebSocketServer } from "ws";
|
|
5
5
|
import http from "http";
|
|
6
6
|
export declare class WsServer extends WebSocketServer {
|
package/lib/server/router.js
CHANGED
|
@@ -15,7 +15,7 @@ class WsServer extends ws_1.WebSocketServer {
|
|
|
15
15
|
async waitResult(event, ...args) {
|
|
16
16
|
return new Promise(resolve => {
|
|
17
17
|
const resolver = (result) => {
|
|
18
|
-
this.clients.forEach(
|
|
18
|
+
this.clients.forEach(client => client.off("message", listener));
|
|
19
19
|
clearTimeout(timer);
|
|
20
20
|
resolve(result);
|
|
21
21
|
};
|
|
@@ -35,11 +35,11 @@ class WsServer extends ws_1.WebSocketServer {
|
|
|
35
35
|
resolver(data.result);
|
|
36
36
|
};
|
|
37
37
|
this.clients.forEach(client => {
|
|
38
|
-
client.on(
|
|
38
|
+
client.on("message", listener);
|
|
39
39
|
client.send(JSON.stringify({
|
|
40
40
|
event,
|
|
41
41
|
echo,
|
|
42
|
-
args
|
|
42
|
+
args,
|
|
43
43
|
}));
|
|
44
44
|
});
|
|
45
45
|
});
|
|
@@ -54,14 +54,14 @@ class Router extends router_1.default {
|
|
|
54
54
|
ws(path, server) {
|
|
55
55
|
const wsServer = new WsServer({ noServer: true, path });
|
|
56
56
|
this.wsStack.push(wsServer);
|
|
57
|
-
server.on(
|
|
57
|
+
server.on("upgrade", (request, socket, head) => {
|
|
58
58
|
const { pathname } = (0, url_1.parse)(request.url);
|
|
59
59
|
if (this.wsStack.findIndex(wss => wss.options.path === path) === -1) {
|
|
60
60
|
socket.destroy();
|
|
61
61
|
}
|
|
62
62
|
else if (pathname === path) {
|
|
63
63
|
wsServer.handleUpgrade(request, socket, head, function done(ws) {
|
|
64
|
-
wsServer.emit(
|
|
64
|
+
wsServer.emit("connection", ws, request);
|
|
65
65
|
});
|
|
66
66
|
}
|
|
67
67
|
});
|
|
@@ -60,6 +60,7 @@ export declare class CommonAction {
|
|
|
60
60
|
};
|
|
61
61
|
submitSlider(this: V11, ticket: string): Promise<any>;
|
|
62
62
|
submitSmsCode(this: V11, code: string): Promise<any>;
|
|
63
|
+
callApi(this: V11, name: string, args: any[]): Promise<any>;
|
|
63
64
|
login(this: V11, password?: string): Promise<any>;
|
|
64
65
|
logout(this: V11, keepalive?: boolean): Promise<any>;
|
|
65
66
|
}
|