🎈 perf: 简化搜书逻辑 & 优化 & 注释

This commit is contained in:
RrOrange 2023-04-10 20:55:36 +08:00
parent 2d25b325f6
commit 3f288a95cc
2 changed files with 23 additions and 52 deletions

View File

@ -302,35 +302,31 @@ export class query extends plugin {
return true; return true;
} }
const replyMessage = async msg => {
if (msg && msg.length > 0) {
await e.reply(await Bot.makeForwardMsg(msg));
}
};
// 集成易书、zBook // 集成易书、zBook
try { try {
const bookList = await Promise.allSettled([getYiBook(e, keyword), getZBook(e, keyword)]); const bookList = await Promise.allSettled([
bookList getYiBook(e, keyword),
.filter(one => one.status === "fulfilled") getZBook(e, keyword),
.map(item => { ]);
replyMessage(item.value); // 压缩直链结果
const combineRet = bookList
.filter(item => item.status === "fulfilled" && item.value && item.value.length > 0)
.flatMap(item => {
return item.value.flat();
}); });
await e.reply(await Bot.makeForwardMsg(combineRet));
// ZHelper 特殊处理
const zHelper = await getZHelper(e, keyword); const zHelper = await getZHelper(e, keyword);
if (zHelper && zHelper.length > 0) { zHelper.unshift({
await replyMessage(zHelper); message: "⚠️⚠️请输入#bookid选择一个你想要的ID、来源例如11918807 superlib\n只回复11918807 默认zlibrary⚠",
const replyText = nickname: e.sender.card || e.user_id,
"请选择一个你想要的ID、来源例如\n" + user_id: e.user_id,
"11918807 superlib\n" + })
"只回复11918807 默认zlibrary\n" + zHelper.length > 1 &&
"书源若不对应则回复无效链接,数字字母之间空格"; e.reply(await Bot.makeForwardMsg(zHelper));
await e.reply(replyText);
this.setContext("searchBookContext");
}
} catch (err) { } catch (err) {
logger.error(err); logger.error(err);
e.reply("搜书正在施工🚧"); e.reply("部分搜书正在施工🚧");
} }
return true; return true;
} }
@ -360,32 +356,6 @@ export class query extends plugin {
return true; return true;
} }
/**
* @link searchBook 的上下文
* @returns {Promise<void>}
*/
async searchBookContext() {
// 当前消息
const curMsg = this.e;
// 上一个消息
// const preMsg = this.getContext();
if (!curMsg.msg) {
this.e.reply("请回复id和来源");
return;
}
// 获取id和来源
let id, source;
if (curMsg.msg.includes(" ")) {
[id, source] = curMsg.msg.split(" ");
} else {
id = /\d+/.exec(curMsg.msg)[0];
source = "";
}
const res = await getBookDetail(curMsg, id, source);
await this.reply(await Bot.makeForwardMsg(res));
this.finish("searchBookContext");
}
// 竹白百科 // 竹白百科
async zhubaiSearch(e) { async zhubaiSearch(e) {
const keyword = e.msg.replace("#竹白", "").trim(); const keyword = e.msg.replace("#竹白", "").trim();

View File

@ -4,7 +4,7 @@ import axios from "axios";
/** /**
* 获取易书下载的来源 * 获取易书下载的来源
* @param keyword 书名 * @param keyword 书名
* @returns {Promise<void>} * @returns {Promise<Array>}
*/ */
async function getYiBook(e, keyword) { async function getYiBook(e, keyword) {
const sendTemplate = { const sendTemplate = {
@ -73,7 +73,7 @@ const zBookDownloadUrl = [
/** /**
* 获取ZBook的数据 * 获取ZBook的数据
* @returns {Promise<void>} * @returns {Promise<Array>}
*/ */
async function getZBook(e, keyword) { async function getZBook(e, keyword) {
const sendTemplate = { const sendTemplate = {
@ -129,8 +129,9 @@ async function getZBook(e, keyword) {
/** /**
* 获取ZHelper的数据 * 获取ZHelper的数据
* @param e
* @param keyword * @param keyword
* @returns {Promise<AxiosResponse<any>>} * @returns {Promise<Array>}
*/ */
async function getZHelper(e, keyword) { async function getZHelper(e, keyword) {
const sendTemplate = { const sendTemplate = {