From 5eb274edb3db6ea6a5074175dd67141c6f761f7c Mon Sep 17 00:00:00 2001 From: yeyang <746659424@qq.com> Date: Tue, 7 Feb 2023 20:41:08 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20=E5=A4=84=E7=90=86ascii2d?= =?UTF-8?q?=E6=90=9C=E5=9B=BE=E5=8F=91=E9=80=81=E5=8D=A1=E7=89=87=E9=98=B2?= =?UTF-8?q?=E6=AD=A2=E9=A3=8E=E6=8E=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/picSearch.js | 50 +++++++++++++++++++----------------- model/PicSearch/whatanime.js | 39 ++++++++++------------------ model/common.js | 21 ++++++++++----- 3 files changed, 56 insertions(+), 54 deletions(-) diff --git a/apps/picSearch.js b/apps/picSearch.js index 497abd5..784633c 100644 --- a/apps/picSearch.js +++ b/apps/picSearch.js @@ -65,38 +65,42 @@ export class newPicSearch extends plugin { async SauceNAO (e) { if (!await this.handelImg(e, 'SauceNAO')) return - let res = await PicSearch.SauceNAO(e.img[0]) - .catch(err => { - e.reply(err.message) - e.reply('SauceNAO搜图出错,自动使用Ascii2D进行搜索') - this.Ascii2D(e) + await PicSearch.SauceNAO(e.img[0]) + .then(async res => { + res.maxSimilarity > 80 + ? common.recallsendMsg(e, res.message, true) + : common.getRecallsendMsg(e, res.message) + if (res.maxSimilarity < Config.picSearch.SauceNAOMinSim) { + e.reply(`SauceNAO 相似度 ${res.maxSimilarity}% 过低,使用Ascii2D进行搜索`) + await this.Ascii2D(e) + } + }) + .catch(async err => { + await e.reply(err.message) + await e.reply('SauceNAO搜图出错,自动使用Ascii2D进行搜索') + await this.Ascii2D(e) }) - if (!res) return - - res.maxSimilarity > 80 ? common.recallsendMsg(e, res.message, true) : common.getRecallsendMsg(e, res.message) - if (res.maxSimilarity < Config.picSearch.SauceNAOMinSim) { - e.reply(`SauceNAO 相似度 ${res.maxSimilarity}% 过低,使用Ascii2D进行搜索`) - await this.Ascii2D(e) - } } async Ascii2D (e) { if (!await this.handelImg(e, 'Ascii2D')) return - let res = await PicSearch.Ascii2D(e.img[0]).catch(err => { - e.reply(err.message) - }) - if (!res) return - common.getRecallsendMsg(e, res.color, { isxml: false }) - common.getRecallsendMsg(e, res.bovw, { isxml: false }) + await PicSearch.Ascii2D(e.img[0]) + .then(res => { + common.getRecallsendMsg(e, res.color, { isxmlMsg: false }) + common.getRecallsendMsg(e, res.bovw, { isxmlMsg: false }) + }) + .catch(err => e.reply(err.message)) } async WhatAnime (e) { if (!await this.handelImg(e, 'WhatAnime')) return - let res = await PicSearch.WhatAnime(e.img[0]) - if (res.error) return e.reply(res.error) - for (let i of res) { - await e.reply(i) - } + await PicSearch.WhatAnime(e.img[0]) + .then(async res => { + for (let i of res) { + await e.reply(i) + } + }) + .catch(err => e.reply(err.message)) } async UploadSauceNAOKey (e) { diff --git a/model/PicSearch/whatanime.js b/model/PicSearch/whatanime.js index 8a79934..84a5e57 100644 --- a/model/PicSearch/whatanime.js +++ b/model/PicSearch/whatanime.js @@ -5,8 +5,7 @@ import common from '../../../../lib/common/common.js' import _ from 'lodash' export default async function doSearch (imgURL) { let result = await getSearchResult(imgURL) - if (!result) return { error: 'api请求失败' } - if (result.error) return { error: result.error } + if (result.error) throw Error(result.error) let { result: [{ @@ -18,7 +17,7 @@ export default async function doSearch (imgURL) { // image // 预览图片 }] } = result - if (_.isEmpty(result)) return { error: '未获取到相关信息' } + if (_.isEmpty(result)) throw Error('未获取到相关信息') similarity = (similarity * 100).toFixed(2) // 相似度 const time = (() => { const s = Math.floor(from) @@ -57,17 +56,12 @@ const date2str = ({ year, month, day }) => [year, month, day].join('-') */ async function getSearchResult (url, key = '') { let host = 'https://api.trace.moe' - try { - const res = await request.get(`${host}/search`, { - params: { - url, - key - } - }) - return await res.json() - } catch (err) { - return console.error(err) - } + return await request.get(`${host}/search`, { + params: { + url, + key + } + }).then(res => res.json()) } const animeInfoQuery = ` query ($id: Int) { @@ -102,17 +96,12 @@ query ($id: Int) { * @returns Prased JSON */ async function getAnimeInfo (id) { - try { - const res = await request.post('https://trace.moe/anilist/', { - data: { - query: animeInfoQuery, - variables: { id } - } - }) - return await res.json() - } catch (err) { - return console.error(err) - } + return await request.post('https://trace.moe/anilist/', { + data: { + query: animeInfoQuery, + variables: { id } + } + }).then(res => res.json()) } async function downFile (url) { diff --git a/model/common.js b/model/common.js index 32f160b..4358245 100644 --- a/model/common.js +++ b/model/common.js @@ -71,10 +71,18 @@ export default new class newCommon { * @param {Boolean} data.isBot 转发信息是否以bot信息发送 * @param {String} data.fkmsg 风控消息不传则默认消息 * @param {Boolean} data.isxml 是否处理卡片 + * @param {Boolean} data.isxmlMsg 是否处理卡片显示消息 * @param {Boolean} data.oneMsg 是否只有一条消息 * @return {Object} 消息是否发送成功的对象 */ - async getforwardMsg (e, message, { recallMsg = 0, isBot = true, fkmsg = '', isxml = false, oneMsg = false } = {}) { + async getforwardMsg (e, message, { + recallMsg = 0, + isBot = true, + fkmsg = '', + isxml = false, + isxmlMsg = true, + oneMsg = false + } = {}) { let forwardMsg = [] if (_.isEmpty(message)) throw Error('[椰奶sendforwardMsg][Error]发送的转发消息不能为空') let add = (msg) => forwardMsg.push( @@ -93,11 +101,12 @@ export default new class newCommon { } if (isxml) { // 处理转发卡片 - forwardMsg.data = forwardMsg.data - .replace('', '') - .replace(/\n/g, '') - .replace(/