koishi-plugin-oni-wiki-qq 0.5.0 → 0.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.
package/lib/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Context, Schema } from "koishi";
2
2
  export declare const name = "oni-wiki-qq";
3
- export declare const usage = "\n - 0.5.0 \u79FB\u9664\u91CD\u5B9A\u5411\u529F\u80FD GG\u7AD9\u70B9\u5DF2\u4FEE\u590D\uFF0C\u4FDD\u9669\u8D77\u89C1\u4FDD\u7559bwiki\u66F4\u65B0\u529F\u80FD\n - 0.4.9 \u6DFB\u52A0\u91CD\u5B9A\u5411\u529F\u80FD\n - 0.4.8 \u91CD\u542Fbwiki\u66F4\u65B0\n - 0.4.6 \u79FB\u9664\u6CA1\u5FC5\u8981\u7684\u529F\u80FD\n - 0.4.5 \u68C0\u6D4B\u6559\u7A0B\u9875\u9762\n";
3
+ export declare const usage = "\n - 0.6.1 \u2705 \u6A21\u7CCA\u5339\u914D\u8FD4\u56DE\u6700\u591A5\u6761\u7ED3\u679C+\u5E8F\u53F7\u7B49\u5F85\u4EA4\u4E92\uFF0C\u8D85\u65F6\u65E0\u8F93\u5165\u5219\u9759\u9ED8\u7ED3\u675F\n - 0.6.0 \u96C6\u6210pinyin-pro\u62FC\u97F3\u6A21\u7CCA\u5339\u914D\uFF0C\u7CBE\u51C6\u5339\u914D\u4F18\u5148\n - 0.5.0 \u79FB\u9664\u91CD\u5B9A\u5411\u529F\u80FD GG\u7AD9\u70B9\u5DF2\u4FEE\u590D\uFF0C\u4FDD\u7559bwiki\u66F4\u65B0\u529F\u80FD\n - 0.4.9 \u6DFB\u52A0\u91CD\u5B9A\u5411\u529F\u80FD\n - 0.4.8 \u91CD\u542Fbwiki\u66F4\u65B0\n - 0.4.6 \u79FB\u9664\u6CA1\u5FC5\u8981\u7684\u529F\u80FD\n - 0.4.5 \u68C0\u6D4B\u6559\u7A0B\u9875\u9762\n";
4
4
  export declare const inject: string[];
5
5
  declare module "koishi" {
6
6
  interface Tables {
package/lib/index.js CHANGED
@@ -29,9 +29,12 @@ __export(src_exports, {
29
29
  module.exports = __toCommonJS(src_exports);
30
30
  var import_koishi = require("koishi");
31
31
  var import_mwn = require("mwn");
32
+ var import_pinyin_pro = require("pinyin-pro");
32
33
  var name = "oni-wiki-qq";
33
34
  var usage = `
34
- - 0.5.0 移除重定向功能 GG站点已修复,保险起见保留bwiki更新功能
35
+ - 0.6.1 模糊匹配返回最多5条结果+序号等待交互,超时无输入则静默结束
36
+ - 0.6.0 集成pinyin-pro拼音模糊匹配,精准匹配优先
37
+ - 0.5.0 移除重定向功能 GG站点已修复,保留bwiki更新功能
35
38
  - 0.4.9 添加重定向功能
36
39
  - 0.4.8 重启bwiki更新
37
40
  - 0.4.6 移除没必要的功能
@@ -41,10 +44,10 @@ var inject = ["database"];
41
44
  var Config = import_koishi.Schema.object({
42
45
  bot_username: import_koishi.Schema.string().description("机器人用户名"),
43
46
  bot_password: import_koishi.Schema.string().description("机器人密码"),
44
- bwiki_session: import_koishi.Schema.string().description("bwiki的session,无法连接到gg时使用")
47
+ bwiki_session: import_koishi.Schema.string().description("bwiki的session,无法连接到gg时使用")
45
48
  });
46
49
  function apply(ctx, config) {
47
- const logger = ctx.logger;
50
+ const logger = ctx.logger(name);
48
51
  let wikibot;
49
52
  ctx.model.extend("wikipages", {
50
53
  id: "integer",
@@ -57,70 +60,87 @@ function apply(ctx, config) {
57
60
  username: config.bot_username,
58
61
  password: config.bot_password,
59
62
  userAgent: "Charles`Bot/2.1",
60
- defaultParams: {
61
- assert: "user"
62
- }
63
- });
64
- wikibot.login().then(() => {
65
- logger.info("Wiki机器人登录成功");
66
- }).catch((err) => {
67
- logger.error("Wiki机器人登录失败", err);
63
+ defaultParams: { assert: "user" }
68
64
  });
65
+ wikibot.login().then(() => logger.info("Wiki机器人登录成功")).catch((err) => logger.error("Wiki机器人登录失败", err));
69
66
  });
