@kotori-bot/core 1.2.0 → 1.4.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/README.md +17 -5
- package/lib/components/config.js +2 -2
- package/lib/components/core.d.ts +11 -12
- package/lib/components/core.js +7 -10
- package/lib/components/index.d.ts +1 -0
- package/lib/components/index.js +1 -0
- package/lib/components/message.d.ts +2 -1
- package/lib/components/message.js +30 -47
- package/lib/components/modules.js +16 -7
- package/lib/constants.d.ts +15 -0
- package/lib/constants.js +19 -0
- package/lib/consts.d.ts +2 -0
- package/lib/consts.js +4 -2
- package/lib/context/context.d.ts +4 -2
- package/lib/context/context.js +38 -17
- package/lib/context/events.d.ts +18 -0
- package/lib/context/events.js +2 -0
- package/lib/context/index.d.ts +3 -0
- package/lib/context/index.js +3 -0
- package/lib/context/modules.d.ts +39 -0
- package/lib/context/modules.js +65 -0
- package/lib/context/service.d.ts +16 -0
- package/lib/context/service.js +22 -0
- package/lib/context/symbols.d.ts +0 -1
- package/lib/context/symbols.js +1 -1
- package/lib/context/test.d.ts +6 -0
- package/lib/context/test.js +12 -0
- package/lib/global/constants.d.ts +15 -0
- package/lib/global/constants.js +19 -0
- package/lib/global/index.d.ts +2 -0
- package/lib/global/index.js +18 -0
- package/lib/global/symbols.d.ts +8 -0
- package/lib/global/symbols.js +12 -0
- package/lib/global/tokens.d.ts +8 -0
- package/lib/global/tokens.js +12 -0
- package/lib/index.d.ts +4 -3
- package/lib/index.js +11 -3
- package/lib/service/adapter.d.ts +9 -6
- package/lib/service/adapter.js +69 -23
- package/lib/service/cache.d.ts +8 -4
- package/lib/service/cache.js +23 -17
- package/lib/service/index.d.ts +0 -2
- package/lib/service/index.js +0 -2
- package/lib/service/service.d.ts +9 -13
- package/lib/service/service.js +7 -18
- package/lib/types/adapter.d.ts +22 -0
- package/lib/types/adapter.js +2 -0
- package/lib/types/config.d.ts +4 -5
- package/lib/types/index.d.ts +1 -3
- package/lib/types/index.js +1 -3
- package/lib/types/message.d.ts +29 -19
- package/lib/types/service.js +0 -1
- package/lib/utils/command.d.ts +1 -2
- package/lib/utils/command.js +11 -7
- package/lib/utils/commandError.d.ts +2 -2
- package/lib/utils/commandError.js +2 -2
- package/lib/utils/container.d.ts +1 -1
- package/lib/utils/error.d.ts +25 -0
- package/lib/utils/error.js +29 -0
- package/lib/utils/errror.d.ts +1 -2
- package/lib/utils/factory.d.ts +2 -2
- package/lib/utils/factory.js +1 -1
- package/package.json +7 -3
- package/lib/service/database.d.ts +0 -5
- package/lib/service/database.js +0 -14
package/README.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
|
-
# @kotori-bot/core
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
-
|
|
1
|
+
# @kotori-bot/core
|
|
2
|
+
|
|
3
|
+
- Context
|
|
4
|
+
- Symbols
|
|
5
|
+
- Service
|
|
6
|
+
- Modules
|
|
7
|
+
- Core
|
|
8
|
+
- Config
|
|
9
|
+
- Message
|
|
10
|
+
- Adapter
|
|
11
|
+
- Api
|
|
12
|
+
- Elements
|
|
13
|
+
- Cache
|
|
14
|
+
|
|
15
|
+
## Reference
|
|
16
|
+
|
|
17
|
+
- [Kotori Docs](https://kotori.js.org/)
|
package/lib/components/config.js
CHANGED
|
@@ -7,7 +7,7 @@ exports.Config = void 0;
|
|
|
7
7
|
const tsukiko_1 = __importDefault(require("tsukiko"));
|
|
8
8
|
const path_1 = require("path");
|
|
9
9
|
const tools_1 = require("@kotori-bot/tools");
|
|
10
|
-
const
|
|
10
|
+
const global_1 = require("../global");
|
|
11
11
|
const packageInfoSchema = tsukiko_1.default.Object({
|
|
12
12
|
name: tsukiko_1.default.String(),
|
|
13
13
|
version: tsukiko_1.default.String(),
|
|
@@ -19,7 +19,7 @@ const packageInfoSchema = tsukiko_1.default.Object({
|
|
|
19
19
|
class Config {
|
|
20
20
|
config;
|
|
21
21
|
pkg;
|
|
22
|
-
constructor(config =
|
|
22
|
+
constructor(config = global_1.DEFAULT_CORE_CONFIG) {
|
|
23
23
|
this.config = config;
|
|
24
24
|
/* load package.json */
|
|
25
25
|
const info = (0, tools_1.loadConfig)((0, path_1.join)(__dirname, '../../package.json'));
|
package/lib/components/core.d.ts
CHANGED
|
@@ -1,28 +1,27 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Http } from '@kotori-bot/tools';
|
|
2
2
|
import I18n from '@kotori-bot/i18n';
|
|
3
3
|
import type { Parser } from 'tsukiko';
|
|
4
|
-
import { Context
|
|
4
|
+
import { Context } from 'fluoro';
|
|
5
5
|
import Config from './config';
|
|
6
6
|
import Message from './message';
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
declare module '
|
|
11
|
-
interface Context
|
|
7
|
+
import type { AdapterClass } from '../types';
|
|
8
|
+
import { Cache, type Api } from '../service';
|
|
9
|
+
import { Symbols } from '../global';
|
|
10
|
+
declare module 'fluoro' {
|
|
11
|
+
interface Context {
|
|
12
12
|
readonly [Symbols.adapter]: Core[typeof Symbols.adapter];
|
|
13
13
|
readonly [Symbols.bot]: Core[typeof Symbols.bot];
|
|
14
14
|
readonly config: Config['config'];
|
|
15
15
|
readonly pkg: Config['pkg'];
|
|
16
|
-
use(modules: Parameters<Modules['use']>[0], config?: Parameters<Modules['use']>[1]): Promise<void>;
|
|
17
|
-
dispose(modules: Parameters<Modules['dispose']>[0]): void;
|
|
18
16
|
readonly [Symbols.midware]: Message[typeof Symbols.midware];
|
|
19
17
|
readonly [Symbols.command]: Message[typeof Symbols.command];
|
|
20
18
|
readonly [Symbols.regexp]: Message[typeof Symbols.regexp];
|
|
21
|
-
midware
|
|
22
|
-
command
|
|
23
|
-
regexp
|
|
19
|
+
midware: Message['midware'];
|
|
20
|
+
command: Message['command'];
|
|
21
|
+
regexp: Message['regexp'];
|
|
24
22
|
http: Http;
|
|
25
23
|
i18n: I18n;
|
|
24
|
+
cache: Cache;
|
|
26
25
|
}
|
|
27
26
|
}
|
|
28
27
|
export declare class Core extends Context {
|
package/lib/components/core.js
CHANGED
|
@@ -6,28 +6,25 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.Core = void 0;
|
|
7
7
|
const tools_1 = require("@kotori-bot/tools");
|
|
8
8
|
const i18n_1 = __importDefault(require("@kotori-bot/i18n"));
|
|
9
|
-
const
|
|
9
|
+
const fluoro_1 = require("fluoro");
|
|
10
10
|
const config_1 = __importDefault(require("./config"));
|
|
11
11
|
const message_1 = __importDefault(require("./message"));
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
[
|
|
12
|
+
const service_1 = require("../service");
|
|
13
|
+
const global_1 = require("../global");
|
|
14
|
+
class Core extends fluoro_1.Context {
|
|
15
|
+
[global_1.Symbols.adapter] = new Map();
|
|
16
|
+
[global_1.Symbols.bot] = new Map();
|
|
16
17
|
constructor(config) {
|
|
17
18
|
super();
|
|
18
19
|
this.provide('config', new config_1.default(config));
|
|
19
20
|
this.mixin('config', ['config', 'pkg']);
|
|
20
|
-
this.provide('events', new tools_1.Events());
|
|
21
|
-
this.mixin('events', ['emit', 'on', 'once', 'off', 'offAll']);
|
|
22
|
-
this.provide('modules', new modules_1.default(this));
|
|
23
|
-
this.mixin('modules', ['use', 'dispose']);
|
|
24
21
|
this.provide('message', new message_1.default(this));
|
|
25
22
|
this.mixin('message', ['midware', 'command', 'regexp']);
|
|
26
23
|
this.provide('http', new tools_1.Http({ validateStatus: () => true }));
|
|
27
24
|
this.inject('http');
|
|
28
25
|
this.provide('i18n', new i18n_1.default({ lang: this.config.global.lang }));
|
|
29
26
|
this.inject('i18n');
|
|
30
|
-
this.
|
|
27
|
+
this.service('cache', new service_1.Cache(this.extends()));
|
|
31
28
|
}
|
|
32
29
|
}
|
|
33
30
|
exports.Core = Core;
|
package/lib/components/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { Context
|
|
1
|
+
import { type Context } from 'fluoro';
|
|
2
2
|
import { type CommandConfig, type MidwareCallback, type RegexpCallback } from '../types';
|
|
3
3
|
import { Command } from '../utils/command';
|
|
4
|
+
import { Symbols } from '../global';
|
|
4
5
|
interface MidwareData {
|
|
5
6
|
callback: MidwareCallback;
|
|
6
7
|
priority: number;
|
|
@@ -5,31 +5,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.Message = void 0;
|
|
7
7
|
const tools_1 = require("@kotori-bot/tools");
|
|
8
|
-
const context_1 = require("../context");
|
|
9
8
|
const factory_1 = require("../utils/factory");
|
|
10
9
|
const command_1 = require("../utils/command");
|
|
11
10
|
const commandError_1 = __importDefault(require("../utils/commandError"));
|
|
12
|
-
|
|
13
|
-
return (obj) => {
|
|
14
|
-
const result = obj;
|
|
15
|
-
Object.keys(result).forEach((key) => {
|
|
16
|
-
if (!result[key] || typeof result[key] !== 'string')
|
|
17
|
-
return;
|
|
18
|
-
result[key] = i18n.locale(result[key]);
|
|
19
|
-
});
|
|
20
|
-
return result;
|
|
21
|
-
};
|
|
22
|
-
}
|
|
11
|
+
const global_1 = require("../global");
|
|
23
12
|
class Message {
|
|
24
|
-
[
|
|
25
|
-
[
|
|
26
|
-
[
|
|
13
|
+
[global_1.Symbols.midware] = new Set();
|
|
14
|
+
[global_1.Symbols.command] = new Set();
|
|
15
|
+
[global_1.Symbols.regexp] = new Set();
|
|
27
16
|
handleMidware(session) {
|
|
28
17
|
const { api } = session;
|
|
29
|
-
api.adapter.status.receivedMsg += 1;
|
|
30
18
|
let isPass = true;
|
|
31
19
|
let midwares = [];
|
|
32
|
-
|
|
20
|
+
api.adapter.status.receivedMsg += 1;
|
|
21
|
+
this[global_1.Symbols.midware].forEach((val) => midwares.push(val));
|
|
33
22
|
midwares = midwares.sort((first, second) => first.priority - second.priority);
|
|
34
23
|
let lastMidwareNum = -1;
|
|
35
24
|
while (midwares.length > 0) {
|
|
@@ -46,7 +35,7 @@ class Message {
|
|
|
46
35
|
const { session } = data;
|
|
47
36
|
if (!data.isPass)
|
|
48
37
|
return;
|
|
49
|
-
this[
|
|
38
|
+
this[global_1.Symbols.regexp].forEach((data) => {
|
|
50
39
|
const match = session.message.match(data.match);
|
|
51
40
|
if (!match)
|
|
52
41
|
return;
|
|
@@ -60,57 +49,51 @@ class Message {
|
|
|
60
49
|
return;
|
|
61
50
|
const params = {
|
|
62
51
|
session,
|
|
63
|
-
|
|
52
|
+
raw: (0, tools_1.stringRightSplit)(session.message, prefix)
|
|
64
53
|
};
|
|
65
54
|
this.ctx.emit('before_parse', params);
|
|
66
55
|
const cancel = (0, factory_1.cancelFactory)();
|
|
67
|
-
|
|
68
|
-
scope: session.type,
|
|
69
|
-
access: session.userId === session.api.adapter.config.master ? 2 /* CommandAccess.ADMIN */ : 0 /* CommandAccess.MEMBER */
|
|
70
|
-
};
|
|
71
|
-
this.ctx.emit('before_command', { cancel: cancel.get(), ...params, ...cmdParams });
|
|
56
|
+
this.ctx.emit('before_command', { cancel: cancel.get(), ...params });
|
|
72
57
|
if (cancel.value)
|
|
73
58
|
return;
|
|
74
|
-
let matched
|
|
75
|
-
this[
|
|
59
|
+
let matched;
|
|
60
|
+
this[global_1.Symbols.command].forEach(async (cmd) => {
|
|
76
61
|
if (matched || !cmd.meta.action)
|
|
77
62
|
return;
|
|
78
|
-
const result = command_1.Command.run(params.
|
|
63
|
+
const result = command_1.Command.run(params.raw, cmd.meta);
|
|
79
64
|
if (result instanceof commandError_1.default && result.value.type === 'unknown')
|
|
80
65
|
return;
|
|
81
|
-
matched =
|
|
82
|
-
this.ctx.emit('parse', { result, ...params, cancel: cancel.get() });
|
|
66
|
+
matched = cmd;
|
|
67
|
+
this.ctx.emit('parse', { command: cmd, result, ...params, cancel: cancel.get() });
|
|
83
68
|
if (cancel.value || result instanceof commandError_1.default)
|
|
84
69
|
return;
|
|
85
70
|
try {
|
|
86
71
|
const executed = await cmd.meta.action({ args: result.args, options: result.options }, session);
|
|
87
72
|
if (executed instanceof commandError_1.default) {
|
|
88
|
-
this.ctx.emit('command', { result, ...params
|
|
73
|
+
this.ctx.emit('command', { command: cmd, result: executed, ...params });
|
|
89
74
|
return;
|
|
90
75
|
}
|
|
91
|
-
if (executed !== undefined)
|
|
92
|
-
session.
|
|
93
|
-
? (0, tools_1.stringTemp)(session.i18n.locale(executed[0]), objectTempFactory(session.i18n)(executed[1]))
|
|
94
|
-
: session.i18n.locale(executed));
|
|
95
|
-
}
|
|
76
|
+
if (executed !== undefined)
|
|
77
|
+
session.quick(executed);
|
|
96
78
|
this.ctx.emit('command', {
|
|
79
|
+
command: cmd,
|
|
97
80
|
result: executed instanceof commandError_1.default ? result : executed,
|
|
98
|
-
...params
|
|
99
|
-
...cmdParams
|
|
81
|
+
...params
|
|
100
82
|
});
|
|
101
83
|
}
|
|
102
84
|
catch (error) {
|
|
103
85
|
this.ctx.emit('command', {
|
|
86
|
+
command: matched,
|
|
104
87
|
result: new commandError_1.default({ type: 'error', error }),
|
|
105
|
-
...params
|
|
106
|
-
...cmdParams
|
|
88
|
+
...params
|
|
107
89
|
});
|
|
108
90
|
}
|
|
109
91
|
});
|
|
110
92
|
if (matched)
|
|
111
93
|
return;
|
|
112
94
|
this.ctx.emit('parse', {
|
|
113
|
-
|
|
95
|
+
command: new command_1.Command(''),
|
|
96
|
+
result: new commandError_1.default({ type: 'unknown', input: params.raw }),
|
|
114
97
|
...params,
|
|
115
98
|
cancel: cancel.get()
|
|
116
99
|
});
|
|
@@ -118,7 +101,7 @@ class Message {
|
|
|
118
101
|
ctx;
|
|
119
102
|
constructor(ctx) {
|
|
120
103
|
this.ctx = ctx;
|
|
121
|
-
this.ctx.on('on_message', (
|
|
104
|
+
this.ctx.on('on_message', (session) => this.handleMidware(session));
|
|
122
105
|
this.ctx.on('midwares', (data) => this.handleCommand(data));
|
|
123
106
|
this.ctx.on('midwares', (data) => this.handleRegexp(data));
|
|
124
107
|
this.ctx.on('before_send', (data) => {
|
|
@@ -128,22 +111,22 @@ class Message {
|
|
|
128
111
|
}
|
|
129
112
|
midware(callback, priority = 100) {
|
|
130
113
|
const data = { callback, priority };
|
|
131
|
-
this[
|
|
132
|
-
const dispose = () => this[
|
|
114
|
+
this[global_1.Symbols.midware].add(data);
|
|
115
|
+
const dispose = () => this[global_1.Symbols.midware].delete(data);
|
|
133
116
|
(0, factory_1.disposeFactory)(this.ctx, dispose);
|
|
134
117
|
return dispose;
|
|
135
118
|
}
|
|
136
119
|
command(template, config) {
|
|
137
120
|
const command = new command_1.Command(template, config);
|
|
138
|
-
this[
|
|
139
|
-
const dispose = () => this[
|
|
121
|
+
this[global_1.Symbols.command].add(command);
|
|
122
|
+
const dispose = () => this[global_1.Symbols.command].delete(command);
|
|
140
123
|
(0, factory_1.disposeFactory)(this.ctx, dispose);
|
|
141
124
|
return command;
|
|
142
125
|
}
|
|
143
126
|
regexp(match, callback) {
|
|
144
127
|
const data = { match, callback };
|
|
145
|
-
this[
|
|
146
|
-
const dispose = () => this[
|
|
128
|
+
this[global_1.Symbols.regexp].add(data);
|
|
129
|
+
const dispose = () => this[global_1.Symbols.regexp].delete(data);
|
|
147
130
|
(0, factory_1.disposeFactory)(this.ctx, dispose);
|
|
148
131
|
return dispose;
|
|
149
132
|
}
|
|
@@ -35,22 +35,31 @@ function checkConfig(schema, config) {
|
|
|
35
35
|
class Modules {
|
|
36
36
|
handleExports(identity, ctx, exports, config) {
|
|
37
37
|
/* before handle */
|
|
38
|
-
const { lang, config: schema, default: defaults, main, Main } = exports.default;
|
|
38
|
+
const { lang, inject, config: schema, default: defaults, main, Main } = exports.default;
|
|
39
39
|
if (lang)
|
|
40
40
|
this.ctx.i18n.use(Array.isArray(lang) ? (0, path_1.resolve)(...lang) : (0, path_1.resolve)(lang));
|
|
41
41
|
/* handle */
|
|
42
|
+
/* service */
|
|
42
43
|
if (isServiceConsructor(defaults)) {
|
|
43
|
-
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
/* adapter */
|
|
47
|
+
if (isAdapterClass(defaults)) {
|
|
44
48
|
const adapterName = identity.split(consts_1.ADAPTER_PREFIX)[1];
|
|
45
|
-
|
|
46
|
-
if (adapterName && isAdapterClass(defaults)) {
|
|
49
|
+
if (adapterName)
|
|
47
50
|
this.ctx[context_1.Symbols.adapter].set(adapterName, [defaults, schema]);
|
|
48
|
-
} /* else if (databaseName && isDatabaseClass(default)) {
|
|
49
|
-
service and database
|
|
50
|
-
} */
|
|
51
51
|
return undefined;
|
|
52
52
|
}
|
|
53
53
|
/* plugin */
|
|
54
|
+
if (inject && Array.isArray(inject)) {
|
|
55
|
+
inject.forEach((identity) => {
|
|
56
|
+
ctx[context_1.Symbols.container].forEach((service, name) => {
|
|
57
|
+
if (!(service instanceof service_1.Service) || service.identity !== identity)
|
|
58
|
+
return;
|
|
59
|
+
ctx.inject(name);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
}
|
|
54
63
|
const moduleConfig = checkConfig(schema, config);
|
|
55
64
|
if (moduleConfig instanceof tsukiko_1.TsuError)
|
|
56
65
|
return new errror_1.ModuleError(`Config format of module ${identity} is error: ${moduleConfig.message}`);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare const OFFICIAL_MODULES_SCOPE = "@kotori-bot/";
|
|
2
|
+
export declare const PLUGIN_PREFIX = "kotori-plugin-";
|
|
3
|
+
export declare const DATABASE_PREFIX = "kotori-plugin-database-";
|
|
4
|
+
export declare const ADAPTER_PREFIX = "kotori-plugin-adapter-";
|
|
5
|
+
export declare const CUSTOM_PREFIX = "kotori-plugin-custom-";
|
|
6
|
+
export declare const DEFAULT_PORT = 720;
|
|
7
|
+
export declare const DEFAULT_CORE_CONFIG: {
|
|
8
|
+
global: {
|
|
9
|
+
lang: "en_US" | "en_GB" | "en_AU" | "zh_CN" | "zh_HK" | "zh_TW" | "zh_SG" | "es_ES" | "es_MX" | "ar_EG" | "ar_AE" | "ru_RU" | "fr_FR" | "fr_CA" | "de_DE" | "de_CH" | "it_IT" | "it_CH" | "hi_IN" | "pt_BR" | "pt_PT" | "tr_TR" | "ja_JP" | "id_ID" | "uk_UA" | "vi_VN" | "th_TH" | "sv_SE" | "nb_NO" | "da_DK" | "fi_FI" | "he_IL" | "sk_SK" | "bg_BG" | "lt_LT" | "sl_SI" | "sr_RS" | "mk_MK" | "sq_AL" | "et_EE" | "mt_MT";
|
|
10
|
+
'command-prefix': string;
|
|
11
|
+
port: number;
|
|
12
|
+
};
|
|
13
|
+
adapter: {};
|
|
14
|
+
plugin: {};
|
|
15
|
+
};
|
package/lib/constants.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_CORE_CONFIG = exports.DEFAULT_PORT = exports.CUSTOM_PREFIX = exports.ADAPTER_PREFIX = exports.DATABASE_PREFIX = exports.PLUGIN_PREFIX = exports.OFFICIAL_MODULES_SCOPE = void 0;
|
|
4
|
+
const i18n_1 = require("@kotori-bot/i18n");
|
|
5
|
+
exports.OFFICIAL_MODULES_SCOPE = '@kotori-bot/';
|
|
6
|
+
exports.PLUGIN_PREFIX = 'kotori-plugin-';
|
|
7
|
+
exports.DATABASE_PREFIX = `${exports.PLUGIN_PREFIX}database-`;
|
|
8
|
+
exports.ADAPTER_PREFIX = `${exports.PLUGIN_PREFIX}adapter-`;
|
|
9
|
+
exports.CUSTOM_PREFIX = `${exports.PLUGIN_PREFIX}custom-`;
|
|
10
|
+
exports.DEFAULT_PORT = 720;
|
|
11
|
+
exports.DEFAULT_CORE_CONFIG = {
|
|
12
|
+
global: {
|
|
13
|
+
lang: i18n_1.DEFAULT_LANG,
|
|
14
|
+
'command-prefix': '/',
|
|
15
|
+
port: exports.DEFAULT_PORT
|
|
16
|
+
},
|
|
17
|
+
adapter: {},
|
|
18
|
+
plugin: {}
|
|
19
|
+
};
|
package/lib/consts.d.ts
CHANGED
|
@@ -3,10 +3,12 @@ export declare const PLUGIN_PREFIX = "kotori-plugin-";
|
|
|
3
3
|
export declare const DATABASE_PREFIX = "kotori-plugin-database-";
|
|
4
4
|
export declare const ADAPTER_PREFIX = "kotori-plugin-adapter-";
|
|
5
5
|
export declare const CUSTOM_PREFIX = "kotori-plugin-custom-";
|
|
6
|
+
export declare const DEFAULT_PORT = 720;
|
|
6
7
|
export declare const DEFAULT_CORE_CONFIG: {
|
|
7
8
|
global: {
|
|
8
9
|
lang: "en_US" | "en_GB" | "en_AU" | "zh_CN" | "zh_HK" | "zh_TW" | "zh_SG" | "es_ES" | "es_MX" | "ar_EG" | "ar_AE" | "ru_RU" | "fr_FR" | "fr_CA" | "de_DE" | "de_CH" | "it_IT" | "it_CH" | "hi_IN" | "pt_BR" | "pt_PT" | "tr_TR" | "ja_JP" | "id_ID" | "uk_UA" | "vi_VN" | "th_TH" | "sv_SE" | "nb_NO" | "da_DK" | "fi_FI" | "he_IL" | "sk_SK" | "bg_BG" | "lt_LT" | "sl_SI" | "sr_RS" | "mk_MK" | "sq_AL" | "et_EE" | "mt_MT";
|
|
9
10
|
'command-prefix': string;
|
|
11
|
+
port: number;
|
|
10
12
|
};
|
|
11
13
|
adapter: {};
|
|
12
14
|
plugin: {};
|
package/lib/consts.js
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DEFAULT_CORE_CONFIG = exports.CUSTOM_PREFIX = exports.ADAPTER_PREFIX = exports.DATABASE_PREFIX = exports.PLUGIN_PREFIX = exports.OFFICIAL_MODULES_SCOPE = void 0;
|
|
3
|
+
exports.DEFAULT_CORE_CONFIG = exports.DEFAULT_PORT = exports.CUSTOM_PREFIX = exports.ADAPTER_PREFIX = exports.DATABASE_PREFIX = exports.PLUGIN_PREFIX = exports.OFFICIAL_MODULES_SCOPE = void 0;
|
|
4
4
|
const i18n_1 = require("@kotori-bot/i18n");
|
|
5
5
|
exports.OFFICIAL_MODULES_SCOPE = '@kotori-bot/';
|
|
6
6
|
exports.PLUGIN_PREFIX = 'kotori-plugin-';
|
|
7
7
|
exports.DATABASE_PREFIX = `${exports.PLUGIN_PREFIX}database-`;
|
|
8
8
|
exports.ADAPTER_PREFIX = `${exports.PLUGIN_PREFIX}adapter-`;
|
|
9
9
|
exports.CUSTOM_PREFIX = `${exports.PLUGIN_PREFIX}custom-`;
|
|
10
|
+
exports.DEFAULT_PORT = 720;
|
|
10
11
|
exports.DEFAULT_CORE_CONFIG = {
|
|
11
12
|
global: {
|
|
12
13
|
lang: i18n_1.DEFAULT_LANG,
|
|
13
|
-
'command-prefix': '/'
|
|
14
|
+
'command-prefix': '/',
|
|
15
|
+
port: exports.DEFAULT_PORT
|
|
14
16
|
},
|
|
15
17
|
adapter: {},
|
|
16
18
|
plugin: {}
|
package/lib/context/context.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import Symbols from './symbols';
|
|
2
|
+
import './events';
|
|
2
3
|
interface obj {
|
|
3
4
|
[propName: string | number | symbol]: any;
|
|
4
5
|
}
|
|
@@ -24,11 +25,12 @@ export declare class Context implements ContextImpl {
|
|
|
24
25
|
readonly [Symbols.container]: Map<string, obj>;
|
|
25
26
|
readonly [Symbols.table]: Map<string, string[]>;
|
|
26
27
|
root: Context;
|
|
28
|
+
parent: Context | null;
|
|
27
29
|
constructor(root?: Context);
|
|
28
|
-
get(prop: string):
|
|
30
|
+
get<T = obj | undefined>(prop: string): T;
|
|
29
31
|
inject<T extends Keys>(prop: T): void;
|
|
30
32
|
provide<T extends obj>(prop: string, value: T): void;
|
|
31
33
|
mixin<K extends Keys>(prop: string, keys: K[]): void;
|
|
32
|
-
extends<T extends obj =
|
|
34
|
+
extends<T extends obj = object>(meta?: T, identity?: string): Context;
|
|
33
35
|
}
|
|
34
36
|
export default Context;
|
package/lib/context/context.js
CHANGED
|
@@ -4,13 +4,39 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.Context = void 0;
|
|
7
|
+
/*
|
|
8
|
+
* @Author: Hotaru biyuehuya@gmail.com
|
|
9
|
+
* @Blog: https://hotaru.icu
|
|
10
|
+
* @Date: 2024-02-07 13:44:38
|
|
11
|
+
* @LastEditors: Hotaru biyuehuya@gmail.com
|
|
12
|
+
* @LastEditTime: 2024-05-01 21:32:19
|
|
13
|
+
*/
|
|
14
|
+
const tools_1 = require("@kotori-bot/tools");
|
|
7
15
|
const symbols_1 = __importDefault(require("./symbols"));
|
|
16
|
+
const modules_1 = __importDefault(require("./modules"));
|
|
17
|
+
const handler = (value, ctx) => {
|
|
18
|
+
if (!value || typeof value !== 'object' || !(value.ctx instanceof Context))
|
|
19
|
+
return value;
|
|
20
|
+
return new Proxy(value, {
|
|
21
|
+
get(target, prop, receiver) {
|
|
22
|
+
if (prop === 'ctx')
|
|
23
|
+
return ctx;
|
|
24
|
+
return Reflect.get(target, prop, receiver);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
const DEFAULT_EXTENDS_NAME = 'sub';
|
|
8
29
|
class Context {
|
|
9
30
|
[symbols_1.default.container] = new Map();
|
|
10
31
|
[symbols_1.default.table] = new Map();
|
|
11
32
|
root;
|
|
33
|
+
parent = null;
|
|
12
34
|
constructor(root) {
|
|
13
35
|
this.root = root || this;
|
|
36
|
+
this.provide('events', root ? root.get('events') : new tools_1.Events());
|
|
37
|
+
this.mixin('events', ['emit', 'on', 'once', 'off', 'offAll']);
|
|
38
|
+
this.provide('modules', new modules_1.default(this));
|
|
39
|
+
this.mixin('modules', ['load', 'unload', 'service']);
|
|
14
40
|
}
|
|
15
41
|
get(prop) {
|
|
16
42
|
return this[symbols_1.default.container].get(prop);
|
|
@@ -35,8 +61,9 @@ class Context {
|
|
|
35
61
|
if (this[key] || !instance[key])
|
|
36
62
|
return;
|
|
37
63
|
this[key] = instance[key];
|
|
38
|
-
if (typeof this[key] === 'function')
|
|
39
|
-
this[key] = this[key]
|
|
64
|
+
if (typeof this[key] === 'function') {
|
|
65
|
+
this[key] = this[key]?.bind(instance);
|
|
66
|
+
}
|
|
40
67
|
});
|
|
41
68
|
}
|
|
42
69
|
extends(meta, identity) {
|
|
@@ -46,31 +73,25 @@ class Context {
|
|
|
46
73
|
if (typeof this[key] === 'function')
|
|
47
74
|
delete metaHandle[key];
|
|
48
75
|
});
|
|
49
|
-
const handler = (value, ctx) => {
|
|
50
|
-
if (!value || typeof value !== 'object' || !value.ctx)
|
|
51
|
-
return value;
|
|
52
|
-
return new Proxy(value, {
|
|
53
|
-
get(target, prop, receiver) {
|
|
54
|
-
if (prop === 'ctx')
|
|
55
|
-
return ctx;
|
|
56
|
-
return Reflect.get(target, prop, receiver);
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
};
|
|
60
76
|
/* set proxy */
|
|
61
77
|
const ctx = new Proxy(new Context(this.root), {
|
|
62
78
|
get: (target, prop) => {
|
|
63
79
|
if (prop === 'identity')
|
|
64
|
-
return identity ?? this.identity ??
|
|
80
|
+
return identity ?? this.identity ?? DEFAULT_EXTENDS_NAME;
|
|
81
|
+
if (prop === 'parent')
|
|
82
|
+
return this;
|
|
65
83
|
if (target[prop])
|
|
66
|
-
return target[prop];
|
|
84
|
+
return handler(target[prop], ctx);
|
|
67
85
|
let value;
|
|
68
86
|
this[symbols_1.default.table].forEach((keys, key) => {
|
|
69
87
|
if (value || (typeof prop === 'string' && !keys.includes(prop)))
|
|
70
88
|
return;
|
|
71
|
-
|
|
89
|
+
const instance = ctx[symbols_1.default.container].get(key);
|
|
90
|
+
if (!instance)
|
|
91
|
+
return;
|
|
92
|
+
value = instance[prop];
|
|
72
93
|
if (typeof value === 'function')
|
|
73
|
-
value = value.bind(
|
|
94
|
+
value = value.bind(instance);
|
|
74
95
|
});
|
|
75
96
|
if (value !== undefined)
|
|
76
97
|
return value;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Events } from '@kotori-bot/tools';
|
|
2
|
+
declare module './context' {
|
|
3
|
+
interface Context {
|
|
4
|
+
emit: Events<EventsMapping>['emit'];
|
|
5
|
+
parallel: Events<EventsMapping>['parallel'];
|
|
6
|
+
on: Events<EventsMapping>['on'];
|
|
7
|
+
once: Events<EventsMapping>['once'];
|
|
8
|
+
off: Events<EventsMapping>['off'];
|
|
9
|
+
offAll: Events<EventsMapping>['offAll'];
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export type EventsList = {
|
|
13
|
+
[K in keyof EventsMapping]: Parameters<EventsMapping[K]>[1] extends never | undefined | null ? Parameters<EventsMapping[K]>[0] : [...Parameters<EventsMapping[K]>];
|
|
14
|
+
};
|
|
15
|
+
export interface EventsMapping {
|
|
16
|
+
ready(): void;
|
|
17
|
+
dispose(): void;
|
|
18
|
+
}
|
package/lib/context/index.d.ts
CHANGED
package/lib/context/index.js
CHANGED
|
@@ -15,4 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./context"), exports);
|
|
18
|
+
__exportStar(require("./events"), exports);
|
|
19
|
+
__exportStar(require("./modules"), exports);
|
|
20
|
+
__exportStar(require("./service"), exports);
|
|
18
21
|
__exportStar(require("./symbols"), exports);
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type Context } from './context';
|
|
2
|
+
import { Service } from './service';
|
|
3
|
+
type ModuleInstanceClass = new (ctx: Context, config: ModuleConfig) => void;
|
|
4
|
+
type ModuleInstanceFunction = (ctx: Context, config: ModuleConfig) => void;
|
|
5
|
+
export interface ModuleExport {
|
|
6
|
+
name?: string;
|
|
7
|
+
main?: ModuleInstanceFunction;
|
|
8
|
+
Main?: ModuleInstanceClass;
|
|
9
|
+
default?: ModuleInstanceFunction | ModuleInstanceClass;
|
|
10
|
+
inject?: string[];
|
|
11
|
+
config?: ModuleConfig;
|
|
12
|
+
}
|
|
13
|
+
export interface ModuleConfig {
|
|
14
|
+
filter: object;
|
|
15
|
+
}
|
|
16
|
+
declare module './events' {
|
|
17
|
+
interface EventsMapping {
|
|
18
|
+
ready_module(data: EventDataModule): void;
|
|
19
|
+
dispose_module(data: EventDataModule): void;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
declare module './context' {
|
|
23
|
+
interface Context {
|
|
24
|
+
load: Modules['load'];
|
|
25
|
+
unload: Modules['unload'];
|
|
26
|
+
service: Modules['service'];
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
interface EventDataModule {
|
|
30
|
+
instance: ModuleExport | ModuleInstanceFunction | ModuleInstanceClass;
|
|
31
|
+
}
|
|
32
|
+
export declare class Modules {
|
|
33
|
+
private readonly ctx;
|
|
34
|
+
constructor(ctx: Context);
|
|
35
|
+
load(instance: EventDataModule['instance']): void;
|
|
36
|
+
unload(instance: EventDataModule['instance']): void;
|
|
37
|
+
service(name: string, instance: Service): void;
|
|
38
|
+
}
|
|
39
|
+
export default Modules;
|