From 67c0bf7a0be09900b7cebf4dc7b2198340bf6e79 Mon Sep 17 00:00:00 2001 From: bietiaop <1527109126@qq.com> Date: Thu, 1 Aug 2024 19:59:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=AE=A1=E7=90=86=E9=9D=A2=E6=9D=BF?= =?UTF-8?q?=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/help.js | 19 +++++++++ apps/manage.js | 10 +++++ apps/manage/panel.js | 98 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) diff --git a/apps/help.js b/apps/help.js index 29f1828..61399ff 100644 --- a/apps/help.js +++ b/apps/help.js @@ -256,6 +256,25 @@ export class Help extends ZZZPlugin { '添加+角色名+角色图', ], }, + { + title: '查看角色面板图', + desc: '查看自定义角色面板图,在添加或者删除角色图后,会导致角色图的ID发生变化,此时需要重新获取图片列表来查看ID,否则可能会删除错误的图片', + needCK: false, + needSK: false, + commands: [ + '查看+角色名+面板图[+页码]', + '查看+角色名+角色图[+页码]', + '获取+角色名+面板图[+页码]', + '获取+角色名+角色图[+页码]', + ], + }, + { + title: '删除角色面板图', + desc: '删除自定义角色面板图,在添加或者删除角色图后,会导致角色图的ID发生变化,此时需要重新获取图片列表来查看ID,否则可能会删除错误的图片', + needCK: false, + needSK: false, + commands: ['删除+角色名+面板图', '删除+角色名+角色图'], + }, ], }, ]; diff --git a/apps/manage.js b/apps/manage.js index 26db293..cb63958 100644 --- a/apps/manage.js +++ b/apps/manage.js @@ -52,6 +52,14 @@ export class Panel extends ZZZPlugin { reg: `${rulePrefix}(上传|添加)(\\S+)(角色|面板)图$`, fnc: 'uploadCharacterImg', }, + { + reg: `${rulePrefix}(获取|查看)(\\S+)(角色|面板)图(\\d+)?$`, + fnc: 'getCharacterImages', + }, + { + reg: `${rulePrefix}删除(\\S+)(角色|面板)图(.+)$`, + fnc: 'deleteCharacterImg', + }, ], }); @@ -65,5 +73,7 @@ export class Panel extends ZZZPlugin { this.addAlias = manage.alias.addAlias; this.deleteAlias = manage.alias.deleteAlias; this.uploadCharacterImg = manage.panel.uploadCharacterImg; + this.getCharacterImages = manage.panel.getCharacterImages; + this.deleteCharacterImg = manage.panel.deleteCharacterImg; } } diff --git a/apps/manage/panel.js b/apps/manage/panel.js index beed3e3..fae9b1c 100644 --- a/apps/manage/panel.js +++ b/apps/manage/panel.js @@ -1,6 +1,8 @@ import { char } from '../../lib/convert.js'; import { downloadFile } from '../../lib/download.js'; import { imageResourcesPath } from '../../lib/path.js'; +import common from '../../../../lib/common/common.js'; +import fs from 'fs'; import path from 'path'; export async function uploadCharacterImg() { if (!this.e.isMaster) { @@ -95,3 +97,99 @@ export async function uploadCharacterImg() { this.reply(`成功上传${success}张图片,失败${failed}张图片。`); return false; } + +export async function getCharacterImages() { + const reg = /(获取|查看)(.+)(角色|面板)图(\d+)?$/; + const match = this.e.msg.match(reg); + if (!match) { + return false; + } + const charName = match[2].trim(); + const name = char.aliasToName(charName); + let page = match[4]; + if (!name) { + this.reply('未找到对应角色'); + return false; + } + const pageSize = 5; + const resourcesImagesPath = imageResourcesPath; + const panelImagesPath = path.join(resourcesImagesPath, `panel/${name}`); + const files = fs.readdirSync(panelImagesPath); + const images = []; + for (const file of files) { + images.push(path.join(panelImagesPath, file)); + } + page = Number(page); + if (!page || page < 1) { + page = 1; + } + const start = (page - 1) * pageSize; + const end = page * pageSize; + if (start >= images.length) { + this.reply('哪有这么多图片'); + return false; + } + const imagePaths = images.slice(start, end); + const imageMsg = imagePaths.map(imagePath => { + const id = String(images.findIndex(value => value === imagePath) + 1); + const msg = [`ID:${id}`, segment.image(imagePath)]; + return msg; + }); + imageMsg.unshift( + `当前页数:${page},总页数:${Math.ceil( + images.length / pageSize + )},查询指定页数请在指令后面加上页码。` + ); + imageMsg.push( + '删除或者添加后会重新排序ID,此时若想删除,请重新获取图片列表,否则可能会删除错误的图片。' + ); + if (imageMsg.length) + await this.reply(await common.makeForwardMsg(this.e, imageMsg)); + + return false; +} + +export async function deleteCharacterImg() { + if (!this.e.isMaster) { + this.reply('只有主人才能删除'); + return false; + } + const reg = /(删除)(.+)(角色|面板)图(.+)$/; + const match = this.e.msg.match(reg); + if (!match) { + return false; + } + const charName = match[2].trim(); + const name = char.aliasToName(charName); + if (!name) { + this.reply('未找到对应角色'); + return false; + } + const ids = match[4].split(/[,,、\s]+/); + const resourcesImagesPath = imageResourcesPath; + const panelImagesPath = path.join(resourcesImagesPath, `panel/${name}`); + const files = fs.readdirSync(panelImagesPath); + const images = []; + for (const file of files) { + images.push(path.join(panelImagesPath, file)); + } + const success = []; + const failed = []; + for (const id of ids) { + const index = Number(id) - 1; + if (index < 0 || index >= images.length) { + failed.push(id); + continue; + } + const imagePath = images[index]; + fs.unlinkSync(imagePath); + success.push(id); + } + const msgs = [ + `成功删除ID为${success.join(',')}的图片`, + failed ? `删除失败ID为${failed.join(',')}` : '无失败ID', + '删除后会重新排序ID,若想要再次删除,请重新获取图片列表,否则可能会删除错误的图片。', + ]; + this.reply(common.makeForwardMsg(this.e, msgs)); + return false; +}