@kotori-bot/loader 1.5.1 → 1.6.0
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/class/loader.js +661 -248
- package/lib/class/runner.d.ts +2 -0
- package/lib/class/runner.js +313 -222
- package/lib/constants.d.ts +4 -2
- package/lib/constants.js +81 -13
- package/lib/decorators/index.d.ts +1 -0
- package/lib/decorators/index.js +161 -20
- package/lib/decorators/plugin.d.ts +7 -0
- package/lib/decorators/plugin.js +50 -0
- package/lib/decorators/utils.d.ts +2 -1
- package/lib/decorators/utils.js +132 -82
- package/lib/index.d.ts +1 -0
- package/lib/index.js +837 -26
- package/lib/service/adapter.d.ts +15 -0
- package/lib/service/adapter.js +80 -0
- package/lib/service/database.js +28 -1
- package/lib/service/file.d.ts +2 -2
- package/lib/service/file.js +65 -27
- package/lib/service/server.d.ts +52 -20
- package/lib/service/server.js +143 -92
- package/lib/types/index.d.ts +1 -0
- package/lib/types/index.js +29 -0
- package/lib/types/internal.d.ts +6 -0
- package/lib/types/internal.js +12 -0
- package/lib/types/server.d.ts +27 -0
- package/lib/types/server.js +29 -0
- package/lib/utils/log.js +53 -16
- package/lib/utils/logger.d.ts +1 -0
- package/lib/utils/logger.js +92 -52
- package/locales/common.json +28 -0
- package/package.json +7 -9
- package/lib/consts.d.ts +0 -8
- package/lib/consts.js +0 -11
- package/lib/loader.d.ts +0 -40
- package/lib/loader.js +0 -260
- package/lib/log.d.ts +0 -3
- package/lib/log.js +0 -20
- package/lib/runner.d.ts +0 -55
- package/lib/runner.js +0 -205
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* @Package @kotori-bot/loader
|
|
4
|
+
* @Version 1.6.0-beta.1
|
|
5
|
+
* @Author Hotaru <biyuehuya@gmail.com>
|
|
6
|
+
* @Copyright 2024 Hotaru. All rights reserved.
|
|
7
|
+
* @License GPL-3.0
|
|
8
|
+
* @Link https://github.com/kotorijs/kotori
|
|
9
|
+
* @Date 2024/6/6 21:03:54
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
"use strict";
|
|
13
|
+
var __defProp = Object.defineProperty;
|
|
14
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
15
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
16
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
17
|
+
var __copyProps = (to, from, except, desc) => {
|
|
18
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
19
|
+
for (let key of __getOwnPropNames(from))
|
|
20
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
21
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
22
|
+
}
|
|
23
|
+
return to;
|
|
24
|
+
};
|
|
25
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
26
|
+
|
|
27
|
+
// src/types/server.ts
|
|
28
|
+
var server_exports = {};
|
|
29
|
+
module.exports = __toCommonJS(server_exports);
|
package/lib/utils/log.js
CHANGED
|
@@ -1,20 +1,57 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* @Package @kotori-bot/loader
|
|
4
|
+
* @Version 1.6.0-beta.1
|
|
5
|
+
* @Author Hotaru <biyuehuya@gmail.com>
|
|
6
|
+
* @Copyright 2024 Hotaru. All rights reserved.
|
|
7
|
+
* @License GPL-3.0
|
|
8
|
+
* @Link https://github.com/kotorijs/kotori
|
|
9
|
+
* @Date 2024/6/6 21:03:54
|
|
10
|
+
*/
|
|
11
|
+
|
|
1
12
|
"use strict";
|
|
2
|
-
Object.defineProperty
|
|
3
|
-
|
|
13
|
+
var __defProp = Object.defineProperty;
|
|
14
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
15
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
16
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
17
|
+
var __export = (target, all) => {
|
|
18
|
+
for (var name in all)
|
|
19
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
20
|
+
};
|
|
21
|
+
var __copyProps = (to, from, except, desc) => {
|
|
22
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
23
|
+
for (let key of __getOwnPropNames(from))
|
|
24
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
25
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
26
|
+
}
|
|
27
|
+
return to;
|
|
28
|
+
};
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// src/utils/log.ts
|
|
32
|
+
var log_exports = {};
|
|
33
|
+
__export(log_exports, {
|
|
34
|
+
default: () => log_default,
|
|
35
|
+
loadInfo: () => loadInfo
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(log_exports);
|
|
4
38
|
function loadInfo(info, ctx) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
39
|
+
process.stdout.write("Kotori Bot is loading...");
|
|
40
|
+
process.stdout.write(`
|
|
41
|
+
\u2588\u2588\u2557 \u2588\u2588\u2557 \u2588\u2588\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2557
|
|
42
|
+
\u2588\u2588\u2551 \u2588\u2588\u2554\u255D\u2588\u2588\u2554\u2550\u2550\u2550\u2588\u2588\u2557\u255A\u2550\u2550\u2588\u2588\u2554\u2550\u2550\u255D\u2588\u2588\u2554\u2550\u2550\u2550\u2588\u2588\u2557\u2588\u2588\u2554\u2550\u2550\u2588\u2588\u2557\u2588\u2588\u2551
|
|
43
|
+
\u2588\u2588\u2588\u2588\u2588\u2554\u255D \u2588\u2588\u2551 \u2588\u2588\u2551 \u2588\u2588\u2551 \u2588\u2588\u2551 \u2588\u2588\u2551\u2588\u2588\u2588\u2588\u2588\u2588\u2554\u255D\u2588\u2588\u2551
|
|
44
|
+
\u2588\u2588\u2554\u2550\u2588\u2588\u2557 \u2588\u2588\u2551 \u2588\u2588\u2551 \u2588\u2588\u2551 \u2588\u2588\u2551 \u2588\u2588\u2551\u2588\u2588\u2554\u2550\u2550\u2588\u2588\u2557\u2588\u2588\u2551
|
|
45
|
+
\u2588\u2588\u2551 \u2588\u2588\u2557\u255A\u2588\u2588\u2588\u2588\u2588\u2588\u2554\u255D \u2588\u2588\u2551 \u255A\u2588\u2588\u2588\u2588\u2588\u2588\u2554\u255D\u2588\u2588\u2551 \u2588\u2588\u2551\u2588\u2588\u2551
|
|
46
|
+
\u255A\u2550\u255D \u255A\u2550\u255D \u255A\u2550\u2550\u2550\u2550\u2550\u255D \u255A\u2550\u255D \u255A\u2550\u2550\u2550\u2550\u2550\u255D \u255A\u2550\u255D \u255A\u2550\u255D\u255A\u2550\u255D
|
|
13
47
|
`);
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
48
|
+
ctx.logger.record("loader base dir:", ctx.baseDir.root);
|
|
49
|
+
ctx.logger.info(`Kotori Bot Version: ${info.version} License: ${info.license}`);
|
|
50
|
+
ctx.logger.info(`Kotori Bot By ${info.author}`);
|
|
51
|
+
ctx.logger.info(`Copyright \xA9 2023 - 2024 ${info.author} All rights reserved`);
|
|
18
52
|
}
|
|
19
|
-
|
|
20
|
-
|
|
53
|
+
var log_default = loadInfo;
|
|
54
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
55
|
+
0 && (module.exports = {
|
|
56
|
+
loadInfo
|
|
57
|
+
});
|
package/lib/utils/logger.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ export declare class KotoriLogger extends Logger {
|
|
|
9
9
|
error(...args: unknown[]): void;
|
|
10
10
|
warn(...args: unknown[]): void;
|
|
11
11
|
info(...args: unknown[]): void;
|
|
12
|
+
record(...args: unknown[]): void;
|
|
12
13
|
debug(...args: unknown[]): void;
|
|
13
14
|
trace(...args: unknown[]): void;
|
|
14
15
|
}
|
package/lib/utils/logger.js
CHANGED
|
@@ -1,53 +1,93 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* @Package @kotori-bot/loader
|
|
4
|
+
* @Version 1.6.0-beta.1
|
|
5
|
+
* @Author Hotaru <biyuehuya@gmail.com>
|
|
6
|
+
* @Copyright 2024 Hotaru. All rights reserved.
|
|
7
|
+
* @License GPL-3.0
|
|
8
|
+
* @Link https://github.com/kotorijs/kotori
|
|
9
|
+
* @Date 2024/6/6 21:03:54
|
|
10
|
+
*/
|
|
11
|
+
|
|
1
12
|
"use strict";
|
|
2
|
-
Object.defineProperty
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
13
|
+
var __defProp = Object.defineProperty;
|
|
14
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
15
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
16
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
17
|
+
var __export = (target, all) => {
|
|
18
|
+
for (var name in all)
|
|
19
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
20
|
+
};
|
|
21
|
+
var __copyProps = (to, from, except, desc) => {
|
|
22
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
23
|
+
for (let key of __getOwnPropNames(from))
|
|
24
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
25
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
26
|
+
}
|
|
27
|
+
return to;
|
|
28
|
+
};
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// src/utils/logger.ts
|
|
32
|
+
var logger_exports = {};
|
|
33
|
+
__export(logger_exports, {
|
|
34
|
+
KotoriLogger: () => KotoriLogger,
|
|
35
|
+
default: () => logger_default
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(logger_exports);
|
|
38
|
+
var import_logger = require("@kotori-bot/logger");
|
|
39
|
+
var KotoriLogger = class extends import_logger.Logger {
|
|
40
|
+
constructor(optionsSelf, ctx) {
|
|
41
|
+
super(optionsSelf);
|
|
42
|
+
this.optionsSelf = optionsSelf;
|
|
43
|
+
this.ctx = ctx;
|
|
44
|
+
}
|
|
45
|
+
setLabel() {
|
|
46
|
+
const origin = Object.create(this.optionsSelf.label);
|
|
47
|
+
const label = this.ctx.identity ? [this.ctx.identity, ...this.optionsSelf.label] : this.optionsSelf.label;
|
|
48
|
+
this[/* @__PURE__ */ (() => "options")()].label = label;
|
|
49
|
+
return () => {
|
|
50
|
+
this[/* @__PURE__ */ (() => "options")()].label = origin;
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
fatal(...args) {
|
|
54
|
+
const dispose = this.setLabel();
|
|
55
|
+
super.fatal(...args);
|
|
56
|
+
dispose();
|
|
57
|
+
}
|
|
58
|
+
error(...args) {
|
|
59
|
+
const dispose = this.setLabel();
|
|
60
|
+
super.error(...args);
|
|
61
|
+
dispose();
|
|
62
|
+
}
|
|
63
|
+
warn(...args) {
|
|
64
|
+
const dispose = this.setLabel();
|
|
65
|
+
super.warn(...args);
|
|
66
|
+
dispose();
|
|
67
|
+
}
|
|
68
|
+
info(...args) {
|
|
69
|
+
const dispose = this.setLabel();
|
|
70
|
+
super.info(...args);
|
|
71
|
+
dispose();
|
|
72
|
+
}
|
|
73
|
+
record(...args) {
|
|
74
|
+
const dispose = this.setLabel();
|
|
75
|
+
super.record(...args);
|
|
76
|
+
dispose();
|
|
77
|
+
}
|
|
78
|
+
debug(...args) {
|
|
79
|
+
const dispose = this.setLabel();
|
|
80
|
+
super.debug(...args);
|
|
81
|
+
dispose();
|
|
82
|
+
}
|
|
83
|
+
trace(...args) {
|
|
84
|
+
const dispose = this.setLabel();
|
|
85
|
+
super.trace(...args);
|
|
86
|
+
dispose();
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
var logger_default = KotoriLogger;
|
|
90
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
91
|
+
0 && (module.exports = {
|
|
92
|
+
KotoriLogger
|
|
93
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"loader.debug.info": "run info: <dim>develop with debugging...</dim>",
|
|
3
|
+
"loader.debug.reload": "file changed, <bold>module</bold> <cyanBright>{0}</cyanBright> <dim>is reloading...</dim>",
|
|
4
|
+
"loader.tips.update.failed": "<red>get update failed</red>, please check your network",
|
|
5
|
+
"loader.tips.update.latest": "kotori is <greenBright>currently the latest version</greenBright>",
|
|
6
|
+
"loader.tips.update.available": "the <magenta>current version</magenta> of Kotori is <magenta>{0}</magenta>, and the <greenBright>latest version</greenBright> is <greenBright>{1}</greenBright>. please go to <blueBright>{2}</blueBright> to update",
|
|
7
|
+
"loader.bots.connect": "<blueBright>connecting server</blueBright> to <blueBright>{0}</blueBright>",
|
|
8
|
+
"loader.bots.reconnect": "re<blueBright>connecting server</blueBright> to <blueBright>{0}</blueBright>",
|
|
9
|
+
"loader.bots.start": "<blueBright>server started</blueBright> at <blueBright>{0}</blueBright>",
|
|
10
|
+
"loader.bots.restart": "<blueBright>server restarted</blueBright> at <blueBright>{0}</blueBright>",
|
|
11
|
+
"loader.bots.ready": "<blueBright>ready completed</blueBright> about <blueBright>{0}</blueBright>",
|
|
12
|
+
"loader.bots.disconnect": "disconnecting server from <blueBright>{0}</blueBright>",
|
|
13
|
+
"loader.bots.disconnect.error": "<red>disconnected server</red> from <blueBright>{0}</blueBright> unexpectedly",
|
|
14
|
+
"loader.bots.stop": "<blueBright>server stopped</blueBright> at <blueBright>{0}</blueBright>",
|
|
15
|
+
"loader.bots.stop.error": "<red>stopped server</red> at <blueBright>{0}</blueBright> unexpectedly",
|
|
16
|
+
"loader.bots.dispose": "<greenBright>dispose completed</greenBright> about <greenBright>{0}</greenBright>",
|
|
17
|
+
"loader.modules.load": "loaded module <cyanBright>{0}</cyanBright> version: {1} authors: <magenta>{2}</magenta>",
|
|
18
|
+
"loader.modules.incomplete": "<yellow>incomplete supported module version</yellow>: <yellow>{0}</yellow>",
|
|
19
|
+
"loader.modules.unsupported": "<red>unsupported module version</red>: <red>{0}</red>",
|
|
20
|
+
"loader.modules.all": "<greenBright>loaded {0} modules successfully</greenBright>",
|
|
21
|
+
"loader.modules.all.failed": "loaded {0} modules successfully, <red>failed to load</red> <redBright>{1}</redBright> modules",
|
|
22
|
+
"loader.adapters.notfound": "cannot find <cyanBright>adapter</cyanBright> <cyanBright>{0}</cyanBright> for <magenta>bot</magenta> <magenta>{1}</magenta>",
|
|
23
|
+
"error.dev.package.illegal": "<yellowBright>illegal package.json</yellowBright> <yellowBright>{0}</yellowBright>",
|
|
24
|
+
"error.dev.package.missing": "package.json format error <yellowBright>{0}</yellowBright>: <redBright>{1}</redBright>",
|
|
25
|
+
"error.dev.main_file": "cannot find <cyanBright>main file</cyanBright> <cyanBright>{0}</cyanBright>",
|
|
26
|
+
"error.module.config": "config format of <cyanBright>module</cyanBright> <cyanBright>{0}</cyanBright> is error: <redBright>{1}</redBright>",
|
|
27
|
+
"error.module.config_bot": "config format of <cyanBright>bot</cyanBright> <cyanBright>{0}</cyanBright> is error: <redBright>{1}</redBright>"
|
|
28
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kotori-bot/loader",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"description": "Loader For KotoriBot",
|
|
5
5
|
"license": "GPL-3.0",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"loader"
|
|
12
12
|
],
|
|
13
13
|
"files": [
|
|
14
|
+
"locales",
|
|
14
15
|
"lib",
|
|
15
16
|
"LICENSE",
|
|
16
17
|
"README.md"
|
|
@@ -24,18 +25,15 @@
|
|
|
24
25
|
},
|
|
25
26
|
"homepage": "https://kotori.js.org",
|
|
26
27
|
"dependencies": {
|
|
27
|
-
"@types/ws": "^8.5.8",
|
|
28
28
|
"express": "^4.18.2",
|
|
29
|
-
"
|
|
29
|
+
"path-to-regexp": "^6.2.2",
|
|
30
30
|
"ws": "^8.14.2",
|
|
31
|
-
"@kotori-bot/
|
|
32
|
-
"@kotori-bot/
|
|
31
|
+
"@kotori-bot/logger": "^1.3.0",
|
|
32
|
+
"@kotori-bot/core": "^1.5.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@types/body-parser": "^1.19.5",
|
|
36
|
-
"@types/connect": "^3.4.38",
|
|
37
35
|
"@types/express": "^4.17.21",
|
|
38
|
-
"@types/
|
|
39
|
-
"
|
|
36
|
+
"@types/ws": "^8.5.8",
|
|
37
|
+
"knex": "^3.1.0"
|
|
40
38
|
}
|
|
41
39
|
}
|
package/lib/consts.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export declare const DEV_FILE = ".ts";
|
|
2
|
-
export declare const BUILD_FILE = ".js";
|
|
3
|
-
export declare const DEV_CODE_DIRS = "./src/";
|
|
4
|
-
export declare const DEV_IMPORT = "./src/index.ts";
|
|
5
|
-
export declare const BUILD_CONFIG_NAME = "kotori.yml";
|
|
6
|
-
export declare const DEV_CONFIG_NAME = "kotori.dev.yml";
|
|
7
|
-
export declare const SUPPORTS_VERSION: RegExp;
|
|
8
|
-
export declare const SUPPORTS_HALF_VERSION: RegExp;
|
package/lib/consts.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SUPPORTS_HALF_VERSION = exports.SUPPORTS_VERSION = exports.DEV_CONFIG_NAME = exports.BUILD_CONFIG_NAME = exports.DEV_IMPORT = exports.DEV_CODE_DIRS = exports.BUILD_FILE = exports.DEV_FILE = void 0;
|
|
4
|
-
exports.DEV_FILE = '.ts';
|
|
5
|
-
exports.BUILD_FILE = '.js';
|
|
6
|
-
exports.DEV_CODE_DIRS = './src/';
|
|
7
|
-
exports.DEV_IMPORT = `${exports.DEV_CODE_DIRS}index.ts`;
|
|
8
|
-
exports.BUILD_CONFIG_NAME = 'kotori.yml';
|
|
9
|
-
exports.DEV_CONFIG_NAME = 'kotori.dev.yml';
|
|
10
|
-
exports.SUPPORTS_VERSION = /(1\.1\.0)|(1\.2\.0)|(1\.3\.(.*?))/;
|
|
11
|
-
exports.SUPPORTS_HALF_VERSION = /(x\.x\.(.*?))/;
|
package/lib/loader.d.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { Container, Symbols } from '@kotori-bot/core';
|
|
2
|
-
import Logger from '@kotori-bot/logger';
|
|
3
|
-
import Runner from './runner';
|
|
4
|
-
import Server from './service/server';
|
|
5
|
-
import type Database from './service/database';
|
|
6
|
-
import File from './service/file';
|
|
7
|
-
declare module '@kotori-bot/core' {
|
|
8
|
-
interface Context {
|
|
9
|
-
readonly baseDir: Runner['baseDir'];
|
|
10
|
-
readonly options: Runner['options'];
|
|
11
|
-
readonly [Symbols.modules]: Runner[typeof Symbols.modules];
|
|
12
|
-
loadAll(): void;
|
|
13
|
-
watcher(): void;
|
|
14
|
-
logger: Logger;
|
|
15
|
-
server: Server;
|
|
16
|
-
db: Database;
|
|
17
|
-
file: File;
|
|
18
|
-
}
|
|
19
|
-
interface GlobalConfig {
|
|
20
|
-
dirs: string[];
|
|
21
|
-
port: number;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
export declare class Loader extends Container {
|
|
25
|
-
private ctx;
|
|
26
|
-
private loadCount;
|
|
27
|
-
constructor(options?: {
|
|
28
|
-
dir?: string;
|
|
29
|
-
mode?: string;
|
|
30
|
-
});
|
|
31
|
-
run(): void;
|
|
32
|
-
private handleError;
|
|
33
|
-
private catchError;
|
|
34
|
-
private listenMessage;
|
|
35
|
-
private setPreService;
|
|
36
|
-
private loadAllModules;
|
|
37
|
-
private loadAllAdapter;
|
|
38
|
-
private checkUpdate;
|
|
39
|
-
}
|
|
40
|
-
export default Loader;
|
package/lib/loader.js
DELETED
|
@@ -1,260 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.Loader = void 0;
|
|
30
|
-
/*
|
|
31
|
-
* @Author: hotaru biyuehuya@gmail.com
|
|
32
|
-
* @Blog: https://hotaru.icu
|
|
33
|
-
* @Date: 2023-06-24 15:12:55
|
|
34
|
-
* @LastEditors: Hotaru biyuehuya@gmail.com
|
|
35
|
-
* @LastEditTime: 2024-05-01 22:06:58
|
|
36
|
-
*/
|
|
37
|
-
const core_1 = require("@kotori-bot/core");
|
|
38
|
-
const path_1 = __importDefault(require("path"));
|
|
39
|
-
const fs_1 = __importDefault(require("fs"));
|
|
40
|
-
const logger_1 = __importDefault(require("@kotori-bot/logger"));
|
|
41
|
-
const runner_1 = __importStar(require("./runner"));
|
|
42
|
-
const log_1 = __importDefault(require("./log"));
|
|
43
|
-
const constants_1 = require("./constants");
|
|
44
|
-
const server_1 = __importDefault(require("./service/server"));
|
|
45
|
-
const file_1 = __importDefault(require("./service/file"));
|
|
46
|
-
function getRunnerConfig(file, dir) {
|
|
47
|
-
const handle = (root) => {
|
|
48
|
-
const baseDir = {
|
|
49
|
-
root,
|
|
50
|
-
modules: path_1.default.join(root, 'modules'),
|
|
51
|
-
data: path_1.default.join(root, 'data'),
|
|
52
|
-
logs: path_1.default.join(root, 'logs')
|
|
53
|
-
};
|
|
54
|
-
Object.values(baseDir).forEach((val) => {
|
|
55
|
-
if (!fs_1.default.existsSync(val))
|
|
56
|
-
fs_1.default.mkdirSync(val);
|
|
57
|
-
});
|
|
58
|
-
return baseDir;
|
|
59
|
-
};
|
|
60
|
-
const options = {
|
|
61
|
-
mode: file === constants_1.DEV_CONFIG_NAME ? 'dev' : 'build'
|
|
62
|
-
};
|
|
63
|
-
if (dir)
|
|
64
|
-
return { baseDir: handle(path_1.default.resolve(dir)), options };
|
|
65
|
-
let root = path_1.default.resolve(__dirname, '..').replace('loader', 'kotori');
|
|
66
|
-
let count = 0;
|
|
67
|
-
while (!fs_1.default.existsSync(path_1.default.join(root, file))) {
|
|
68
|
-
if (count > 5) {
|
|
69
|
-
logger_1.default.fatal(`cannot find file ${file} `);
|
|
70
|
-
process.exit();
|
|
71
|
-
}
|
|
72
|
-
root = path_1.default.join(root, '..');
|
|
73
|
-
count += 1;
|
|
74
|
-
}
|
|
75
|
-
return { baseDir: handle(root), options };
|
|
76
|
-
}
|
|
77
|
-
/* eslint consistent-return: 0 */
|
|
78
|
-
function getCoreConfig(file, baseDir) {
|
|
79
|
-
try {
|
|
80
|
-
const result1 = core_1.Tsu.Object({
|
|
81
|
-
global: core_1.Tsu.Object({
|
|
82
|
-
dirs: core_1.Tsu.Array(core_1.Tsu.String()).default([]),
|
|
83
|
-
port: core_1.Tsu.Number().default(core_1.DEFAULT_PORT),
|
|
84
|
-
lang: runner_1.localeTypeSchema.default(core_1.DEFAULT_CORE_CONFIG.global.lang),
|
|
85
|
-
'command-prefix': core_1.Tsu.String().default(core_1.DEFAULT_CORE_CONFIG.global['command-prefix'])
|
|
86
|
-
}),
|
|
87
|
-
plugin: core_1.Tsu.Object({})
|
|
88
|
-
.index(core_1.Tsu.Object({
|
|
89
|
-
filter: core_1.Tsu.Object({}).default({})
|
|
90
|
-
}).default({ filter: {} }))
|
|
91
|
-
.default(core_1.DEFAULT_CORE_CONFIG.plugin)
|
|
92
|
-
})
|
|
93
|
-
.default({ global: Object.assign(core_1.DEFAULT_CORE_CONFIG.global), plugin: core_1.DEFAULT_CORE_CONFIG.plugin })
|
|
94
|
-
.parse((0, core_1.loadConfig)(path_1.default.join(baseDir.root, file), 'yaml'));
|
|
95
|
-
return core_1.Tsu.Object({
|
|
96
|
-
adapter: core_1.Tsu.Object({})
|
|
97
|
-
.index(core_1.Tsu.Object({
|
|
98
|
-
extends: core_1.Tsu.String(),
|
|
99
|
-
master: core_1.Tsu.Union([core_1.Tsu.Number(), core_1.Tsu.String()]),
|
|
100
|
-
lang: runner_1.localeTypeSchema.default(result1.global.lang),
|
|
101
|
-
'command-prefix': core_1.Tsu.String().default(result1.global['command-prefix'])
|
|
102
|
-
}))
|
|
103
|
-
.default(core_1.DEFAULT_CORE_CONFIG.adapter)
|
|
104
|
-
}).parse(result1);
|
|
105
|
-
}
|
|
106
|
-
catch (err) {
|
|
107
|
-
if (!(err instanceof core_1.TsuError))
|
|
108
|
-
throw err;
|
|
109
|
-
logger_1.default.fatal(`file ${file} format error: ${err.message}`);
|
|
110
|
-
process.exit();
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
class Loader extends core_1.Container {
|
|
114
|
-
ctx;
|
|
115
|
-
loadCount = 0;
|
|
116
|
-
constructor(options) {
|
|
117
|
-
super();
|
|
118
|
-
const file = options && options.mode === 'dev' ? constants_1.DEV_CONFIG_NAME : constants_1.BUILD_CONFIG_NAME;
|
|
119
|
-
const runnerConfig = getRunnerConfig(file, options?.dir);
|
|
120
|
-
const ctx = new core_1.Core(getCoreConfig(file, runnerConfig.baseDir));
|
|
121
|
-
ctx.provide('runner', new runner_1.default(ctx, runnerConfig));
|
|
122
|
-
ctx.mixin('runner', ['baseDir', 'options']);
|
|
123
|
-
core_1.Container.setInstance(ctx);
|
|
124
|
-
this.ctx = core_1.Container.getInstance();
|
|
125
|
-
}
|
|
126
|
-
run() {
|
|
127
|
-
(0, log_1.default)(this.ctx.pkg, this.ctx);
|
|
128
|
-
this.catchError();
|
|
129
|
-
this.listenMessage();
|
|
130
|
-
this.setPreService();
|
|
131
|
-
this.loadAllModules();
|
|
132
|
-
this.checkUpdate();
|
|
133
|
-
}
|
|
134
|
-
handleError(err, prefix) {
|
|
135
|
-
if (!(err instanceof core_1.KotoriError)) {
|
|
136
|
-
if (err instanceof Error) {
|
|
137
|
-
this.ctx.logger.label(prefix).error(err.message, err.stack);
|
|
138
|
-
}
|
|
139
|
-
else {
|
|
140
|
-
this.ctx.logger.label(prefix).error(err);
|
|
141
|
-
}
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
144
|
-
const list = {
|
|
145
|
-
ServiceError: () => this.ctx.logger.label('service').warn,
|
|
146
|
-
ModuleError: () => this.ctx.logger.label('module').error,
|
|
147
|
-
UnknownError: () => this.ctx.logger.error,
|
|
148
|
-
DevError: () => this.ctx.logger.label('error').debug
|
|
149
|
-
};
|
|
150
|
-
list[err.name]().bind(this.ctx.logger)(err.message, err.stack);
|
|
151
|
-
}
|
|
152
|
-
catchError() {
|
|
153
|
-
process.on('uncaughtExceptionMonitor', (err) => this.handleError(err, 'sync'));
|
|
154
|
-
process.on('unhandledRejection', (err) => this.handleError(err, 'async'));
|
|
155
|
-
process.on('SIGINT', () => process.exit());
|
|
156
|
-
this.ctx.logger.debug('run info: develop with debuing...');
|
|
157
|
-
}
|
|
158
|
-
listenMessage() {
|
|
159
|
-
this.ctx.on('connect', (data) => {
|
|
160
|
-
const { type, mode, normal, address, adapter } = data;
|
|
161
|
-
let msg;
|
|
162
|
-
if (type === 'connect') {
|
|
163
|
-
switch (mode) {
|
|
164
|
-
case 'ws':
|
|
165
|
-
msg = `${normal ? 'Connect' : 'Reconnect'} server to ${address}`;
|
|
166
|
-
break;
|
|
167
|
-
case 'ws-reverse':
|
|
168
|
-
msg = `server ${normal ? 'start' : 'restart'} at ${address}`;
|
|
169
|
-
break;
|
|
170
|
-
default:
|
|
171
|
-
msg = `ready completed about ${address}`;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
else {
|
|
175
|
-
switch (mode) {
|
|
176
|
-
case 'ws':
|
|
177
|
-
msg = `disconnect server from ${address}${normal ? '' : ' unexpectedly'}`;
|
|
178
|
-
break;
|
|
179
|
-
case 'ws-reverse':
|
|
180
|
-
msg = `server stop at ${address}${normal ? '' : ' unexpectedly'}`;
|
|
181
|
-
break;
|
|
182
|
-
default:
|
|
183
|
-
msg = `dispose completed about ${address}`;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
adapter.ctx.logger[normal ? 'info' : 'warn'](msg);
|
|
187
|
-
});
|
|
188
|
-
this.ctx.on('status', (data) => {
|
|
189
|
-
const { status, adapter } = data;
|
|
190
|
-
adapter.ctx.logger.info(status);
|
|
191
|
-
});
|
|
192
|
-
this.ctx.on('ready_module', (data) => {
|
|
193
|
-
if (typeof data.instance !== 'object')
|
|
194
|
-
return;
|
|
195
|
-
const pkg = data.instance.name
|
|
196
|
-
? this.ctx.get('runner')[core_1.Symbols.modules].get(data.instance.name)
|
|
197
|
-
: undefined;
|
|
198
|
-
if (!pkg)
|
|
199
|
-
return;
|
|
200
|
-
this.loadCount += 1;
|
|
201
|
-
const { name, version, author, peerDependencies } = pkg[0].pkg;
|
|
202
|
-
this.ctx.logger.info(`loaded module ${name} version: ${version} ${Array.isArray(author) ? `authors: ${author.join(',')}` : `author: ${author}`}`);
|
|
203
|
-
const requiredVersion = peerDependencies['kotori-bot'];
|
|
204
|
-
if (!requiredVersion.includes('workspace') &&
|
|
205
|
-
(!constants_1.SUPPORTS_VERSION.exec(requiredVersion) || requiredVersion !== this.ctx.pkg.version)) {
|
|
206
|
-
if (constants_1.SUPPORTS_HALF_VERSION.exec(requiredVersion)) {
|
|
207
|
-
this.ctx.logger.warn(`incomplete supported module version: ${requiredVersion}`);
|
|
208
|
-
}
|
|
209
|
-
else {
|
|
210
|
-
this.ctx.logger.error(`unsupported module version: ${requiredVersion}`);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
setPreService() {
|
|
216
|
-
this.ctx.service('server', new server_1.default(this.ctx.extends(), { port: this.ctx.config.global.port }));
|
|
217
|
-
this.ctx.service('file', new file_1.default(this.ctx.extends()));
|
|
218
|
-
}
|
|
219
|
-
loadAllModules() {
|
|
220
|
-
this.ctx.get('runner').loadAll();
|
|
221
|
-
const failLoadCount = this.ctx.get('runner')[core_1.Symbols.modules].size - this.loadCount;
|
|
222
|
-
this.ctx.logger.info(`loaded ${this.loadCount} modules successfully${failLoadCount > 0 ? `, failed to load ${failLoadCount} modules` : ''} `);
|
|
223
|
-
this.loadAllAdapter();
|
|
224
|
-
this.ctx.emit('ready');
|
|
225
|
-
}
|
|
226
|
-
loadAllAdapter() {
|
|
227
|
-
const adapters = this.ctx[core_1.Symbols.adapter];
|
|
228
|
-
Object.keys(this.ctx.config.adapter).forEach((botName) => {
|
|
229
|
-
const botConfig = this.ctx.config.adapter[botName];
|
|
230
|
-
const array = adapters.get(botConfig.extends);
|
|
231
|
-
if (!array) {
|
|
232
|
-
this.ctx.logger.warn(`cannot find adapter '${botConfig.extends}' for ${botName}`);
|
|
233
|
-
return;
|
|
234
|
-
}
|
|
235
|
-
const result = array[1]?.parseSafe(botConfig);
|
|
236
|
-
if (result && !result.value)
|
|
237
|
-
throw new core_1.ModuleError(`Config format of adapter ${botName} is error: ${result.error.message}`);
|
|
238
|
-
const bot = new array[0](this.ctx.extends({}, `${botConfig.extends}/${botName}`), result ? result.data : botConfig, botName);
|
|
239
|
-
this.ctx.on('ready', () => bot.start());
|
|
240
|
-
this.ctx.on('dispose', () => bot.stop());
|
|
241
|
-
});
|
|
242
|
-
}
|
|
243
|
-
async checkUpdate() {
|
|
244
|
-
const { version } = this.ctx.pkg;
|
|
245
|
-
const res = await this.ctx.http
|
|
246
|
-
.get("https://hotaru.icu/api/agent/?url=https://raw.githubusercontent.com/kotorijs/kotori/master/packages/core/package.json" /* GLOBAL.UPDATE */)
|
|
247
|
-
.catch(() => this.ctx.logger.error('get update failed, please check your network'));
|
|
248
|
-
if (!res || !core_1.Tsu.Object({ version: core_1.Tsu.String() }).check(res)) {
|
|
249
|
-
this.ctx.logger.warn(`detection update failed`);
|
|
250
|
-
}
|
|
251
|
-
else if (version === res.version) {
|
|
252
|
-
this.ctx.logger.info('kotori is currently the latest version');
|
|
253
|
-
}
|
|
254
|
-
else {
|
|
255
|
-
this.ctx.logger.warn(`the current version of Kotori is ${version}, and the latest version is ${res.version}. please go to ${"https://github.com/kotorijs/kotori" /* GLOBAL.REPO */} to update`);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
exports.Loader = Loader;
|
|
260
|
-
exports.default = Loader;
|
package/lib/log.d.ts
DELETED