koishi-plugin-oni-wiki-qq 0.5.1 → 0.7.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.1 \u539F\u7AD9\u70B9\u91CD\u5B9A\u5411\u81F3\u7B80\u4F53\u4E2D\u6587\u7AD9\u70B9\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.7.0 \u5B9E\u73B0\u77ED\u94FE\u8DEF\u7531\u8F6C\u53D1\uFF0C\u94FE\u63A5\u6539\u4E3Aklei.vip/ggwiki\u6216\u8005bwiki+\u9875\u9762ID\n - 0.6.1 \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 {
@@ -10,12 +10,12 @@ declare module "koishi" {
10
10
  export interface WikiPages {
11
11
  id: number;
12
12
  title: string;
13
- redirect: string;
14
13
  }
15
14
  export interface Config {
16
15
  bot_username: string;
17
16
  bot_password: string;
18
17
  bwiki_session: string;
18
+ domain: string;
19
19
  }
20
20
  export declare const Config: Schema<Config>;
21
21
  export declare function apply(ctx: Context, config: Config): void;
package/lib/index.js CHANGED
@@ -29,28 +29,55 @@ __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.1 原站点重定向至简体中文站点
35
- - 0.5.0 移除重定向功能 GG站点已修复,保险起见保留bwiki更新功能
35
+ - 0.7.0 实现短链路由转发,链接改为klei.vip/ggwiki或者bwiki+页面ID
36
+ - 0.6.1 模糊匹配返回最多5条结果+序号等待交互,超时无输入则静默结束
37
+ - 0.6.0 集成pinyin-pro拼音模糊匹配,精准匹配优先
38
+ - 0.5.0 移除重定向功能 GG站点已修复,保留bwiki更新功能
36
39
  - 0.4.9 添加重定向功能
37
40
  - 0.4.8 重启bwiki更新
38
41
  - 0.4.6 移除没必要的功能
39
42
  - 0.4.5 检测教程页面
40
43
  `;
41
- var inject = ["database"];
44
+ var inject = ["database", "server"];
42
45
  var Config = import_koishi.Schema.object({
43
46
  bot_username: import_koishi.Schema.string().description("机器人用户名"),
44
47
  bot_password: import_koishi.Schema.string().description("机器人密码"),
45
- bwiki_session: import_koishi.Schema.string().description("bwiki的session,无法连接到gg时使用")
48
+ bwiki_session: import_koishi.Schema.string().description(
49
+ "bwiki的session,无法连接到gg时使用"
50
+ ),
51
+ domain: import_koishi.Schema.string().description("你的短链域名(必填,如:klei.vip)").default("klei.vip")
46
52
  });
47
53
  function apply(ctx, config) {
48
- const logger = ctx.logger;
54
+ const logger = ctx.logger(name);
49
55
  let wikibot;
50
56
  ctx.model.extend("wikipages", {
51
57
  id: "integer",
52
- title: "string",
53
- redirect: "string"
58
+ title: "string"
59
+ });
60
+ ctx.server.get("/ggwiki/:id", async (ctx2) => {
61
+ const pageId = Number(ctx2.params.id);
62
+ if (isNaN(pageId)) return ctx2.body = "❌ 无效的页面ID,必须为数字!";
63
+ const [page] = await ctx2.database.get("wikipages", { id: pageId });
64
+ if (!page)
65
+ return ctx2.body = `❌ 未找到ID为【${pageId}】的页面,请联系管理员更新缓存!`;
66
+ const targetUrl = `https://oni.wiki/${encodeURIComponent(
67
+ page.title
68
+ )}?variant=zh`;
69
+ ctx2.redirect(targetUrl);
70
+ });
71
+ ctx.server.get("/bwiki/:id", async (ctx2) => {
72
+ const pageId = Number(ctx2.params.id);
73
+ if (isNaN(pageId)) return ctx2.body = "❌ 无效的页面ID,必须为数字!";
74
+ const [page] = await ctx2.database.get("wikipages", { id: pageId });
75
+ if (!page)
76
+ return ctx2.body = `❌ 未找到ID为【${pageId}】的页面,请联系管理员更新缓存!`;
77
+ const targetUrl = `https://wiki.biligame.com/oni/${encodeURIComponent(
78
+ page.title
79
+ )}`;
80
+ ctx2.redirect(targetUrl);
54
81
  });
55
82
  ctx.on("ready", async () => {
56
83
  wikibot = new import_mwn.Mwn({
@@ -58,43 +85,83 @@ function apply(ctx, config) {
58
85
  username: config.bot_username,
59
86
  password: config.bot_password,
60
87
  userAgent: "Charles`Bot/2.1",
