@kotori-bot/loader 1.1.0 → 1.3.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/LICENSE +674 -674
- package/README.md +5 -0
- package/lib/consts.d.ts +8 -0
- package/lib/consts.js +6 -2
- package/lib/index.d.ts +3 -0
- package/lib/index.js +19 -8
- package/lib/loader.d.ts +33 -0
- package/lib/loader.js +257 -0
- package/lib/log.d.ts +3 -0
- package/lib/log.js +9 -9
- package/lib/runner.d.ts +29 -0
- package/lib/runner.js +183 -0
- package/package.json +18 -15
- package/lib/global.js +0 -40
- package/lib/main.js +0 -124
- package/lib/modules.js +0 -83
package/lib/global.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.globalConfig = exports.baseDir = void 0;
|
|
7
|
-
const fs_1 = __importDefault(require("fs"));
|
|
8
|
-
const path_1 = __importDefault(require("path"));
|
|
9
|
-
const kotori_bot_1 = require("kotori-bot");
|
|
10
|
-
const CONFIG_FILE = 'kotori.yml';
|
|
11
|
-
exports.baseDir = (() => {
|
|
12
|
-
let root = path_1.default.resolve(__dirname, '..').replace('loader', 'kotori');
|
|
13
|
-
let count = 0;
|
|
14
|
-
while (!fs_1.default.existsSync(path_1.default.join(root, CONFIG_FILE))) {
|
|
15
|
-
if (count > 5)
|
|
16
|
-
throw new kotori_bot_1.CoreError(`cannot find kotori-bot global ${CONFIG_FILE}`);
|
|
17
|
-
root = path_1.default.join(root, '..');
|
|
18
|
-
count += 1;
|
|
19
|
-
}
|
|
20
|
-
return {
|
|
21
|
-
root,
|
|
22
|
-
modules: path_1.default.join(root, 'modules'),
|
|
23
|
-
};
|
|
24
|
-
})();
|
|
25
|
-
exports.globalConfig = (() => {
|
|
26
|
-
const data = (0, kotori_bot_1.loadConfig)(path_1.default.join(exports.baseDir.root, CONFIG_FILE), 'yaml');
|
|
27
|
-
const isExistsGlobal = data && typeof data === 'object' && data.global && typeof data.global === 'object';
|
|
28
|
-
try {
|
|
29
|
-
if (!isExistsGlobal)
|
|
30
|
-
throw new kotori_bot_1.TsuError('en_US', 'array_error');
|
|
31
|
-
const lang = data.global.lang ? kotori_bot_1.localeTypeSchema.parse(data.global.lang) : undefined;
|
|
32
|
-
const commandPrefix = data.global['command-prefix'] ? data.global['command-prefix'] : undefined;
|
|
33
|
-
return (0, kotori_bot_1.globalConfigSchemaController)(lang, commandPrefix).parse(data);
|
|
34
|
-
}
|
|
35
|
-
catch (err) {
|
|
36
|
-
if (!(err instanceof kotori_bot_1.TsuError))
|
|
37
|
-
throw err;
|
|
38
|
-
throw new kotori_bot_1.CoreError(`kotori-bot global ${CONFIG_FILE} format error`);
|
|
39
|
-
}
|
|
40
|
-
})();
|
package/lib/main.js
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
/*
|
|
7
|
-
* @Author: hotaru biyuehuya@gmail.com
|
|
8
|
-
* @Blog: https://hotaru.icu
|
|
9
|
-
* @Date: 2023-06-24 15:12:55
|
|
10
|
-
* @LastEditors: Hotaru biyuehuya@gmail.com
|
|
11
|
-
* @LastEditTime: 2023-12-17 15:54:48
|
|
12
|
-
*/
|
|
13
|
-
const kotori_bot_1 = require("kotori-bot");
|
|
14
|
-
const modules_1 = __importDefault(require("./modules"));
|
|
15
|
-
const global_1 = require("./global");
|
|
16
|
-
const log_1 = __importDefault(require("./log"));
|
|
17
|
-
const isDev = process.env.NODE_ENV === 'dev';
|
|
18
|
-
const kotoriConfig = {
|
|
19
|
-
baseDir: global_1.baseDir,
|
|
20
|
-
config: global_1.globalConfig,
|
|
21
|
-
options: {
|
|
22
|
-
env: isDev ? 'dev' : 'build',
|
|
23
|
-
},
|
|
24
|
-
};
|
|
25
|
-
class Main extends kotori_bot_1.ContextInstance {
|
|
26
|
-
ctx;
|
|
27
|
-
constructor() {
|
|
28
|
-
kotori_bot_1.ContextInstance.setInstance(new modules_1.default(kotoriConfig));
|
|
29
|
-
super();
|
|
30
|
-
this.ctx = kotori_bot_1.ContextInstance.getInstance();
|
|
31
|
-
// 静态类型继续居然她妈是隔离的
|
|
32
|
-
}
|
|
33
|
-
run() {
|
|
34
|
-
(0, log_1.default)(this.ctx.package, this.ctx);
|
|
35
|
-
this.catchError();
|
|
36
|
-
this.listenMessage();
|
|
37
|
-
this.loadAllModule();
|
|
38
|
-
this.checkUpdate();
|
|
39
|
-
}
|
|
40
|
-
handleError(err, prefix) {
|
|
41
|
-
const isKotoriError = err instanceof kotori_bot_1.KotoriError;
|
|
42
|
-
if (!isKotoriError) {
|
|
43
|
-
this.ctx.logger.tag(prefix, 'default', prefix === 'UCE' ? 'cyanBG' : 'greenBG').error(err);
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
this.ctx.logger
|
|
47
|
-
.tag(err.name.split('Error')[0].toUpperCase(), 'yellow', 'default')[err.level === 'normal' ? 'error' : err.level](err.message, err.stack);
|
|
48
|
-
if (err.name === 'CoreError')
|
|
49
|
-
process.emit('SIGINT');
|
|
50
|
-
}
|
|
51
|
-
catchError() {
|
|
52
|
-
process.on('uncaughtExceptionMonitor', err => this.handleError(err, 'UCE'));
|
|
53
|
-
process.on('unhandledRejection', err => this.handleError(err, 'UHR'));
|
|
54
|
-
process.on('SIGINT', () => {
|
|
55
|
-
process.exit();
|
|
56
|
-
});
|
|
57
|
-
this.ctx.logger.debug('Run info: develop with debuing...');
|
|
58
|
-
}
|
|
59
|
-
listenMessage() {
|
|
60
|
-
const handleConnectInfo = (data) => {
|
|
61
|
-
if (!data.info)
|
|
62
|
-
return;
|
|
63
|
-
this.ctx.logger[data.normal ? 'log' : 'warn'](`[${data.adapter.platform}]`, `${data.adapter.identity}:`, data.info);
|
|
64
|
-
};
|
|
65
|
-
this.ctx.on('connect', handleConnectInfo);
|
|
66
|
-
this.ctx.on('disconnect', handleConnectInfo);
|
|
67
|
-
this.ctx.on('load_module', data => {
|
|
68
|
-
if (!data.module)
|
|
69
|
-
return;
|
|
70
|
-
const { name, version, author } = data.module.package;
|
|
71
|
-
this.ctx.logger.info(`Loaded ${data.moduleType} ${name} Version: ${version} ${Array.isArray(author) ? `Authors: ${author.join(',')}` : `Author: ${author}`}`);
|
|
72
|
-
});
|
|
73
|
-
this.ctx.on('load_all_module', data => {
|
|
74
|
-
const failed = data.expected - data.reality;
|
|
75
|
-
this.ctx.logger.info(`Loaded ${data.reality} modules (plugins)${failed > 0 ? `, failed to load ${failed} modules` : ''}`);
|
|
76
|
-
this.loadAllAdapter();
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
loadAllModule() {
|
|
80
|
-
this.ctx.moduleAll();
|
|
81
|
-
if (isDev)
|
|
82
|
-
this.ctx.watchFile();
|
|
83
|
-
}
|
|
84
|
-
loadAllAdapter() {
|
|
85
|
-
const adapters = this.ctx.internal.getAdapters();
|
|
86
|
-
Object.keys(this.ctx.config.adapter).forEach(botName => {
|
|
87
|
-
const botConfig = this.ctx.config.adapter[botName];
|
|
88
|
-
if (!(botConfig.extends in adapters)) {
|
|
89
|
-
this.ctx.logger.warn(`Cannot find adapter '${botConfig.extends}' for ${botName}`);
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
const array = adapters[botConfig.extends];
|
|
93
|
-
const isSchema = array[1]?.parseSafe(botConfig);
|
|
94
|
-
if (isSchema && !isSchema.value) {
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
const bot = new array[0](this.ctx, isSchema ? isSchema.data : botConfig, botName);
|
|
98
|
-
// if (!(botConfig.extend in Adapter)) Adapter.apis[botConfig.extend] = []; // I dont know whats this
|
|
99
|
-
// this.ctx.botStack[botConfig.extend].push(bot.api);
|
|
100
|
-
bot.start();
|
|
101
|
-
}); /*
|
|
102
|
-
const adapters: Adapter[] = [];
|
|
103
|
-
Object.values(this.ctx.botStack).forEach(apis => {
|
|
104
|
-
apis.forEach(api => adapters.push(api.adapter));
|
|
105
|
-
}); */
|
|
106
|
-
// this.ctx.emit({ type: 'adapters', adapters });
|
|
107
|
-
}
|
|
108
|
-
async checkUpdate() {
|
|
109
|
-
const { version } = this.ctx.package;
|
|
110
|
-
const res = await this.ctx.http
|
|
111
|
-
.get('https://hotaru.icu/api/agent/?url=https://raw.githubusercontent.com/kotorijs/kotori/master/packages/kotori/package.json')
|
|
112
|
-
.catch(() => this.ctx.logger.error('Get update failed, please check your network'));
|
|
113
|
-
if (!res || !kotori_bot_1.Tsu.Object({ version: kotori_bot_1.Tsu.String() }).check(res)) {
|
|
114
|
-
this.ctx.logger.error(`Detection update failed`);
|
|
115
|
-
}
|
|
116
|
-
else if (version === res.version) {
|
|
117
|
-
this.ctx.logger.log('Kotori is currently the latest version');
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
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`);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
exports.default = Main;
|
package/lib/modules.js
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.Modules = void 0;
|
|
7
|
-
const fs_1 = __importDefault(require("fs"));
|
|
8
|
-
const path_1 = __importDefault(require("path"));
|
|
9
|
-
const kotori_bot_1 = require("kotori-bot");
|
|
10
|
-
const consts_1 = require("./consts");
|
|
11
|
-
class Modules extends kotori_bot_1.Context {
|
|
12
|
-
isDev = this.options.env === 'dev';
|
|
13
|
-
moduleRootDir = [];
|
|
14
|
-
getDirFiles(rootDir) {
|
|
15
|
-
const files = fs_1.default.readdirSync(rootDir);
|
|
16
|
-
const list = [];
|
|
17
|
-
files.forEach(fileName => {
|
|
18
|
-
const file = path_1.default.join(rootDir, fileName);
|
|
19
|
-
if (fs_1.default.statSync(file).isDirectory()) {
|
|
20
|
-
list.push(...this.getDirFiles(file));
|
|
21
|
-
}
|
|
22
|
-
if (path_1.default.parse(file).ext !== (this.isDev ? consts_1.DEV_FILE : consts_1.BUILD_FILE))
|
|
23
|
-
return;
|
|
24
|
-
list.push(path_1.default.resolve(file));
|
|
25
|
-
});
|
|
26
|
-
return list;
|
|
27
|
-
}
|
|
28
|
-
getModuleRootDir() {
|
|
29
|
-
Object.assign(this.config.global.dirs, this.baseDir.modules).forEach(dir => {
|
|
30
|
-
if (fs_1.default.existsSync(dir) && fs_1.default.statSync(dir).isDirectory())
|
|
31
|
-
this.moduleRootDir.push(dir);
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
getModuleList(rootDir) {
|
|
35
|
-
const files = fs_1.default.readdirSync(rootDir);
|
|
36
|
-
files.forEach(fileName => {
|
|
37
|
-
const dir = path_1.default.join(rootDir, fileName);
|
|
38
|
-
if (!fs_1.default.statSync(dir).isDirectory())
|
|
39
|
-
return;
|
|
40
|
-
const packagePath = path_1.default.join(dir, 'package.json');
|
|
41
|
-
let packageJson;
|
|
42
|
-
if (!fs_1.default.existsSync(packagePath))
|
|
43
|
-
return;
|
|
44
|
-
try {
|
|
45
|
-
packageJson = JSON.parse(fs_1.default.readFileSync(packagePath).toString());
|
|
46
|
-
}
|
|
47
|
-
catch {
|
|
48
|
-
throw new kotori_bot_1.DevError(`illegal package.json ${packagePath}`);
|
|
49
|
-
}
|
|
50
|
-
if (!kotori_bot_1.ModulePackageSchema.check(packageJson)) {
|
|
51
|
-
if (rootDir !== this.baseDir.modules)
|
|
52
|
-
return;
|
|
53
|
-
throw new kotori_bot_1.DevError(`package.json format error ${packagePath}`);
|
|
54
|
-
}
|
|
55
|
-
const mainPath = path_1.default.join(dir, this.isDev ? consts_1.DEV_IMPORT : packageJson.main);
|
|
56
|
-
if (!fs_1.default.existsSync(mainPath))
|
|
57
|
-
throw new kotori_bot_1.DevError(`cannot find ${mainPath}`);
|
|
58
|
-
const codeDirs = path_1.default.join(dir, this.isDev ? consts_1.DEV_CODE_DIRS : path_1.default.dirname(packageJson.main));
|
|
59
|
-
this.moduleStack.push({
|
|
60
|
-
package: packageJson,
|
|
61
|
-
fileList: fs_1.default.statSync(codeDirs).isDirectory() ? this.getDirFiles(codeDirs) : [],
|
|
62
|
-
mainPath: path_1.default.resolve(mainPath),
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
moduleQuick(moduleData) {
|
|
67
|
-
return this.use(moduleData, this, this.config.plugin[(0, kotori_bot_1.stringRightSplit)(moduleData.package.name, kotori_bot_1.PLUGIN_PREFIX)] ?? {});
|
|
68
|
-
}
|
|
69
|
-
moduleAll = async () => {
|
|
70
|
-
this.getModuleRootDir();
|
|
71
|
-
this.moduleRootDir.forEach(dir => {
|
|
72
|
-
this.getModuleList(dir);
|
|
73
|
-
});
|
|
74
|
-
const array = this.moduleStack.filter(data => data.package.name.startsWith(kotori_bot_1.OFFICIAL_MODULES_SCOPE));
|
|
75
|
-
array.push(...this.moduleStack.filter(data => !array.includes(data)));
|
|
76
|
-
array.forEach(moduleData => this.moduleQuick(moduleData));
|
|
77
|
-
};
|
|
78
|
-
watchFile = async () => {
|
|
79
|
-
this.moduleStack.forEach(moduleData => moduleData.fileList.forEach(file => fs_1.default.watchFile(file, () => this.dispose(moduleData) && this.moduleQuick(moduleData))));
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
exports.Modules = Modules;
|
|
83
|
-
exports.default = Modules;
|