From f591446314a57d25aa93cc4b35b391d2fc6849a7 Mon Sep 17 00:00:00 2001 From: woxigousade Date: Sat, 25 Jun 2022 21:56:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=96=B5=E5=96=B5=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E8=A7=92=E8=89=B2=E5=86=99=E7=9C=9F=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/uploadCharacterImage.js | 120 +++++++++++++++++++++++++++++++++++ index.js | 10 ++- 2 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 apps/uploadCharacterImage.js diff --git a/apps/uploadCharacterImage.js b/apps/uploadCharacterImage.js new file mode 100644 index 00000000..ddef57ab --- /dev/null +++ b/apps/uploadCharacterImage.js @@ -0,0 +1,120 @@ +import { segment } from "oicq"; +import fetch from "node-fetch"; +import fs from "fs"; +import Data from "../components/Data.js"; +import { Character } from "../components/models.js"; +import lodash from "lodash"; +import { promisify } from "util"; +import { pipeline } from "stream"; + + +const rootPath = process.cwd() + "/plugins/miao-plugin/"; +let regex = /^#*喵喵(上传|添加)(.+)写真.*$/; + +export const rule = { + uploadCharacterImage: { + hashMark: true, + reg: "^#*喵喵(上传|添加)(.+)写真.*$", + describe: "喵喵上传角色写真", + }, +}; + +export async function uploadCharacterImage(e) { + let promise = await isAllowedToUploadCharacterImage(e); + if (!promise) { + return true; + } + + let imageMessages = []; + let msg = e.msg; + let regexResult = regex.exec(msg); + //通过解析正则获取消息中的角色名 + let characterName = regexResult[2]; + //将消息中的角色名转换为官方名称 + let officialName = Character.get(characterName).name; + if (officialName === undefined) { + e.reply("未查询到该角色。请输入有效的角色名或别名。"); + return true; + } + console.log("本次要上传的角色是: ", officialName); + for (let val of e.message) { + if ("image" === val.type) { + imageMessages.push(val); + } + } + if (imageMessages.length <= 0) { + e.reply("消息中未找到图片,无法添加。"); + return true; + } + await saveImages(e, officialName, imageMessages); + return true; +} + +async function saveImages(e, officialName, imageMessages) { + let imgMaxSize = e.groupConfig.imgMaxSize || 1; + let pathSuffix = "resources/miao-res-plus/character-img/" + officialName; + let path = rootPath + pathSuffix; + + if (!fs.existsSync(path)) { + console.log("路径不存在,创建目录: ", path); + Data.createDir(rootPath, pathSuffix); + } + let senderName = lodash.truncate(e.sender.card, { length: 8 }); + for (let val of imageMessages) { + const response = await fetch(val.url); + if (!response.ok) { + e.reply("图片下载失败。"); + return true; + } + if (response.headers.get("size") > 1024 * 1024 * imgMaxSize) { + e.reply([segment.at(e.user_id, senderName), "添加失败:图片太大了。"]); + return true; + } + let fileName = val.file.substring(0, val.file.lastIndexOf(".")); + let fileType = val.file.substring(val.file.lastIndexOf(".") + 1); + if (response.headers.get("content-type") === "image/gif") { + fileType = "gif"; + } + + const streamPipeline = promisify(pipeline); + await streamPipeline(response.body, fs.createWriteStream(`${path}/${fileName}.${fileType}`)); + Bot.logger.mark(`添加成功: ${path}/${fileName}`); + } + e.reply([segment.at(e.user_id, senderName), `\n添加${officialName}信息成功。`]); + return true; +} + +async function isAllowedToUploadCharacterImage(e) { + if (!e.message) { + return false; + } + if (!e.msg) { + return false; + } + if (e.isPrivate) { + if (!e.isMaster) { + e.reply(`只有主人才能添加。`); + return false; + } + return true; + } + + let group_id = e.group_id; + if (!group_id) { + return false; + } + if (e.groupConfig.imgAddLimit === 2) { + if (!e.isMaster) { + e.reply(`只有主人才能添加。`); + return false; + } + } + if (e.groupConfig.imgAddLimit === 1 && !e.isMaster) { + if (!(e.sender.role === "owner" || e.sender.role === "admin")) { + e.reply(`只有管理员才能添加。`); + return false; + } + } + return true; +} + diff --git a/index.js b/index.js index ac4c3890..f9afa456 100644 --- a/index.js +++ b/index.js @@ -16,6 +16,9 @@ import lodash from "lodash"; import common from "../../lib/common.js"; import { rule as adminRule, updateRes, sysCfg, updateMiaoPlugin, profileCfg } from "./apps/admin.js"; import { currentVersion } from "./components/Changelog.js"; +import { + uploadCharacterImage +} from "./apps/uploadCharacterImage.js"; export { character, @@ -36,7 +39,8 @@ export { profileHelp, calendar, profileCfg, - getOriginalPicture + getOriginalPicture, + uploadCharacterImage }; @@ -66,6 +70,10 @@ let rule = { reg: "^#?(获取|给我|我要|求|发|发下|发个|发一下)?原图(吧|呗)?$", describe: "【#原图】 回复角色卡片,可获取原图", }, + uploadCharacterImage: { + reg: "^#*喵喵(上传|添加)(.+)写真.*$", + describe: "喵喵上传角色写真", + }, consStat: { reg: "^#(喵喵)?角色(持有|持有率|命座|命之座|.命)(分布|统计|持有|持有率)?$", describe: "【#统计】 #角色持有率 #角色5命统计",