@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.
Files changed (79) hide show
  1. package/README.md +10 -4
  2. package/lib/app/config.d.ts +32 -0
  3. package/lib/app/config.js +65 -0
  4. package/lib/app/core.d.ts +138 -0
  5. package/lib/{components → app}/core.js +24 -10
  6. package/lib/app/index.d.ts +1 -0
  7. package/lib/{service → app}/index.js +7 -13
  8. package/lib/{components → app}/message.d.ts +11 -10
  9. package/lib/app/message.js +253 -0
  10. package/lib/components/adapter.d.ts +122 -0
  11. package/lib/components/adapter.js +75 -0
  12. package/lib/components/api.d.ts +417 -0
  13. package/lib/components/api.js +546 -0
  14. package/lib/components/cache.d.ts +37 -0
  15. package/lib/{service → components}/cache.js +27 -6
  16. package/lib/components/command.d.ts +153 -0
  17. package/lib/{utils → components}/command.js +154 -48
  18. package/lib/components/elements.d.ts +144 -0
  19. package/lib/components/elements.js +179 -0
  20. package/lib/components/filter.d.ts +22 -0
  21. package/lib/components/filter.js +130 -0
  22. package/lib/components/index.d.ts +8 -2
  23. package/lib/components/index.js +19 -7
  24. package/lib/components/messages.d.ts +186 -0
  25. package/lib/components/messages.js +218 -0
  26. package/lib/components/session.d.ts +177 -0
  27. package/lib/components/session.js +275 -0
  28. package/lib/decorators/index.d.ts +7 -0
  29. package/lib/{components/config.js → decorators/index.js} +23 -39
  30. package/lib/decorators/plugin.d.ts +7 -0
  31. package/lib/{utils/commandError.js → decorators/plugin.js} +16 -16
  32. package/lib/decorators/utils.d.ts +59 -0
  33. package/lib/decorators/utils.js +189 -0
  34. package/lib/global/constants.d.ts +1 -8
  35. package/lib/global/constants.js +6 -25
  36. package/lib/global/index.js +3 -3
  37. package/lib/global/symbols.d.ts +13 -8
  38. package/lib/global/symbols.js +18 -12
  39. package/lib/index.d.ts +4 -4
  40. package/lib/index.js +10 -11
  41. package/lib/types/adapter.d.ts +1 -1
  42. package/lib/types/adapter.js +3 -3
  43. package/lib/types/api.d.ts +72 -0
  44. package/lib/{utils/jsxFactory.js → types/api.js} +5 -5
  45. package/lib/types/command.d.ts +78 -0
  46. package/lib/types/command.js +50 -0
  47. package/lib/types/config.d.ts +4 -6
  48. package/lib/types/config.js +3 -3
  49. package/lib/types/filter.d.ts +51 -0
  50. package/lib/types/filter.js +87 -0
  51. package/lib/types/index.d.ts +4 -0
  52. package/lib/types/index.js +12 -4
  53. package/lib/types/message.d.ts +128 -193
  54. package/lib/types/message.js +12 -31
  55. package/lib/types/session.d.ts +349 -0
  56. package/lib/types/session.js +27 -0
  57. package/lib/utils/container.d.ts +6 -6
  58. package/lib/utils/container.js +12 -16
  59. package/lib/utils/error.d.ts +46 -22
  60. package/lib/utils/error.js +38 -21
  61. package/lib/utils/factory.d.ts +10 -16
  62. package/lib/utils/factory.js +41 -101
  63. package/lib/utils/internal.d.ts +46 -0
  64. package/lib/utils/internal.js +102 -0
  65. package/package.json +10 -7
  66. package/lib/components/config.d.ts +0 -16
  67. package/lib/components/core.d.ts +0 -34
  68. package/lib/components/message.js +0 -195
  69. package/lib/service/adapter.d.ts +0 -41
  70. package/lib/service/adapter.js +0 -131
  71. package/lib/service/api.d.ts +0 -32
  72. package/lib/service/api.js +0 -80
  73. package/lib/service/cache.d.ts +0 -13
  74. package/lib/service/elements.d.ts +0 -12
  75. package/lib/service/elements.js +0 -73
  76. package/lib/service/index.d.ts +0 -4
  77. package/lib/utils/command.d.ts +0 -51
  78. package/lib/utils/commandError.d.ts +0 -7
  79. package/lib/utils/jsxFactory.d.ts +0 -6
