koishi-plugin-ciyi 1.2.6 → 1.2.7

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/lib/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { Context, Schema } from "koishi";
1
+ import { Context, Schema, Session } from "koishi";
2
2
  import { History } from "./view";
3
3
  export declare const name = "ciyi";
4
- export declare const usage = "## \u4F7F\u7528\n\n\u8BBE\u7F6E\u6307\u4EE4\u522B\u540D\u540E\uFF0C\u53D1\u9001 `ciyi` \u67E5\u770B\u73A9\u6CD5\u3002\u6BCF\u65E5\u85CF\u4E00\u4E2A\u4E24\u5B57\u8BCD\uFF0C\u7B2C\u4E00\u6B21\u731C\u6D4B\u4F1A\u81EA\u52A8\u5F00\u9898\u3002\n\n## \u6307\u4EE4\n\n| \u6307\u4EE4 | \u8BF4\u660E |\n| --- | --- |\n| `ciyi` | \u73A9\u6CD5\u8BF4\u660E |\n| `ciyi.\u731C <\u8BCD>` | \u5F00\u59CB\u4ECA\u65E5\u6E38\u620F\u5E76\u63D0\u4EA4\u731C\u6D4B |\n| `ciyi.\u6392\u884C\u699C` | \u731C\u4E2D\u6B21\u6570\u699C |";
4
+ export declare const usage = "## \u4F7F\u7528\n\n\u8BBE\u7F6E\u6307\u4EE4\u522B\u540D\u540E\uFF0C\u53D1\u9001 `ciyi` \u67E5\u770B\u73A9\u6CD5\u3002\u6BCF\u65E5\u85CF\u4E00\u4E2A\u4E24\u5B57\u8BCD\uFF0C\u4F7F\u7528 `ciyi.\u731C <\u8BCD>` \u63D0\u4EA4\u7B2C\u4E00\u6B21\u731C\u6D4B\u5E76\u5F00\u9898\uFF1B\u5F00\u9898\u540E\u53EF\u76F4\u63A5\u53D1\u9001\u4E24\u5B57\u8BCD\u7EE7\u7EED\u731C\u6D4B\u3002\n\n## \u6307\u4EE4\n\n| \u6307\u4EE4 | \u8BF4\u660E |\n| --- | --- |\n| `ciyi` | \u73A9\u6CD5\u8BF4\u660E |\n| `ciyi.\u731C <\u8BCD>` | \u5F00\u59CB\u4ECA\u65E5\u6E38\u620F\u5E76\u63D0\u4EA4\u731C\u6D4B |\n| `ciyi.\u6392\u884C\u699C` | \u731C\u4E2D\u6B21\u6570\u699C |";
5
5
  export declare const inject: {
6
6
  required: string[];
7
7
  optional: string[];
@@ -39,4 +39,11 @@ export interface CiyiRank {
39
39
  score: number;
40
40
  }
41
41
  export type { History };
42
+ /**
43
+ * 裸词中间件只接受一条完整、无修饰的两字中文纯文本消息。
44
+ * 引用、@、图片、富文本以及机器人消息都可能只是普通聊天的一部分,不能据此猜词。
45
+ */
46
+ export declare function getMiddlewareGuess(session: Session): string | null;
47
+ /** 裸词只能加入已开始且未封题的游戏,不能开题,也不重复提交旧猜测。 */
48
+ export declare function canHandleMiddlewareGuess(game: Ciyi | null | undefined, guess: string): boolean;
42
49
  export declare function apply(ctx: Context, cfg: Config): void;
package/lib/index.js CHANGED
@@ -22,6 +22,8 @@ var index_exports = {};
22
22
  __export(index_exports, {
23
23
  Config: () => Config,
24
24
  apply: () => apply,
25
+ canHandleMiddlewareGuess: () => canHandleMiddlewareGuess,
26
+ getMiddlewareGuess: () => getMiddlewareGuess,
25
27
  inject: () => inject,
26
28
  name: () => name,
27
29
  usage: () => usage
@@ -578,7 +580,7 @@ function renderIntroCard(service, opts) {
578
580
  textLeft(ctx, "ciyi.猜 山水", ix + s(96), y + s(24), `${s(13.5)}px ${FONT_NUM}`, C.ink);
579
581
  textLeftFit(
580
582
  ctx,
581
- `开题并报一个两字词${opts.middleware ? ",也可以直接把词发出来" : ""}`,
583
+ `开题并报一个两字词${opts.middleware ? ";开题后可直接发词" : ""}`,
582
584
  ix + s(230),
583
585
  y + s(24),
584
586
  iw - s(268),
@@ -860,7 +862,7 @@ __name(renderRankCard, "renderRankCard");
860
862
  var name = "ciyi";
861
863
  var usage = `## 使用
862
864
 
863
- 设置指令别名后,发送 \`ciyi\` 查看玩法。每日藏一个两字词,第一次猜测会自动开题。
865
+ 设置指令别名后,发送 \`ciyi\` 查看玩法。每日藏一个两字词,使用 \`ciyi.猜 <词>\` 提交第一次猜测并开题;开题后可直接发送两字词继续猜测。
864
866
 
865
867
  ## 指令
866
868
 
@@ -873,11 +875,33 @@ var inject = { required: ["database"], optional: ["canvas"] };
873
875
  var Config = import_koishi.Schema.object({
874
876
  atReply: import_koishi.Schema.boolean().default(false).description("响应时 @"),
875
877
  quoteReply: import_koishi.Schema.boolean().default(false).description("响应时引用"),
876
- isEnableMiddleware: import_koishi.Schema.boolean().default(true).description("是否启用中间件(若启用,猜测词语时可以不使用指令直接猜测)"),
878
+ isEnableMiddleware: import_koishi.Schema.boolean().default(true).description("是否启用中间件(若启用,已开题时可以不使用指令直接猜测)"),
877
879
  renderImage: import_koishi.Schema.boolean().default(true).description("渲染图片(复用 Koishi Canvas 服务;不可用时使用等价文本)"),
878
880
  maxHistory: import_koishi.Schema.number().default(10).min(0).description("猜测板最多列出的历史条数(最新一次猜测始终会列出)"),
879
881
  maxRank: import_koishi.Schema.number().default(10).min(0).description("最大排行榜人数")
880
882
  });
883
+ function getMiddlewareGuess(session) {
884
+ const message = session.event.message;
885
+ if (!message || message.quote || session.event.user?.isBot || message.user?.isBot) {
886
+ return null;
887
+ }
888
+ const elements = message.elements ?? [];
889
+ if (elements.length !== 1 || elements[0].type !== "text") {
890
+ return null;
891
+ }
892
+ const text = elements[0].attrs.content;
893
+ if (typeof text !== "string" || text !== text.trim() || Array.from(text).length !== 2 || !/^\p{Script=Han}{2}$/u.test(text) || !allWords_default.includes(text)) {
894
+ return null;
895
+ }
896
+ return text;
897
+ }
898
+ __name(getMiddlewareGuess, "getMiddlewareGuess");
899
+ function canHandleMiddlewareGuess(game, guess) {
900
+ if (!game || game.isOver) return false;
901
+ if (game.guessedHistoryInOneGame?.includes(guess)) return false;
902
+ return guess === game.answer || game.rankList?.includes(guess) === true;
903
+ }
904
+ __name(canHandleMiddlewareGuess, "canHandleMiddlewareGuess");
881
905
  function apply(ctx, cfg) {
882
906
  ctx.model.extend(
883
907
  "ciyi",
@@ -909,14 +933,21 @@ function apply(ctx, cfg) {
909
933
  const RULE = "────────────";
910
934
  if (cfg.isEnableMiddleware) {
911
935
  ctx.middleware(async (session, next) => {
912
- const text = `${import_koishi.h.select(session.event.message.elements, "text")}`;
913
- if (text.length !== 2) {
914
- return await next();
915
- }
916
- if (!allWords_default.includes(text)) {
936
+ const guess = getMiddlewareGuess(session);
937
+ if (!guess || !session.channelId) return await next();
938
+ try {
939
+ const [game] = await ctx.database.get("ciyi", {
940
+ channelId: session.channelId
941
+ });
942
+ if (!canHandleMiddlewareGuess(game, guess)) return await next();
943
+ } catch (error) {
944
+ logger.warn(
945
+ "中间件读取游戏状态失败,本次消息不作猜测:%s",
946
+ error?.message ?? error
947
+ );
917
948
  return await next();
918
949
  }
919
- return await session.execute(`ciyi.猜 ${text}`);
950
+ return await session.execute(`ciyi.猜 ${guess}`);
920
951
  });
921
952
  }
922
953
  ctx.command("ciyi", "词意(猜词游戏)").action(async ({ session }) => {
@@ -1018,9 +1049,10 @@ function apply(ctx, cfg) {
1018
1049
  "词意 · 按意思远近找词",
1019
1050
  `每天藏起一个两字词。你报词,我回它与答案的语义排名 —— 名次越小越近,#1 就是答案本身。`,
1020
1051
  [
1021
- `开始 ciyi.猜 山水  开题并报一个两字词${cfg.isEnableMiddleware ? "(也可直接发词)" : ""}`,
1052
+ `开始 ciyi.猜 山水  开题并报一个两字词`,
1053
+ cfg.isEnableMiddleware ? `续猜 直接发送两字词 仅限已开题且未结束` : null,
1022
1054
  `排行 ciyi.排行榜  看谁猜中得最多`
1023
- ],
1055
+ ].filter(Boolean),
1024
1056
  [
1025
1057
  `读板 ?好)企业(地? #467 · 沾边`,
1026
1058
  `   #467 是「企业」与答案的相近名次`,
@@ -1286,6 +1318,8 @@ __name(apply, "apply");
1286
1318
  0 && (module.exports = {
1287
1319
  Config,
1288
1320
  apply,
1321
+ canHandleMiddlewareGuess,
1322
+ getMiddlewareGuess,
1289
1323
  inject,
1290
1324
  name,
1291
1325
  usage
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-ciyi",
3
- "description": "Koishi 的词意(猜词游戏)插件。根据词语的含义相似程度,猜测正确的词语。",
4
- "version": "1.2.6",
3
+ "description": "Koishi 的词意猜词插件。",
4
+ "version": "1.2.7",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
@@ -41,7 +41,12 @@
41
41
  "词意"
42
42
  ],
43
43
  "devDependencies": {
44
- "@koishijs/canvas": "^0.2.0"
44
+ "@koishijs/canvas": "^0.2.0",
45
+ "koishi": "^4.18.11",
46
+ "tsx": "^4.23.13"
47
+ },
48
+ "scripts": {
49
+ "test": "tsx --test test/*.test.ts"
45
50
  },
46
51
  "peerDependencies": {
47
52
  "koishi": "^4.18.11"
package/readme.md CHANGED
@@ -8,7 +8,7 @@ Koishi 的词意猜词插件。
8
8
 
9
9
  ## 使用
10
10
 
11
- 设置指令别名后,发送 `ciyi` 查看玩法。每日藏一个两字词,第一次猜测会自动开题。
11
+ 设置指令别名后,发送 `ciyi` 查看玩法。每日藏一个两字词,使用 `ciyi.猜 <词>` 提交第一次猜测并开题;开题后可直接发送两字词继续猜测。
12
12
 
13
13
  ## 指令
14
14