️ 处理ascii2d搜图发送卡片防止风控

This commit is contained in:
yeyang
2023-02-07 20:41:08 +08:00
parent e19e37c5f8
commit 5eb274edb3
3 changed files with 56 additions and 54 deletions

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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('<?xml version="1.0" encoding="utf-8"?>', '<?xml version="1.0" encoding="utf-8" ?>')
.replace(/\n/g, '')
.replace(/<title color="#777777" size="26">(.+?)<\/title>/g, '___')
.replace(/___+/, '<title color="#777777" size="26">涩批(//// ^ ////)</title>')
forwardMsg.data = forwardMsg.data.replace('<?xml version="1.0" encoding="utf-8"?>', '<?xml version="1.0" encoding="utf-8" ?>')
if (isxmlMsg) {
forwardMsg.data = forwardMsg.data.replace(/\n/g, '')
.replace(/<title color="#777777" size="26">(.+?)<\/title>/g, '___')
.replace(/___+/, '<title color="#777777" size="26">涩批(//// ^ ////)</title>')
}
}
// 发送消息
let res = await e.reply(forwardMsg, false, { recallMsg })