61
- defaultParams: {
62
- assert: "user"
63
- }
64
- });
65
- wikibot.login().then(() => {
66
- logger.info("Wiki机器人登录成功");
67
- }).catch((err) => {
68
- logger.error("Wiki机器人登录失败", err);
88
+ defaultParams: { assert: "user" }
69
89
  });
90
+ wikibot.login().then(() => logger.info("Wiki机器人登录成功")).catch((err) => logger.error("Wiki机器人登录失败", err));
70
91
  });
71
- ctx.command("x <itemName>", "查询缺氧中文wiki").alias("/查wiki").action(async ({ session }, itemName = "电解器") => {
72
- if (/教程/.test(itemName)) {
73
- return `请点击链接前往站点查看:
74
- 原站点: http://oni.wiki/${encodeURI(
75
- `教程`
76
- )}?variant=zh
77
- 镜像站: http://klei.vip/oni/usiz6d/${encodeURI(`教程`)}`;
92
+ ctx.command("x <itemName>", "查询缺氧中文wiki,精准匹配+拼音模糊匹配+序号选择").alias("/查wiki").action(async ({ session }, itemName = "电解器") => {
93
+ const queryKey = itemName.trim();
94
+ const preciseRes = await ctx.database.get("wikipages", {
95
+ title: queryKey
96
+ });
97
+ if (preciseRes.length > 0) {
98
+ const { id: id2 } = preciseRes[0];
99
+ return `✅ 精准匹配成功
100
+ 原站点: http://${config.domain}/ggwiki/${id2}
101
+ 镜像站: http://${config.domain}/bwiki/${id2}`;
102
+ }
103
+ const allPages = await ctx.database.get("wikipages", {});
104
+ if (allPages.length === 0) {
105
+ return `❌ 本地缓存为空,请联系管理员执行【update】指令更新缓存!`;
106
+ }
107
+ const userPinyin = (0, import_pinyin_pro.pinyin)(queryKey, {
108
+ toneType: "none",
109
+ type: "string",
110
+ separator: ""
111
+ });
112
+ const userFirstLetter = (0, import_pinyin_pro.pinyin)(queryKey, {
113
+ type: "string",
114
+ separator: ""
115
+ }).toLowerCase();
116
+ const matchResult = [];
117
+ allPages.forEach((page) => {
118
+ const targetTitle = page.title || "";
119
+ if (!targetTitle) return;
120
+ const titlePinyin = (0, import_pinyin_pro.pinyin)(targetTitle, {
121
+ toneType: "none",
122
+ type: "string",
123
+ separator: ""
124
+ });
125
+ const titleFirstLetter = (0, import_pinyin_pro.pinyin)(targetTitle, {
126
+ type: "string",
127
+ separator: ""
128
+ }).toLowerCase();
129
+ let score = 0;
130
+ if (titlePinyin.includes(userPinyin) || userPinyin.includes(titlePinyin))
131
+ score += 5;
132
+ if (targetTitle.includes(queryKey)) score += 4;
133
+ if (titleFirstLetter.includes(userFirstLetter) || userFirstLetter.includes(titleFirstLetter))
134
+ score += 3;
135
+ if (score > 0)
136
+ matchResult.push({ id: page.id, title: targetTitle, score });
137
+ });
138
+ if (matchResult.length === 0) {
139
+ return `❌ 未找到【${queryKey}】相关内容,请按游戏内标准名称重新查询!`;
78
140
  }
79
- const res = await ctx.database.get("wikipages", {
80
- $or: [
81
- {
82
- title: itemName
83
- },
84
- {
85
- redirect: itemName
86
- }
87
- ]
141
+ const sortedResult = matchResult.sort((a, b) => b.score - a.score);
142
+ const uniqueResult = Array.from(
143
+ new Map(sortedResult.map((item) => [item.title, item])).values()
144
+ ).slice(0, 5);
145
+ const resultCount = uniqueResult.length;
146
+ let replyMsg = `🔍 未找到精准匹配,为你找到【${resultCount}】个相似结果,请输入序号选择(10秒内有效):
147
+ `;
148
+ uniqueResult.forEach((item, index) => {
149
+ replyMsg += `${index + 1}. ${item.title}
150
+ `;
88
151
  });
89
- if (res.length == 0) {
90
- return `在Wiki里没找到,如有需要,请按照游戏内名称重新发起查询....`;
152
+ replyMsg += `
153
+ ❗️ 提示:超时将静默结束,无任何回应,没有待选结果请艾特机器人任意内容结束本轮查询`;
154
+ await session.send(replyMsg);
155
+ const userInput = await session.prompt(1e4);
156
+ if (!userInput) return;
157
+ const selectNum = parseInt(userInput.trim());
158
+ if (isNaN(selectNum) || selectNum < 1 || selectNum > resultCount) {
159
+ return `❌ 输入无效!请输入 1-${resultCount} 之间的数字序号`;
91
160
  }
92
- const pageName = res[0].title;
93
- return `请点击链接前往站点查看:
94
- 原站点: http://oni.wiki/${encodeURI(
95
- pageName
96
- )}?variant=zh
97
- 镜像站: http://klei.vip/oni/usiz6d/${encodeURI(pageName)}`;
161
+ const { id } = uniqueResult[selectNum - 1];
162
+ return `✅ 选择成功
163
+ 原站点: http://${config.domain}/ggwiki/${id}
164
+ 镜像站: http://${config.domain}/bwiki/${id}`;
98
165
  });
99
166
  ctx.command("update", "更新本地页面缓存", { authority: 2 }).action(async ({ session }) => {
100
167
  wikibot.request({
@@ -112,16 +179,14 @@ function apply(ctx, config) {
112
179
  });
113
180
  session.send(`检索到 ${pages.length} 个页面,已尝试更新至数据库`);
114
181
  logger.info(`检索到 ${pages.length} 个页面,已尝试更新至数据库`);
115
- }).catch((err) => {
116
- logger.error("查询失败", err);
117
- });
182
+ }).catch((err) => logger.error("查询失败", err));
118
183
  });
119
- ctx.command("update.delete", " 删除本地页面缓存", { authority: 4 }).action(async ({ session }) => {
184
+ ctx.command("update.delete", "删除本地页面缓存", { authority: 4 }).action(async ({ session }) => {
120
185
  const count = await ctx.database.remove("wikipages", {});
121
186
  session.send(`已删除 ${count.removed} 条本地缓存`);
122
187
  logger.info(`已删除 ${count.removed} 条本地缓存`);
123
188
  });
124
- ctx.command("update.bwiki", "使用bwiki的session更新本地页面缓存", { authority: 2 }).action(async ({ session }) => {
189
+ ctx.command("update.bwiki", "使用bwiki的session更新缓存", { authority: 2 }).action(async ({ session }) => {
125
190
  const headers = {
126
191
  "Content-Type": "application/json",
127
192
  "user-agent": "Charles'queryBot",
@@ -134,14 +199,18 @@ function apply(ctx, config) {
134
199
  { id: page.pageid, title: page.title }
135
200
  ]);
136
201
  });
137
- session.send(`检索到 ${res["query"]["allpages"].length} 个页面,已尝试更新至数据库`);
138
- logger.info(`检索到 ${res["query"]["allpages"].length} 个页面,已尝试更新至数据库`);
202
+ session.send(
203
+ `检索到 ${res["query"]["allpages"].length} 个页面,已尝试更新至数据库`
204
+ );
205
+ logger.info(
206
+ `检索到 ${res["query"]["allpages"].length} 个页面,已尝试更新至数据库`
207
+ );
139
208
  }).catch((err) => {
140
209
  session.send("更新失败,请联系管理员检查日志");
141
210
  logger.error("更新失败", err);
142
211
  });
143
212
  });
144
- ctx.command("update.status", "查询本地页面缓存数量", { authority: 1 }).action(async ({ session }) => {
213
+ ctx.command("update.status", "查询本地缓存数量", { authority: 1 }).action(async ({ session }) => {
145
214
  const count = await ctx.database.get("wikipages", {});
146
215
  session.send(`数据库中缓存了 ${count.length} 条页面`);
147
216
  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.1",
4
+ "version": "0.7.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
+ }