koishi-plugin-oni-wiki-qq 0.0.2
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 +12 -0
- package/lib/index.js +249 -0
- package/lib/lib.d.ts +34 -0
- package/lib/lib.js +126 -0
- package/package.json +20 -0
- package/readme.md +5 -0
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Context, Schema } from "koishi";
|
|
2
|
+
export declare const name = "oni-wiki-qq";
|
|
3
|
+
export declare const inject: string[];
|
|
4
|
+
export declare const usage = "\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";
|
|
5
|
+
export interface Config {
|
|
6
|
+
mdId: string;
|
|
7
|
+
api: string;
|
|
8
|
+
imgPath: string;
|
|
9
|
+
publicUrl: string;
|
|
10
|
+
}
|
|
11
|
+
export declare const Config: Schema<Config>;
|
|
12
|
+
export declare function apply(ctx: Context, config: Config): void;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.apply = exports.Config = exports.usage = exports.inject = exports.name = void 0;
|
|
30
|
+
const koishi_1 = require("koishi");
|
|
31
|
+
const fs_1 = __importDefault(require("fs"));
|
|
32
|
+
const os_1 = __importDefault(require("os"));
|
|
33
|
+
const lib = __importStar(require("./lib"));
|
|
34
|
+
exports.name = "oni-wiki-qq";
|
|
35
|
+
exports.inject = ["puppeteer"];
|
|
36
|
+
exports.usage = `
|
|
37
|
+
- 0.0.2 基本功能完成
|
|
38
|
+
- 0.0.1 初始化,明天再慢慢写了(
|
|
39
|
+
`;
|
|
40
|
+
exports.Config = koishi_1.Schema.object({
|
|
41
|
+
mdId: koishi_1.Schema.string().description("模板ID"),
|
|
42
|
+
api: koishi_1.Schema.string()
|
|
43
|
+
.description("api地址")
|
|
44
|
+
.default("https://oxygennotincluded.fandom.com/zh/api.php"),
|
|
45
|
+
imgPath: koishi_1.Schema.string()
|
|
46
|
+
.description("图片保存路径")
|
|
47
|
+
.default(os_1.default.homedir() + "\\wikiImg\\"),
|
|
48
|
+
publicUrl: koishi_1.Schema.string().description("图片公网访问路径"),
|
|
49
|
+
});
|
|
50
|
+
function apply(ctx, config) {
|
|
51
|
+
const logger = ctx.logger(exports.name);
|
|
52
|
+
// 注册指令
|
|
53
|
+
ctx
|
|
54
|
+
.command("c <itemName>", "查询缺氧中文wiki")
|
|
55
|
+
.alias("/查wiki")
|
|
56
|
+
.option("update", "-u 更新本地缓存", { authority: 2 })
|
|
57
|
+
.option("delete", "-d 删除本地缓存", { authority: 2 })
|
|
58
|
+
.option("rename", "-r <newName> 重命名本地缓存", { authority: 2 })
|
|
59
|
+
.action(async ({ session, options }, itemName) => {
|
|
60
|
+
let filePath = config.imgPath +
|
|
61
|
+
itemName.replace(/\//g, "-").replace(/:/g, "-").replace(/'/g, "-") +
|
|
62
|
+
".jpeg";
|
|
63
|
+
if (options.update) {
|
|
64
|
+
let url = "https://oxygennotincluded.fandom.com/zh/wiki/" + encodeURI(itemName);
|
|
65
|
+
await lib.screenShot(url, ctx, itemName, config);
|
|
66
|
+
return `已尝试为您更新${itemName}的缓存...}`;
|
|
67
|
+
}
|
|
68
|
+
if (options.delete) {
|
|
69
|
+
let filePath = config.imgPath + itemName + ".jpeg";
|
|
70
|
+
if (lib.checkFileExists(filePath)) {
|
|
71
|
+
fs_1.default.unlinkSync(filePath);
|
|
72
|
+
return `已尝试删除${itemName}的缓存...`;
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
return `文件不存在...`;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (options.rename) {
|
|
79
|
+
if (lib.checkFileExists(filePath)) {
|
|
80
|
+
fs_1.default.renameSync(filePath, config.imgPath + options.rename + ".jpeg");
|
|
81
|
+
return `已尝试重命名文件...`;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
return `文件不存在...`;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// 主流程
|
|
88
|
+
session.send("正在查询中,请稍后...");
|
|
89
|
+
await lib.delay(1000);
|
|
90
|
+
// 判断文件是否在本地
|
|
91
|
+
if (lib.checkFileExists(filePath)) {
|
|
92
|
+
return `文件缓存已命中,缓存时间为:${lib.getFileModifyTime(filePath)} 请前往以下网址查看: \n ${config.publicUrl +
|
|
93
|
+
encodeURI(itemName.replace(/\//g, "-").replace(/:/g, "-").replace(/'/g, "-"))}.jpeg`;
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
// 没有缓存
|
|
97
|
+
let res = await lib.getFromFandom(config.api, ctx, itemName);
|
|
98
|
+
if (res.length === 0) {
|
|
99
|
+
// session.send()
|
|
100
|
+
return `在Wiki里没找到或API查询超时或....`;
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
const title = [...res[0]];
|
|
104
|
+
let res_url = [...res[1]];
|
|
105
|
+
logger.info(`API返回的数据为: ${title}`);
|
|
106
|
+
if (title[0] === itemName) {
|
|
107
|
+
return lib.screenShot(res_url[0], ctx, itemName, config);
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
let [one = "曼德拉草汤", two = "火龙果派", three = "大肉汤", four = "饺子", five = "萌新骨头汤",] = title;
|
|
111
|
+
// 发送md消息
|
|
112
|
+
await session.bot.internal.sendMessage(session.guildId, {
|
|
113
|
+
content: "111",
|
|
114
|
+
msg_type: 2,
|
|
115
|
+
markdown: {
|
|
116
|
+
custom_template_id: config.mdId,
|
|
117
|
+
params: [
|
|
118
|
+
{
|
|
119
|
+
key: `one`,
|
|
120
|
+
values: [one],
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
key: `two`,
|
|
124
|
+
values: [two],
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
key: `three`,
|
|
128
|
+
values: [three],
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
key: `four`,
|
|
132
|
+
values: [four],
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
key: `five`,
|
|
136
|
+
values: [five],
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
},
|
|
140
|
+
keyboard: {
|
|
141
|
+
content: {
|
|
142
|
+
rows: [
|
|
143
|
+
{
|
|
144
|
+
buttons: [
|
|
145
|
+
{
|
|
146
|
+
id: "1",
|
|
147
|
+
render_data: {
|
|
148
|
+
label: "①",
|
|
149
|
+
visited_label: "①",
|
|
150
|
+
},
|
|
151
|
+
action: {
|
|
152
|
+
type: 2,
|
|
153
|
+
permission: {
|
|
154
|
+
type: 2,
|
|
155
|
+
},
|
|
156
|
+
unsupport_tips: "兼容文本",
|
|
157
|
+
data: "1",
|
|
158
|
+
enter: true,
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
id: "2",
|
|
163
|
+
render_data: {
|
|
164
|
+
label: "②",
|
|
165
|
+
visited_label: "②",
|
|
166
|
+
},
|
|
167
|
+
action: {
|
|
168
|
+
type: 2,
|
|
169
|
+
permission: {
|
|
170
|
+
type: 2,
|
|
171
|
+
},
|
|
172
|
+
unsupport_tips: "兼容文本",
|
|
173
|
+
data: "2",
|
|
174
|
+
enter: true,
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
id: "3",
|
|
179
|
+
render_data: {
|
|
180
|
+
label: "③",
|
|
181
|
+
visited_label: "③",
|
|
182
|
+
},
|
|
183
|
+
action: {
|
|
184
|
+
type: 2,
|
|
185
|
+
permission: {
|
|
186
|
+
type: 2,
|
|
187
|
+
},
|
|
188
|
+
unsupport_tips: "兼容文本",
|
|
189
|
+
data: "3",
|
|
190
|
+
enter: true,
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
id: "4",
|
|
195
|
+
render_data: {
|
|
196
|
+
label: "④",
|
|
197
|
+
visited_label: "④",
|
|
198
|
+
},
|
|
199
|
+
action: {
|
|
200
|
+
type: 2,
|
|
201
|
+
permission: {
|
|
202
|
+
type: 2,
|
|
203
|
+
},
|
|
204
|
+
unsupport_tips: "兼容文本",
|
|
205
|
+
data: "4",
|
|
206
|
+
enter: true,
|
|
207
|
+
},
|
|
208
|
+
},
|
|
209
|
+
{
|
|
210
|
+
id: "5",
|
|
211
|
+
render_data: {
|
|
212
|
+
label: "⑤",
|
|
213
|
+
visited_label: "⑤",
|
|
214
|
+
},
|
|
215
|
+
action: {
|
|
216
|
+
type: 2,
|
|
217
|
+
permission: {
|
|
218
|
+
type: 2,
|
|
219
|
+
},
|
|
220
|
+
unsupport_tips: "兼容文本",
|
|
221
|
+
data: "5",
|
|
222
|
+
enter: true,
|
|
223
|
+
},
|
|
224
|
+
},
|
|
225
|
+
],
|
|
226
|
+
},
|
|
227
|
+
],
|
|
228
|
+
},
|
|
229
|
+
},
|
|
230
|
+
msg_id: session.messageId,
|
|
231
|
+
timestamp: session.timestamp,
|
|
232
|
+
msg_seq: Math.floor(Math.random() * 500),
|
|
233
|
+
});
|
|
234
|
+
const awlist = [1, 2, 3, 4, 5];
|
|
235
|
+
const awser = +(await session.prompt(50 * 1000))
|
|
236
|
+
?.replace(/\s+/g, "")
|
|
237
|
+
?.slice(-1) || NaN;
|
|
238
|
+
if (awlist.includes(awser)) {
|
|
239
|
+
return lib.screenShot(res_url[awser - 1], ctx, itemName, config);
|
|
240
|
+
}
|
|
241
|
+
else if (Number.isNaN(awser)) {
|
|
242
|
+
return `您输入的选项有误,已完结本轮查询。如需,如有需要,请重新发起查询.`;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
exports.apply = apply;
|
package/lib/lib.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Context } from "koishi";
|
|
2
|
+
import { Config } from ".";
|
|
3
|
+
/**
|
|
4
|
+
* 检查文件是否存在
|
|
5
|
+
* @param filePath 文件路径
|
|
6
|
+
* @returns true:存在 false:不存在
|
|
7
|
+
*/
|
|
8
|
+
export declare function checkFileExists(filePath: string): boolean;
|
|
9
|
+
/**
|
|
10
|
+
* @desc 获取文件修改时间
|
|
11
|
+
* @param filePath 文件路径
|
|
12
|
+
* @returns string 文件修改时间
|
|
13
|
+
*/
|
|
14
|
+
export declare function getFileModifyTime(filePath: string): string;
|
|
15
|
+
/**
|
|
16
|
+
* @desc 截屏
|
|
17
|
+
* @param url 截屏地址
|
|
18
|
+
* @param ctx 上下文
|
|
19
|
+
* @param config 配置
|
|
20
|
+
* @param itemName 物品名称
|
|
21
|
+
* @returns string
|
|
22
|
+
*/
|
|
23
|
+
export declare function screenShot(url: string, ctx: Context, itemName: string, config: Config): Promise<string>;
|
|
24
|
+
/**
|
|
25
|
+
*
|
|
26
|
+
* @param ms 延迟毫秒数
|
|
27
|
+
* @returns void
|
|
28
|
+
*/
|
|
29
|
+
export declare function delay(ms: number): Promise<unknown>;
|
|
30
|
+
/**
|
|
31
|
+
* @desc 从fandom获取数据
|
|
32
|
+
* @returns []
|
|
33
|
+
*/
|
|
34
|
+
export declare function getFromFandom(url: string, ctx: Context, itemName: string): Promise<any[]>;
|
package/lib/lib.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getFromFandom = exports.delay = exports.screenShot = exports.getFileModifyTime = exports.checkFileExists = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
/**
|
|
9
|
+
* 检查文件是否存在
|
|
10
|
+
* @param filePath 文件路径
|
|
11
|
+
* @returns true:存在 false:不存在
|
|
12
|
+
*/
|
|
13
|
+
function checkFileExists(filePath) {
|
|
14
|
+
try {
|
|
15
|
+
fs_1.default.accessSync(filePath, fs_1.default.constants.F_OK);
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
catch (err) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.checkFileExists = checkFileExists;
|
|
23
|
+
/**
|
|
24
|
+
* @desc 获取文件修改时间
|
|
25
|
+
* @param filePath 文件路径
|
|
26
|
+
* @returns string 文件修改时间
|
|
27
|
+
*/
|
|
28
|
+
function getFileModifyTime(filePath) {
|
|
29
|
+
const stats = fs_1.default.statSync(filePath);
|
|
30
|
+
const fileModifiedTime = stats.mtime.getTime();
|
|
31
|
+
return new Date(fileModifiedTime).toLocaleDateString("zh-CN", {
|
|
32
|
+
year: "numeric",
|
|
33
|
+
month: "long",
|
|
34
|
+
day: "numeric",
|
|
35
|
+
hour: "numeric",
|
|
36
|
+
minute: "numeric",
|
|
37
|
+
second: "numeric",
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
exports.getFileModifyTime = getFileModifyTime;
|
|
41
|
+
/**
|
|
42
|
+
* @desc 截屏
|
|
43
|
+
* @param url 截屏地址
|
|
44
|
+
* @param ctx 上下文
|
|
45
|
+
* @param config 配置
|
|
46
|
+
* @param itemName 物品名称
|
|
47
|
+
* @returns string
|
|
48
|
+
*/
|
|
49
|
+
async function screenShot(url, ctx, itemName, config) {
|
|
50
|
+
if (!url) {
|
|
51
|
+
return `你游还没有这些东西...`;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
const page = await ctx.puppeteer.page();
|
|
55
|
+
await page.goto(url, {
|
|
56
|
+
timeout: 0,
|
|
57
|
+
});
|
|
58
|
+
await delay(5000);
|
|
59
|
+
await page.addStyleTag({
|
|
60
|
+
// 添加详情页边框
|
|
61
|
+
content: "#mw-content-text{padding: 40px}",
|
|
62
|
+
});
|
|
63
|
+
await delay(3000);
|
|
64
|
+
const selector = await page.$("#mw-content-text");
|
|
65
|
+
await delay(2000);
|
|
66
|
+
return await selector
|
|
67
|
+
.screenshot({
|
|
68
|
+
type: "jpeg",
|
|
69
|
+
quality: 80,
|
|
70
|
+
path: `${config.imgPath}${itemName
|
|
71
|
+
.replace(/\//g, "-")
|
|
72
|
+
.replace(/:/g, "-")
|
|
73
|
+
.replace(/'/g, "-")}.jpeg`,
|
|
74
|
+
})
|
|
75
|
+
.then(async () => {
|
|
76
|
+
// console.info(`截图成功...`);
|
|
77
|
+
return `截图已保存到以下网址,请自行点击查看:\n ${config.publicUrl}${encodeURI(itemName.replace(/\//g, "-").replace(/:/g, "-").replace(/'/g, "-"))}.jpeg`;
|
|
78
|
+
})
|
|
79
|
+
.catch((err) => {
|
|
80
|
+
console.error(err);
|
|
81
|
+
return `截图失败...`;
|
|
82
|
+
})
|
|
83
|
+
.finally(async () => {
|
|
84
|
+
await page.close();
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
exports.screenShot = screenShot;
|
|
89
|
+
/**
|
|
90
|
+
*
|
|
91
|
+
* @param ms 延迟毫秒数
|
|
92
|
+
* @returns void
|
|
93
|
+
*/
|
|
94
|
+
async function delay(ms) {
|
|
95
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
96
|
+
}
|
|
97
|
+
exports.delay = delay;
|
|
98
|
+
/**
|
|
99
|
+
* @desc 从fandom获取数据
|
|
100
|
+
* @returns []
|
|
101
|
+
*/
|
|
102
|
+
async function getFromFandom(url, ctx, itemName) {
|
|
103
|
+
return await ctx.http
|
|
104
|
+
.get(url, {
|
|
105
|
+
headers: {
|
|
106
|
+
"Content-Type": "application/json",
|
|
107
|
+
"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",
|
|
108
|
+
},
|
|
109
|
+
params: {
|
|
110
|
+
action: `opensearch`,
|
|
111
|
+
search: itemName,
|
|
112
|
+
limit: 5,
|
|
113
|
+
redirects: "return",
|
|
114
|
+
format: "json",
|
|
115
|
+
},
|
|
116
|
+
})
|
|
117
|
+
.then(async (res) => {
|
|
118
|
+
console.log(res);
|
|
119
|
+
return [res[1], res[3]];
|
|
120
|
+
})
|
|
121
|
+
.catch((err) => {
|
|
122
|
+
console.error(err);
|
|
123
|
+
return [];
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
exports.getFromFandom = getFromFandom;
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "koishi-plugin-oni-wiki-qq",
|
|
3
|
+
"description": "缺氧Wiki查询。QQ群版。需要官方群机器人md模板和群机器人权限。自用",
|
|
4
|
+
"version": "0.0.2",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"typings": "lib/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"lib",
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"keywords": [
|
|
13
|
+
"chatbot",
|
|
14
|
+
"koishi",
|
|
15
|
+
"plugin"
|
|
16
|
+
],
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"koishi": "^4.17.1"
|
|
19
|
+
}
|
|
20
|
+
}
|