mioki 0.7.6 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -84,16 +84,22 @@ declare function ensureBuffer(buffer?: Buffer | null | undefined, text?: string)
84
84
  * @returns 可读字符串
85
85
  */
86
86
  declare function formatDuration(ms: number): string;
87
- type MatchPatternItem = null | undefined | void | false | Sendable;
87
+ type MatchPatternItem = null | undefined | void | false | Sendable | Sendable[];
88
+ type MatchValue<E extends MessageEvent> = MatchPatternItem | ((matches: RegExpMatchArray, event: E) => MatchPatternItem) | ((matches: RegExpMatchArray, event: E) => Promise<MatchPatternItem>);
88
89
  /**
89
90
  * 匹配输入文本与匹配模式,如果匹配成功,则回复匹配结果
90
91
  *
92
+ * 支持:
93
+ * - 精确匹配
94
+ * - 正则表达式匹配(以 `/` 开头和结尾的字符串)
95
+ * - 通配符匹配(使用 `*` 作为通配符)
96
+ *
91
97
  * @param event 消息事件
92
98
  * @param pattern 匹配模式
93
99
  * @param quote 是否引用回复
94
100
  * @returns 匹配结果
95
101
  */
96
- declare function match(event: MessageEvent, pattern: Record<string, MatchPatternItem | (() => MatchPatternItem) | (() => Promise<MatchPatternItem>)>, quote?: boolean): Promise<{
102
+ declare function match<E extends MessageEvent>(event: E, pattern: Record<string, MatchValue<E>>, quote?: boolean): Promise<{
97
103
  message_id: number;
98
104
  } | null>;
99
105
  /**
package/dist/index.d.mts CHANGED
@@ -82,16 +82,22 @@ declare function ensureBuffer(buffer?: Buffer | null | undefined, text?: string)
82
82
  * @returns 可读字符串
83
83
  */
84
84
  declare function formatDuration(ms: number): string;
85
- type MatchPatternItem = null | undefined | void | false | Sendable;
85
+ type MatchPatternItem = null | undefined | void | false | Sendable | Sendable[];
86
+ type MatchValue<E extends MessageEvent> = MatchPatternItem | ((matches: RegExpMatchArray, event: E) => MatchPatternItem) | ((matches: RegExpMatchArray, event: E) => Promise<MatchPatternItem>);
86
87
  /**
87
88
  * 匹配输入文本与匹配模式,如果匹配成功,则回复匹配结果
88
89
  *
90
+ * 支持:
91
+ * - 精确匹配
92
+ * - 正则表达式匹配(以 `/` 开头和结尾的字符串)
93
+ * - 通配符匹配(使用 `*` 作为通配符)
94
+ *
89
95
  * @param event 消息事件
90
96
  * @param pattern 匹配模式
91
97
  * @param quote 是否引用回复
92
98
  * @returns 匹配结果
93
99
  */
94
- declare function match(event: MessageEvent, pattern: Record<string, MatchPatternItem | (() => MatchPatternItem) | (() => Promise<MatchPatternItem>)>, quote?: boolean): Promise<{
100
+ declare function match<E extends MessageEvent>(event: E, pattern: Record<string, MatchValue<E>>, quote?: boolean): Promise<{
95
101
  message_id: number;
96
102
  } | null>;
97
103
  /**
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { t as __export } from "./chunk-DYZpOiH0.mjs";
2
- import { t as version } from "./package-lxAr8UyG.mjs";
2
+ import { t as version } from "./package-BbEBqdaV.mjs";
3
3
  import fs, { default as fs$1 } from "node:fs";
4
4
  import util from "node:util";
5
5
  import path, { default as path$1 } from "node:path";
@@ -193,6 +193,11 @@ function formatDuration(ms) {
193
193
  /**
194
194
  * 匹配输入文本与匹配模式,如果匹配成功,则回复匹配结果
195
195
  *
196
+ * 支持:
197
+ * - 精确匹配
198
+ * - 正则表达式匹配(以 `/` 开头和结尾的字符串)
199
+ * - 通配符匹配(使用 `*` 作为通配符)
200
+ *
196
201
  * @param event 消息事件
197
202
  * @param pattern 匹配模式
198
203
  * @param quote 是否引用回复
@@ -200,9 +205,35 @@ function formatDuration(ms) {
200
205
  */
201
206
  async function match(event, pattern, quote = true) {
202
207
  const inputText = text(event);
203
- for (const [key, value] of Object.entries(pattern)) if (key === inputText) {
204
- const res = await (typeof value === "function" ? value() : value);
205
- if (res) return event.reply(res, quote);
208
+ async function handleMatch(key, value) {
209
+ let isMatched = false;
210
+ let matches = null;
211
+ const isRegExpLikeString = key.match(/^\/.+\/$/);
212
+ const hasWildcard = key.includes("*");
213
+ if (isRegExpLikeString) try {
214
+ const regex = new RegExp(key.slice(1, -1));
215
+ const matchesValue = inputText.match(regex);
216
+ if (matchesValue) {
217
+ isMatched = true;
218
+ matches = matchesValue;
219
+ }
220
+ } catch (err) {
221
+ throw new Error(`无效的正则表达式: ${key}`, { cause: err });
222
+ }
223
+ else if (hasWildcard) {
224
+ const regexPattern = `^${key.replace(/\./g, "\\.").replace(/\*/g, ".*")}$`;
225
+ const regex = new RegExp(regexPattern);
226
+ const matchesValue = inputText.match(regex);
227
+ if (matchesValue) {
228
+ isMatched = true;
229
+ matches = matchesValue;
230
+ }
231
+ } else if (key === inputText) isMatched = true;
232
+ if (isMatched) return typeof value === "function" ? await value(matches, event) : value;
233
+ }
234
+ for (const [key, value] of Object.entries(pattern)) {
235
+ const result = await handleMatch(key, value);
236
+ if (result) return event.reply(result, quote);
206
237
  }
207
238
  return null;
208
239
  }
@@ -1815,7 +1846,7 @@ async function start(options = {}) {
1815
1846
  const failedCount = failedImportPlugins.length + failedEnablePlugins.length;
1816
1847
  const failedInfo = failedCount > 0 ? `${colors$1.red(failedCount)} 个失败 (导入 ${colors$1.red(failedImportPlugins.length)},启用 ${colors$1.red(failedEnablePlugins.length)})` : "";
1817
1848
  napcat.logger.info(`成功加载了 ${colors$1.green(runtimePlugins.size)} 个插件,${failedInfo ? failedInfo : ""}总耗时 ${colors$1.green(costTime.toFixed(2))} 毫秒`);
1818
- napcat.logger.info(colors$1.green(`mioki v${version} 启动完成,祝您使用愉快 🎉️`));
1849
+ napcat.logger.info(colors$1.green(`mioki v${version} 启动完成,向机器人发送「${colors$1.magentaBright(`${botConfig.prefix}帮助`)}」查看消息指令`));
1819
1850
  if (botConfig.online_push) await noticeMainOwner(napcat, `✅ mioki v${version} 已就绪`).catch((err) => {
1820
1851
  napcat.logger.error(`发送就绪通知失败: ${stringifyError(err)}`);
1821
1852
  });