From dc1e280f06a1836c391768046ce92044112137dd Mon Sep 17 00:00:00 2001 From: ycxom Date: Tue, 31 Dec 2024 14:53:13 +0800 Subject: [PATCH] =?UTF-8?q?feat(bym):=20=E9=87=8D=E6=9E=84=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=EF=BC=8C=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=20ToDoimg=20=E5=B7=A5=E5=85=B7=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/bym.js | 104 ++---------------------------------------- utils/ToDoimg.js | 116 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 100 deletions(-) create mode 100644 utils/ToDoimg.js diff --git a/apps/bym.js b/apps/bym.js index 61c9ac3..44d5a07 100644 --- a/apps/bym.js +++ b/apps/bym.js @@ -17,10 +17,9 @@ import { JinyanTool } from '../utils/tools/JinyanTool.js' import { KickOutTool } from '../utils/tools/KickOutTool.js' import { SetTitleTool } from '../utils/tools/SetTitleTool.js' import { SerpTool } from '../utils/tools/SerpTool.js' +import { getToimg, downImg } from '../utils/ToDoimg.js' import fs from "fs"; -import { fileTypeFromBuffer } from 'file-type'; -import moment from 'moment'; import pathModule from 'path'; const _path = process.cwd(); const path = _path + "/temp/tp-bq"; @@ -38,13 +37,6 @@ const DefaultConfig = { debug: false } -if (!fs.existsSync(path)) { - fs.mkdirSync(path, { recursive: true }) -} -if (!fs.existsSync(pathModule.join(path, 'pictures'))) { - fs.mkdirSync(pathModule.join(path, 'pictures'), { recursive: true }) -} - // 轻微黑名单用户 let RoleFalseUser = [] @@ -340,7 +332,7 @@ export class bym extends plugin { if (tag === "") { t = t.replace(getImgRegex, ' ').trim(); } else { - await getToimg(e, tag, path); + await getToimg(e, tag); t = t.replace(getImgRegex, ' ').trim(); } } @@ -354,98 +346,10 @@ export class bym extends plugin { const downImgRegex = /DOWNIMG:\s*(.+)/i; const match = t?.match(downImgRegex); if (match) { - await downImg(e, opt.image, path); + await downImg(e, opt.image); continue; } - async function getToimg(e, tag, path) { - const picturesPath = pathModule.join(path, 'pictures'); - const fileImgList = await fs.promises.readdir(picturesPath); - - try { - const sanitizedTag = tag - .replace(/[\u200B-\u200D\uFEFF]/g, '') - .replace(/[\[\]]/g, '') - .trim() - .replace(/[^a-zA-Z0-9\u4e00-\u9fa5-_.]/g, '-'); - let matchedFiles = fileImgList.filter(file => file === sanitizedTag); - if (matchedFiles.length === 0) { - matchedFiles = fileImgList.filter(file => file.startsWith(sanitizedTag)); - } - if (matchedFiles.length === 0) { - matchedFiles = fileImgList.filter(file => file.includes(sanitizedTag)); - } - if (matchedFiles.length === 0) { - logger.warn(`未找到匹配的表情包: ${sanitizedTag}`); - return; - } - const selectedFile = matchedFiles[0]; - const picPath = pathModule.join(picturesPath, selectedFile); - try { - await fs.promises.access(picPath); - } catch { - logger.warn(`找不到指定的表情包文件: ${picPath}`); - return; - } - e.reply(segment.image('file:///' + picPath)); - - logger.info(`发送表情包: ${picPath}`); - return false; - } catch (error) { - logger.error('Error in getToimg:', error); - } - } - async function downImg(e, image, path) { - try { - let reply; - if (e.source) { - if (e.isGroup) { - reply = (await e.group.getChatHistory(e.source.seq, 1)).pop()?.message; - } else { - reply = (await e.friend.getChatHistory(e.source.time, 1)).pop()?.message; - } - if (reply) { - for (let val of reply) { - if (val.type === "image") { - e.img = [val.url]; - break; - } - } - } - } - if (!e.img && !image) { - return false; - } - let kWordReg = /^#?(DOWNIMG:)\s*(.*)/i; - t = t.replace(/[\u200B-\u200D\uFEFF]/g, '').trim(); - const match = kWordReg.exec(t); - if (!match) { - logger.error('DOWNIMG command format invalid:', t); - return; - } - let rawmsg = match[2] || "defaultTag"; - let kWord = rawmsg.replace(/,|,|、| |。/g, "-").replace(/--+/g, "-").replace(/^-|-$|--/g, "").trim() || "defaultTag"; - if (image) { - const imageBuffer = Buffer.from(image, 'base64'); - const type = await fileTypeFromBuffer(imageBuffer); - let picType = 'png'; - if (type && type.ext) { - picType = type.ext; - } - const currentTime = moment().format("YYMMDDHHmmss"); - const safeTag = kWord.replace(/[^a-zA-Z0-9\u4e00-\u9fa5-_]/g, '-'); - const picPath = pathModule.join(path, 'pictures', `${currentTime}-${safeTag.substring(0, 200)}.${picType}`); - logger.mark("DOWNIMG:", picPath); - if (!fs.existsSync(pathModule.join(path, 'pictures'))) { - fs.mkdirSync(pathModule.join(path, 'pictures'), { recursive: true }); - } - fs.writeFileSync(picPath, imageBuffer); - logger.info(`图片已保存,标签为:${kWord}`); - } - } catch (error) { - logger.error('Error in downImg:', error); - logger.error("保存图片时发生错误"); - } - } + if (t) { let finalMsg = await convertFaces(t, true, e) logger.info(JSON.stringify(finalMsg)) diff --git a/utils/ToDoimg.js b/utils/ToDoimg.js new file mode 100644 index 0000000..cb65702 --- /dev/null +++ b/utils/ToDoimg.js @@ -0,0 +1,116 @@ +import pathModule from 'path'; +import { fileTypeFromBuffer } from 'file-type'; +import moment from 'moment'; +const _path = process.cwd(); +const path = _path + "/temp/tp-bq"; + +// 没文件夹就创建一个 +if (!fs.existsSync(path)) { + fs.mkdirSync(path, { recursive: true }) +} +if (!fs.existsSync(pathModule.join(path, 'pictures'))) { + fs.mkdirSync(pathModule.join(path, 'pictures'), { recursive: true }) +} + +/** + * + * @param {*} e - 输入的消息 + * @param {*} tag - 表情包标签 + * @returns + */ +export async function getToimg(e, tag) { + const picturesPath = pathModule.join(path, 'pictures'); + const fileImgList = await fs.promises.readdir(picturesPath); + + try { + const sanitizedTag = tag + .replace(/[\u200B-\u200D\uFEFF]/g, '') + .replace(/[\[\]]/g, '') + .trim() + .replace(/[^a-zA-Z0-9\u4e00-\u9fa5-_.]/g, '-'); + let matchedFiles = fileImgList.filter(file => file === sanitizedTag); + if (matchedFiles.length === 0) { + matchedFiles = fileImgList.filter(file => file.startsWith(sanitizedTag)); + } + if (matchedFiles.length === 0) { + matchedFiles = fileImgList.filter(file => file.includes(sanitizedTag)); + } + if (matchedFiles.length === 0) { + logger.warn(`未找到匹配的表情包: ${sanitizedTag}`); + return; + } + const selectedFile = matchedFiles[0]; + const picPath = pathModule.join(picturesPath, selectedFile); + try { + await fs.promises.access(picPath); + } catch { + logger.warn(`找不到指定的表情包文件: ${picPath}`); + return; + } + e.reply(segment.image('file:///' + picPath)); + + logger.info(`发送表情包: ${picPath}`); + return false; + } catch (error) { + logger.error('Error in getToimg:', error); + } +} + +/** + * + * @param {*} e - 输入的消息 + * @param {*} image - 图片Base64 + * @returns + */ +export async function downImg(e, image) { + try { + let reply; + if (e.source) { + if (e.isGroup) { + reply = (await e.group.getChatHistory(e.source.seq, 1)).pop()?.message; + } else { + reply = (await e.friend.getChatHistory(e.source.time, 1)).pop()?.message; + } + if (reply) { + for (let val of reply) { + if (val.type === "image") { + e.img = [val.url]; + break; + } + } + } + } + if (!e.img && !image) { + return false; + } + let kWordReg = /^#?(DOWNIMG:)\s*(.*)/i; + t = t.replace(/[\u200B-\u200D\uFEFF]/g, '').trim(); + const match = kWordReg.exec(t); + if (!match) { + logger.error('DOWNIMG command format invalid:', t); + return; + } + let rawmsg = match[2] || "defaultTag"; + let kWord = rawmsg.replace(/,|,|、| |。/g, "-").replace(/--+/g, "-").replace(/^-|-$|--/g, "").trim() || "defaultTag"; + if (image) { + const imageBuffer = Buffer.from(image, 'base64'); + const type = await fileTypeFromBuffer(imageBuffer); + let picType = 'png'; + if (type && type.ext) { + picType = type.ext; + } + const currentTime = moment().format("YYMMDDHHmmss"); + const safeTag = kWord.replace(/[^a-zA-Z0-9\u4e00-\u9fa5-_]/g, '-'); + const picPath = pathModule.join(path, 'pictures', `${currentTime}-${safeTag.substring(0, 200)}.${picType}`); + logger.mark("DOWNIMG:", picPath); + if (!fs.existsSync(pathModule.join(path, 'pictures'))) { + fs.mkdirSync(pathModule.join(path, 'pictures'), { recursive: true }); + } + fs.writeFileSync(picPath, imageBuffer); + logger.info(`图片已保存,标签为:${kWord}`); + } + } catch (error) { + logger.error('Error in downImg:', error); + logger.error("保存图片时发生错误"); + } + } \ No newline at end of file