@kotori-bot/core 1.5.1 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -4
- package/lib/app/config.d.ts +32 -0
- package/lib/app/config.js +65 -0
- package/lib/app/core.d.ts +138 -0
- package/lib/{components → app}/core.js +24 -10
- package/lib/app/index.d.ts +1 -0
- package/lib/{service → app}/index.js +7 -13
- package/lib/{components → app}/message.d.ts +11 -10
- package/lib/app/message.js +253 -0
- package/lib/components/adapter.d.ts +122 -0
- package/lib/components/adapter.js +75 -0
- package/lib/components/api.d.ts +417 -0
- package/lib/components/api.js +546 -0
- package/lib/components/cache.d.ts +37 -0
- package/lib/{service → components}/cache.js +27 -6
- package/lib/components/command.d.ts +153 -0
- package/lib/{utils → components}/command.js +154 -48
- package/lib/components/elements.d.ts +144 -0
- package/lib/components/elements.js +179 -0
- package/lib/components/filter.d.ts +22 -0
- package/lib/components/filter.js +130 -0
- package/lib/components/index.d.ts +8 -2
- package/lib/components/index.js +19 -7
- package/lib/components/messages.d.ts +186 -0
- package/lib/components/messages.js +218 -0
- package/lib/components/session.d.ts +177 -0
- package/lib/components/session.js +275 -0
- package/lib/decorators/index.d.ts +7 -0
- package/lib/{components/config.js → decorators/index.js} +23 -39
- package/lib/decorators/plugin.d.ts +7 -0
- package/lib/{utils/commandError.js → decorators/plugin.js} +16 -16
- package/lib/decorators/utils.d.ts +59 -0
- package/lib/decorators/utils.js +189 -0
- package/lib/global/constants.d.ts +1 -8
- package/lib/global/constants.js +6 -25
- package/lib/global/index.js +3 -3
- package/lib/global/symbols.d.ts +13 -8
- package/lib/global/symbols.js +18 -12
- package/lib/index.d.ts +4 -4
- package/lib/index.js +10 -11
- package/lib/types/adapter.d.ts +1 -1
- package/lib/types/adapter.js +3 -3
- package/lib/types/api.d.ts +72 -0
- package/lib/{utils/jsxFactory.js → types/api.js} +5 -5
- package/lib/types/command.d.ts +78 -0
- package/lib/types/command.js +50 -0
- package/lib/types/config.d.ts +4 -6
- package/lib/types/config.js +3 -3
- package/lib/types/filter.d.ts +51 -0
- package/lib/types/filter.js +87 -0
- package/lib/types/index.d.ts +4 -0
- package/lib/types/index.js +12 -4
- package/lib/types/message.d.ts +128 -193
- package/lib/types/message.js +12 -31
- package/lib/types/session.d.ts +349 -0
- package/lib/types/session.js +27 -0
- package/lib/utils/container.d.ts +6 -6
- package/lib/utils/container.js +12 -16
- package/lib/utils/error.d.ts +46 -22
- package/lib/utils/error.js +38 -21
- package/lib/utils/factory.d.ts +10 -16
- package/lib/utils/factory.js +41 -101
- package/lib/utils/internal.d.ts +46 -0
- package/lib/utils/internal.js +102 -0
- package/package.json +10 -7
- package/lib/components/config.d.ts +0 -16
- package/lib/components/core.d.ts +0 -34
- package/lib/components/message.js +0 -195
- package/lib/service/adapter.d.ts +0 -41
- package/lib/service/adapter.js +0 -131
- package/lib/service/api.d.ts +0 -32
- package/lib/service/api.js +0 -80
- package/lib/service/cache.d.ts +0 -13
- package/lib/service/elements.d.ts +0 -12
- package/lib/service/elements.js +0 -73
- package/lib/service/index.d.ts +0 -4
- package/lib/utils/command.d.ts +0 -51
- package/lib/utils/commandError.d.ts +0 -7
- package/lib/utils/jsxFactory.d.ts +0 -6
package/README.md
CHANGED
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
# @kotori-bot/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
It's Kotori Bot's core library, and it only used ecmascript standard (less than es2021), so you can use it in any environment that supports ecmascript standard (Such as node.js, browser, web worker, quick.js and more...).
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
4
7
|
- Symbols
|
|
5
|
-
- Service
|
|
6
|
-
- Modules
|
|
7
8
|
- Core
|
|
8
9
|
- Config
|
|
9
10
|
- Message
|
|
11
|
+
- Command
|
|
12
|
+
- Session
|
|
13
|
+
- Messages
|
|
14
|
+
- Decorators
|
|
10
15
|
- Adapter
|
|
11
16
|
- Api
|
|
12
17
|
- Elements
|
|
13
|
-
-
|
|
18
|
+
- i18n (from `@kotori-bot/i18n`)
|
|
19
|
+
- http (from `@kotori-bot/tools`)
|
|
14
20
|
|
|
15
21
|
## Reference
|
|
16
22
|
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { CoreConfig } from '../types';
|
|
2
|
+
/** Meta information. */
|
|
3
|
+
interface MetaInfo {
|
|
4
|
+
/** Program name */
|
|
5
|
+
name: string;
|
|
6
|
+
/** Program core version */
|
|
7
|
+
coreVersion: string;
|
|
8
|
+
/** Program loader version if exists */
|
|
9
|
+
loaderVersion?: string;
|
|
10
|
+
/** Program version if exists */
|
|
11
|
+
version?: string;
|
|
12
|
+
/** Program description */
|
|
13
|
+
description: string;
|
|
14
|
+
/** Program entry file */
|
|
15
|
+
main: string;
|
|
16
|
+
/**
|
|
17
|
+
* Program license
|
|
18
|
+
*
|
|
19
|
+
* @constant
|
|
20
|
+
*/
|
|
21
|
+
license: 'GPL-3.0';
|
|
22
|
+
/** Program author */
|
|
23
|
+
author: string;
|
|
24
|
+
}
|
|
25
|
+
export declare class Config {
|
|
26
|
+
readonly config: CoreConfig;
|
|
27
|
+
readonly meta: MetaInfo;
|
|
28
|
+
constructor(config?: Omit<Partial<CoreConfig>, 'global'> & {
|
|
29
|
+
global?: Partial<CoreConfig['global']>;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
export default Config;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* @Package @kotori-bot/core
|
|
4
|
+
* @Version 1.6.0-rc.1
|
|
5
|
+
* @Author Arimura Sena <me@hotaru.icu>
|
|
6
|
+
* @Copyright 2024 Hotaru. All rights reserved.
|
|
7
|
+
* @License GPL-3.0
|
|
8
|
+
* @Link https://github.com/kotorijs/kotori
|
|
9
|
+
* @Date 2024/8/9 17:33:05
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
"use strict";
|
|
13
|
+
var __defProp = Object.defineProperty;
|
|
14
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
15
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
16
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
17
|
+
var __export = (target, all) => {
|
|
18
|
+
for (var name in all)
|
|
19
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
20
|
+
};
|
|
21
|
+
var __copyProps = (to, from, except, desc) => {
|
|
22
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
23
|
+
for (let key of __getOwnPropNames(from))
|
|
24
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
25
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
26
|
+
}
|
|
27
|
+
return to;
|
|
28
|
+
};
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
var config_exports = {};
|
|
31
|
+
__export(config_exports, {
|
|
32
|
+
Config: () => Config,
|
|
33
|
+
default: () => config_default
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(config_exports);
|
|
36
|
+
var import_global = require("../global");
|
|
37
|
+
class Config {
|
|
38
|
+
config;
|
|
39
|
+
meta;
|
|
40
|
+
constructor(config = {}) {
|
|
41
|
+
this.config = { ...import_global.DEFAULT_CORE_CONFIG, ...config };
|
|
42
|
+
this.config.global = { ...import_global.DEFAULT_CORE_CONFIG.global, ...this.config.global };
|
|
43
|
+
const pkg = typeof require === "function" ? require("../../package.json") : {
|
|
44
|
+
name: "@kotori-bot/core",
|
|
45
|
+
version: "BROWSER",
|
|
46
|
+
description: "",
|
|
47
|
+
author: "Arimura Sena <me@hotaru.icu>",
|
|
48
|
+
main: "lib/index.js"
|
|
49
|
+
};
|
|
50
|
+
this.meta = {
|
|
51
|
+
name: pkg.name,
|
|
52
|
+
description: pkg.description,
|
|
53
|
+
main: pkg.main,
|
|
54
|
+
license: "GPL-3.0",
|
|
55
|
+
author: pkg.author,
|
|
56
|
+
coreVersion: pkg.version
|
|
57
|
+
};
|
|
58
|
+
globalThis.kotori = this.meta;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
var config_default = Config;
|
|
62
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
63
|
+
0 && (module.exports = {
|
|
64
|
+
Config
|
|
65
|
+
});
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { Http } from '@kotori-bot/tools';
|
|
2
|
+
import I18n from '@kotori-bot/i18n';
|
|
3
|
+
import type { Parser } from 'tsukiko';
|
|
4
|
+
import Context from 'fluoro';
|
|
5
|
+
import Config from './config';
|
|
6
|
+
import Message from './message';
|
|
7
|
+
import type { AdapterClass } from '../types';
|
|
8
|
+
import { Cache, type Api } from '../components';
|
|
9
|
+
import { Symbols } from '../global';
|
|
10
|
+
declare module 'fluoro' {
|
|
11
|
+
interface Context {
|
|
12
|
+
/**
|
|
13
|
+
* Adapter constructors list.
|
|
14
|
+
*
|
|
15
|
+
* @readonly
|
|
16
|
+
*/
|
|
17
|
+
readonly [Symbols.adapter]: Core[typeof Symbols.adapter];
|
|
18
|
+
/**
|
|
19
|
+
* Bot instances list.
|
|
20
|
+
*
|
|
21
|
+
* @readonly
|
|
22
|
+
*/
|
|
23
|
+
readonly [Symbols.bot]: Core[typeof Symbols.bot];
|
|
24
|
+
/**
|
|
25
|
+
* Core config.
|
|
26
|
+
*
|
|
27
|
+
* @readonly
|
|
28
|
+
*/
|
|
29
|
+
readonly config: Config['config'];
|
|
30
|
+
/**
|
|
31
|
+
* Meta information.
|
|
32
|
+
*
|
|
33
|
+
* @readonly
|
|
34
|
+
*/
|
|
35
|
+
readonly meta: Config['meta'];
|
|
36
|
+
/**
|
|
37
|
+
* Registered middlewares list.
|
|
38
|
+
*
|
|
39
|
+
* @readonly
|
|
40
|
+
*/
|
|
41
|
+
readonly [Symbols.midware]: Message[typeof Symbols.midware];
|
|
42
|
+
/**
|
|
43
|
+
* Registered commands list.
|
|
44
|
+
*
|
|
45
|
+
* @readonly
|
|
46
|
+
*/
|
|
47
|
+
readonly [Symbols.command]: Message[typeof Symbols.command];
|
|
48
|
+
/**
|
|
49
|
+
* Registered regexps list.
|
|
50
|
+
*
|
|
51
|
+
* @readonly
|
|
52
|
+
*/
|
|
53
|
+
readonly [Symbols.regexp]: Message[typeof Symbols.regexp];
|
|
54
|
+
/**
|
|
55
|
+
* Registered scheduled tasks list.
|
|
56
|
+
*
|
|
57
|
+
* @readonly
|
|
58
|
+
*/
|
|
59
|
+
readonly [Symbols.task]: Message[typeof Symbols.task];
|
|
60
|
+
/**
|
|
61
|
+
* Registered session filters list.
|
|
62
|
+
*
|
|
63
|
+
* @readonly
|
|
64
|
+
*/
|
|
65
|
+
readonly [Symbols.filter]: Message[typeof Symbols.filter];
|
|
66
|
+
/**
|
|
67
|
+
* Session promises in progress list.
|
|
68
|
+
*
|
|
69
|
+
* @readonly
|
|
70
|
+
*/
|
|
71
|
+
readonly [Symbols.promise]: Message[typeof Symbols.promise];
|
|
72
|
+
/**
|
|
73
|
+
* Register a message handled middleware.
|
|
74
|
+
*
|
|
75
|
+
* @param callback - Middleware callback
|
|
76
|
+
* @param priority - Middleware priority, default is 100
|
|
77
|
+
* @returns dispose function
|
|
78
|
+
*/
|
|
79
|
+
midware: Message['midware'];
|
|
80
|
+
/**
|
|
81
|
+
* Register a command.
|
|
82
|
+
*
|
|
83
|
+
* @param template - Command template
|
|
84
|
+
* @param config - Command config, optional
|
|
85
|
+
* @returns Command instance
|
|
86
|
+
*/
|
|
87
|
+
command: Message['command'];
|
|
88
|
+
/**
|
|
89
|
+
* Register a regexp.
|
|
90
|
+
*
|
|
91
|
+
* @param match - Regexp to match
|
|
92
|
+
* @param callback - Regexp callback
|
|
93
|
+
* @returns dispose function
|
|
94
|
+
*/
|
|
95
|
+
regexp: Message['regexp'];
|
|
96
|
+
/**
|
|
97
|
+
* Register a scheduled task.
|
|
98
|
+
*
|
|
99
|
+
* @param options - Task options
|
|
100
|
+
* @param callback - Task callback
|
|
101
|
+
* @returns dispose function
|
|
102
|
+
*/
|
|
103
|
+
task: Message['task'];
|
|
104
|
+
/**
|
|
105
|
+
* Register a session filter.
|
|
106
|
+
*
|
|
107
|
+
* @param option - Filter option
|
|
108
|
+
* @returns new context
|
|
109
|
+
*/
|
|
110
|
+
filter: Message['filter'];
|
|
111
|
+
/**
|
|
112
|
+
* Send a notified message to the master of first bot instance at config.
|
|
113
|
+
*
|
|
114
|
+
* @experimental
|
|
115
|
+
*/
|
|
116
|
+
notify: Message['notify'];
|
|
117
|
+
/**
|
|
118
|
+
* Send a message to all sessions on all bots.
|
|
119
|
+
*
|
|
120
|
+
* @experimental
|
|
121
|
+
*/
|
|
122
|
+
boardcast: Message['boardcast'];
|
|
123
|
+
/** Http request methods */
|
|
124
|
+
http: Http;
|
|
125
|
+
/** International methods */
|
|
126
|
+
i18n: I18n;
|
|
127
|
+
/** Cache service */
|
|
128
|
+
cache: Cache;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
export declare class Core extends Context {
|
|
132
|
+
readonly [Symbols.adapter]: Map<string, [AdapterClass, Parser<unknown>?]>;
|
|
133
|
+
readonly [Symbols.bot]: Map<string, Set<Api>>;
|
|
134
|
+
constructor(config?: ConstructorParameters<typeof Config>[0]);
|
|
135
|
+
start(): void;
|
|
136
|
+
stop(): void;
|
|
137
|
+
}
|
|
138
|
+
export default Core;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
|
|
2
2
|
/**
|
|
3
3
|
* @Package @kotori-bot/core
|
|
4
|
-
* @Version 1.
|
|
5
|
-
* @Author
|
|
4
|
+
* @Version 1.6.0-rc.1
|
|
5
|
+
* @Author Arimura Sena <me@hotaru.icu>
|
|
6
6
|
* @Copyright 2024 Hotaru. All rights reserved.
|
|
7
7
|
* @License GPL-3.0
|
|
8
8
|
* @Link https://github.com/kotorijs/kotori
|
|
9
|
-
* @Date 2024/
|
|
9
|
+
* @Date 2024/8/9 17:33:05
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
"use strict";
|
|
@@ -45,25 +45,39 @@ __export(core_exports, {
|
|
|
45
45
|
module.exports = __toCommonJS(core_exports);
|
|
46
46
|
var import_tools = require("@kotori-bot/tools");
|
|
47
47
|
var import_i18n = __toESM(require("@kotori-bot/i18n"));
|
|
48
|
-
var import_fluoro = require("fluoro");
|
|
48
|
+
var import_fluoro = __toESM(require("fluoro"));
|
|
49
49
|
var import_config = __toESM(require("./config"));
|
|
50
50
|
var import_message = __toESM(require("./message"));
|
|
51
|
-
var
|
|
51
|
+
var import_components = require("../components");
|
|
52
52
|
var import_global = require("../global");
|
|
53
|
-
class Core extends import_fluoro.
|
|
53
|
+
class Core extends import_fluoro.default {
|
|
54
54
|
[import_global.Symbols.adapter] = /* @__PURE__ */ new Map();
|
|
55
55
|
[import_global.Symbols.bot] = /* @__PURE__ */ new Map();
|
|
56
56
|
constructor(config) {
|
|
57
57
|
super();
|
|
58
58
|
this.provide("config", new import_config.default(config));
|
|
59
|
-
this.mixin("config", ["config", "
|
|
59
|
+
this.mixin("config", ["config", "meta"]);
|
|
60
60
|
this.provide("message", new import_message.default(this));
|
|
61
|
-
this.mixin("message", [
|
|
62
|
-
|
|
61
|
+
this.mixin("message", [
|
|
62
|
+
"midware",
|
|
63
|
+
"command",
|
|
64
|
+
"regexp",
|
|
65
|
+
"notify",
|
|
66
|
+
"boardcast",
|
|
67
|
+
"task",
|
|
68
|
+
"filter",
|
|
69
|
+
import_global.Symbols.command,
|
|
70
|
+
import_global.Symbols.midware,
|
|
71
|
+
import_global.Symbols.task,
|
|
72
|
+
import_global.Symbols.filter,
|
|
73
|
+
import_global.Symbols.promise,
|
|
74
|
+
import_global.Symbols.regexp
|
|
75
|
+
]);
|
|
76
|
+
this.provide("http", new import_tools.Http());
|
|
63
77
|
this.inject("http");
|
|
64
78
|
this.provide("i18n", new import_i18n.default({ lang: this.config.global.lang }));
|
|
65
79
|
this.inject("i18n");
|
|
66
|
-
this.service("cache", new
|
|
80
|
+
this.service("cache", new import_components.Cache(this.extends("cache")));
|
|
67
81
|
}
|
|
68
82
|
}
|
|
69
83
|
var core_default = Core;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './core';
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
|
|
2
2
|
/**
|
|
3
3
|
* @Package @kotori-bot/core
|
|
4
|
-
* @Version 1.
|
|
5
|
-
* @Author
|
|
4
|
+
* @Version 1.6.0-rc.1
|
|
5
|
+
* @Author Arimura Sena <me@hotaru.icu>
|
|
6
6
|
* @Copyright 2024 Hotaru. All rights reserved.
|
|
7
7
|
* @License GPL-3.0
|
|
8
8
|
* @Link https://github.com/kotorijs/kotori
|
|
9
|
-
* @Date 2024/
|
|
9
|
+
* @Date 2024/8/9 17:33:05
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
"use strict";
|
|
@@ -24,16 +24,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
24
24
|
};
|
|
25
25
|
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
26
26
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
|
-
var
|
|
28
|
-
module.exports = __toCommonJS(
|
|
29
|
-
__reExport(
|
|
30
|
-
__reExport(service_exports, require("./api"), module.exports);
|
|
31
|
-
__reExport(service_exports, require("./elements"), module.exports);
|
|
32
|
-
__reExport(service_exports, require("./cache"), module.exports);
|
|
27
|
+
var app_exports = {};
|
|
28
|
+
module.exports = __toCommonJS(app_exports);
|
|
29
|
+
__reExport(app_exports, require("./core"), module.exports);
|
|
33
30
|
// Annotate the CommonJS export names for ESM import in node:
|
|
34
31
|
0 && (module.exports = {
|
|
35
|
-
...require("./
|
|
36
|
-
...require("./api"),
|
|
37
|
-
...require("./elements"),
|
|
38
|
-
...require("./cache")
|
|
32
|
+
...require("./core")
|
|
39
33
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Context } from 'fluoro';
|
|
2
2
|
import { CronJob } from 'cron';
|
|
3
|
-
import { type CommandConfig, type MidwareCallback, type RegexpCallback,
|
|
4
|
-
import { Command } from '../
|
|
3
|
+
import { type CommandConfig, type MidwareCallback, type RegexpCallback, type TaskCallback, type FilterOption, type TaskOptions } from '../types';
|
|
4
|
+
import { Filter, Command, type SessionMsg } from '../components';
|
|
5
5
|
import { Symbols } from '../global';
|
|
6
6
|
interface MidwareData {
|
|
7
7
|
callback: MidwareCallback;
|
|
@@ -15,19 +15,20 @@ export declare class Message {
|
|
|
15
15
|
readonly [Symbols.midware]: Set<MidwareData>;
|
|
16
16
|
readonly [Symbols.command]: Set<Command>;
|
|
17
17
|
readonly [Symbols.regexp]: Set<RegexpData>;
|
|
18
|
+
readonly [Symbols.task]: Set<CronJob>;
|
|
19
|
+
readonly [Symbols.filter]: Map<string, Filter>;
|
|
20
|
+
readonly [Symbols.promise]: Map<string, ((value: SessionMsg['message']) => void)[]>;
|
|
18
21
|
private handleMidware;
|
|
19
22
|
private handleRegexp;
|
|
20
23
|
private handleCommand;
|
|
21
24
|
private readonly ctx;
|
|
22
25
|
constructor(ctx: Context);
|
|
23
26
|
midware(callback: MidwareCallback, priority?: number): () => boolean;
|
|
24
|
-
command<T extends string>(template: T, config?: CommandConfig): Command<T, {}>;
|
|
27
|
+
command<T extends string>(template: T, config?: CommandConfig): Command<T, {}, "all">;
|
|
25
28
|
regexp(match: RegExp, callback: RegexpCallback): () => boolean;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
timeZone?: string;
|
|
31
|
-
}, callback: TaskCallback): CronJob<null, null>;
|
|
29
|
+
boardcast(): void;
|
|
30
|
+
notify(): void;
|
|
31
|
+
task(options: TaskOptions, callback: TaskCallback): () => boolean;
|
|
32
|
+
filter(option: FilterOption): Context;
|
|
32
33
|
}
|
|
33
34
|
export default Message;
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* @Package @kotori-bot/core
|
|
4
|
+
* @Version 1.6.0-rc.1
|
|
5
|
+
* @Author Arimura Sena <me@hotaru.icu>
|
|
6
|
+
* @Copyright 2024 Hotaru. All rights reserved.
|
|
7
|
+
* @License GPL-3.0
|
|
8
|
+
* @Link https://github.com/kotorijs/kotori
|
|
9
|
+
* @Date 2024/8/9 17:33:05
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
"use strict";
|
|
13
|
+
var __create = Object.create;
|
|
14
|
+
var __defProp = Object.defineProperty;
|
|
15
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
16
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
17
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
18
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
19
|
+
var __export = (target, all) => {
|
|
20
|
+
for (var name in all)
|
|
21
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
22
|
+
};
|
|
23
|
+
var __copyProps = (to, from, except, desc) => {
|
|
24
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
25
|
+
for (let key of __getOwnPropNames(from))
|
|
26
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
27
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
28
|
+
}
|
|
29
|
+
return to;
|
|
30
|
+
};
|
|
31
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
32
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
33
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
34
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
35
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
36
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
37
|
+
mod
|
|
38
|
+
));
|
|
39
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
40
|
+
var message_exports = {};
|
|
41
|
+
__export(message_exports, {
|
|
42
|
+
Message: () => Message,
|
|
43
|
+
default: () => message_default
|
|
44
|
+
});
|
|
45
|
+
module.exports = __toCommonJS(message_exports);
|
|
46
|
+
var import_cron = require("cron");
|
|
47
|
+
var import_types = require("../types");
|
|
48
|
+
var import_internal = require("../utils/internal");
|
|
49
|
+
var import_components = require("../components");
|
|
50
|
+
var import_error = require("../utils/error");
|
|
51
|
+
var import_global = require("../global");
|
|
52
|
+
var import_node_crypto = require("node:crypto");
|
|
53
|
+
var import_utils = __toESM(require("../decorators/utils"));
|
|
54
|
+
class Message {
|
|
55
|
+
[import_global.Symbols.midware] = /* @__PURE__ */ new Set();
|
|
56
|
+
[import_global.Symbols.command] = /* @__PURE__ */ new Set();
|
|
57
|
+
[import_global.Symbols.regexp] = /* @__PURE__ */ new Set();
|
|
58
|
+
[import_global.Symbols.task] = /* @__PURE__ */ new Set();
|
|
59
|
+
[import_global.Symbols.filter] = /* @__PURE__ */ new Map();
|
|
60
|
+
[import_global.Symbols.promise] = /* @__PURE__ */ new Map();
|
|
61
|
+
handleMidware(session) {
|
|
62
|
+
const { api } = session;
|
|
63
|
+
api.adapter.status.receivedMsg += 1;
|
|
64
|
+
const setup = Array.from(this[import_global.Symbols.midware].values()).sort((first, last) => first.priority - last.priority).map(({ callback }) => callback).reverse().reduce(
|
|
65
|
+
(first, last) => {
|
|
66
|
+
const next = () => {
|
|
67
|
+
first(() => {
|
|
68
|
+
}, session);
|
|
69
|
+
};
|
|
70
|
+
return () => {
|
|
71
|
+
last(next, session);
|
|
72
|
+
};
|
|
73
|
+
},
|
|
74
|
+
(_, session2) => {
|
|
75
|
+
this.handleCommand(session2);
|
|
76
|
+
this.handleRegexp(session2);
|
|
77
|
+
}
|
|
78
|
+
);
|
|
79
|
+
setup(() => {
|
|
80
|
+
}, session);
|
|
81
|
+
}
|
|
82
|
+
async handleRegexp(session) {
|
|
83
|
+
for (const data of this[import_global.Symbols.regexp]) {
|
|
84
|
+
const result = session.message.match(data.match);
|
|
85
|
+
if (!result) continue;
|
|
86
|
+
const cancel = (0, import_internal.cancelFactory)();
|
|
87
|
+
this.ctx.emit("before_regexp", {
|
|
88
|
+
session,
|
|
89
|
+
regexp: data.match,
|
|
90
|
+
raw: session.message.toString(),
|
|
91
|
+
cancel: cancel.get()
|
|
92
|
+
});
|
|
93
|
+
if (cancel.value) continue;
|
|
94
|
+
session.quick(data.callback(result, session));
|
|
95
|
+
this.ctx.emit("regexp", { result, session, regexp: data.match, raw: session.message.toString() });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async handleCommand(session) {
|
|
99
|
+
const prefix = session.api.adapter.config.commandPrefix ?? this.ctx.config.global.commandPrefix;
|
|
100
|
+
for (const cmd of this[import_global.Symbols.command]) {
|
|
101
|
+
for (const shortcut of cmd.meta.shortcut) {
|
|
102
|
+
if (!session.message.startsWith(shortcut)) continue;
|
|
103
|
+
session.message = session.message.replace(shortcut, `${prefix}${cmd.meta.root}`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (!session.message.startsWith(prefix)) return;
|
|
107
|
+
const raw = session.message.slice(prefix.length);
|
|
108
|
+
if (!raw) return;
|
|
109
|
+
let matched;
|
|
110
|
+
for (const cmd of this[import_global.Symbols.command]) {
|
|
111
|
+
if (matched || !cmd.meta.action) continue;
|
|
112
|
+
const result2 = import_components.Command.run(raw, cmd.meta);
|
|
113
|
+
if (result2 instanceof import_error.CommandError && result2.value.type === "unknown") continue;
|
|
114
|
+
matched = cmd;
|
|
115
|
+
const cancel = (0, import_internal.cancelFactory)();
|
|
116
|
+
this.ctx.emit("before_command", { command: cmd, result: result2, raw, session, cancel: cancel.get() });
|
|
117
|
+
if (cancel.value) continue;
|
|
118
|
+
if (result2 instanceof import_error.CommandError) continue;
|
|
119
|
+
try {
|
|
120
|
+
const executed = await cmd.meta.action({ args: result2.args, options: result2.options }, session);
|
|
121
|
+
if (executed instanceof import_error.CommandError) {
|
|
122
|
+
this.ctx.emit("command", { command: cmd, result: executed, raw, session });
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (executed !== void 0) session.quick(executed);
|
|
126
|
+
this.ctx.emit("command", {
|
|
127
|
+
command: cmd,
|
|
128
|
+
result: executed instanceof import_error.CommandError ? result2 : executed,
|
|
129
|
+
raw,
|
|
130
|
+
session
|
|
131
|
+
});
|
|
132
|
+
} catch (error) {
|
|
133
|
+
this.ctx.emit("command", { command: matched, result: new import_error.CommandError({ type: "error", error }), raw, session });
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (matched) return;
|
|
137
|
+
const result = new import_error.CommandError({ type: "unknown", input: raw });
|
|
138
|
+
this.ctx.emit("command", { command: new import_components.Command(""), result, raw, session });
|
|
139
|
+
}
|
|
140
|
+
ctx;
|
|
141
|
+
constructor(ctx) {
|
|
142
|
+
this.ctx = ctx;
|
|
143
|
+
this.ctx.on("ready", () => {
|
|
144
|
+
for (const bots of this.ctx[import_global.Symbols.bot].values()) {
|
|
145
|
+
for (const bot of bots) {
|
|
146
|
+
for (const key of ["sendPrivateMsg", "sendGroupMsg", "sendChannelMsg"]) {
|
|
147
|
+
;
|
|
148
|
+
bot[key] = new Proxy(bot[key], {
|
|
149
|
+
apply: (target, thisArg, argArray) => {
|
|
150
|
+
const [message, id1, id2] = argArray;
|
|
151
|
+
const cancel = (0, import_internal.cancelFactory)();
|
|
152
|
+
this.ctx.emit("before_send", {
|
|
153
|
+
api: bot,
|
|
154
|
+
message,
|
|
155
|
+
cancel: cancel.get(),
|
|
156
|
+
target: key === "sendPrivateMsg" ? { type: import_types.MessageScope.PRIVATE, userId: id1 } : key === "sendGroupMsg" ? { type: import_types.MessageScope.GROUP, groupId: id1 } : { type: import_types.MessageScope.CHANNEL, guildId: id1, channelId: id2 }
|
|
157
|
+
});
|
|
158
|
+
if (cancel.value) return;
|
|
159
|
+
Reflect.apply(target, thisArg, argArray);
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
this.ctx.on("on_message", (session) => this.handleMidware(session));
|
|
167
|
+
this.ctx.on("before_send", (data) => {
|
|
168
|
+
const { api } = data;
|
|
169
|
+
api.adapter.status.sentMsg += 1;
|
|
170
|
+
api.adapter.status.lastMsgTime = /* @__PURE__ */ new Date();
|
|
171
|
+
});
|
|
172
|
+
const test = (identity, session) => {
|
|
173
|
+
for (const [key, filter] of this[import_global.Symbols.filter].entries()) {
|
|
174
|
+
if (identity.toString().includes(key)) return filter.test(session);
|
|
175
|
+
}
|
|
176
|
+
return true;
|
|
177
|
+
};
|
|
178
|
+
this.ctx.on("before_command", (data) => {
|
|
179
|
+
const { identity } = (0, import_internal.getCommandMeta)(data.command) ?? {};
|
|
180
|
+
if (identity && !test(identity, data.session)) {
|
|
181
|
+
data.cancel();
|
|
182
|
+
const result = new import_error.CommandError({ type: "unknown", input: data.raw });
|
|
183
|
+
this.ctx.emit("command", { command: new import_components.Command(""), result, raw: data.raw, session: data.session });
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
this.ctx.on("before_regexp", (data) => {
|
|
187
|
+
const { callback } = Array.from(this[import_global.Symbols.regexp]).find(({ match }) => match === data.regexp) ?? {};
|
|
188
|
+
if (!callback) return;
|
|
189
|
+
const { identity } = (0, import_internal.getRegExpMeta)(callback) ?? {};
|
|
190
|
+
if (identity && !test(identity, data.session)) data.cancel();
|
|
191
|
+
});
|
|
192
|
+
this.midware((next, session) => {
|
|
193
|
+
const resolves = this[import_global.Symbols.promise].get(session.id);
|
|
194
|
+
if (resolves) {
|
|
195
|
+
for (const resolve of resolves) resolve(session.message);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
next();
|
|
199
|
+
}, 10);
|
|
200
|
+
import_utils.default.setup(this.ctx);
|
|
201
|
+
}
|
|
202
|
+
midware(callback, priority = 100) {
|
|
203
|
+
(0, import_internal.setMidwareMeta)(callback, { identity: this.ctx.identity, priority });
|
|
204
|
+
const data = { callback, priority };
|
|
205
|
+
this[import_global.Symbols.midware].add(data);
|
|
206
|
+
return () => this[import_global.Symbols.midware].delete(data);
|
|
207
|
+
}
|
|
208
|
+
command(template, config) {
|
|
209
|
+
const command = new import_components.Command(template, config);
|
|
210
|
+
this[import_global.Symbols.command].add(command);
|
|
211
|
+
(0, import_internal.setCommandMeta)(command, { identity: this.ctx.identity, ...command.meta });
|
|
212
|
+
return command;
|
|
213
|
+
}
|
|
214
|
+
regexp(match, callback) {
|
|
215
|
+
(0, import_internal.setRegExpMeta)(callback, { identity: this.ctx.identity, match });
|
|
216
|
+
const data = { match, callback };
|
|
217
|
+
this[import_global.Symbols.regexp].add(data);
|
|
218
|
+
return () => this[import_global.Symbols.regexp].delete(data);
|
|
219
|
+
}
|
|
220
|
+
boardcast() {
|
|
221
|
+
}
|
|
222
|
+
notify() {
|
|
223
|
+
const mainAdapterIdentity = Object.keys(this.ctx.config.adapter)[0];
|
|
224
|
+
for (const apis of this.ctx[import_global.Symbols.bot].values()) {
|
|
225
|
+
for (const api of apis) {
|
|
226
|
+
if (api.adapter.identity !== mainAdapterIdentity) continue;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
task(options, callback) {
|
|
231
|
+
const [baseOption, extraOptions] = typeof options === "string" ? [options, {}] : [options.cron, options];
|
|
232
|
+
const task = new import_cron.CronJob(
|
|
233
|
+
baseOption,
|
|
234
|
+
() => callback(this.ctx),
|
|
235
|
+
null,
|
|
236
|
+
extraOptions.start ?? true,
|
|
237
|
+
extraOptions.timeZone
|
|
238
|
+
);
|
|
239
|
+
(0, import_internal.setTaskMeta)(task, { identity: this.ctx.identity, task, options });
|
|
240
|
+
this[import_global.Symbols.task].add(task);
|
|
241
|
+
return () => this[import_global.Symbols.task].delete(task);
|
|
242
|
+
}
|
|
243
|
+
filter(option) {
|
|
244
|
+
const filterId = (0, import_node_crypto.randomUUID)().slice(0, 12).replaceAll("-", "");
|
|
245
|
+
this[import_global.Symbols.filter].set(filterId, new import_components.Filter(option));
|
|
246
|
+
return this.ctx.extends(void 0, `${this.ctx.identity?.toString()}_${filterId}`);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
var message_default = Message;
|
|
250
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
251
|
+
0 && (module.exports = {
|
|
252
|
+
Message
|
|
253
|
+
});
|