70
- ctx.command("x <itemName>", "查询缺氧中文wiki").alias("/查wiki").action(async ({ session }, itemName = "电解器") => {
67
+ ctx.command("x <itemName>", "查询缺氧中文wiki,精准匹配+拼音模糊匹配+序号选择").alias("/查wiki").action(async ({ session }, itemName = "电解器") => {
71
68
  if (/教程/.test(itemName)) {
72
69
  return `请点击链接前往站点查看:
73
- 原站点: http://oni.wiki/${encodeURI(
74
- `教程`
75
- )}
70
+ 原站点: http://oni.wiki/${encodeURI(`教程`)}?variant=zh
76
71
  镜像站: http://klei.vip/oni/usiz6d/${encodeURI(`教程`)}`;
77
72
  }
78
- const res = await ctx.database.get("wikipages", {
79
- $or: [
80
- {
81
- title: itemName
82
- },
83
- {
84
- redirect: itemName
85
- }
86
- ]
73
+ const queryKey = itemName.trim();
74
+ const preciseRes = await ctx.database.get("wikipages", {
75
+ $or: [{ title: queryKey }, { redirect: queryKey }]
87
76
  });
88
- if (res.length == 0) {
89
- return `在Wiki里没找到,如有需要,请按照游戏内名称重新发起查询....`;
90
- }
91
- const pageName = res[0].title;
92
- return `请点击链接前往站点查看:
93
- 原站点: http://oni.wiki/${encodeURI(
94
- pageName
95
- )}
77
+ if (preciseRes.length > 0) {
78
+ const pageName = preciseRes[0].title;
79
+ return `✅ 精准匹配成功
80
+ 原站点: http://oni.wiki/${encodeURI(pageName)}?variant=zh
96
81
  镜像站: http://klei.vip/oni/usiz6d/${encodeURI(pageName)}`;
82
+ }
83
+ const allPages = await ctx.database.get("wikipages", {});
84
+ if (allPages.length === 0) {
85
+ return `❌ 本地缓存为空,请联系管理员执行【update】指令更新缓存!`;
86
+ }
87
+ const userPinyin = (0, import_pinyin_pro.pinyin)(queryKey, { toneType: "none", type: "string", separator: "" });
88
+ const userFirstLetter = (0, import_pinyin_pro.pinyin)(queryKey, { type: "string", separator: "" }).toLowerCase();
89
+ const matchResult = [];
90
+ allPages.forEach((page) => {
91
+ const targetTitle = page.title || "";
92
+ if (!targetTitle) return;
93
+ const titlePinyin = (0, import_pinyin_pro.pinyin)(targetTitle, { toneType: "none", type: "string", separator: "" });
94
+ const titleFirstLetter = (0, import_pinyin_pro.pinyin)(targetTitle, { type: "string", separator: "" }).toLowerCase();
95
+ let score = 0;
96
+ if (titlePinyin.includes(userPinyin) || userPinyin.includes(titlePinyin)) score += 5;
97
+ if (targetTitle.includes(queryKey)) score += 4;
98
+ if (titleFirstLetter.includes(userFirstLetter) || userFirstLetter.includes(titleFirstLetter)) score += 3;
99
+ if (score > 0) matchResult.push({ title: targetTitle, score });
100
+ });
101
+ if (matchResult.length === 0) {
102
+ return `❌ 未找到【${queryKey}】相关内容,请按游戏内标准名称重新查询!`;
103
+ }
104
+ const sortedResult = matchResult.sort((a, b) => b.score - a.score);
105
+ const uniqueResult = Array.from(new Map(sortedResult.map((item) => [item.title, item])).values()).slice(0, 5);
106
+ const resultCount = uniqueResult.length;
107
+ let replyMsg = `🔍 未找到精准匹配,为你找到【${resultCount}】个相似结果,请输入序号选择(10秒内有效):
108
+ `;
109
+ uniqueResult.forEach((item, index) => {
110
+ replyMsg += `${index + 1}. ${item.title}
111
+ `;
112
+ });
113
+ replyMsg += `
114
+ ❗️ 提示:超时将静默结束,无任何回应,没有有待选结果请艾特机器人任意内容结束本轮查询或等待超时后重新发起查询`;
115
+ await session.send(replyMsg);
116
+ const userInput = await session.prompt(1e4);
117
+ if (!userInput) return;
118
+ const selectNum = parseInt(userInput.trim());
119
+ if (isNaN(selectNum) || selectNum < 1 || selectNum > resultCount) {
120
+ return `❌ 输入无效!请输入 1-${resultCount} 之间的数字序号`;
121
+ }
122
+ const targetPage = uniqueResult[selectNum - 1].title;
123
+ return `✅ 选择成功
124
+ 原站点: http://oni.wiki/${encodeURI(targetPage)}?variant=zh
125
+ 镜像站: http://klei.vip/oni/usiz6d/${encodeURI(targetPage)}`;
97
126
  });
98
127
  ctx.command("update", "更新本地页面缓存", { authority: 2 }).action(async ({ session }) => {
99
- wikibot.request({
100
- action: "query",
101
- list: "allpages",
102
- format: "json",
103
- aplimit: "max"
104
- }).then((res) => {
128
+ wikibot.request({ action: "query", list: "allpages", format: "json", aplimit: "max" }).then((res) => {
105
129
  logger.info("查询成功");
106
130
  const pages = res.query.allpages;
107
131
  pages.forEach((page) => {
108
- ctx.database.upsert("wikipages", () => [
109
- { id: page.pageid, title: page.title }
110
- ]);
132
+ ctx.database.upsert("wikipages", () => [{ id: page.pageid, title: page.title }]);
111
133
  });
112
134
  session.send(`检索到 ${pages.length} 个页面,已尝试更新至数据库`);
113
135
  logger.info(`检索到 ${pages.length} 个页面,已尝试更新至数据库`);
114
- }).catch((err) => {
115
- logger.error("查询失败", err);
116
- });
136
+ }).catch((err) => logger.error("查询失败", err));
117
137
  });
118
- ctx.command("update.delete", " 删除本地页面缓存", { authority: 4 }).action(async ({ session }) => {
138
+ ctx.command("update.delete", "删除本地页面缓存", { authority: 4 }).action(async ({ session }) => {
119
139
  const count = await ctx.database.remove("wikipages", {});
120
140
  session.send(`已删除 ${count.removed} 条本地缓存`);
121
141
  logger.info(`已删除 ${count.removed} 条本地缓存`);
122
142
  });
123
- ctx.command("update.bwiki", "使用bwiki的session更新本地页面缓存", { authority: 2 }).action(async ({ session }) => {
143
+ ctx.command("update.bwiki", "使用bwiki的session更新缓存", { authority: 2 }).action(async ({ session }) => {
124
144
  const headers = {
125
145
  "Content-Type": "application/json",
126
146
  "user-agent": "Charles'queryBot",
@@ -129,9 +149,7 @@ function apply(ctx, config) {
129
149
  const url = `https://wiki.biligame.com/oni/api.php?action=query&list=allpages&apnamespace=0&aplimit=5000&format=json`;
130
150
  ctx.http.get(url, { headers }).then((res) => {
131
151
  res["query"]["allpages"].forEach((page) => {
132
- ctx.database.upsert("wikipages", () => [
133
- { id: page.pageid, title: page.title }
134
- ]);
152
+ ctx.database.upsert("wikipages", () => [{ id: page.pageid, title: page.title }]);
135
153
  });
136
154
  session.send(`检索到 ${res["query"]["allpages"].length} 个页面,已尝试更新至数据库`);
137
155
  logger.info(`检索到 ${res["query"]["allpages"].length} 个页面,已尝试更新至数据库`);
@@ -140,7 +158,7 @@ function apply(ctx, config) {
140
158
  logger.error("更新失败", err);
141
159
  });
142
160
  });
143
- ctx.command("update.status", "查询本地页面缓存数量", { authority: 1 }).action(async ({ session }) => {
161
+ ctx.command("update.status", "查询本地缓存数量", { authority: 1 }).action(async ({ session }) => {
144
162
  const count = await ctx.database.get("wikipages", {});
145
163
  session.send(`数据库中缓存了 ${count.length} 条页面`);
146
164
  logger.info(`数据库中缓存了 ${count.length} 条页面`);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-oni-wiki-qq",
3
3
  "description": "缺氧wiki查询,自用",
4
- "version": "0.5.0",
4
+ "version": "0.6.0",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
@@ -15,9 +15,10 @@
15
15
  "plugin"
16
16
  ],
17
17
  "peerDependencies": {
18
- "koishi": "^4.18.9"
18
+ "koishi": "^4.18.10"
19
19
  },
20
20
  "dependencies": {
21
- "mwn": "^3.0.1"
21
+ "mwn": "^3.0.1",
22
+ "pinyin-pro": "^3.27.0"
22
23
  }
23
- }
24
+ }