@kotori-bot/core 1.2.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/README.md +12 -0
- package/lib/components/core.d.ts +8 -10
- package/lib/components/core.js +2 -6
- package/lib/components/message.d.ts +1 -1
- package/lib/components/message.js +16 -33
- package/lib/components/modules.js +16 -7
- package/lib/consts.d.ts +2 -0
- package/lib/consts.js +4 -2
- package/lib/context/context.d.ts +3 -2
- package/lib/context/context.js +33 -16
- 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/index.d.ts +1 -0
- package/lib/index.js +8 -0
- package/lib/service/adapter.d.ts +9 -6
- package/lib/service/adapter.js +61 -15
- 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 +3 -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 +5 -1
- package/lib/utils/errror.d.ts +1 -1
- package/lib/utils/factory.js +1 -1
- package/package.json +3 -3
- package/lib/service/database.d.ts +0 -5
- package/lib/service/database.js +0 -14
package/README.md
CHANGED
package/lib/components/core.d.ts
CHANGED
|
@@ -1,28 +1,26 @@
|
|
|
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
4
|
import { Context, Symbols } from '../context';
|
|
5
5
|
import Config from './config';
|
|
6
6
|
import Message from './message';
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import type { Api } from '../service';
|
|
7
|
+
import type { AdapterClass } from '../types';
|
|
8
|
+
import { Cache, type Api } from '../service';
|
|
10
9
|
declare module '../context' {
|
|
11
|
-
interface Context
|
|
10
|
+
interface Context {
|
|
12
11
|
readonly [Symbols.adapter]: Core[typeof Symbols.adapter];
|
|
13
12
|
readonly [Symbols.bot]: Core[typeof Symbols.bot];
|
|
14
13
|
readonly config: Config['config'];
|
|
15
14
|
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
15
|
readonly [Symbols.midware]: Message[typeof Symbols.midware];
|
|
19
16
|
readonly [Symbols.command]: Message[typeof Symbols.command];
|
|
20
17
|
readonly [Symbols.regexp]: Message[typeof Symbols.regexp];
|
|
21
|
-
midware
|
|
22
|
-
command
|
|
23
|
-
regexp
|
|
18
|
+
midware: Message['midware'];
|
|
19
|
+
command: Message['command'];
|
|
20
|
+
regexp: Message['regexp'];
|
|
24
21
|
http: Http;
|
|
25
22
|
i18n: I18n;
|
|
23
|
+
cache: Cache;
|
|
26
24
|
}
|
|
27
25
|
}
|
|
28
26
|
export declare class Core extends Context {
|
package/lib/components/core.js
CHANGED
|
@@ -9,7 +9,7 @@ const i18n_1 = __importDefault(require("@kotori-bot/i18n"));
|
|
|
9
9
|
const context_1 = require("../context");
|
|
10
10
|
const config_1 = __importDefault(require("./config"));
|
|
11
11
|
const message_1 = __importDefault(require("./message"));
|
|
12
|
-
const
|
|
12
|
+
const service_1 = require("../service");
|
|
13
13
|
class Core extends context_1.Context {
|
|
14
14
|
[context_1.Symbols.adapter] = new Map();
|
|
15
15
|
[context_1.Symbols.bot] = new Map();
|
|
@@ -17,17 +17,13 @@ class Core extends context_1.Context {
|
|
|
17
17
|
super();
|
|
18
18
|
this.provide('config', new config_1.default(config));
|
|
19
19
|
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
20
|
this.provide('message', new message_1.default(this));
|
|
25
21
|
this.mixin('message', ['midware', 'command', 'regexp']);
|
|
26
22
|
this.provide('http', new tools_1.Http({ validateStatus: () => true }));
|
|
27
23
|
this.inject('http');
|
|
28
24
|
this.provide('i18n', new i18n_1.default({ lang: this.config.global.lang }));
|
|
29
25
|
this.inject('i18n');
|
|
30
|
-
this.
|
|
26
|
+
this.service('cache', new service_1.Cache(this.extends()));
|
|
31
27
|
}
|
|
32
28
|
}
|
|
33
29
|
exports.Core = Core;
|
|
@@ -9,17 +9,6 @@ const context_1 = require("../context");
|
|
|
9
9
|
const factory_1 = require("../utils/factory");
|
|
10
10
|
const command_1 = require("../utils/command");
|
|
11
11
|
const commandError_1 = __importDefault(require("../utils/commandError"));
|
|
12
|
-
function objectTempFactory(i18n) {
|
|
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
|
-
}
|
|
23
12
|
class Message {
|
|
24
13
|
[context_1.Symbols.midware] = new Set();
|
|
25
14
|
[context_1.Symbols.command] = new Set();
|
|
@@ -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
|
|
59
|
+
let matched;
|
|
75
60
|
this[context_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) => {
|
|
@@ -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}`);
|
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
|
}
|
|
@@ -25,10 +26,10 @@ export declare class Context implements ContextImpl {
|
|
|
25
26
|
readonly [Symbols.table]: Map<string, string[]>;
|
|
26
27
|
root: Context;
|
|
27
28
|
constructor(root?: Context);
|
|
28
|
-
get(prop: string):
|
|
29
|
+
get<T = obj | undefined>(prop: string): T;
|
|
29
30
|
inject<T extends Keys>(prop: T): void;
|
|
30
31
|
provide<T extends obj>(prop: string, value: T): void;
|
|
31
32
|
mixin<K extends Keys>(prop: string, keys: K[]): void;
|
|
32
|
-
extends<T extends obj =
|
|
33
|
+
extends<T extends obj = object>(meta?: T, identity?: string): Context;
|
|
33
34
|
}
|
|
34
35
|
export default Context;
|
package/lib/context/context.js
CHANGED
|
@@ -4,13 +4,37 @@ 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-02-21 10:38:39
|
|
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
|
+
};
|
|
8
28
|
class Context {
|
|
9
29
|
[symbols_1.default.container] = new Map();
|
|
10
30
|
[symbols_1.default.table] = new Map();
|
|
11
31
|
root;
|
|
12
32
|
constructor(root) {
|
|
13
33
|
this.root = root || this;
|
|
34
|
+
this.provide('events', root ? root.get('events') : new tools_1.Events());
|
|
35
|
+
this.mixin('events', ['emit', 'on', 'once', 'off', 'offAll']);
|
|
36
|
+
this.provide('modules', new modules_1.default(this));
|
|
37
|
+
this.mixin('modules', ['load', 'unload', 'service']);
|
|
14
38
|
}
|
|
15
39
|
get(prop) {
|
|
16
40
|
return this[symbols_1.default.container].get(prop);
|
|
@@ -35,8 +59,9 @@ class Context {
|
|
|
35
59
|
if (this[key] || !instance[key])
|
|
36
60
|
return;
|
|
37
61
|
this[key] = instance[key];
|
|
38
|
-
if (typeof this[key] === 'function')
|
|
39
|
-
this[key] = this[key]
|
|
62
|
+
if (typeof this[key] === 'function') {
|
|
63
|
+
this[key] = this[key]?.bind(instance);
|
|
64
|
+
}
|
|
40
65
|
});
|
|
41
66
|
}
|
|
42
67
|
extends(meta, identity) {
|
|
@@ -46,31 +71,23 @@ class Context {
|
|
|
46
71
|
if (typeof this[key] === 'function')
|
|
47
72
|
delete metaHandle[key];
|
|
48
73
|
});
|
|
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
74
|
/* set proxy */
|
|
61
75
|
const ctx = new Proxy(new Context(this.root), {
|
|
62
76
|
get: (target, prop) => {
|
|
63
77
|
if (prop === 'identity')
|
|
64
78
|
return identity ?? this.identity ?? 'sub';
|
|
65
79
|
if (target[prop])
|
|
66
|
-
return target[prop];
|
|
80
|
+
return handler(target[prop], ctx);
|
|
67
81
|
let value;
|
|
68
82
|
this[symbols_1.default.table].forEach((keys, key) => {
|
|
69
83
|
if (value || (typeof prop === 'string' && !keys.includes(prop)))
|
|
70
84
|
return;
|
|
71
|
-
|
|
85
|
+
const instance = ctx[symbols_1.default.container].get(key);
|
|
86
|
+
if (!instance)
|
|
87
|
+
return;
|
|
88
|
+
value = instance[prop];
|
|
72
89
|
if (typeof value === 'function')
|
|
73
|
-
value = value.bind(
|
|
90
|
+
value = value.bind(instance);
|
|
74
91
|
});
|
|
75
92
|
if (value !== undefined)
|
|
76
93
|
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;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Modules = void 0;
|
|
4
|
+
/* eslint import/no-dynamic-require: 0 */
|
|
5
|
+
/* eslint global-require: 0 */
|
|
6
|
+
const tools_1 = require("@kotori-bot/tools");
|
|
7
|
+
const symbols_1 = require("./symbols");
|
|
8
|
+
const service_1 = require("./service");
|
|
9
|
+
function handleFunction(func, ctx, config) {
|
|
10
|
+
func(ctx, config);
|
|
11
|
+
}
|
|
12
|
+
function handleCconstructor(Class, ctx, config) {
|
|
13
|
+
(0, tools_1.none)(new Class(ctx, config));
|
|
14
|
+
}
|
|
15
|
+
const DEFAULT_MODULE_CONFIG = { filter: {} };
|
|
16
|
+
class Modules {
|
|
17
|
+
ctx;
|
|
18
|
+
constructor(ctx) {
|
|
19
|
+
this.ctx = ctx;
|
|
20
|
+
}
|
|
21
|
+
load(instance) {
|
|
22
|
+
const ctx = this.ctx.extends({}, !this.ctx.identity && typeof instance === 'object' ? instance.name : this.ctx.identity);
|
|
23
|
+
if (instance instanceof Function) {
|
|
24
|
+
if ((0, tools_1.isClass)(instance))
|
|
25
|
+
handleCconstructor(instance, ctx, DEFAULT_MODULE_CONFIG);
|
|
26
|
+
else
|
|
27
|
+
instance(ctx, DEFAULT_MODULE_CONFIG);
|
|
28
|
+
this.ctx.emit('ready_module', { instance });
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const { main, Main, inject, default: defaults, config } = instance;
|
|
32
|
+
if (Array.isArray(inject)) {
|
|
33
|
+
inject.forEach((identity) => {
|
|
34
|
+
ctx[symbols_1.Symbols.container].forEach((service, name) => {
|
|
35
|
+
if (!(service instanceof service_1.Service) || service.identity !== identity)
|
|
36
|
+
return;
|
|
37
|
+
ctx.inject(name);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
if (defaults) {
|
|
42
|
+
if ((0, tools_1.isClass)(defaults))
|
|
43
|
+
handleCconstructor(defaults, ctx, config ?? DEFAULT_MODULE_CONFIG);
|
|
44
|
+
else
|
|
45
|
+
handleFunction(defaults, ctx, config ?? DEFAULT_MODULE_CONFIG);
|
|
46
|
+
}
|
|
47
|
+
else if (main) {
|
|
48
|
+
handleFunction(main, ctx, config ?? DEFAULT_MODULE_CONFIG);
|
|
49
|
+
}
|
|
50
|
+
else if (Main) {
|
|
51
|
+
handleCconstructor(Main, ctx, config ?? DEFAULT_MODULE_CONFIG);
|
|
52
|
+
}
|
|
53
|
+
this.ctx.emit('ready_module', { instance });
|
|
54
|
+
}
|
|
55
|
+
unload(instance) {
|
|
56
|
+
this.ctx.emit('dispose_module', { instance });
|
|
57
|
+
}
|
|
58
|
+
service(name, instance) {
|
|
59
|
+
this.ctx.provide(name, instance);
|
|
60
|
+
this.ctx.on('ready', () => this.ctx.get(name).start());
|
|
61
|
+
this.ctx.on('dispose', () => this.ctx.get(name).stop());
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
exports.Modules = Modules;
|
|
65
|
+
exports.default = Modules;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Context } from './context';
|
|
2
|
+
interface ServiceImpl<T extends object = object> {
|
|
3
|
+
readonly identity: string;
|
|
4
|
+
readonly config: T;
|
|
5
|
+
start(): void;
|
|
6
|
+
stop(): void;
|
|
7
|
+
}
|
|
8
|
+
export declare abstract class Service<T extends object = object> implements ServiceImpl<T> {
|
|
9
|
+
readonly ctx: Context;
|
|
10
|
+
readonly config: T;
|
|
11
|
+
readonly identity: string;
|
|
12
|
+
constructor(ctx: Context, config: T, identity: string);
|
|
13
|
+
start(): void;
|
|
14
|
+
stop(): void;
|
|
15
|
+
}
|
|
16
|
+
export default Service;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Service = void 0;
|
|
4
|
+
const tools_1 = require("@kotori-bot/tools");
|
|
5
|
+
class Service {
|
|
6
|
+
ctx;
|
|
7
|
+
config;
|
|
8
|
+
identity;
|
|
9
|
+
constructor(ctx, config, identity) {
|
|
10
|
+
this.ctx = ctx;
|
|
11
|
+
this.config = config;
|
|
12
|
+
this.identity = identity;
|
|
13
|
+
}
|
|
14
|
+
start() {
|
|
15
|
+
return (0, tools_1.none)(this);
|
|
16
|
+
}
|
|
17
|
+
stop() {
|
|
18
|
+
return (0, tools_1.none)(this);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.Service = Service;
|
|
22
|
+
exports.default = Service;
|
package/lib/context/symbols.d.ts
CHANGED
|
@@ -2,7 +2,6 @@ export declare class Symbols {
|
|
|
2
2
|
static readonly container: unique symbol;
|
|
3
3
|
static readonly table: unique symbol;
|
|
4
4
|
static readonly containerKey: (prop: string) => symbol;
|
|
5
|
-
static readonly service: unique symbol;
|
|
6
5
|
static readonly adapter: unique symbol;
|
|
7
6
|
static readonly bot: unique symbol;
|
|
8
7
|
static readonly midware: unique symbol;
|
package/lib/context/symbols.js
CHANGED
|
@@ -6,7 +6,7 @@ class Symbols {
|
|
|
6
6
|
static table = Symbol.for('kotori.context.table');
|
|
7
7
|
static containerKey = (prop) => Symbol.for(`kotori.context.container.${prop}`);
|
|
8
8
|
/* custom */
|
|
9
|
-
static service = Symbol.for('kotori.core.service');
|
|
9
|
+
// static readonly service = Symbol.for('kotori.core.service');
|
|
10
10
|
static adapter = Symbol.for('kotori.core.adapter');
|
|
11
11
|
static bot = Symbol.for('kotori.core.bot');
|
|
12
12
|
static midware = Symbol.for('kotori.core.midware');
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const symbols_1 = require("./symbols");
|
|
4
|
+
const cache_1 = require("../service/cache");
|
|
5
|
+
const context_1 = require("./context");
|
|
6
|
+
class K extends context_1.Context {
|
|
7
|
+
}
|
|
8
|
+
const ctx = new K();
|
|
9
|
+
ctx.provide('cache', new cache_1.Cache(ctx));
|
|
10
|
+
const ctx2 = ctx.extends({}, 'tt');
|
|
11
|
+
ctx2.inject('cache');
|
|
12
|
+
console.log(ctx2[symbols_1.Symbols.container].get('cache').ctx.identity, ctx2.cache.ctx.identity, ctx[symbols_1.Symbols.container].get('cache').ctx.identity, ctx2.get('cache').ctx.identity);
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -14,6 +14,13 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
/*
|
|
18
|
+
* @Author: Hotaru biyuehuya@gmail.com
|
|
19
|
+
* @Blog: https://hotaru.icu
|
|
20
|
+
* @Date: 2024-02-07 13:44:38
|
|
21
|
+
* @LastEditors: Hotaru biyuehuya@gmail.com
|
|
22
|
+
* @LastEditTime: 2024-02-07 14:47:23
|
|
23
|
+
*/
|
|
17
24
|
__exportStar(require("./context"), exports);
|
|
18
25
|
__exportStar(require("./components"), exports);
|
|
19
26
|
__exportStar(require("./service"), exports);
|
|
@@ -24,4 +31,5 @@ __exportStar(require("./utils/container"), exports);
|
|
|
24
31
|
__exportStar(require("./consts"), exports);
|
|
25
32
|
__exportStar(require("./types"), exports);
|
|
26
33
|
__exportStar(require("@kotori-bot/tools"), exports);
|
|
34
|
+
__exportStar(require("@kotori-bot/i18n"), exports);
|
|
27
35
|
__exportStar(require("tsukiko"), exports);
|
package/lib/service/adapter.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type Api from './api';
|
|
2
|
-
import { Context } from '../context';
|
|
3
|
-
import { EventDataApiBase,
|
|
4
|
-
import Service from './service';
|
|
2
|
+
import { Context, EventsList } from '../context';
|
|
3
|
+
import { EventDataApiBase, EventDataTargetId, AdapterConfig } from '../types';
|
|
5
4
|
import Elements from './elements';
|
|
6
5
|
type EventApiType = {
|
|
7
6
|
[K in keyof EventsList]: EventsList[K] extends EventDataApiBase ? EventsList[K] : never;
|
|
@@ -14,8 +13,9 @@ interface AdapterStatus {
|
|
|
14
13
|
sentMsg: number;
|
|
15
14
|
offlineTimes: number;
|
|
16
15
|
}
|
|
17
|
-
interface AdapterImpl<T extends Api = Api>
|
|
16
|
+
interface AdapterImpl<T extends Api = Api> {
|
|
18
17
|
readonly ctx: Context;
|
|
18
|
+
readonly config: AdapterConfig;
|
|
19
19
|
readonly platform: string;
|
|
20
20
|
readonly selfId: EventDataTargetId;
|
|
21
21
|
readonly identity: string;
|
|
@@ -23,12 +23,15 @@ interface AdapterImpl<T extends Api = Api> extends Service {
|
|
|
23
23
|
readonly elements: Elements;
|
|
24
24
|
readonly status: AdapterStatus;
|
|
25
25
|
}
|
|
26
|
-
export declare abstract class Adapter<T extends Api = Api>
|
|
26
|
+
export declare abstract class Adapter<T extends Api = Api> implements AdapterImpl<T> {
|
|
27
27
|
constructor(ctx: Context, config: AdapterConfig, identity: string, Api: new (adapter: Adapter) => T, el?: Elements);
|
|
28
|
+
abstract handle(...data: unknown[]): void;
|
|
29
|
+
abstract start(): void;
|
|
30
|
+
abstract stop(): void;
|
|
28
31
|
abstract send(action: string, params?: object): void | object | Promise<unknown> | null | undefined;
|
|
29
32
|
protected online(): void;
|
|
30
33
|
protected offline(): void;
|
|
31
|
-
protected session<N extends keyof EventApiType>(type: N, data: Omit<EventApiType[N], 'api' | 'send' | 'i18n' | 'quick' | 'el' | 'error'>): void;
|
|
34
|
+
protected session<N extends keyof EventApiType>(type: N, data: Omit<EventApiType[N], 'api' | 'send' | 'i18n' | 'format' | 'quick' | 'prompt' | 'confirm' | 'el' | 'error'>): void;
|
|
32
35
|
readonly ctx: Context;
|
|
33
36
|
readonly config: AdapterConfig;
|
|
34
37
|
readonly identity: string;
|
package/lib/service/adapter.js
CHANGED
|
@@ -7,7 +7,6 @@ exports.Adapter = void 0;
|
|
|
7
7
|
const tools_1 = require("@kotori-bot/tools");
|
|
8
8
|
const context_1 = require("../context");
|
|
9
9
|
const types_1 = require("../types");
|
|
10
|
-
const service_1 = __importDefault(require("./service"));
|
|
11
10
|
const elements_1 = __importDefault(require("./elements"));
|
|
12
11
|
const factory_1 = require("../utils/factory");
|
|
13
12
|
const commandError_1 = __importDefault(require("../utils/commandError"));
|
|
@@ -35,8 +34,8 @@ function setProxy(api, ctx) {
|
|
|
35
34
|
});
|
|
36
35
|
return proxy;
|
|
37
36
|
}
|
|
38
|
-
function sendMessageFactory(adapter, data) {
|
|
39
|
-
if (data.type === types_1.MessageScope.GROUP && 'groupId' in data) {
|
|
37
|
+
function sendMessageFactory(adapter, type, data) {
|
|
38
|
+
if ((data.type === types_1.MessageScope.GROUP || type.includes('group')) && 'groupId' in data) {
|
|
40
39
|
return (message) => {
|
|
41
40
|
adapter.api.sendGroupMsg(message, data.groupId, data.extra);
|
|
42
41
|
};
|
|
@@ -45,6 +44,24 @@ function sendMessageFactory(adapter, data) {
|
|
|
45
44
|
adapter.api.sendPrivateMsg(message, data.userId, data.extra);
|
|
46
45
|
};
|
|
47
46
|
}
|
|
47
|
+
function formatFactory(i18n) {
|
|
48
|
+
return (template, data) => {
|
|
49
|
+
const params = data;
|
|
50
|
+
if (Array.isArray(params)) {
|
|
51
|
+
let str = i18n.locale(template);
|
|
52
|
+
params.forEach((value, index) => {
|
|
53
|
+
str = str.replaceAll(`{${index}}`, i18n.locale(typeof value === 'string' ? value : String(value)));
|
|
54
|
+
});
|
|
55
|
+
return str;
|
|
56
|
+
}
|
|
57
|
+
Object.keys(params).forEach((key) => {
|
|
58
|
+
if (typeof params[key] !== 'string')
|
|
59
|
+
params[key] = String(params[key]);
|
|
60
|
+
params[key] = i18n.locale(params[key]);
|
|
61
|
+
});
|
|
62
|
+
return (0, tools_1.stringTemp)(i18n.locale(template), params);
|
|
63
|
+
};
|
|
64
|
+
}
|
|
48
65
|
function qucikFactory(send, i18n) {
|
|
49
66
|
return async (message) => {
|
|
50
67
|
const msg = await message;
|
|
@@ -54,21 +71,47 @@ function qucikFactory(send, i18n) {
|
|
|
54
71
|
send(i18n.locale(msg));
|
|
55
72
|
return;
|
|
56
73
|
}
|
|
57
|
-
|
|
58
|
-
Object.keys(params).forEach((key) => {
|
|
59
|
-
if (typeof params[key] !== 'string')
|
|
60
|
-
return;
|
|
61
|
-
params[key] = i18n.locale(params[key]);
|
|
62
|
-
});
|
|
63
|
-
send((0, tools_1.stringTemp)(i18n.locale(msg[0]), params));
|
|
74
|
+
send(formatFactory(i18n)(...msg));
|
|
64
75
|
};
|
|
65
76
|
}
|
|
77
|
+
function isSameSender(adapter, data, session) {
|
|
78
|
+
return (session.api.adapter.identity === adapter.identity &&
|
|
79
|
+
session.api.adapter.platform === adapter.platform &&
|
|
80
|
+
session.type === data.type &&
|
|
81
|
+
session.groupId === data.groupId &&
|
|
82
|
+
session.userId === data.userId &&
|
|
83
|
+
'messageId' in data &&
|
|
84
|
+
session.messageId !== data.messageId);
|
|
85
|
+
}
|
|
86
|
+
function promptFactory(quick, adapter, data) {
|
|
87
|
+
return (message) => new Promise((resolve) => {
|
|
88
|
+
const handle = (session) => {
|
|
89
|
+
if (isSameSender(adapter, data, session)) {
|
|
90
|
+
resolve(session.message);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
adapter.ctx.once('on_message', handle);
|
|
94
|
+
};
|
|
95
|
+
quick(message ?? 'corei18n.template.prompt').then(() => adapter.ctx.once('on_message', handle));
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
function confirmFactory(quick, adapter, data) {
|
|
99
|
+
return (options) => new Promise((resolve) => {
|
|
100
|
+
const handle = (session) => {
|
|
101
|
+
if (isSameSender(adapter, data, session)) {
|
|
102
|
+
resolve(session.message === (options?.sure ?? 'corei18n.template.confirm.sure'));
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
adapter.ctx.once('on_message', handle);
|
|
106
|
+
};
|
|
107
|
+
quick(options?.message ?? 'corei18n.template.confirm').then(() => adapter.ctx.once('on_message', handle));
|
|
108
|
+
});
|
|
109
|
+
}
|
|
66
110
|
function error(type, data) {
|
|
67
111
|
return new commandError_1.default(Object.assign(data ?? {}, { type }));
|
|
68
112
|
}
|
|
69
|
-
class Adapter
|
|
113
|
+
class Adapter {
|
|
70
114
|
constructor(ctx, config, identity, Api, el = new elements_1.default()) {
|
|
71
|
-
super('adapter', '');
|
|
72
115
|
this.ctx = ctx;
|
|
73
116
|
this.config = config;
|
|
74
117
|
this.identity = identity;
|
|
@@ -93,11 +136,14 @@ class Adapter extends service_1.default {
|
|
|
93
136
|
this.status.offlineTimes += 1;
|
|
94
137
|
}
|
|
95
138
|
session(type, data) {
|
|
96
|
-
const i18n = this.ctx.i18n.extends(this.config.lang
|
|
97
|
-
const send = sendMessageFactory(this, data);
|
|
139
|
+
const i18n = this.ctx.i18n.extends(this.config.lang);
|
|
140
|
+
const send = sendMessageFactory(this, type, data);
|
|
141
|
+
const format = formatFactory(i18n);
|
|
98
142
|
const quick = qucikFactory(send, i18n);
|
|
143
|
+
const prompt = promptFactory(quick, this, data);
|
|
144
|
+
const confirm = confirmFactory(quick, this, data);
|
|
99
145
|
const { api, elements: el } = this;
|
|
100
|
-
this.ctx.emit(type, { ...data, api, el, send, i18n, quick, error });
|
|
146
|
+
this.ctx.emit(type, ...[{ ...data, api, el, send, i18n, format, quick, prompt, confirm, error }]);
|
|
101
147
|
}
|
|
102
148
|
ctx;
|
|
103
149
|
config;
|
package/lib/service/cache.d.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import Service from '
|
|
1
|
+
import { Service } from '../context';
|
|
2
|
+
type CacheKey = string | symbol;
|
|
3
|
+
type CacheValue = string | number | object;
|
|
2
4
|
export declare class Cache extends Service {
|
|
3
|
-
private
|
|
4
|
-
constructor();
|
|
5
|
-
handle(data: unknown[]): void;
|
|
5
|
+
private cache?;
|
|
6
|
+
constructor(ctx: ConstructorParameters<typeof Service>[0]);
|
|
6
7
|
start(): void;
|
|
7
8
|
stop(): void;
|
|
9
|
+
getContainer(): Map<CacheKey, CacheValue>;
|
|
10
|
+
get<T = CacheValue>(prop: CacheKey): T;
|
|
11
|
+
set(prop: CacheKey, value: CacheValue): void;
|
|
8
12
|
}
|
|
9
13
|
export default Cache;
|
package/lib/service/cache.js
CHANGED
|
@@ -1,27 +1,33 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.Cache = void 0;
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
super('custom', 'cache');
|
|
13
|
-
}
|
|
14
|
-
handle(data) {
|
|
15
|
-
(0, tools_1.none)(this);
|
|
4
|
+
const context_1 = require("../context");
|
|
5
|
+
class Cache extends context_1.Service {
|
|
6
|
+
cache;
|
|
7
|
+
constructor(ctx) {
|
|
8
|
+
super(ctx, {}, 'cache');
|
|
16
9
|
}
|
|
17
10
|
start() {
|
|
18
|
-
this.
|
|
11
|
+
if (this.cache)
|
|
12
|
+
return;
|
|
13
|
+
this.cache = new Map();
|
|
19
14
|
}
|
|
20
15
|
stop() {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
16
|
+
this.cache?.forEach((el) => el.clear());
|
|
17
|
+
this.cache?.clear();
|
|
18
|
+
delete this.cache;
|
|
19
|
+
}
|
|
20
|
+
getContainer() {
|
|
21
|
+
const key = this.ctx.identity ?? 'root';
|
|
22
|
+
if (!this.cache.has(key))
|
|
23
|
+
this.cache.set(key, new Map());
|
|
24
|
+
return this.cache.get(key);
|
|
25
|
+
}
|
|
26
|
+
get(prop) {
|
|
27
|
+
return this.getContainer().get(prop);
|
|
28
|
+
}
|
|
29
|
+
set(prop, value) {
|
|
30
|
+
this.getContainer().set(prop, value);
|
|
25
31
|
}
|
|
26
32
|
}
|
|
27
33
|
exports.Cache = Cache;
|
package/lib/service/index.d.ts
CHANGED
package/lib/service/index.js
CHANGED
|
@@ -14,9 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./service"), exports);
|
|
18
17
|
__exportStar(require("./adapter"), exports);
|
|
19
18
|
__exportStar(require("./api"), exports);
|
|
20
19
|
__exportStar(require("./elements"), exports);
|
|
21
|
-
__exportStar(require("./database"), exports);
|
|
22
20
|
__exportStar(require("./cache"), exports);
|
package/lib/service/service.d.ts
CHANGED
|
@@ -1,20 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
readonly config:
|
|
5
|
-
readonly serviceType: ServiceType;
|
|
6
|
-
readonly service: string;
|
|
7
|
-
handle(...data: unknown[]): void;
|
|
1
|
+
import { Context } from '../context';
|
|
2
|
+
interface ServiceImpl<T extends object = object> {
|
|
3
|
+
readonly identity: string;
|
|
4
|
+
readonly config: T;
|
|
8
5
|
start(): void;
|
|
9
6
|
stop(): void;
|
|
10
7
|
}
|
|
11
|
-
export declare abstract class Service implements ServiceImpl {
|
|
12
|
-
|
|
8
|
+
export declare abstract class Service<T extends object = object> implements ServiceImpl<T> {
|
|
9
|
+
readonly ctx: Context;
|
|
10
|
+
readonly config: T;
|
|
11
|
+
readonly identity: string;
|
|
12
|
+
constructor(ctx: Context, config: T, identity: string);
|
|
13
13
|
start(): void;
|
|
14
14
|
stop(): void;
|
|
15
|
-
readonly serviceType: ServiceType;
|
|
16
|
-
readonly service: string;
|
|
17
|
-
readonly config: object;
|
|
18
|
-
constructor(serviceType: ServiceType, service: string, config?: object);
|
|
19
15
|
}
|
|
20
16
|
export default Service;
|
package/lib/service/service.js
CHANGED
|
@@ -3,8 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Service = void 0;
|
|
4
4
|
const tools_1 = require("@kotori-bot/tools");
|
|
5
5
|
class Service {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
ctx;
|
|
7
|
+
config;
|
|
8
|
+
identity;
|
|
9
|
+
constructor(ctx, config, identity) {
|
|
10
|
+
this.ctx = ctx;
|
|
11
|
+
this.config = config;
|
|
12
|
+
this.identity = identity;
|
|
8
13
|
}
|
|
9
14
|
start() {
|
|
10
15
|
return (0, tools_1.none)(this);
|
|
@@ -12,22 +17,6 @@ class Service {
|
|
|
12
17
|
stop() {
|
|
13
18
|
return (0, tools_1.none)(this);
|
|
14
19
|
}
|
|
15
|
-
serviceType;
|
|
16
|
-
service;
|
|
17
|
-
config;
|
|
18
|
-
constructor(serviceType, service, config = {}) {
|
|
19
|
-
this.serviceType = serviceType;
|
|
20
|
-
this.config = config;
|
|
21
|
-
if (serviceType === 'adapter') {
|
|
22
|
-
this.service = 'adapter';
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
if (serviceType === 'database') {
|
|
26
|
-
this.service = 'database';
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
this.service = service;
|
|
30
|
-
}
|
|
31
20
|
}
|
|
32
21
|
exports.Service = Service;
|
|
33
22
|
exports.default = Service;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Context } from '../context';
|
|
2
|
+
import type { Adapter } from '../service';
|
|
3
|
+
import type { AdapterConfig } from './config';
|
|
4
|
+
declare module '../context/events' {
|
|
5
|
+
interface EventsMapping {
|
|
6
|
+
connect(data: EventDataConnect): void;
|
|
7
|
+
status(data: EventDataStatus): void;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
interface EventDataConnect {
|
|
11
|
+
adapter: Adapter;
|
|
12
|
+
type: 'connect' | 'disconnect';
|
|
13
|
+
normal: boolean;
|
|
14
|
+
mode: 'ws' | 'ws-reverse' | 'other';
|
|
15
|
+
address: string;
|
|
16
|
+
}
|
|
17
|
+
interface EventDataStatus {
|
|
18
|
+
adapter: Adapter;
|
|
19
|
+
status: Adapter['status']['value'];
|
|
20
|
+
}
|
|
21
|
+
export type AdapterClass = new (ctx: Context, config: AdapterConfig, identity: string) => Adapter;
|
|
22
|
+
export {};
|
package/lib/types/config.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { LocaleType } from '@kotori-bot/i18n';
|
|
2
|
-
import { EventDataTargetId } from '
|
|
1
|
+
import type { LocaleType } from '@kotori-bot/i18n';
|
|
2
|
+
import type { EventDataTargetId } from './message';
|
|
3
|
+
import { ModuleConfig } from '../context';
|
|
3
4
|
export interface CoreConfig {
|
|
4
5
|
global: GlobalConfig;
|
|
5
6
|
adapter: {
|
|
@@ -19,6 +20,3 @@ export interface AdapterConfig {
|
|
|
19
20
|
lang: LocaleType;
|
|
20
21
|
'command-prefix': string;
|
|
21
22
|
}
|
|
22
|
-
export interface ModuleConfig {
|
|
23
|
-
filter: object;
|
|
24
|
-
}
|
package/lib/types/index.d.ts
CHANGED
package/lib/types/index.js
CHANGED
|
@@ -14,8 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./core"), exports);
|
|
18
17
|
__exportStar(require("./config"), exports);
|
|
19
|
-
__exportStar(require("./modules"), exports);
|
|
20
18
|
__exportStar(require("./message"), exports);
|
|
21
|
-
__exportStar(require("./
|
|
19
|
+
__exportStar(require("./adapter"), exports);
|
package/lib/types/message.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import type { obj } from '@kotori-bot/tools';
|
|
2
1
|
import Tsu, { TsuError } from 'tsukiko';
|
|
3
2
|
import type I18n from '@kotori-bot/i18n';
|
|
4
|
-
import type { EventsList } from '
|
|
3
|
+
import type { EventsList } from '../context';
|
|
5
4
|
import type CommandError from '../utils/commandError';
|
|
6
5
|
import type { Api, Elements } from '../service';
|
|
7
|
-
|
|
6
|
+
import { Command } from '../utils/command';
|
|
7
|
+
declare module '../context/events' {
|
|
8
8
|
interface EventsMapping {
|
|
9
9
|
midwares(data: EventDataMidwares): void;
|
|
10
10
|
before_parse(data: EventDataBeforeParse): void;
|
|
@@ -30,7 +30,7 @@ export declare const enum CommandAccess {
|
|
|
30
30
|
}
|
|
31
31
|
export type CommandAction = (data: {
|
|
32
32
|
args: CommandArgType[];
|
|
33
|
-
options:
|
|
33
|
+
options: Record<string, CommandArgType>;
|
|
34
34
|
}, session: SessionData) => MessageQuick;
|
|
35
35
|
export type CommandArgType = string | number | boolean;
|
|
36
36
|
export declare const commandArgTypeSignSchema: import("tsukiko").UnionParser<[import("tsukiko").UnionParser<[import("tsukiko").LiteralParser<"string">, import("tsukiko").LiteralParser<"number">]>, import("tsukiko").LiteralParser<"boolean">]>;
|
|
@@ -70,11 +70,13 @@ export interface CommandResult extends CommandParseResult {
|
|
|
70
70
|
error: {
|
|
71
71
|
error: unknown;
|
|
72
72
|
};
|
|
73
|
+
data_error: {
|
|
74
|
+
target: string | number;
|
|
75
|
+
};
|
|
73
76
|
res_error: {
|
|
74
77
|
error: TsuError;
|
|
75
78
|
};
|
|
76
79
|
num_error: null;
|
|
77
|
-
num_choose: null;
|
|
78
80
|
no_access_manger: null;
|
|
79
81
|
no_access_admin: null;
|
|
80
82
|
disable: null;
|
|
@@ -83,11 +85,11 @@ export interface CommandResult extends CommandParseResult {
|
|
|
83
85
|
};
|
|
84
86
|
no_exists: CommandResult['exists'];
|
|
85
87
|
}
|
|
86
|
-
type CommandResultNoArgs = 'num_error' | '
|
|
88
|
+
type CommandResultNoArgs = 'num_error' | 'no_access_manger' | 'no_access_admin' | 'disable';
|
|
87
89
|
export type CommandResultExtra = {
|
|
88
90
|
[K in keyof CommandResult]: {
|
|
89
91
|
type: K;
|
|
90
|
-
} & (K extends CommandResultNoArgs ?
|
|
92
|
+
} & (K extends CommandResultNoArgs ? object : CommandResult[K]);
|
|
91
93
|
};
|
|
92
94
|
export type SessionData = EventsList['on_message'];
|
|
93
95
|
export declare enum MessageScope {
|
|
@@ -95,7 +97,7 @@ export declare enum MessageScope {
|
|
|
95
97
|
GROUP = 1
|
|
96
98
|
}
|
|
97
99
|
export type MessageRaw = string;
|
|
98
|
-
export type MessageQuickReal = MessageRaw | [string,
|
|
100
|
+
export type MessageQuickReal = MessageRaw | [string, Record<string, CommandArgType> | CommandArgType[]] | CommandError | void;
|
|
99
101
|
export type MessageQuick = MessageQuickReal | Promise<MessageQuickReal>;
|
|
100
102
|
export type MidwareCallback = (next: () => void, session: SessionData) => MessageQuick;
|
|
101
103
|
export type RegexpCallback = (match: RegExpMatchArray, session: SessionData) => MessageQuick;
|
|
@@ -107,26 +109,24 @@ interface EventDataMidwares {
|
|
|
107
109
|
}
|
|
108
110
|
interface EventDataBeforeParse {
|
|
109
111
|
session: SessionData;
|
|
110
|
-
|
|
112
|
+
raw: string;
|
|
111
113
|
}
|
|
112
114
|
interface EventDataParse {
|
|
113
115
|
session: SessionData;
|
|
114
|
-
command:
|
|
116
|
+
command: Command;
|
|
117
|
+
raw: string;
|
|
115
118
|
result: CommandError | Parameters<CommandAction>[0];
|
|
116
119
|
cancel(): void;
|
|
117
120
|
}
|
|
118
121
|
interface EventDataBeforeCommand {
|
|
119
122
|
session: SessionData;
|
|
120
|
-
|
|
121
|
-
scope: MessageScope;
|
|
122
|
-
access: CommandAccess;
|
|
123
|
+
raw: string;
|
|
123
124
|
cancel(): void;
|
|
124
125
|
}
|
|
125
126
|
interface EventDataCommand {
|
|
126
127
|
session: SessionData;
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
access: CommandAccess;
|
|
128
|
+
raw: string;
|
|
129
|
+
command: Command;
|
|
130
130
|
result: EventDataParse['result'] | MessageQuick;
|
|
131
131
|
}
|
|
132
132
|
interface EventDataBeforeSend {
|
|
@@ -154,7 +154,13 @@ export interface EventDataApiBase {
|
|
|
154
154
|
operatorId?: EventDataTargetId;
|
|
155
155
|
i18n: I18n;
|
|
156
156
|
send(message: MessageRaw): void;
|
|
157
|
+
format(template: string, data: Record<string, unknown> | CommandArgType[]): string;
|
|
157
158
|
quick(message: MessageQuick): void;
|
|
159
|
+
prompt(message?: MessageRaw): Promise<MessageRaw>;
|
|
160
|
+
confirm(options?: {
|
|
161
|
+
message: MessageRaw;
|
|
162
|
+
sure: MessageRaw;
|
|
163
|
+
}): Promise<boolean>;
|
|
158
164
|
error<T extends Exclude<keyof CommandResult, CommandResultNoArgs>>(type: T, data: CommandResult[T] extends object ? CommandResult[T] : never): CommandError;
|
|
159
165
|
error<T extends CommandResultNoArgs>(type: T): CommandError;
|
|
160
166
|
extra?: unknown;
|
|
@@ -169,7 +175,11 @@ interface EventDataGroupMsg extends EventDataApiBase {
|
|
|
169
175
|
type: MessageScope.GROUP;
|
|
170
176
|
messageId: EventDataTargetId;
|
|
171
177
|
message: MessageRaw;
|
|
172
|
-
sender: SessionDataSender
|
|
178
|
+
sender: SessionDataSender & {
|
|
179
|
+
level: string;
|
|
180
|
+
role: 'owner' | 'admin' | 'member';
|
|
181
|
+
title: string;
|
|
182
|
+
};
|
|
173
183
|
groupId: EventDataTargetId;
|
|
174
184
|
}
|
|
175
185
|
interface EventDataPrivateRecall extends EventDataApiBase {
|
|
@@ -211,8 +221,8 @@ interface EventDataGroupAdmin extends EventDataApiBase {
|
|
|
211
221
|
}
|
|
212
222
|
interface EventDataGroupBan extends EventDataApiBase {
|
|
213
223
|
userId: EventDataTargetId | 0;
|
|
214
|
-
operatorId
|
|
215
|
-
time
|
|
224
|
+
operatorId: EventDataTargetId;
|
|
225
|
+
time: number | -1;
|
|
216
226
|
groupId: EventDataTargetId;
|
|
217
227
|
}
|
|
218
228
|
export {};
|
package/lib/types/service.js
CHANGED
package/lib/utils/command.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { obj } from '@kotori-bot/tools';
|
|
2
1
|
import { CommandAccess, type CommandAction, type CommandArgType, type CommandArgTypeSign, type CommandConfig } from '../types';
|
|
3
2
|
import { CommandError } from './commandError';
|
|
4
3
|
interface CommandArg {
|
|
@@ -28,7 +27,7 @@ interface CommandData {
|
|
|
28
27
|
export declare class Command {
|
|
29
28
|
static run(input: string, data: CommandData): CommandError | {
|
|
30
29
|
args: CommandArgType[];
|
|
31
|
-
options:
|
|
30
|
+
options: Record<string, CommandArgType>;
|
|
32
31
|
};
|
|
33
32
|
private template;
|
|
34
33
|
readonly meta: CommandData;
|
package/lib/utils/command.js
CHANGED
|
@@ -40,7 +40,11 @@ class Command {
|
|
|
40
40
|
if (!starts)
|
|
41
41
|
return new commandError_1.CommandError({ type: 'unknown', input });
|
|
42
42
|
/* handle CommandOption[] to minimist.opts */
|
|
43
|
-
const opts = {
|
|
43
|
+
const opts = {
|
|
44
|
+
string: [],
|
|
45
|
+
boolean: [],
|
|
46
|
+
alias: {}
|
|
47
|
+
};
|
|
44
48
|
data.options.forEach((option) => {
|
|
45
49
|
if (option.type === 'string') {
|
|
46
50
|
opts.string.push(option.realname);
|
package/lib/utils/errror.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
type KotoriErrorType = '
|
|
2
|
+
type KotoriErrorType = 'ServiceError' | 'ModuleError' | 'UnknownError' | 'DevError';
|
|
3
3
|
interface KotoriErrorImpl {
|
|
4
4
|
readonly name: KotoriErrorType;
|
|
5
5
|
readonly extend: () => typeof KotoriError;
|
package/lib/utils/factory.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.cancelFactory = exports.disposeFactory = void 0;
|
|
4
4
|
function disposeFactory(ctx, dispose) {
|
|
5
5
|
ctx.once('dispose_module', (data) => {
|
|
6
|
-
if ((typeof data.
|
|
6
|
+
if ((typeof data.instance === 'object' ? data.instance.name : data.instance) !== ctx.identity) {
|
|
7
7
|
disposeFactory(ctx, dispose);
|
|
8
8
|
return;
|
|
9
9
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kotori-bot/core",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Kotori Core",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"license": "GPL-3.0",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"@types/minimist": "^1.2.5",
|
|
28
28
|
"minimist": "^1.2.8",
|
|
29
29
|
"tsukiko": "^1.2.1",
|
|
30
|
-
"@kotori-bot/
|
|
31
|
-
"@kotori-bot/
|
|
30
|
+
"@kotori-bot/tools": "^1.2.1",
|
|
31
|
+
"@kotori-bot/i18n": "^1.2.1"
|
|
32
32
|
}
|
|
33
33
|
}
|
package/lib/service/database.js
DELETED
|
@@ -1,14 +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.Database = void 0;
|
|
7
|
-
const service_1 = __importDefault(require("./service"));
|
|
8
|
-
class Database extends service_1.default {
|
|
9
|
-
constructor() {
|
|
10
|
-
super('database', '');
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
exports.Database = Database;
|
|
14
|
-
exports.default = Database;
|