@@ -1,12 +1,12 @@
1
1
 
2
2
  /**
3
3
  * @Package @kotori-bot/core
4
- * @Version 1.5.0
5
- * @Author Hotaru <biyuehuya@gmail.com>
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/6/7 11:22:22
9
+ * @Date 2024/8/9 17:33:05
10
10
  */
11
11
 
12
12
  "use strict";
@@ -29,112 +29,52 @@ var __copyProps = (to, from, except, desc) => {
29
29
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
30
  var factory_exports = {};
31
31
  __export(factory_exports, {
32
- cancelFactory: () => cancelFactory,
33
- confirmFactory: () => confirmFactory,
34
- disposeFactory: () => disposeFactory,
35
- formatFactory: () => formatFactory,
36
- promptFactory: () => promptFactory,
37
- quickFactory: () => quickFactory,
38
- sendMessageFactory: () => sendMessageFactory
32
+ formatFactory: () => formatFactory
39
33
  });
40
34
  module.exports = __toCommonJS(factory_exports);
41
35
  var import_tools = require("@kotori-bot/tools");
42
- var import_types = require("../types");
43
- var import__ = require("..");
44
- function disposeFactory(ctx, dispose) {
45
- ctx.once("dispose_module", (data) => {
46
- if ((typeof data.instance === "object" ? data.instance.name : data.instance) !== ctx.identity) {
47
- disposeFactory(ctx, dispose);
48
- return;
49
- }
50
- dispose();
51
- });
52
- }
53
- function cancelFactory() {
54
- return {
55
- get() {
56
- return () => this.fn();
57
- },
58
- fn() {
59
- this.value = true;
60
- },
61
- value: false
62
- };
63
- }
36
+ var import_components = require("../components");
64
37
  function formatFactory(i18n) {
65
- return (template, data) => {
66
- const params = data;
67
- if (Array.isArray(params)) {
68
- let str = i18n.locale(template);
69
- params.forEach((value, index) => {
70
- if (value === void 0 || value === null) return;
71
- str = str.replaceAll(`{${index}}`, i18n.locale(typeof value === "string" ? value : String(value)));
72
- });
73
- return str;
74
- }
75
- Object.keys(params).forEach((key) => {
76
- if (params[key] === void 0 || params[key] === null) return;
77
- if (typeof params[key] !== "string") params[key] = String(params[key]);
78
- params[key] = i18n.locale(params[key]);
79
- });
80
- return (0, import_tools.stringTemp)(i18n.locale(template), params);
81
- };
82
- }
83
- function sendMessageFactory(adapter, type, data) {
84
- if ((data.type === import_types.MessageScope.GROUP || type.includes("group")) && "groupId" in data) {
85
- return (message) => {
86
- adapter.api.sendGroupMsg(message, data.groupId, data.extra);
87
- };
88
- }
89
- return (message) => {
90
- adapter.api.sendPrivateMsg(message, data.userId, data.extra);
91
- };
92
- }
93
- function quickFactory(send, i18n) {
94
- return async (message) => {
95
- const msg = await message;
96
- if (!msg || msg instanceof import__.CommandError) return;
97
- if (typeof msg === "string") {
98
- send(i18n.locale(msg));
99
- return;
38
+ function format(template, data) {
39
+ if (!Array.isArray(data)) {
40
+ for (const key of Object.keys(data)) {
41
+ if (data[key] === void 0 || data[key] === null) continue;
42
+ if (typeof data[key] !== "string") data[key] = String(data[key]);
43
+ data[key] = i18n.locale(data[key]);
44
+ }
45
+ return (0, import_tools.stringTemp)(i18n.locale(template), data);
100
46
  }
101
- send(formatFactory(i18n)(...msg));
102
- };
103
- }
104
- function isSameSender(adapter, data, session) {
105
- return session.api.adapter.identity === adapter.identity && session.api.adapter.platform === adapter.platform && session.type === data.type && session.groupId === data.groupId && session.userId === data.userId && "messageId" in data && session.messageId !== data.messageId;
106
- }
107
- function promptFactory(quick, adapter, data) {
108
- return (message) => new Promise((resolve) => {
109
- const handle = (session) => {
110
- if (isSameSender(adapter, data, session)) {
111
- resolve(session.message);
112
- return;
47
+ const isPureString = (Array.isArray(data) ? data : Object.values(data)).every(
48
+ (value) => !(value instanceof import_components.MessageList || value instanceof import_components.MessageSingle)
49
+ );
50
+ const parts = i18n.locale(template).split(/(\{[0-9]+\})/);
51
+ const result = [];
52
+ let currentString = "";
53
+ for (const part of parts) {
54
+ if (part === void 0 || part === null) continue;
55
+ if (!part.match(/^\{[0-9]+\}$/)) {
56
+ currentString += part;
57
+ continue;
113
58
  }
114
- adapter.ctx.once("on_message", handle);
115
- };
116
- quick(message ?? "corei18n.template.prompt").then(() => adapter.ctx.once("on_message", handle));
117
- });
118
- }
119
- function confirmFactory(quick, adapter, data) {
120
- return (options) => new Promise((resolve) => {
121
- const handle = (session) => {
122
- if (isSameSender(adapter, data, session)) {
123
- resolve(session.message === (options?.sure ?? "corei18n.template.confirm.sure"));
124
- return;
59
+ const index = Number.parseInt(part.slice(1, -1), 10);
60
+ const value = data[index];
61
+ if (value === void 0 || value === null) continue;
62
+ if (value instanceof import_components.MessageList || value instanceof import_components.MessageSingle) {
63
+ if (currentString) {
64
+ result.push(currentString);
65
+ currentString = "";
66
+ }
67
+ result.push(value);
68
+ } else {
69
+ currentString += String(value);
125
70
  }
126
- adapter.ctx.once("on_message", handle);
127
- };
128
- quick(options?.message ?? "corei18n.template.confirm").then(() => adapter.ctx.once("on_message", handle));
129
- });
71
+ }
72
+ if (currentString) result.push(currentString);
73
+ return isPureString ? result.join("") : (0, import_components.Messages)(...result);
74
+ }
75
+ return format;
130
76
  }
131
77
  // Annotate the CommonJS export names for ESM import in node:
132
78
  0 && (module.exports = {
133
- cancelFactory,
134
- confirmFactory,
135
- disposeFactory,
136
- formatFactory,
137
- promptFactory,
138
- quickFactory,
139
- sendMessageFactory
79
+ formatFactory
140
80
  });
@@ -0,0 +1,46 @@
1
+ import 'reflect-metadata';
2
+ import type { OptsOrigin, MidwareCallback, RegexpCallback, TaskOptions } from '../types';
3
+ import type { Command } from '../components';
4
+ import type { CronJob } from 'cron';
5
+ import type { IdentityType } from 'fluoro';
6
+ export declare function cancelFactory(): {
7
+ get(): () => void;
8
+ fn(): void;
9
+ value: boolean;
10
+ };
11
+ type CommandOriginData = Command<any, OptsOrigin>['meta'];
12
+ interface CommandMeta extends CommandOriginData {
13
+ identity?: IdentityType;
14
+ }
15
+ interface MidwareMeta {
16
+ identity?: IdentityType;
17
+ priority: number;
18
+ }
19
+ interface RegExpMeta {
20
+ identity?: IdentityType;
21
+ match: RegExp;
22
+ }
23
+ interface TaskMeta {
24
+ identity?: IdentityType;
25
+ task: CronJob;
26
+ options: TaskOptions;
27
+ }
28
+ export declare function getCommandMeta(command: Command<any, any>): CommandMeta | undefined;
29
+ export declare function setCommandMeta(command: Command<any, any>, meta: CommandMeta): void;
30
+ export declare function getMidwareMeta(callback: MidwareCallback): MidwareMeta | undefined;
31
+ export declare function setMidwareMeta(callback: MidwareCallback, meta: MidwareMeta): void;
32
+ export declare function getRegExpMeta(callback: RegexpCallback): RegExpMeta | undefined;
33
+ export declare function setRegExpMeta(callback: RegexpCallback, meta: RegExpMeta): void;
34
+ export declare function getTaskMeta(task: CronJob): TaskMeta | undefined;
35
+ export declare function setTaskMeta(task: CronJob, meta: TaskMeta): void;
36
+ declare const _default: {
37
+ getCommandMeta: typeof getCommandMeta;
38
+ setCommandMeta: typeof setCommandMeta;
39
+ getMidwareMeta: typeof getMidwareMeta;
40
+ setMidwareMeta: typeof setMidwareMeta;
41
+ getRegExpMeta: typeof getRegExpMeta;
42
+ setRegExpMeta: typeof setRegExpMeta;
43
+ getTaskMeta: typeof getTaskMeta;
44
+ setTaskMeta: typeof setTaskMeta;
45
+ };
46
+ export default _default;
@@ -0,0 +1,102 @@
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 internal_exports = {};
31
+ __export(internal_exports, {
32
+ cancelFactory: () => cancelFactory,
33
+ default: () => internal_default,
34
+ getCommandMeta: () => getCommandMeta,
35
+ getMidwareMeta: () => getMidwareMeta,
36
+ getRegExpMeta: () => getRegExpMeta,
37
+ getTaskMeta: () => getTaskMeta,
38
+ setCommandMeta: () => setCommandMeta,
39
+ setMidwareMeta: () => setMidwareMeta,
40
+ setRegExpMeta: () => setRegExpMeta,
41
+ setTaskMeta: () => setTaskMeta
42
+ });
43
+ module.exports = __toCommonJS(internal_exports);
44
+ var import_reflect_metadata = require("reflect-metadata");
45
+ var import_global = require("../global");
46
+ function cancelFactory() {
47
+ return {
48
+ get() {
49
+ return () => this.fn();
50
+ },
51
+ fn() {
52
+ this.value = true;
53
+ },
54
+ value: false
55
+ };
56
+ }
57
+ function getCommandMeta(command) {
58
+ return Reflect.getMetadata(import_global.Symbols.command, command);
59
+ }
60
+ function setCommandMeta(command, meta) {
61
+ Reflect.defineMetadata(import_global.Symbols.command, meta, command);
62
+ }
63
+ function getMidwareMeta(callback) {
64
+ return Reflect.getMetadata(import_global.Symbols.midware, callback);
65
+ }
66
+ function setMidwareMeta(callback, meta) {
67
+ Reflect.defineMetadata(import_global.Symbols.midware, meta, callback);
68
+ }
69
+ function getRegExpMeta(callback) {
70
+ return Reflect.getMetadata(import_global.Symbols.regexp, callback);
71
+ }
72
+ function setRegExpMeta(callback, meta) {
73
+ Reflect.defineMetadata(import_global.Symbols.regexp, meta, callback);
74
+ }
75
+ function getTaskMeta(task) {
76
+ return Reflect.getMetadata(import_global.Symbols.task, task);
77
+ }
78
+ function setTaskMeta(task, meta) {
79
+ Reflect.defineMetadata(import_global.Symbols.task, meta, task);
80
+ }
81
+ var internal_default = {
82
+ getCommandMeta,
83
+ setCommandMeta,
84
+ getMidwareMeta,
85
+ setMidwareMeta,
86
+ getRegExpMeta,
87
+ setRegExpMeta,
88
+ getTaskMeta,
89
+ setTaskMeta
90
+ };
91
+ // Annotate the CommonJS export names for ESM import in node:
92
+ 0 && (module.exports = {
93
+ cancelFactory,
94
+ getCommandMeta,
95
+ getMidwareMeta,
96
+ getRegExpMeta,
97
+ getTaskMeta,
98
+ setCommandMeta,
99
+ setMidwareMeta,
100
+ setRegExpMeta,
101
+ setTaskMeta
102
+ });
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@kotori-bot/core",
3
- "version": "1.5.1",
3
+ "version": "1.6.0",
4
4
  "description": "Kotori Core",
5
5
  "main": "lib/index.js",
6
6
  "license": "GPL-3.0",
7
- "author": "Hotaru <biyuehuya@gmail.com>",
7
+ "author": "Arimura Sena <me@hotaru.icu>",
8
8
  "keywords": [
9
9
  "kotori",
10
10
  "chatbot",
@@ -24,12 +24,15 @@
24
24
  },
25
25
  "homepage": "https://kotori.js.org",
26
26
  "dependencies": {
27
- "@types/minimist": "^1.2.5",
28
27
  "cron": "^3.1.7",
29
- "fluoro": "^1.0.2",
30
28
  "minimist": "^1.2.8",
31
- "tsukiko": "^1.2.5",
32
- "@kotori-bot/i18n": "^1.3.1",
33
- "@kotori-bot/tools": "^1.4.1"
29
+ "reflect-metadata": "^0.2.2",
30
+ "tsukiko": "^1.4.2",
31
+ "@kotori-bot/i18n": "^1.3.2",
32
+ "@kotori-bot/tools": "^1.5.0",
33
+ "fluoro": "^1.1.0"
34
+ },
35
+ "devDependencies": {
36
+ "@types/minimist": "^1.2.5"
34
37
  }
35
38
  }
@@ -1,16 +0,0 @@
1
- import Tsu from 'tsukiko';
2
- import { CoreConfig } from '../types';
3
- declare const packageInfoSchema: import("tsukiko").ObjectParser<{
4
- name: import("tsukiko").StringParser;
5
- version: import("tsukiko").StringParser;
6
- description: import("tsukiko").StringParser;
7
- main: import("tsukiko").StringParser;
8
- license: import("tsukiko").LiteralParser<"GPL-3.0">;
9
- author: import("tsukiko").StringParser;
10
- }>;
11
- export declare class Config {
12
- readonly config: CoreConfig;
13
- readonly pkg: Tsu.infer<typeof packageInfoSchema>;
14
- constructor(config?: CoreConfig);
15
- }
16
- export default Config;
@@ -1,34 +0,0 @@
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 '../service';
9
- import { Symbols } from '../global';
10
- declare module 'fluoro' {
11
- interface Context {
12
- readonly [Symbols.adapter]: Core[typeof Symbols.adapter];
13
- readonly [Symbols.bot]: Core[typeof Symbols.bot];
14
- readonly config: Config['config'];
15
- readonly pkg: Config['pkg'];
16
- readonly [Symbols.midware]: Message[typeof Symbols.midware];
17
- readonly [Symbols.command]: Message[typeof Symbols.command];
18
- readonly [Symbols.regexp]: Message[typeof Symbols.regexp];
19
- midware: Message['midware'];
20
- command: Message['command'];
21
- regexp: Message['regexp'];
22
- notify: Message['notify'];
23
- task: Message['task'];
24
- http: Http;
25
- i18n: I18n;
26
- cache: Cache;
27
- }
28
- }
29
- export declare class Core extends Context {
30
- readonly [Symbols.adapter]: Map<string, [AdapterClass, Parser<unknown>?]>;
31
- readonly [Symbols.bot]: Map<string, Set<Api>>;
32
- constructor(config?: ConstructorParameters<typeof Config>[0]);
33
- }
34
- export default Core;
@@ -1,195 +0,0 @@
1
-
2
- /**
3
- * @Package @kotori-bot/core
4
- * @Version 1.5.0
5
- * @Author Hotaru <biyuehuya@gmail.com>
6
- * @Copyright 2024 Hotaru. All rights reserved.
7
- * @License GPL-3.0
8
- * @Link https://github.com/kotorijs/kotori
9
- * @Date 2024/6/7 11:22:22
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_tools = require("@kotori-bot/tools");
47
- var import_cron = require("cron");
48
- var import_factory = require("../utils/factory");
49
- var import_command = require("../utils/command");
50
- var import_commandError = __toESM(require("../utils/commandError"));
51
- var import_global = require("../global");
52
- class Message {
53
- [import_global.Symbols.midware] = /* @__PURE__ */ new Set();
54
- [import_global.Symbols.command] = /* @__PURE__ */ new Set();
55
- [import_global.Symbols.regexp] = /* @__PURE__ */ new Set();
56
- handleMidware(session) {
57
- const { api } = session;
58
- let isPass = true;
59
- let midwares = [];
60
- api.adapter.status.receivedMsg += 1;
61
- this[import_global.Symbols.midware].forEach((val) => midwares.push(val));
62
- midwares = midwares.sort((first, second) => first.priority - second.priority);
63
- let lastMidwareNum = -1;
64
- while (midwares.length > 0) {
65
- if (lastMidwareNum === midwares.length) {
66
- isPass = false;
67
- break;
68
- }
69
- lastMidwareNum = midwares.length;
70
- session.quick(midwares[0].callback(() => midwares.shift(), session));
71
- }
72
- this.ctx.emit("midwares", { isPass, session });
73
- }
74
- async handleRegexp(data) {
75
- const { session } = data;
76
- if (!data.isPass) return;
77
- this[import_global.Symbols.regexp].forEach((data2) => {
78
- const result = session.message.match(data2.match);
79
- if (!result) return;
80
- session.quick(data2.callback(result, session));
81
- this.ctx.emit("regexp", { result, session, regexp: data2.match, raw: session.message });
82
- });
83
- }
84
- handleCommand(data) {
85
- const { session } = data;
86
- const prefix = session.api.adapter.config["command-prefix"] ?? this.ctx.config.global["command-prefix"];
87
- if (!session.message.startsWith(prefix)) return;
88
- const params = {
89
- session,
90
- raw: (0, import_tools.stringRightSplit)(session.message, prefix)
91
- };
92
- this.ctx.emit("before_parse", params);
93
- const cancel = (0, import_factory.cancelFactory)();
94
- this.ctx.emit("before_command", { cancel: cancel.get(), ...params });
95
- if (cancel.value) return;
96
- let matched;
97
- this[import_global.Symbols.command].forEach(async (cmd) => {
98
- if (matched || !cmd.meta.action) return;
99
- const result = import_command.Command.run(params.raw, cmd.meta);
100
- if (result instanceof import_commandError.default && result.value.type === "unknown") return;
101
- matched = cmd;
102
- this.ctx.emit("parse", { command: cmd, result, ...params, cancel: cancel.get() });
103
- if (cancel.value || result instanceof import_commandError.default) return;
104
- try {
105
- const executed = await cmd.meta.action({ args: result.args, options: result.options }, session);
106
- if (executed instanceof import_commandError.default) {
107
- this.ctx.emit("command", { command: cmd, result: executed, ...params });
108
- return;
109
- }
110
- if (executed !== void 0) session.quick(executed);
111
- this.ctx.emit("command", {
112
- command: cmd,
113
- result: executed instanceof import_commandError.default ? result : executed,
114
- ...params
115
- });
116
- } catch (error) {
117
- this.ctx.emit("command", {
118
- command: matched,
119
- result: new import_commandError.default({ type: "error", error }),
120
- ...params
121
- });
122
- }
123
- });
124
- if (matched) return;
125
- this.ctx.emit("parse", {
126
- command: new import_command.Command(""),
127
- result: new import_commandError.default({ type: "unknown", input: params.raw }),
128
- ...params,
129
- cancel: cancel.get()
130
- });
131
- }
132
- ctx;
133
- constructor(ctx) {
134
- this.ctx = ctx;
135
- this.ctx.on("on_message", (session) => this.handleMidware(session));
136
- this.ctx.on("midwares", (data) => this.handleCommand(data));
137
- this.ctx.on("midwares", (data) => this.handleRegexp(data));
138
- this.ctx.on("before_send", (data) => {
139
- const { api } = data;
140
- api.adapter.status.sentMsg += 1;
141
- });
142
- }
143
- midware(callback, priority = 100) {
144
- const data = { callback, priority };
145
- this[import_global.Symbols.midware].add(data);
146
- const dispose = () => this[import_global.Symbols.midware].delete(data);
147
- (0, import_factory.disposeFactory)(this.ctx, dispose);
148
- return dispose;
149
- }
150
- command(template, config) {
151
- const command = new import_command.Command(template, config);
152
- this[import_global.Symbols.command].add(command);
153
- const dispose = () => this[import_global.Symbols.command].delete(command);
154
- (0, import_factory.disposeFactory)(this.ctx, dispose);
155
- return command;
156
- }
157
- regexp(match, callback) {
158
- const data = { match, callback };
159
- this[import_global.Symbols.regexp].add(data);
160
- const dispose = () => this[import_global.Symbols.regexp].delete(data);
161
- (0, import_factory.disposeFactory)(this.ctx, dispose);
162
- return dispose;
163
- }
164
- // boardcast(type: MessageScope, message: MessageRaw) {
165
- // const send =
166
- // type === 'private'
167
- // ? (api: Api) => api.send_on_message(message, 1)
168
- // : (api: Api) => api.send_on_message(message, 1);
169
- // /* this need support of database... */
170
- // Object.values(this.botStack).forEach((apis) => {
171
- // apis.forEach((api) => send(api));
172
- // });
173
- // }
174
- notify(message) {
175
- const mainAdapterIdentity = Object.keys(this.ctx.config.adapter)[0];
176
- this.ctx[import_global.Symbols.bot].forEach(
177
- (apis) => apis.forEach((api) => {
178
- if (api.adapter.identity !== mainAdapterIdentity) return;
179
- (0, import_factory.quickFactory)(
180
- (0, import_factory.sendMessageFactory)(api.adapter, "on_message", { userId: api.adapter.config.master }),
181
- this.ctx.i18n.extends(api.adapter.config.lang)
182
- )(message);
183
- })
184
- );
185
- }
186
- task(options, callback) {
187
- const [cron, extraOptions] = typeof options === "string" ? [options, {}] : [options.cron, options];
188
- return new import_cron.CronJob(cron, () => callback(this.ctx), null, extraOptions.start ?? true, extraOptions.timeZone);
189
- }
190
- }
191
- var message_default = Message;
192
- // Annotate the CommonJS export names for ESM import in node:
193
- 0 && (module.exports = {
194
- Message
195
- });
@@ -1,41 +0,0 @@
1
- import { Context } from 'fluoro';
2
- import type Api from './api';
3
- import { AdapterConfig, EventDataTargetId, EventApiType } from '../types';
4
- import Elements from './elements';
5
- interface AdapterStatus {
6
- value: 'online' | 'offline';
7
- createTime: Date;
8
- lastMsgTime: Date | null;
9
- receivedMsg: number;
10
- sentMsg: number;
11
- offlineTimes: number;
12
- }
13
- interface AdapterImpl<T extends Api = Api> {
14
- readonly ctx: Context;
15
- readonly config: AdapterConfig;
16
- readonly platform: string;
17
- readonly selfId: EventDataTargetId;
18
- readonly identity: string;
19
- readonly api: T;
20
- readonly elements: Elements;
21
- readonly status: AdapterStatus;
22
- }
23
- export declare abstract class Adapter<T extends Api = Api> implements AdapterImpl<T> {
24
- constructor(ctx: Context, config: AdapterConfig, identity: string, Api: new (adapter: Adapter) => T, el?: Elements);
25
- abstract handle(...data: unknown[]): void;
26
- abstract start(): void;
27
- abstract stop(): void;
28
- abstract send(action: string, params?: object): void | object | Promise<unknown> | null | undefined;
29
- protected online(): void;
30
- protected offline(): void;
31
- protected session<N extends keyof EventApiType>(type: N, data: Omit<EventApiType[N], 'api' | 'send' | 'i18n' | 'format' | 'quick' | 'prompt' | 'confirm' | 'el' | 'error'>): void;
32
- readonly ctx: Context;
33
- readonly config: AdapterConfig;
34
- readonly identity: string;
35
- readonly platform: string;
36
- readonly api: T;
37
- readonly elements: Elements;
38
- readonly status: AdapterStatus;
39
- selfId: EventDataTargetId;
40
- }
41
- export default Adapter;