koishi-plugin-oni-wiki-qq 0.1.1 → 0.2.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,7 +1,7 @@
1
1
  import { Context, Schema } from "koishi";
2
2
  export declare const name = "oni-wiki-qq";
3
3
  export declare const inject: string[];
4
- export declare const usage = "\n - 0.1.0 \u6DFB\u52A0\u767B\u5F55\u548C\u70B9\u51FBcookies\u6309\u94AE,\u5220\u9664\u6CA1\u6CD5\u5224\u65AD\u7B54\u6848\u7684\u4EE3\u7801\n - 0.0.9 \u5C1D\u8BD5\u79FB\u9664\u5BFC\u822A\u6846,\u66F4\u65B0koishi\u4F9D\u8D56\n - 0.0.8 \u5BF9\u9009\u62E9\u7684\u5176\u4ED6\u9879\u8FDB\u884C\u5F31\u667A\u4E00\u6837\u7684\u5904\u7406,\u514D\u5F97 errlog \u90FD\u5FEB 13M \u4E86\n - 0.0.7 \u5BF9\u7F51\u5740\u8FDB\u884C\u7F16\u7801\u4EE5\u786E\u4FDD\u4E0D\u4F1A\u51FA\u73B0\u5947\u5947\u602A\u602A\u7684\u95EE\u9898\n - 0.0.6 \u7A7A\u7F51\u5740\u9519\u8BEF\u5904\u7406\n - 0.0.5 \u592A\u4E45\u6CA1\u5199\u5FD8\u8BB0build\u4E86...\n - 0.0.4 \u715E\u7B14\u817E\u8BAF\u4E0D\u7ED9\u53D1md\u4E86\u8279\n - 0.0.3 \u5C06\u94FE\u63A5\u6539\u4E3Amd\u53D1\u9001\u4EE5\u7ED5\u8FC7\u6C9F\u817E\u8BAF\u7684\u94FE\u63A5\u62E6\u622A\n - 0.0.2 \u57FA\u672C\u529F\u80FD\u5B8C\u6210\n - 0.0.1 \u521D\u59CB\u5316\uFF0C\u660E\u5929\u518D\u6162\u6162\u5199\u4E86\uFF08\n";
4
+ export declare const usage = "\n - 0.2.0 \u5C1D\u8BD5\u6DFB\u52A0 MWN \u5E93\n - 0.1.0 \u6DFB\u52A0\u767B\u5F55\u548C\u70B9\u51FBcookies\u6309\u94AE,\u5220\u9664\u6CA1\u6CD5\u5224\u65AD\u7B54\u6848\u7684\u4EE3\u7801\n - 0.0.9 \u5C1D\u8BD5\u79FB\u9664\u5BFC\u822A\u6846,\u66F4\u65B0koishi\u4F9D\u8D56\n";
5
5
  export interface Config {
6
6
  api: string;
7
7
  imgPath: string;
package/lib/index.js CHANGED
@@ -38,21 +38,56 @@ __export(src_exports, {
38
38
  });
39
39
  module.exports = __toCommonJS(src_exports);
40
40
  var import_koishi = require("koishi");
41
- var import_fs = __toESM(require("fs"));
41
+ var import_fs2 = __toESM(require("fs"));
42
42
  var import_node_os = __toESM(require("node:os"));
43
+
44
+ // src/lib.ts
45
+ var import_fs = __toESM(require("fs"));
46
+ var import_mwn = require("mwn");
47
+ function checkFileExists(filePath) {
48
+ try {
49
+ import_fs.default.accessSync(filePath, import_fs.default.constants.F_OK);
50
+ return true;
51
+ } catch (err) {
52
+ return false;
53
+ }
54
+ }
55
+ __name(checkFileExists, "checkFileExists");
56
+ async function getWiki(itemName, config) {
57
+ const { api, userName, password } = config;
58
+ const bot = await import_mwn.Mwn.init({
59
+ apiUrl: api,
60
+ username: userName,
61
+ password,
62
+ userAgent: "queryBot 0.0.3 ([[user:Charles-lf]])",
63
+ defaultParams: {
64
+ assert: "user"
65
+ }
66
+ });
67
+ return await bot.request({
68
+ action: "opensearch",
69
+ search: itemName,
70
+ namespace: "*",
71
+ limit: 3,
72
+ redirects: "return",
73
+ format: "json"
74
+ }).then(async (res) => {
75
+ if (res[1][0] == itemName) {
76
+ return res[3][0];
77
+ } else {
78
+ return "";
79
+ }
80
+ }).catch(async (err) => console.log(err));
81
+ }
82
+ __name(getWiki, "getWiki");
83
+
84
+ // src/index.ts
43
85
  var name = "oni-wiki-qq";
44
86
  var inject = ["puppeteer"];
45
87
  var usage = `
88
+ - 0.2.0 尝试添加 MWN 库
46
89
  - 0.1.0 添加登录和点击cookies按钮,删除没法判断答案的代码
47
90
  - 0.0.9 尝试移除导航框,更新koishi依赖
48
- - 0.0.8 对选择的其他项进行弱智一样的处理,免得 errlog 都快 13M 了
49
- - 0.0.7 对网址进行编码以确保不会出现奇奇怪怪的问题
50
- - 0.0.6 空网址错误处理
51
- - 0.0.5 太久没写忘记build了...
52
- - 0.0.4 煞笔腾讯不给发md了艹
53
- - 0.0.3 将链接改为md发送以绕过沟腾讯的链接拦截
54
- - 0.0.2 基本功能完成
55
- - 0.0.1 初始化,明天再慢慢写了(
56
91
  `;
57
92
  var Config = import_koishi.Schema.object({
58
93
  api: import_koishi.Schema.string().default("https://oxygennotincluded.wiki.gg/zh/api.php").description("api地址"),
@@ -73,62 +108,41 @@ var Config = import_koishi.Schema.object({
73
108
  });
74
109
  function apply(ctx, config) {
75
110
  const logger = ctx.logger;
76
- ctx.command("x <itemName>", "查询缺氧中文wiki").alias("/查wiki").option("delete", "-d 删除本地缓存", { authority: 2 }).action(async ({ session, options }, itemName = "电解器") => {
111
+ ctx.command("x <itemName>", "查询缺氧中文wiki").alias("/查wiki").option("delete", "-d 删除本地缓存", { authority: 1 }).action(async ({ session, options }, itemName = "电解器") => {
77
112
  const filePath = config.imgPath + itemName.replace(/\//g, "-").replace(/:/g, "-").replace(/'/g, "-") + ".jpeg";
78
113
  const urlPath = config.urlPath + itemName.replace(/\//g, "-").replace(/:/g, "-").replace(/'/g, "-") + ".jpeg";
79
114
  if (options.delete) {
80
115
  if (checkFileExists(filePath)) {
81
- import_fs.default.unlinkSync(filePath);
116
+ import_fs2.default.unlinkSync(filePath);
82
117
  return `已尝试删除${itemName}的缓存...`;
83
118
  } else {
84
119
  return `文件不存在.请检查....`;
85
120
  }
86
121
  }
87
122
  if (checkFileExists(filePath)) {
88
- console.log(urlPath);
89
- return (0, import_koishi.h)("img", { src: `${encodeURI(urlPath)}` });
123
+ return (0, import_koishi.h)(
124
+ "p",
125
+ `图片已保存至:${encodeURI(urlPath)}`,
126
+ (0, import_koishi.h)("img", { src: `${encodeURI(urlPath)}` })
127
+ );
90
128
  }
91
129
  session.send(`您查询的「${itemName}」进行中,请稍等...`);
92
130
  await (0, import_koishi.sleep)(500);
93
- const res = await getWiki(config.api);
94
- if (res.length == 0) {
131
+ const res = await getWiki(itemName, config);
132
+ if (!res) {
95
133
  return `在Wiki里没找到或API查询超时,如有需要,请按照游戏内名称重新发起查询....`;
96
134
  }
97
- const title = [...res[0]];
98
- let itemUrl = [...res[1]];
99
- logger.info(`API返回的数据为: ${title}`);
100
- if (title[0] === itemName) {
101
- let res2 = await screenShot(itemUrl[0]);
102
- if (res2) {
103
- return (0, import_koishi.h)("img", { src: `${encodeURI(urlPath)}` });
104
- } else {
105
- return `截图发生错误.请稍后重试..`;
106
- }
135
+ let screenShotRes = await screenShot(res);
136
+ logger.info(`API返回的数据为: ${screenShotRes}`);
137
+ if (screenShotRes) {
138
+ return (0, import_koishi.h)(
139
+ "p",
140
+ `图片已保存至:${encodeURI(urlPath)}`,
141
+ (0, import_koishi.h)("img", { src: `${encodeURI(urlPath)}` })
142
+ );
107
143
  } else {
108
- return `没有在wiki内找到${itemName},如有需要,请按照游戏内名称重新发起查询`;
144
+ return `截图发生错误.请稍后重试..`;
109
145
  }
110
- async function getWiki(api) {
111
- return await ctx.http.get(api, {
112
- headers: {
113
- "Content-Type": "application/json",
114
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
115
- },
116
- params: {
117
- action: `opensearch`,
118
- search: itemName,
119
- limit: 3,
120
- redirects: "return",
121
- format: "json"
122
- }
123
- }).then(async (res2) => {
124
- logger.info(res2);
125
- return [res2[1], res2[3]];
126
- }).catch((err) => {
127
- logger.error(err);
128
- return [];
129
- });
130
- }
131
- __name(getWiki, "getWiki");
132
146
  async function screenShot(url) {
133
147
  const page = await ctx.puppeteer.page();
134
148
  await page.goto(url, {
@@ -136,7 +150,7 @@ function apply(ctx, config) {
136
150
  });
137
151
  await (0, import_koishi.sleep)(2e3);
138
152
  await page.addStyleTag({
139
- content: "#mw-content-text{padding: 40px}"
153
+ content: `${config.contentSelector}{padding: 40px}`
140
154
  });
141
155
  try {
142
156
  await page.$eval(config.navSelector, (el) => el.remove());
@@ -157,7 +171,9 @@ function apply(ctx, config) {
157
171
  }
158
172
  __name(screenShot, "screenShot");
159
173
  });
160
- ctx.command("loginwiki", "使用账户登录", { authority: 4 }).action(async ({ session }) => {
174
+ ctx.command("loginwiki [BotuserName] <BotPassword>", "使用机器人账户登录", {
175
+ authority: 4
176
+ }).action(async ({ session }, BotuserName = "", BotPassword) => {
161
177
  const page = await ctx.puppeteer.page();
162
178
  logger.info(`正在前往登录页:${config.loginUrl}`);
163
179
  await page.goto(config.loginUrl, {
@@ -165,10 +181,10 @@ function apply(ctx, config) {
165
181
  });
166
182
  await (0, import_koishi.sleep)(4e3);
167
183
  logger.info(`输入用户名`);
168
- await page.type(config.userNameSelector, config.userName);
184
+ await page.type(config.userNameSelector, BotuserName);
169
185
  await (0, import_koishi.sleep)(1e3);
170
186
  logger.info(`输入密码`);
171
- await page.type(config.passwordSelector, config.password);
187
+ await page.type(config.passwordSelector, BotPassword);
172
188
  await (0, import_koishi.sleep)(1e3);
173
189
  logger.info("点击保持登录复选框");
174
190
  await page.click(config.checkBoxSelector);
@@ -180,7 +196,7 @@ function apply(ctx, config) {
180
196
  await page.close().then(() => logger.info(`页面已经成功关闭`));
181
197
  return `登录已完成.`;
182
198
  });
183
- ctx.command("clickBtn").action(async ({ session }) => {
199
+ ctx.command("clickBtn", { authority: 4 }).action(async ({ session }) => {
184
200
  const page = await ctx.puppeteer.page();
185
201
  await page.goto(config.mainPage, { timeout: 0 });
186
202
  const selector = await page.$(config.btnSelector);
@@ -199,15 +215,6 @@ function apply(ctx, config) {
199
215
  });
200
216
  }
201
217
  __name(apply, "apply");
202
- function checkFileExists(filePath) {
203
- try {
204
- import_fs.default.accessSync(filePath, import_fs.default.constants.F_OK);
205
- return true;
206
- } catch (err) {
207
- return false;
208
- }
209
- }
210
- __name(checkFileExists, "checkFileExists");
211
218
  // Annotate the CommonJS export names for ESM import in node:
212
219
  0 && (module.exports = {
213
220
  Config,
package/lib/lib.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { Config } from ".";
2
+ export declare function checkFileExists(filePath: string): boolean;
3
+ export declare function getWiki(itemName: string, config: Config): Promise<string>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-oni-wiki-qq",
3
- "description": "缺氧wiki查询",
4
- "version": "0.1.1",
3
+ "description": "缺氧wiki查询,自用",
4
+ "version": "0.2.0",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
@@ -15,6 +15,9 @@
15
15
  "plugin"
16
16
  ],
17
17
  "peerDependencies": {
18
- "koishi": "^4.17.11"
18
+ "koishi": "^4.17.12"
19
+ },
20
+ "dependencies": {
21
+ "mwn": "^2.0.3"
19
22
  }
20
23
  }