@kotori-bot/core 1.5.0 → 1.5.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.
@@ -1,12 +1,12 @@
1
1
 
2
2
  /**
3
3
  * @Package @kotori-bot/core
4
- * @Version 1.4.2-beta.1
4
+ * @Version 1.5.0
5
5
  * @Author Hotaru <biyuehuya@gmail.com>
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/6 21:03:53
9
+ * @Date 2024/6/7 11:22:22
10
10
  */
11
11
 
12
12
  "use strict";
@@ -28,7 +28,6 @@ var __copyProps = (to, from, except, desc) => {
28
28
  }
29
29
  return to;
30
30
  };
31
- var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
32
31
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
33
32
  // If the importer is in node compatibility mode or this is not an ESM
34
33
  // file that has been converted to a CommonJS file using a Babel-
@@ -38,739 +37,24 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
38
37
  mod
39
38
  ));
40
39
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
41
-
42
- // src/service/adapter.ts
43
40
  var adapter_exports = {};
44
41
  __export(adapter_exports, {
45
42
  Adapter: () => Adapter,
46
43
  default: () => adapter_default
47
44
  });
48
45
  module.exports = __toCommonJS(adapter_exports);
49
-
50
- // src/types/message.ts
51
- var import_tsukiko = __toESM(require("tsukiko"));
52
- var CommandAccess = /* @__PURE__ */ ((CommandAccess2) => {
53
- CommandAccess2[CommandAccess2["MEMBER"] = 0] = "MEMBER";
54
- CommandAccess2[CommandAccess2["MANGER"] = 1] = "MANGER";
55
- CommandAccess2[CommandAccess2["ADMIN"] = 2] = "ADMIN";
56
- return CommandAccess2;
57
- })(CommandAccess || {});
58
- var commandArgTypeSignSchema = import_tsukiko.default.Union([
59
- import_tsukiko.default.Union([import_tsukiko.default.Literal("string"), import_tsukiko.default.Literal("number")]),
60
- import_tsukiko.default.Literal("boolean")
61
- ]);
62
- var MessageScope = /* @__PURE__ */ ((MessageScope2) => {
63
- MessageScope2[MessageScope2["PRIVATE"] = 0] = "PRIVATE";
64
- MessageScope2[MessageScope2["GROUP"] = 1] = "GROUP";
65
- return MessageScope2;
66
- })(MessageScope || {});
67
- var eventDataTargetIdSchema = import_tsukiko.default.Union([import_tsukiko.default.Number(), import_tsukiko.default.String()]);
68
-
69
- // src/service/elements.ts
70
- var import_tools = require("@kotori-bot/tools");
71
- var Elements = class _Elements {
72
- default(...args) {
73
- (0, import_tools.none)(this, args);
74
- return "";
75
- }
76
- at(target, ...extra) {
77
- return this.default(target, extra);
78
- }
79
- image(url, ...extra) {
80
- return this.default(url, extra);
81
- }
82
- voice(url, ...extra) {
83
- return this.default(url, extra);
84
- }
85
- video(url, ...extra) {
86
- return this.default(url, extra);
87
- }
88
- face(id, ...extra) {
89
- return this.default(id, extra);
90
- }
91
- file(data, ...extra) {
92
- return this.default(data, extra);
93
- }
94
- supports() {
95
- const supports = [];
96
- const keys = ["at", "image", "voice", "video", "face", "file"];
97
- keys.forEach((key) => {
98
- if (this[key] !== new _Elements()[key]) supports.push(key);
99
- });
100
- return supports;
101
- }
102
- };
103
- var elements_default = Elements;
104
-
105
- // src/utils/factory.ts
106
- var import_tools8 = require("@kotori-bot/tools");
107
-
108
- // src/index.ts
109
- var src_exports = {};
110
- __export(src_exports, {
111
- ADAPTER_PREFIX: () => ADAPTER_PREFIX,
112
- Adapter: () => Adapter,
113
- Api: () => Api,
114
- CUSTOM_PREFIX: () => CUSTOM_PREFIX,
115
- Cache: () => Cache,
116
- Command: () => Command,
117
- CommandAccess: () => CommandAccess,
118
- CommandError: () => CommandError,
119
- Container: () => Container,
120
- Core: () => Core,
121
- DATABASE_PREFIX: () => DATABASE_PREFIX,
122
- DEFAULT_CORE_CONFIG: () => DEFAULT_CORE_CONFIG,
123
- DEFAULT_PORT: () => DEFAULT_PORT,
124
- DevError: () => DevError,
125
- Elements: () => Elements,
126
- KotoriError: () => KotoriError,
127
- MessageScope: () => MessageScope,
128
- ModuleError: () => ModuleError,
129
- OFFICIAL_MODULES_SCOPE: () => OFFICIAL_MODULES_SCOPE,
130
- PLUGIN_PREFIX: () => PLUGIN_PREFIX,
131
- Symbols: () => Symbols,
132
- cancelFactory: () => cancelFactory,
133
- commandArgTypeSignSchema: () => commandArgTypeSignSchema,
134
- confirmFactory: () => confirmFactory,
135
- disposeFactory: () => disposeFactory,
136
- eventDataTargetIdSchema: () => eventDataTargetIdSchema,
137
- formatFactory: () => formatFactory,
138
- promptFactory: () => promptFactory,
139
- quickFactory: () => quickFactory,
140
- sendMessageFactory: () => sendMessageFactory
141
- });
142
- __reExport(src_exports, require("fluoro"));
143
-
144
- // src/components/core.ts
145
- var import_tools6 = require("@kotori-bot/tools");
146
- var import_i18n2 = __toESM(require("@kotori-bot/i18n"));
147
- var import_fluoro2 = require("fluoro");
148
-
149
- // src/components/config.ts
150
- var import_tsukiko2 = __toESM(require("tsukiko"));
151
- var import_node_path = require("path");
152
- var import_tools2 = require("@kotori-bot/tools");
153
-
154
- // src/global/constants.ts
155
- var import_i18n = require("@kotori-bot/i18n");
156
- var OFFICIAL_MODULES_SCOPE = "@kotori-bot/";
157
- var PLUGIN_PREFIX = "kotori-plugin-";
158
- var DATABASE_PREFIX = `${PLUGIN_PREFIX}database-`;
159
- var ADAPTER_PREFIX = `${PLUGIN_PREFIX}adapter-`;
160
- var CUSTOM_PREFIX = `${PLUGIN_PREFIX}custom-`;
161
- var DEFAULT_PORT = 720;
162
- var DEFAULT_CORE_CONFIG = {
163
- global: {
164
- lang: import_i18n.DEFAULT_LANG,
165
- "command-prefix": "/",
166
- port: DEFAULT_PORT
167
- },
168
- adapter: {},
169
- plugin: {}
170
- };
171
-
172
- // src/global/symbols.ts
173
- var Symbols = class {
174
- static adapter = Symbol.for("kotori.core.adapter");
175
- static bot = Symbol.for("kotori.core.bot");
176
- static midware = Symbol.for("kotori.core.midware");
177
- static command = Symbol.for("kotori.core.command");
178
- static regexp = Symbol.for("kotori.core.regexp");
179
- static modules = Symbol.for("kotori.loader.module");
180
- static job = Symbol.for("kotori.loader.job");
181
- };
182
-
183
- // src/components/config.ts
184
- var packageInfoSchema = import_tsukiko2.default.Object({
185
- name: import_tsukiko2.default.String(),
186
- version: import_tsukiko2.default.String(),
187
- description: import_tsukiko2.default.String(),
188
- main: import_tsukiko2.default.String(),
189
- license: import_tsukiko2.default.Literal("GPL-3.0"),
190
- author: import_tsukiko2.default.String()
191
- });
192
- var Config = class {
193
- config;
194
- pkg;
195
- constructor(config = DEFAULT_CORE_CONFIG) {
196
- this.config = config;
197
- const info = (0, import_tools2.loadConfig)((0, import_node_path.resolve)(__dirname, "../../package.json"));
198
- if (!info || Object.values(info).length === 0) {
199
- process.stderr.write(`Cannot find kotori-bot package.json
200
- `);
201
- process.exit();
202
- }
203
- const result = packageInfoSchema.parseSafe(info);
204
- if (!result.value) {
205
- process.stderr.write(`File package.json format error: ${result.error.message}
206
- `);
207
- process.exit();
208
- }
209
- this.pkg = result.data;
210
- }
211
- };
212
- var config_default = Config;
213
-
214
- // src/components/message.ts
215
- var import_tools4 = require("@kotori-bot/tools");
216
- var import_cron = require("cron");
217
-
218
- // src/utils/command.ts
219
- var import_tools3 = require("@kotori-bot/tools");
220
- var import_minimist = __toESM(require("minimist"));
221
-
222
- // src/utils/error.ts
223
- var KotoriError = class _KotoriError extends Error {
224
- constructor(message, extra, type = "UnknownError") {
225
- super(message);
226
- this.name = type;
227
- this.extra = extra;
228
- }
229
- extra;
230
- name;
231
- extend() {
232
- const { message: fatherMessage, name: fatherType, extra: fatherExtra } = this;
233
- return new Proxy(_KotoriError, {
234
- construct(Class, params) {
235
- const args = params;
236
- args[0] = `${fatherMessage} ${args[0]}`;
237
- args[1] = args[1] ?? fatherExtra;
238
- args[2] = args[2] ?? fatherType;
239
- return new Class(...args);
240
- }
241
- });
242
- }
243
- };
244
- var ModuleError = new KotoriError(void 0, void 0, "ModuleError").extend();
245
- var DevError = new KotoriError(void 0, void 0, "DevError").extend();
246
-
247
- // src/utils/commandError.ts
248
- var CommandError = class extends KotoriError {
249
- value;
250
- constructor(value) {
251
- super();
252
- this.value = value;
253
- }
254
- };
255
- var commandError_default = CommandError;
256
-
257
- // src/utils/command.ts
258
- var requiredParamMatch = /<(\.\.\.)?(.*?)(:(.*?))?(=(.*?))?>/;
259
- var optionalParamMatch = /\[(\.\.\.)?(.*?)(:(.*?))?(=(.*?))?\]/;
260
- var defaultType = "string";
261
- function handleDefaultValue(value, type) {
262
- if (type === "boolean") return value !== "false" && !!value;
263
- if (type === "number") {
264
- if (typeof value === "number") return value;
265
- if (value === true) return 1;
266
- if (value === false) return 0;
267
- const float = parseFloat(value);
268
- const int = parseInt(value, 10);
269
- return float === int ? int : float;
270
- }
271
- return value.toString();
272
- }
273
- var Command = class {
274
- static run(input, data) {
275
- let starts = "";
276
- [data.root, ...data.alias].forEach((el) => {
277
- if (starts) return;
278
- if (input.startsWith(`${el} `) || input === el) starts = el;
279
- });
280
- if (!starts) return new CommandError({ type: "unknown", input });
281
- const opts = {
282
- string: [],
283
- boolean: [],
284
- alias: {}
285
- };
286
- data.options.forEach((option) => {
287
- if (option.type === "string") {
288
- opts.string.push(option.realname);
289
- } else if (option.type === "boolean") {
290
- opts.boolean.push(option.realname);
291
- }
292
- opts.alias[option.realname] = option.name;
293
- });
294
- const arr = (0, import_tools3.parseArgs)(input.slice(starts.length).trim());
295
- if (!Array.isArray(arr)) return new CommandError({ type: "syntax", ...arr });
296
- const result = (0, import_minimist.default)(arr, opts);
297
- const args = result._;
298
- const count = {
299
- expected: data.args.filter((el) => !el.optional).length,
300
- reality: args.length
301
- };
302
- if (count.reality < count.expected) return new CommandError({ type: "arg_few", ...count });
303
- count.expected = data.args.length;
304
- if ((data.args.length <= 0 || !data.args[data.args.length - 1].rest) && count.reality > count.expected)
305
- return new CommandError({ type: "arg_many", ...count });
306
- let error2;
307
- data.args.forEach((val, index) => {
308
- if (error2 || index > 0 && !args[index - 1]) return;
309
- if (!args[index] && val.default) {
310
- args[index] = val.default;
311
- return;
312
- }
313
- if (val.rest || !args[index]) return;
314
- args[index] = handleDefaultValue(args[index], val.type);
315
- if (!Number.isNaN(args[index])) return;
316
- error2 = new CommandError({ type: "arg_error", expected: "number", reality: "string", index });
317
- });
318
- if (error2) return error2;
319
- const options = {};
320
- data.options.forEach((val) => {
321
- if (!(val.realname in result)) return;
322
- options[val.realname] = Array.isArray(result[val.realname]) ? result[val.realname][0] : result[val.realname];
323
- options[val.realname] = handleDefaultValue(options[val.realname], val.type);
324
- if (Number.isNaN(options[val.realname]))
325
- error2 = new CommandError({ type: "option_error", expected: "number", reality: "string", target: val.realname });
326
- });
327
- if (error2) return error2;
328
- return {
329
- args: data.args.length > 0 && data.args[data.args.length - 1].rest ? args : args.slice(0, data.args.length),
330
- options
331
- };
332
- }
333
- template;
334
- meta = {
335
- root: "",
336
- alias: [],
337
- scope: "all",
338
- access: 0 /* MEMBER */,
339
- args: [],
340
- options: []
341
- };
342
- constructor(template, config) {
343
- this.template = template;
344
- this.meta = Object.assign(this.meta, config);
345
- this.parse();
346
- }
347
- parse() {
348
- const [str, description] = this.template.trim().split(" - ");
349
- this.meta.description = description;
350
- const requiredIndex = str.indexOf(" <");
351
- const optionalIndex = str.indexOf(" [");
352
- if (requiredIndex === -1 && optionalIndex === -1) {
353
- this.meta.root = str.trim();
354
- return;
355
- }
356
- if (requiredIndex !== -1 && (optionalIndex === -1 || requiredIndex < optionalIndex)) {
357
- this.meta.root = str.substring(0, requiredIndex).trim();
358
- } else {
359
- this.meta.root = str.substring(0, optionalIndex).trim();
360
- }
361
- const args = (0, import_minimist.default)(str.split(" "))._;
362
- args.forEach((arg) => {
363
- const current = this.meta.args[this.meta.args.length - 1];
364
- if (current && current.rest) return;
365
- let result = optionalParamMatch.exec(arg);
366
- if (result) {
367
- if (!result[2]) return;
368
- const type = commandArgTypeSignSchema.parseSafe(result[4]).value ? result[4] : defaultType;
369
- this.meta.args.push({
370
- name: result[2],
371
- type,
372
- rest: !!result[1],
373
- optional: true,
374
- default: result[6] ? handleDefaultValue(result[6], type) : void 0
375
- });
376
- }
377
- result = requiredParamMatch.exec(arg);
378
- if (!result || !result[2]) return;
379
- if (!result[6] && current && current.optional) return;
380
- this.meta.args.push({
381
- name: result[2],
382
- type: commandArgTypeSignSchema.parseSafe(result[4]).value ? result[4] : defaultType,
383
- rest: !!result[1],
384
- optional: false
385
- });
386
- });
387
- }
388
- alias(alias) {
389
- if (typeof alias === "string") this.meta.alias.push(alias);
390
- else this.meta.alias.push(...alias);
391
- return this;
392
- }
393
- scope(scope) {
394
- this.meta.scope = scope;
395
- return this;
396
- }
397
- access(access) {
398
- this.meta.access = access;
399
- return this;
400
- }
401
- option(name, template) {
402
- const [str, description] = template.trim().split(" ");
403
- const [realname, type] = str.split(":");
404
- this.meta.options.push({
405
- realname,
406
- description,
407
- type: commandArgTypeSignSchema.parseSafe(type).value ? type : defaultType,
408
- name: name.charAt(0)
409
- });
410
- return this;
411
- }
412
- action(callback) {
413
- this.meta.action = callback;
414
- return this;
415
- }
416
- help(text) {
417
- this.meta.help = text;
418
- return this;
419
- }
420
- };
421
-
422
- // src/components/message.ts
423
- var Message = class {
424
- [Symbols.midware] = /* @__PURE__ */ new Set();
425
- [Symbols.command] = /* @__PURE__ */ new Set();
426
- [Symbols.regexp] = /* @__PURE__ */ new Set();
427
- handleMidware(session) {
428
- const { api } = session;
429
- let isPass = true;
430
- let midwares = [];
431
- api.adapter.status.receivedMsg += 1;
432
- this[Symbols.midware].forEach((val) => midwares.push(val));
433
- midwares = midwares.sort((first, second) => first.priority - second.priority);
434
- let lastMidwareNum = -1;
435
- while (midwares.length > 0) {
436
- if (lastMidwareNum === midwares.length) {
437
- isPass = false;
438
- break;
439
- }
440
- lastMidwareNum = midwares.length;
441
- session.quick(midwares[0].callback(() => midwares.shift(), session));
442
- }
443
- this.ctx.emit("midwares", { isPass, session });
444
- }
445
- async handleRegexp(data) {
446
- const { session } = data;
447
- if (!data.isPass) return;
448
- this[Symbols.regexp].forEach((data2) => {
449
- const result = session.message.match(data2.match);
450
- if (!result) return;
451
- session.quick(data2.callback(result, session));
452
- this.ctx.emit("regexp", { result, session, regexp: data2.match, raw: session.message });
453
- });
454
- }
455
- handleCommand(data) {
456
- const { session } = data;
457
- const prefix = session.api.adapter.config["command-prefix"] ?? this.ctx.config.global["command-prefix"];
458
- if (!session.message.startsWith(prefix)) return;
459
- const params = {
460
- session,
461
- raw: (0, import_tools4.stringRightSplit)(session.message, prefix)
462
- };
463
- this.ctx.emit("before_parse", params);
464
- const cancel = cancelFactory();
465
- this.ctx.emit("before_command", { cancel: cancel.get(), ...params });
466
- if (cancel.value) return;
467
- let matched;
468
- this[Symbols.command].forEach(async (cmd) => {
469
- if (matched || !cmd.meta.action) return;
470
- const result = Command.run(params.raw, cmd.meta);
471
- if (result instanceof commandError_default && result.value.type === "unknown") return;
472
- matched = cmd;
473
- this.ctx.emit("parse", { command: cmd, result, ...params, cancel: cancel.get() });
474
- if (cancel.value || result instanceof commandError_default) return;
475
- try {
476
- const executed = await cmd.meta.action({ args: result.args, options: result.options }, session);
477
- if (executed instanceof commandError_default) {
478
- this.ctx.emit("command", { command: cmd, result: executed, ...params });
479
- return;
480
- }
481
- if (executed !== void 0) session.quick(executed);
482
- this.ctx.emit("command", {
483
- command: cmd,
484
- result: executed instanceof commandError_default ? result : executed,
485
- ...params
486
- });
487
- } catch (error2) {
488
- this.ctx.emit("command", {
489
- command: matched,
490
- result: new commandError_default({ type: "error", error: error2 }),
491
- ...params
492
- });
493
- }
494
- });
495
- if (matched) return;
496
- this.ctx.emit("parse", {
497
- command: new Command(""),
498
- result: new commandError_default({ type: "unknown", input: params.raw }),
499
- ...params,
500
- cancel: cancel.get()
501
- });
502
- }
503
- ctx;
504
- constructor(ctx) {
505
- this.ctx = ctx;
506
- this.ctx.on("on_message", (session) => this.handleMidware(session));
507
- this.ctx.on("midwares", (data) => this.handleCommand(data));
508
- this.ctx.on("midwares", (data) => this.handleRegexp(data));
509
- this.ctx.on("before_send", (data) => {
510
- const { api } = data;
511
- api.adapter.status.sentMsg += 1;
512
- });
513
- }
514
- midware(callback, priority = 100) {
515
- const data = { callback, priority };
516
- this[Symbols.midware].add(data);
517
- const dispose = () => this[Symbols.midware].delete(data);
518
- disposeFactory(this.ctx, dispose);
519
- return dispose;
520
- }
521
- command(template, config) {
522
- const command = new Command(template, config);
523
- this[Symbols.command].add(command);
524
- const dispose = () => this[Symbols.command].delete(command);
525
- disposeFactory(this.ctx, dispose);
526
- return command;
527
- }
528
- regexp(match, callback) {
529
- const data = { match, callback };
530
- this[Symbols.regexp].add(data);
531
- const dispose = () => this[Symbols.regexp].delete(data);
532
- disposeFactory(this.ctx, dispose);
533
- return dispose;
534
- }
535
- // boardcast(type: MessageScope, message: MessageRaw) {
536
- // const send =
537
- // type === 'private'
538
- // ? (api: Api) => api.send_on_message(message, 1)
539
- // : (api: Api) => api.send_on_message(message, 1);
540
- // /* this need support of database... */
541
- // Object.values(this.botStack).forEach((apis) => {
542
- // apis.forEach((api) => send(api));
543
- // });
544
- // }
545
- notify(message) {
546
- const mainAdapterIdentity = Object.keys(this.ctx.config.adapter)[0];
547
- this.ctx[Symbols.bot].forEach(
548
- (apis) => apis.forEach((api) => {
549
- if (api.adapter.identity !== mainAdapterIdentity) return;
550
- quickFactory(
551
- sendMessageFactory(api.adapter, "on_message", { userId: api.adapter.config.master }),
552
- this.ctx.i18n.extends(api.adapter.config.lang)
553
- )(message);
554
- })
555
- );
556
- }
557
- task(options, callback) {
558
- const [cron, extraOptions] = typeof options === "string" ? [options, {}] : [options.cron, options];
559
- return new import_cron.CronJob(cron, () => callback(this.ctx), null, extraOptions.start ?? true, extraOptions.timeZone);
560
- }
561
- };
562
- var message_default = Message;
563
-
564
- // src/service/api.ts
565
- var import_tools5 = require("@kotori-bot/tools");
566
- var Api = class {
567
- adapter;
568
- constructor(adapter) {
569
- this.adapter = adapter;
570
- }
571
- sendPrivateMsg(message, userId, ...extra) {
572
- (0, import_tools5.none)(this, message, userId, extra);
573
- }
574
- sendGroupMsg(message, groupId, ...extra) {
575
- (0, import_tools5.none)(this, message, groupId, extra, extra);
576
- }
577
- deleteMsg(messageId, ...extra) {
578
- (0, import_tools5.none)(this, messageId, extra);
579
- }
580
- setGroupName(groupId, groupName, ...extra) {
581
- (0, import_tools5.none)(this, groupId, groupName, extra);
582
- }
583
- setGroupAvatar(groupId, image, ...extra) {
584
- (0, import_tools5.none)(this, groupId, image, extra);
585
- }
586
- setGroupAdmin(groupId, userId, enable, ...extra) {
587
- (0, import_tools5.none)(this, groupId, userId, enable, extra);
588
- }
589
- setGroupCard(groupId, userId, card, ...extra) {
590
- (0, import_tools5.none)(this, groupId, userId, card, extra);
591
- }
592
- setGroupBan(groupId, userId, time, ...extra) {
593
- (0, import_tools5.none)(this, groupId, userId, time, extra);
594
- }
595
- sendGroupNotice(groupId, content, image, ...extra) {
596
- (0, import_tools5.none)(this, groupId, content, image, extra);
597
- }
598
- setGroupKick(groupId, userId, ...extra) {
599
- (0, import_tools5.none)(this, groupId, userId, extra);
600
- }
601
- setGroupLeave(groupId, ...extra) {
602
- (0, import_tools5.none)(this, groupId, groupId, extra);
603
- }
604
- };
605
-
606
- // src/service/cache.ts
607
- var import_fluoro = require("fluoro");
608
- var Cache = class extends import_fluoro.Service {
609
- cache;
610
- constructor(ctx) {
611
- super(ctx, {}, "cache");
612
- }
613
- start() {
614
- if (this.cache) return;
615
- this.cache = /* @__PURE__ */ new Map();
616
- }
617
- stop() {
618
- this.cache?.forEach((el) => el.clear());
619
- this.cache?.clear();
620
- delete this.cache;
621
- }
622
- getContainer() {
623
- const key = this.ctx.identity ?? "root";
624
- if (!this.cache.has(key)) this.cache.set(key, /* @__PURE__ */ new Map());
625
- return this.cache.get(key);
626
- }
627
- get(prop) {
628
- return this.getContainer().get(prop);
629
- }
630
- set(prop, value) {
631
- this.getContainer().set(prop, value);
632
- }
633
- };
634
-
635
- // src/components/core.ts
636
- var Core = class extends import_fluoro2.Context {
637
- [Symbols.adapter] = /* @__PURE__ */ new Map();
638
- [Symbols.bot] = /* @__PURE__ */ new Map();
639
- constructor(config) {
640
- super();
641
- this.provide("config", new config_default(config));
642
- this.mixin("config", ["config", "pkg"]);
643
- this.provide("message", new message_default(this));
644
- this.mixin("message", ["midware", "command", "regexp", "notify", "task"]);
645
- this.provide("http", new import_tools6.Http({ validateStatus: () => true }));
646
- this.inject("http");
647
- this.provide("i18n", new import_i18n2.default({ lang: this.config.global.lang }));
648
- this.inject("i18n");
649
- this.service("cache", new Cache(this.extends()));
650
- }
651
- };
652
-
653
- // src/utils/container.ts
654
- var import_tools7 = require("@kotori-bot/tools");
655
- var Container = class _Container {
656
- constructor() {
657
- (0, import_tools7.none)();
658
- }
659
- static instance = {};
660
- static setInstance(ctx) {
661
- this.instance = ctx;
662
- }
663
- static getInstance() {
664
- return this.instance;
665
- }
666
- static getMixin() {
667
- return Object.assign(
668
- _Container.getInstance()
669
- /* , Context */
670
- );
671
- }
672
- };
673
-
674
- // src/index.ts
675
- __reExport(src_exports, require("@kotori-bot/tools"));
676
- __reExport(src_exports, require("@kotori-bot/i18n"));
677
- __reExport(src_exports, require("tsukiko"));
678
-
679
- // src/utils/factory.ts
680
- function disposeFactory(ctx, dispose) {
681
- ctx.once("dispose_module", (data) => {
682
- if ((typeof data.instance === "object" ? data.instance.name : data.instance) !== ctx.identity) {
683
- disposeFactory(ctx, dispose);
684
- return;
685
- }
686
- dispose();
687
- });
688
- }
689
- function cancelFactory() {
690
- return {
691
- get() {
692
- return () => this.fn();
693
- },
694
- fn() {
695
- this.value = true;
696
- },
697
- value: false
698
- };
699
- }
700
- function formatFactory(i18n) {
701
- return (template, data) => {
702
- const params = data;
703
- if (Array.isArray(params)) {
704
- let str = i18n.locale(template);
705
- params.forEach((value, index) => {
706
- str = str.replaceAll(`{${index}}`, i18n.locale(typeof value === "string" ? value : String(value)));
707
- });
708
- return str;
709
- }
710
- Object.keys(params).forEach((key) => {
711
- if (typeof params[key] !== "string") params[key] = String(params[key]);
712
- params[key] = i18n.locale(params[key]);
713
- });
714
- return (0, import_tools8.stringTemp)(i18n.locale(template), params);
715
- };
716
- }
717
- function sendMessageFactory(adapter, type, data) {
718
- if ((data.type === 1 /* GROUP */ || type.includes("group")) && "groupId" in data) {
719
- return (message) => {
720
- adapter.api.sendGroupMsg(message, data.groupId, data.extra);
721
- };
722
- }
723
- return (message) => {
724
- adapter.api.sendPrivateMsg(message, data.userId, data.extra);
725
- };
726
- }
727
- function quickFactory(send, i18n) {
728
- return async (message) => {
729
- const msg = await message;
730
- if (!msg || msg instanceof CommandError) return;
731
- if (typeof msg === "string") {
732
- send(i18n.locale(msg));
733
- return;
734
- }
735
- send(formatFactory(i18n)(...msg));
736
- };
737
- }
738
- function isSameSender(adapter, data, session) {
739
- 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;
740
- }
741
- function promptFactory(quick, adapter, data) {
742
- return (message) => new Promise((resolve2) => {
743
- const handle = (session) => {
744
- if (isSameSender(adapter, data, session)) {
745
- resolve2(session.message);
746
- return;
747
- }
748
- adapter.ctx.once("on_message", handle);
749
- };
750
- quick(message ?? "corei18n.template.prompt").then(() => adapter.ctx.once("on_message", handle));
751
- });
752
- }
753
- function confirmFactory(quick, adapter, data) {
754
- return (options) => new Promise((resolve2) => {
755
- const handle = (session) => {
756
- if (isSameSender(adapter, data, session)) {
757
- resolve2(session.message === (options?.sure ?? "corei18n.template.confirm.sure"));
758
- return;
759
- }
760
- adapter.ctx.once("on_message", handle);
761
- };
762
- quick(options?.message ?? "corei18n.template.confirm").then(() => adapter.ctx.once("on_message", handle));
763
- });
764
- }
765
-
766
- // src/service/adapter.ts
46
+ var import_types = require("../types");
47
+ var import_elements = __toESM(require("./elements"));
48
+ var import_factory = require("../utils/factory");
49
+ var import_commandError = __toESM(require("../utils/commandError"));
50
+ var import_global = require("../global");
767
51
  function setProxy(api, ctx) {
768
52
  const proxy = Object.create(api);
769
53
  proxy.sendPrivateMsg = new Proxy(api.sendPrivateMsg, {
770
54
  apply(_, __, argArray) {
771
55
  const [message, targetId] = argArray;
772
- const cancel = cancelFactory();
773
- ctx.emit("before_send", { api, message, messageType: 0 /* PRIVATE */, targetId, cancel: cancel.get() });
56
+ const cancel = (0, import_factory.cancelFactory)();
57
+ ctx.emit("before_send", { api, message, messageType: import_types.MessageScope.PRIVATE, targetId, cancel: cancel.get() });
774
58
  if (cancel.value) return;
775
59
  api.sendPrivateMsg(message, targetId, argArray[2]);
776
60
  }
@@ -778,8 +62,8 @@ function setProxy(api, ctx) {
778
62
  proxy.sendGroupMsg = new Proxy(api.sendGroupMsg, {
779
63
  apply(_, __, argArray) {
780
64
  const [message, targetId] = argArray;
781
- const cancel = cancelFactory();
782
- ctx.emit("before_send", { api, message, messageType: 0 /* PRIVATE */, targetId, cancel: cancel.get() });
65
+ const cancel = (0, import_factory.cancelFactory)();
66
+ ctx.emit("before_send", { api, message, messageType: import_types.MessageScope.PRIVATE, targetId, cancel: cancel.get() });
783
67
  if (cancel.value) return;
784
68
  api.sendGroupMsg(message, targetId, argArray[2]);
785
69
  }
@@ -787,18 +71,18 @@ function setProxy(api, ctx) {
787
71
  return proxy;
788
72
  }
789
73
  function error(type, data) {
790
- return new commandError_default(Object.assign(data ?? {}, { type }));
74
+ return new import_commandError.default(Object.assign(data ?? {}, { type }));
791
75
  }
792
- var Adapter = class {
793
- constructor(ctx, config, identity, Api2, el = new elements_default()) {
76
+ class Adapter {
77
+ constructor(ctx, config, identity, Api, el = new import_elements.default()) {
794
78
  this.ctx = ctx;
795
79
  this.config = config;
796
80
  this.identity = identity;
797
81
  this.platform = config.extends;
798
- this.api = setProxy(new Api2(this), this.ctx);
82
+ this.api = setProxy(new Api(this), this.ctx);
799
83
  this.elements = el;
800
- if (!this.ctx[Symbols.bot].get(this.platform)) this.ctx[Symbols.bot].set(this.platform, /* @__PURE__ */ new Set());
801
- this.ctx[Symbols.bot].get(this.platform).add(this.api);
84
+ if (!this.ctx[import_global.Symbols.bot].get(this.platform)) this.ctx[import_global.Symbols.bot].set(this.platform, /* @__PURE__ */ new Set());
85
+ this.ctx[import_global.Symbols.bot].get(this.platform).add(this.api);
802
86
  }
803
87
  online() {
804
88
  if (this.status.value !== "offline") return;
@@ -813,11 +97,11 @@ var Adapter = class {
813
97
  }
814
98
  session(type, data) {
815
99
  const i18n = this.ctx.i18n.extends(this.config.lang);
816
- const send = sendMessageFactory(this, type, data);
817
- const format = formatFactory(i18n);
818
- const quick = quickFactory(send, i18n);
819
- const prompt = promptFactory(quick, this, data);
820
- const confirm = confirmFactory(quick, this, data);
100
+ const send = (0, import_factory.sendMessageFactory)(this, type, data);
101
+ const format = (0, import_factory.formatFactory)(i18n);
102
+ const quick = (0, import_factory.quickFactory)(send, i18n);
103
+ const prompt = (0, import_factory.promptFactory)(quick, this, data);
104
+ const confirm = (0, import_factory.confirmFactory)(quick, this, data);
821
105
  const { api, elements: el } = this;
822
106
  this.ctx.emit(
823
107
  type,
@@ -839,7 +123,7 @@ var Adapter = class {
839
123
  offlineTimes: 0
840
124
  };
841
125
  selfId = -1;
842
- };
126
+ }
843
127
  var adapter_default = Adapter;
844
128
  // Annotate the CommonJS export names for ESM import in node:
845
129
  0 && (module.exports = {