@kotori-bot/core 1.3.0 → 1.4.1

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 CHANGED
@@ -1,17 +1,17 @@
1
- # @kotori-bot/core
2
-
3
- - Context
4
- - Symbols
5
- - Service
6
- - Modules
7
- - Core
8
- - Config
9
- - Message
10
- - Adapter
11
- - Api
12
- - Elements
13
- - Cache
14
-
15
- ## Reference
16
-
17
- - [Kotori Docs](https://kotori.js.org/)
1
+ # @kotori-bot/core
2
+
3
+ - Context
4
+ - Symbols
5
+ - Service
6
+ - Modules
7
+ - Core
8
+ - Config
9
+ - Message
10
+ - Adapter
11
+ - Api
12
+ - Elements
13
+ - Cache
14
+
15
+ ## Reference
16
+
17
+ - [Kotori Docs](https://kotori.js.org/)
@@ -5,9 +5,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Config = void 0;
7
7
  const tsukiko_1 = __importDefault(require("tsukiko"));
8
- const path_1 = require("path");
8
+ const node_path_1 = require("node:path");
9
9
  const tools_1 = require("@kotori-bot/tools");
10
- const consts_1 = require("../consts");
10
+ const global_1 = require("../global");
11
11
  const packageInfoSchema = tsukiko_1.default.Object({
12
12
  name: tsukiko_1.default.String(),
13
13
  version: tsukiko_1.default.String(),
@@ -19,10 +19,10 @@ const packageInfoSchema = tsukiko_1.default.Object({
19
19
  class Config {
20
20
  config;
21
21
  pkg;
22
- constructor(config = consts_1.DEFAULT_CORE_CONFIG) {
22
+ constructor(config = global_1.DEFAULT_CORE_CONFIG) {
23
23
  this.config = config;
24
24
  /* load package.json */
25
- const info = (0, tools_1.loadConfig)((0, path_1.join)(__dirname, '../../package.json'));
25
+ const info = (0, tools_1.loadConfig)((0, node_path_1.join)(__dirname, '../../package.json'));
26
26
  if (!info || Object.values(info).length === 0) {
27
27
  process.stderr.write(`Cannot find kotori-bot package.json\n`);
28
28
  process.exit();
@@ -1,12 +1,13 @@
1
1
  import { Http } from '@kotori-bot/tools';
2
2
  import I18n from '@kotori-bot/i18n';
3
3
  import type { Parser } from 'tsukiko';
4
- import { Context, Symbols } from '../context';
4
+ import { Context } from 'fluoro';
5
5
  import Config from './config';
6
6
  import Message from './message';
7
7
  import type { AdapterClass } from '../types';
8
8
  import { Cache, type Api } from '../service';
9
- declare module '../context' {
9
+ import { Symbols } from '../global';
10
+ declare module 'fluoro' {
10
11
  interface Context {
11
12
  readonly [Symbols.adapter]: Core[typeof Symbols.adapter];
12
13
  readonly [Symbols.bot]: Core[typeof Symbols.bot];
@@ -6,13 +6,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Core = void 0;
7
7
  const tools_1 = require("@kotori-bot/tools");
8
8
  const i18n_1 = __importDefault(require("@kotori-bot/i18n"));
9
- const context_1 = require("../context");
9
+ const fluoro_1 = require("fluoro");
10
10
  const config_1 = __importDefault(require("./config"));
11
11
  const message_1 = __importDefault(require("./message"));
12
12
  const service_1 = require("../service");
13
- class Core extends context_1.Context {
14
- [context_1.Symbols.adapter] = new Map();
15
- [context_1.Symbols.bot] = new Map();
13
+ const global_1 = require("../global");
14
+ class Core extends fluoro_1.Context {
15
+ [global_1.Symbols.adapter] = new Map();
16
+ [global_1.Symbols.bot] = new Map();
16
17
  constructor(config) {
17
18
  super();
18
19
  this.provide('config', new config_1.default(config));
@@ -1 +1,2 @@
1
1
  export * from './core';
2
+ export * from '../global';
@@ -15,3 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./core"), exports);
18
+ __exportStar(require("../global"), exports);
@@ -1,6 +1,7 @@
1
- import { type Context, Symbols } from '../context';
1
+ import { type Context } from 'fluoro';
2
2
  import { type CommandConfig, type MidwareCallback, type RegexpCallback } from '../types';
3
3
  import { Command } from '../utils/command';
4
+ import { Symbols } from '../global';
4
5
  interface MidwareData {
5
6
  callback: MidwareCallback;
6
7
  priority: number;
@@ -5,20 +5,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Message = void 0;
7
7
  const tools_1 = require("@kotori-bot/tools");
8
- const context_1 = require("../context");
9
8
  const factory_1 = require("../utils/factory");
10
9
  const command_1 = require("../utils/command");
11
10
  const commandError_1 = __importDefault(require("../utils/commandError"));
11
+ const global_1 = require("../global");
12
12
  class Message {
13
- [context_1.Symbols.midware] = new Set();
14
- [context_1.Symbols.command] = new Set();
15
- [context_1.Symbols.regexp] = new Set();
13
+ [global_1.Symbols.midware] = new Set();
14
+ [global_1.Symbols.command] = new Set();
15
+ [global_1.Symbols.regexp] = new Set();
16
16
  handleMidware(session) {
17
17
  const { api } = session;
18
- api.adapter.status.receivedMsg += 1;
19
18
  let isPass = true;
20
19
  let midwares = [];
21
- this[context_1.Symbols.midware].forEach((val) => midwares.push(val));
20
+ api.adapter.status.receivedMsg += 1;
21
+ this[global_1.Symbols.midware].forEach((val) => midwares.push(val));
22
22
  midwares = midwares.sort((first, second) => first.priority - second.priority);
23
23
  let lastMidwareNum = -1;
24
24
  while (midwares.length > 0) {
@@ -35,7 +35,7 @@ class Message {
35
35
  const { session } = data;
36
36
  if (!data.isPass)
37
37
  return;
38
- this[context_1.Symbols.regexp].forEach((data) => {
38
+ this[global_1.Symbols.regexp].forEach((data) => {
39
39
  const match = session.message.match(data.match);
40
40
  if (!match)
41
41
  return;
@@ -57,7 +57,7 @@ class Message {
57
57
  if (cancel.value)
58
58
  return;
59
59
  let matched;
60
- this[context_1.Symbols.command].forEach(async (cmd) => {
60
+ this[global_1.Symbols.command].forEach(async (cmd) => {
61
61
  if (matched || !cmd.meta.action)
62
62
  return;
63
63
  const result = command_1.Command.run(params.raw, cmd.meta);
@@ -111,22 +111,22 @@ class Message {
111
111
  }
112
112
  midware(callback, priority = 100) {
113
113
  const data = { callback, priority };
114
- this[context_1.Symbols.midware].add(data);
115
- const dispose = () => this[context_1.Symbols.midware].delete(data);
114
+ this[global_1.Symbols.midware].add(data);
115
+ const dispose = () => this[global_1.Symbols.midware].delete(data);
116
116
  (0, factory_1.disposeFactory)(this.ctx, dispose);
117
117
  return dispose;
118
118
  }
119
119
  command(template, config) {
120
120
  const command = new command_1.Command(template, config);
121
- this[context_1.Symbols.command].add(command);
122
- const dispose = () => this[context_1.Symbols.command].delete(command);
121
+ this[global_1.Symbols.command].add(command);
122
+ const dispose = () => this[global_1.Symbols.command].delete(command);
123
123
  (0, factory_1.disposeFactory)(this.ctx, dispose);
124
124
  return command;
125
125
  }
126
126
  regexp(match, callback) {
127
127
  const data = { match, callback };
128
- this[context_1.Symbols.regexp].add(data);
129
- const dispose = () => this[context_1.Symbols.regexp].delete(data);
128
+ this[global_1.Symbols.regexp].add(data);
129
+ const dispose = () => this[global_1.Symbols.regexp].delete(data);
130
130
  (0, factory_1.disposeFactory)(this.ctx, dispose);
131
131
  return dispose;
132
132
  }
@@ -0,0 +1,15 @@
1
+ export declare const OFFICIAL_MODULES_SCOPE = "@kotori-bot/";
2
+ export declare const PLUGIN_PREFIX = "kotori-plugin-";
3
+ export declare const DATABASE_PREFIX = "kotori-plugin-database-";
4
+ export declare const ADAPTER_PREFIX = "kotori-plugin-adapter-";
5
+ export declare const CUSTOM_PREFIX = "kotori-plugin-custom-";
6
+ export declare const DEFAULT_PORT = 720;
7
+ export declare const DEFAULT_CORE_CONFIG: {
8
+ global: {
9
+ lang: "en_US" | "en_GB" | "en_AU" | "zh_CN" | "zh_HK" | "zh_TW" | "zh_SG" | "es_ES" | "es_MX" | "ar_EG" | "ar_AE" | "ru_RU" | "fr_FR" | "fr_CA" | "de_DE" | "de_CH" | "it_IT" | "it_CH" | "hi_IN" | "pt_BR" | "pt_PT" | "tr_TR" | "ja_JP" | "id_ID" | "uk_UA" | "vi_VN" | "th_TH" | "sv_SE" | "nb_NO" | "da_DK" | "fi_FI" | "he_IL" | "sk_SK" | "bg_BG" | "lt_LT" | "sl_SI" | "sr_RS" | "mk_MK" | "sq_AL" | "et_EE" | "mt_MT";
10
+ 'command-prefix': string;
11
+ port: number;
12
+ };
13
+ adapter: {};
14
+ plugin: {};
15
+ };
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_CORE_CONFIG = exports.DEFAULT_PORT = exports.CUSTOM_PREFIX = exports.ADAPTER_PREFIX = exports.DATABASE_PREFIX = exports.PLUGIN_PREFIX = exports.OFFICIAL_MODULES_SCOPE = void 0;
4
+ const i18n_1 = require("@kotori-bot/i18n");
5
+ exports.OFFICIAL_MODULES_SCOPE = '@kotori-bot/';
6
+ exports.PLUGIN_PREFIX = 'kotori-plugin-';
7
+ exports.DATABASE_PREFIX = `${exports.PLUGIN_PREFIX}database-`;
8
+ exports.ADAPTER_PREFIX = `${exports.PLUGIN_PREFIX}adapter-`;
9
+ exports.CUSTOM_PREFIX = `${exports.PLUGIN_PREFIX}custom-`;
10
+ exports.DEFAULT_PORT = 720;
11
+ exports.DEFAULT_CORE_CONFIG = {
12
+ global: {
13
+ lang: i18n_1.DEFAULT_LANG,
14
+ 'command-prefix': '/',
15
+ port: exports.DEFAULT_PORT
16
+ },
17
+ adapter: {},
18
+ plugin: {}
19
+ };
@@ -25,6 +25,7 @@ export declare class Context implements ContextImpl {
25
25
  readonly [Symbols.container]: Map<string, obj>;
26
26
  readonly [Symbols.table]: Map<string, string[]>;
27
27
  root: Context;
28
+ parent: Context | null;
28
29
  constructor(root?: Context);
29
30
  get<T = obj | undefined>(prop: string): T;
30
31
  inject<T extends Keys>(prop: T): void;
@@ -9,7 +9,7 @@ exports.Context = void 0;
9
9
  * @Blog: https://hotaru.icu
10
10
  * @Date: 2024-02-07 13:44:38
11
11
  * @LastEditors: Hotaru biyuehuya@gmail.com
12
- * @LastEditTime: 2024-02-21 10:38:39
12
+ * @LastEditTime: 2024-05-01 21:32:19
13
13
  */
14
14
  const tools_1 = require("@kotori-bot/tools");
15
15
  const symbols_1 = __importDefault(require("./symbols"));
@@ -25,10 +25,12 @@ const handler = (value, ctx) => {
25
25
  }
26
26
  });
27
27
  };
28
+ const DEFAULT_EXTENDS_NAME = 'sub';
28
29
  class Context {
29
30
  [symbols_1.default.container] = new Map();
30
31
  [symbols_1.default.table] = new Map();
31
32
  root;
33
+ parent = null;
32
34
  constructor(root) {
33
35
  this.root = root || this;
34
36
  this.provide('events', root ? root.get('events') : new tools_1.Events());
@@ -75,7 +77,9 @@ class Context {
75
77
  const ctx = new Proxy(new Context(this.root), {
76
78
  get: (target, prop) => {
77
79
  if (prop === 'identity')
78
- return identity ?? this.identity ?? 'sub';
80
+ return identity ?? this.identity ?? DEFAULT_EXTENDS_NAME;
81
+ if (prop === 'parent')
82
+ return this;
79
83
  if (target[prop])
80
84
  return handler(target[prop], ctx);
81
85
  let value;
@@ -9,7 +9,7 @@ const service_1 = require("./service");
9
9
  function handleFunction(func, ctx, config) {
10
10
  func(ctx, config);
11
11
  }
12
- function handleCconstructor(Class, ctx, config) {
12
+ function handleConstructor(Class, ctx, config) {
13
13
  (0, tools_1.none)(new Class(ctx, config));
14
14
  }
15
15
  const DEFAULT_MODULE_CONFIG = { filter: {} };
@@ -22,9 +22,9 @@ class Modules {
22
22
  const ctx = this.ctx.extends({}, !this.ctx.identity && typeof instance === 'object' ? instance.name : this.ctx.identity);
23
23
  if (instance instanceof Function) {
24
24
  if ((0, tools_1.isClass)(instance))
25
- handleCconstructor(instance, ctx, DEFAULT_MODULE_CONFIG);
25
+ handleConstructor(instance, ctx, DEFAULT_MODULE_CONFIG);
26
26
  else
27
- instance(ctx, DEFAULT_MODULE_CONFIG);
27
+ handleFunction(instance, ctx, DEFAULT_MODULE_CONFIG);
28
28
  this.ctx.emit('ready_module', { instance });
29
29
  return;
30
30
  }
@@ -40,7 +40,7 @@ class Modules {
40
40
  }
41
41
  if (defaults) {
42
42
  if ((0, tools_1.isClass)(defaults))
43
- handleCconstructor(defaults, ctx, config ?? DEFAULT_MODULE_CONFIG);
43
+ handleConstructor(defaults, ctx, config ?? DEFAULT_MODULE_CONFIG);
44
44
  else
45
45
  handleFunction(defaults, ctx, config ?? DEFAULT_MODULE_CONFIG);
46
46
  }
@@ -48,7 +48,7 @@ class Modules {
48
48
  handleFunction(main, ctx, config ?? DEFAULT_MODULE_CONFIG);
49
49
  }
50
50
  else if (Main) {
51
- handleCconstructor(Main, ctx, config ?? DEFAULT_MODULE_CONFIG);
51
+ handleConstructor(Main, ctx, config ?? DEFAULT_MODULE_CONFIG);
52
52
  }
53
53
  this.ctx.emit('ready_module', { instance });
54
54
  }
@@ -0,0 +1,15 @@
1
+ export declare const OFFICIAL_MODULES_SCOPE = "@kotori-bot/";
2
+ export declare const PLUGIN_PREFIX = "kotori-plugin-";
3
+ export declare const DATABASE_PREFIX = "kotori-plugin-database-";
4
+ export declare const ADAPTER_PREFIX = "kotori-plugin-adapter-";
5
+ export declare const CUSTOM_PREFIX = "kotori-plugin-custom-";
6
+ export declare const DEFAULT_PORT = 720;
7
+ export declare const DEFAULT_CORE_CONFIG: {
8
+ global: {
9
+ lang: "common" | "en_US" | "en_GB" | "en_AU" | "zh_CN" | "zh_HK" | "zh_TW" | "zh_SG" | "es_ES" | "es_MX" | "ar_EG" | "ar_AE" | "ru_RU" | "fr_FR" | "fr_CA" | "de_DE" | "de_CH" | "it_IT" | "it_CH" | "hi_IN" | "pt_BR" | "pt_PT" | "tr_TR" | "ja_JP" | "id_ID" | "uk_UA" | "vi_VN" | "th_TH" | "sv_SE" | "nb_NO" | "da_DK" | "fi_FI" | "he_IL" | "sk_SK" | "bg_BG" | "lt_LT" | "sl_SI" | "sr_RS" | "mk_MK" | "sq_AL" | "et_EE" | "mt_MT";
10
+ 'command-prefix': string;
11
+ port: number;
12
+ };
13
+ adapter: {};
14
+ plugin: {};
15
+ };
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_CORE_CONFIG = exports.DEFAULT_PORT = exports.CUSTOM_PREFIX = exports.ADAPTER_PREFIX = exports.DATABASE_PREFIX = exports.PLUGIN_PREFIX = exports.OFFICIAL_MODULES_SCOPE = void 0;
4
+ const i18n_1 = require("@kotori-bot/i18n");
5
+ exports.OFFICIAL_MODULES_SCOPE = '@kotori-bot/';
6
+ exports.PLUGIN_PREFIX = 'kotori-plugin-';
7
+ exports.DATABASE_PREFIX = `${exports.PLUGIN_PREFIX}database-`;
8
+ exports.ADAPTER_PREFIX = `${exports.PLUGIN_PREFIX}adapter-`;
9
+ exports.CUSTOM_PREFIX = `${exports.PLUGIN_PREFIX}custom-`;
10
+ exports.DEFAULT_PORT = 720;
11
+ exports.DEFAULT_CORE_CONFIG = {
12
+ global: {
13
+ lang: i18n_1.DEFAULT_LANG,
14
+ 'command-prefix': '/',
15
+ port: exports.DEFAULT_PORT
16
+ },
17
+ adapter: {},
18
+ plugin: {}
19
+ };
@@ -0,0 +1,2 @@
1
+ export * from './constants';
2
+ export * from './symbols';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./constants"), exports);
18
+ __exportStar(require("./symbols"), exports);
@@ -0,0 +1,8 @@
1
+ export declare class Symbols {
2
+ static readonly adapter: unique symbol;
3
+ static readonly bot: unique symbol;
4
+ static readonly midware: unique symbol;
5
+ static readonly command: unique symbol;
6
+ static readonly regexp: unique symbol;
7
+ static readonly modules: unique symbol;
8
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Symbols = void 0;
4
+ class Symbols {
5
+ static adapter = Symbol.for('kotori.core.adapter');
6
+ static bot = Symbol.for('kotori.core.bot');
7
+ static midware = Symbol.for('kotori.core.midware');
8
+ static command = Symbol.for('kotori.core.command');
9
+ static regexp = Symbol.for('kotori.core.regexp');
10
+ static modules = Symbol.for('kotori.loader.module');
11
+ }
12
+ exports.Symbols = Symbols;
@@ -0,0 +1,8 @@
1
+ export declare class Tokens {
2
+ static readonly adapter: unique symbol;
3
+ static readonly bot: unique symbol;
4
+ static readonly midware: unique symbol;
5
+ static readonly command: unique symbol;
6
+ static readonly regexp: unique symbol;
7
+ static readonly modules: unique symbol;
8
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Tokens = void 0;
4
+ class Tokens {
5
+ static adapter = Symbol.for('kotori.core.adapter');
6
+ static bot = Symbol.for('kotori.core.bot');
7
+ static midware = Symbol.for('kotori.core.midware');
8
+ static command = Symbol.for('kotori.core.command');
9
+ static regexp = Symbol.for('kotori.core.regexp');
10
+ static modules = Symbol.for('kotori.loader.module');
11
+ }
12
+ exports.Tokens = Tokens;
package/lib/index.d.ts CHANGED
@@ -1,11 +1,12 @@
1
- export * from './context';
1
+ export * from 'fluoro';
2
2
  export * from './components';
3
3
  export * from './service';
4
4
  export * from './utils/command';
5
- export * from './utils/errror';
5
+ export * from './utils/error';
6
6
  export * from './utils/commandError';
7
7
  export * from './utils/container';
8
- export * from './consts';
8
+ export * from './utils/factory';
9
+ export * from './global';
9
10
  export * from './types';
10
11
  export * from '@kotori-bot/tools';
11
12
  export * from '@kotori-bot/i18n';
package/lib/index.js CHANGED
@@ -19,16 +19,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
19
19
  * @Blog: https://hotaru.icu
20
20
  * @Date: 2024-02-07 13:44:38
21
21
  * @LastEditors: Hotaru biyuehuya@gmail.com
22
- * @LastEditTime: 2024-02-07 14:47:23
22
+ * @LastEditTime: 2024-05-03 11:36:22
23
23
  */
24
- __exportStar(require("./context"), exports);
24
+ __exportStar(require("fluoro"), exports);
25
25
  __exportStar(require("./components"), exports);
26
26
  __exportStar(require("./service"), exports);
27
27
  __exportStar(require("./utils/command"), exports);
28
- __exportStar(require("./utils/errror"), exports);
28
+ __exportStar(require("./utils/error"), exports);
29
29
  __exportStar(require("./utils/commandError"), exports);
30
30
  __exportStar(require("./utils/container"), exports);
31
- __exportStar(require("./consts"), exports);
31
+ __exportStar(require("./utils/factory"), exports);
32
+ __exportStar(require("./global"), exports);
32
33
  __exportStar(require("./types"), exports);
33
34
  __exportStar(require("@kotori-bot/tools"), exports);
34
35
  __exportStar(require("@kotori-bot/i18n"), exports);
@@ -1,5 +1,5 @@
1
+ import { Context, EventsList } from 'fluoro';
1
2
  import type Api from './api';
2
- import { Context, EventsList } from '../context';
3
3
  import { EventDataApiBase, EventDataTargetId, AdapterConfig } from '../types';
4
4
  import Elements from './elements';
5
5
  type EventApiType = {
@@ -4,17 +4,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Adapter = void 0;
7
- const tools_1 = require("@kotori-bot/tools");
8
- const context_1 = require("../context");
9
7
  const types_1 = require("../types");
10
8
  const elements_1 = __importDefault(require("./elements"));
11
9
  const factory_1 = require("../utils/factory");
12
10
  const commandError_1 = __importDefault(require("../utils/commandError"));
11
+ const global_1 = require("../global");
13
12
  function setProxy(api, ctx) {
14
13
  const proxy = Object.create(api);
15
14
  proxy.sendPrivateMsg = new Proxy(api.sendPrivateMsg, {
16
15
  apply(_, __, argArray) {
17
- const { '0': message, '1': targetId } = argArray;
16
+ const [message, targetId] = argArray;
18
17
  const cancel = (0, factory_1.cancelFactory)();
19
18
  ctx.emit('before_send', { api, message, messageType: types_1.MessageScope.PRIVATE, targetId, cancel: cancel.get() });
20
19
  if (cancel.value)
@@ -24,7 +23,7 @@ function setProxy(api, ctx) {
24
23
  });
25
24
  proxy.sendGroupMsg = new Proxy(api.sendGroupMsg, {
26
25
  apply(_, __, argArray) {
27
- const { '0': message, '1': targetId } = argArray;
26
+ const [message, targetId] = argArray;
28
27
  const cancel = (0, factory_1.cancelFactory)();
29
28
  ctx.emit('before_send', { api, message, messageType: types_1.MessageScope.PRIVATE, targetId, cancel: cancel.get() });
30
29
  if (cancel.value)
@@ -44,25 +43,7 @@ function sendMessageFactory(adapter, type, data) {
44
43
  adapter.api.sendPrivateMsg(message, data.userId, data.extra);
45
44
  };
46
45
  }
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
- }
65
- function qucikFactory(send, i18n) {
46
+ function quickFactory(send, i18n) {
66
47
  return async (message) => {
67
48
  const msg = await message;
68
49
  if (!msg || msg instanceof commandError_1.default)
@@ -71,7 +52,7 @@ function qucikFactory(send, i18n) {
71
52
  send(i18n.locale(msg));
72
53
  return;
73
54
  }
74
- send(formatFactory(i18n)(...msg));
55
+ send((0, factory_1.formatFactory)(i18n)(...msg));
75
56
  };
76
57
  }
77
58
  function isSameSender(adapter, data, session) {
@@ -118,9 +99,9 @@ class Adapter {
118
99
  this.platform = config.extends;
119
100
  this.api = setProxy(new Api(this), this.ctx);
120
101
  this.elements = el;
121
- if (!this.ctx[context_1.Symbols.bot].get(this.platform))
122
- this.ctx[context_1.Symbols.bot].set(this.platform, new Set());
123
- this.ctx[context_1.Symbols.bot].get(this.platform).add(this.api);
102
+ if (!this.ctx[global_1.Symbols.bot].get(this.platform))
103
+ this.ctx[global_1.Symbols.bot].set(this.platform, new Set());
104
+ this.ctx[global_1.Symbols.bot].get(this.platform).add(this.api);
124
105
  }
125
106
  online() {
126
107
  if (this.status.value !== 'offline')
@@ -138,8 +119,8 @@ class Adapter {
138
119
  session(type, data) {
139
120
  const i18n = this.ctx.i18n.extends(this.config.lang);
140
121
  const send = sendMessageFactory(this, type, data);
141
- const format = formatFactory(i18n);
142
- const quick = qucikFactory(send, i18n);
122
+ const format = (0, factory_1.formatFactory)(i18n);
123
+ const quick = quickFactory(send, i18n);
143
124
  const prompt = promptFactory(quick, this, data);
144
125
  const confirm = confirmFactory(quick, this, data);
145
126
  const { api, elements: el } = this;
@@ -1,4 +1,4 @@
1
- import { Service } from '../context';
1
+ import { Service } from 'fluoro';
2
2
  type CacheKey = string | symbol;
3
3
  type CacheValue = string | number | object;
4
4
  export declare class Cache extends Service {
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Cache = void 0;
4
- const context_1 = require("../context");
5
- class Cache extends context_1.Service {
4
+ const fluoro_1 = require("fluoro");
5
+ class Cache extends fluoro_1.Service {
6
6
  cache;
7
7
  constructor(ctx) {
8
8
  super(ctx, {}, 'cache');
@@ -1,7 +1,7 @@
1
- import type { Context } from '../context';
1
+ import type { Context } from 'fluoro';
2
2
  import type { Adapter } from '../service';
3
3
  import type { AdapterConfig } from './config';
4
- declare module '../context/events' {
4
+ declare module 'fluoro' {
5
5
  interface EventsMapping {
6
6
  connect(data: EventDataConnect): void;
7
7
  status(data: EventDataStatus): void;
@@ -1,6 +1,6 @@
1
1
  import type { LocaleType } from '@kotori-bot/i18n';
2
+ import { ModuleConfig } from 'fluoro';
2
3
  import type { EventDataTargetId } from './message';
3
- import { ModuleConfig } from '../context';
4
4
  export interface CoreConfig {
5
5
  global: GlobalConfig;
6
6
  adapter: {
@@ -11,6 +11,7 @@ export interface CoreConfig {
11
11
  };
12
12
  }
13
13
  export interface GlobalConfig {
14
+ level?: number;
14
15
  lang: LocaleType;
15
16
  'command-prefix': string;
16
17
  }
@@ -1,10 +1,10 @@
1
1
  import Tsu, { TsuError } from 'tsukiko';
2
2
  import type I18n from '@kotori-bot/i18n';
3
- import type { EventsList } from '../context';
3
+ import type { EventsList } from 'fluoro';
4
4
  import type CommandError from '../utils/commandError';
5
5
  import type { Api, Elements } from '../service';
6
6
  import { Command } from '../utils/command';
7
- declare module '../context/events' {
7
+ declare module 'fluoro' {
8
8
  interface EventsMapping {
9
9
  midwares(data: EventDataMidwares): void;
10
10
  before_parse(data: EventDataBeforeParse): void;
@@ -61,15 +61,15 @@ class Command {
61
61
  const result = (0, minimist_1.default)(arr, opts);
62
62
  /* handle args */
63
63
  const args = result._;
64
- const nums = {
64
+ const count = {
65
65
  expected: data.args.filter((el) => !el.optional).length,
66
66
  reality: args.length
67
67
  };
68
- if (nums.reality < nums.expected)
69
- return new commandError_1.CommandError({ type: 'arg_few', ...nums });
70
- nums.expected = data.args.length;
71
- if ((data.args.length <= 0 || !data.args[data.args.length - 1].rest) && nums.reality > nums.expected)
72
- return new commandError_1.CommandError({ type: 'arg_many', ...nums });
68
+ if (count.reality < count.expected)
69
+ return new commandError_1.CommandError({ type: 'arg_few', ...count });
70
+ count.expected = data.args.length;
71
+ if ((data.args.length <= 0 || !data.args[data.args.length - 1].rest) && count.reality > count.expected)
72
+ return new commandError_1.CommandError({ type: 'arg_many', ...count });
73
73
  let error;
74
74
  data.args.forEach((val, index) => {
75
75
  /* exit when happen error or last arg is empty */
@@ -1,7 +1,7 @@
1
1
  import { CommandResultExtra } from '../types';
2
- import { KotoriError } from './errror';
2
+ import { KotoriError } from './error';
3
3
  export declare class CommandError extends KotoriError {
4
- value: CommandResultExtra[keyof CommandResultExtra];
4
+ readonly value: CommandResultExtra[keyof CommandResultExtra];
5
5
  constructor(value: CommandResultExtra[keyof CommandResultExtra]);
6
6
  }
7
7
  export default CommandError;
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CommandError = void 0;
4
- const errror_1 = require("./errror");
5
- class CommandError extends errror_1.KotoriError {
4
+ const error_1 = require("./error");
5
+ class CommandError extends error_1.KotoriError {
6
6
  value;
7
7
  constructor(value) {
8
8
  super();
@@ -1,4 +1,4 @@
1
- import { Context } from '../context';
1
+ import { Context } from 'fluoro';
2
2
  export declare class Container {
3
3
  protected constructor();
4
4
  private static instance;
@@ -0,0 +1,25 @@
1
+ /// <reference types="node" />
2
+ type KotoriErrorType = 'ServiceError' | 'ModuleError' | 'UnknownError' | 'DevError';
3
+ interface KotoriErrorImpl {
4
+ readonly name: KotoriErrorType;
5
+ readonly extend: () => typeof KotoriError;
6
+ }
7
+ export declare class KotoriError<T extends object = object> extends Error implements KotoriErrorImpl {
8
+ constructor(message?: string, extra?: T, type?: KotoriErrorType);
9
+ readonly extra?: T;
10
+ readonly name: KotoriErrorType;
11
+ extend(): typeof KotoriError<T>;
12
+ }
13
+ export declare const ModuleError: {
14
+ new (message?: string, extra?: object | undefined, type?: KotoriErrorType): KotoriError<object>;
15
+ captureStackTrace(targetObject: object, constructorOpt?: Function | undefined): void;
16
+ prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
17
+ stackTraceLimit: number;
18
+ };
19
+ export declare const DevError: {
20
+ new (message?: string, extra?: object | undefined, type?: KotoriErrorType): KotoriError<object>;
21
+ captureStackTrace(targetObject: object, constructorOpt?: Function | undefined): void;
22
+ prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
23
+ stackTraceLimit: number;
24
+ };
25
+ export default KotoriError;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DevError = exports.ModuleError = exports.KotoriError = void 0;
4
+ class KotoriError extends Error {
5
+ constructor(message, extra, type = 'UnknownError') {
6
+ super(message);
7
+ this.name = type;
8
+ this.extra = extra;
9
+ }
10
+ extra;
11
+ name;
12
+ extend() {
13
+ const { message: fatherMessage, name: fatherType, extra: fatherExtra } = this;
14
+ // const newClass: typeof KotoriError = Object.create(KotoriError);
15
+ return new Proxy((KotoriError), {
16
+ construct(Class, params) {
17
+ const args = params;
18
+ args[0] = `${fatherMessage} ${args[0]}`;
19
+ args[1] = args[1] ?? fatherExtra;
20
+ args[2] = args[2] ?? fatherType;
21
+ return new Class(...args);
22
+ }
23
+ });
24
+ }
25
+ }
26
+ exports.KotoriError = KotoriError;
27
+ exports.ModuleError = new KotoriError(undefined, undefined, 'ModuleError').extend();
28
+ exports.DevError = new KotoriError(undefined, undefined, 'DevError').extend();
29
+ exports.default = KotoriError;
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  type KotoriErrorType = 'ServiceError' | 'ModuleError' | 'UnknownError' | 'DevError';
3
2
  interface KotoriErrorImpl {
4
3
  readonly name: KotoriErrorType;
@@ -1,7 +1,10 @@
1
- import type { Context } from '../context';
2
- export declare function disposeFactory(ctx: Context, dispose: Function): void;
1
+ import I18n from '@kotori-bot/i18n/src/common';
2
+ import type { Context } from 'fluoro';
3
+ import { CommandArgType } from '..';
4
+ export declare function disposeFactory(ctx: Context, dispose: () => void): void;
3
5
  export declare function cancelFactory(): {
4
6
  get(): () => void;
5
7
  fn(): void;
6
8
  value: boolean;
7
9
  };
10
+ export declare function formatFactory(i18n: I18n): (template: string, data: Record<string, unknown> | CommandArgType[]) => string;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.cancelFactory = exports.disposeFactory = void 0;
3
+ exports.formatFactory = exports.cancelFactory = exports.disposeFactory = void 0;
4
+ const __1 = require("..");
4
5
  function disposeFactory(ctx, dispose) {
5
6
  ctx.once('dispose_module', (data) => {
6
7
  if ((typeof data.instance === 'object' ? data.instance.name : data.instance) !== ctx.identity) {
@@ -23,3 +24,22 @@ function cancelFactory() {
23
24
  };
24
25
  }
25
26
  exports.cancelFactory = cancelFactory;
27
+ function formatFactory(i18n) {
28
+ return (template, data) => {
29
+ const params = data;
30
+ if (Array.isArray(params)) {
31
+ let str = i18n.locale(template);
32
+ params.forEach((value, index) => {
33
+ str = str.replaceAll(`{${index}}`, i18n.locale(typeof value === 'string' ? value : String(value)));
34
+ });
35
+ return str;
36
+ }
37
+ Object.keys(params).forEach((key) => {
38
+ if (typeof params[key] !== 'string')
39
+ params[key] = String(params[key]);
40
+ params[key] = i18n.locale(params[key]);
41
+ });
42
+ return (0, __1.stringTemp)(i18n.locale(template), params);
43
+ };
44
+ }
45
+ exports.formatFactory = formatFactory;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kotori-bot/core",
3
- "version": "1.3.0",
3
+ "version": "1.4.1",
4
4
  "description": "Kotori Core",
5
5
  "main": "lib/index.js",
6
6
  "license": "GPL-3.0",
@@ -27,7 +27,11 @@
27
27
  "@types/minimist": "^1.2.5",
28
28
  "minimist": "^1.2.8",
29
29
  "tsukiko": "^1.2.1",
30
- "@kotori-bot/tools": "^1.2.1",
31
- "@kotori-bot/i18n": "^1.2.1"
30
+ "@kotori-bot/i18n": "^1.3.0",
31
+ "@kotori-bot/tools": "^1.3.1",
32
+ "fluoro": "^1.0.1"
33
+ },
34
+ "scripts": {
35
+ "build": "tsc --build"
32
36
  }
33
37
  }