ZZZ-Plugin/apps/manage/panel.js
2024-08-01 19:15:27 +08:00

97 lines
2.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { char } from '../../lib/convert.js';
import { downloadFile } from '../../lib/download.js';
import { imageResourcesPath } from '../../lib/path.js';
import path from 'path';
export async function uploadCharacterImg() {
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 images = [];
// 下面方法来源于miao-plugin/apps/character/ImgUpload.js
for (const val of this.e.message) {
if (val.type === 'image') {
images.push(val);
}
}
if (images.length === 0) {
let source;
if (this.e.getReply) {
source = await this.e.getReply();
} else if (this.e.source) {
if (this.e.group?.getChatHistory) {
// 支持at图片添加以及支持后发送
source = (
await this.e.group.getChatHistory(this.e.source?.seq, 1)
).pop();
} else if (this.e.friend?.getChatHistory) {
source = (
await this.e.friend.getChatHistory(this.e.source?.time + 1, 1)
).pop();
}
}
if (source) {
for (const val of source.message) {
if (val.type === 'image') {
images.push(val);
} else if (val.type === 'xml' || val.type === 'forward') {
let resid;
try {
resid = val.data
.match(/m_resid="(\d|\w|\/|\+)*"/)[0]
.replace(/m_resid=|"/g, '');
} catch (err) {
resid = val.id;
}
if (!resid) break;
let message = await this.e.bot.getForwardMsg(resid);
for (const item of message) {
for (const i of item.message) {
if (i.type === 'image') {
images.push(i);
}
}
}
}
}
}
}
if (images.length <= 0) {
this.reply(
'消息中未找到图片,请将要发送的图片与消息一同发送或引用要添加的图像。'
);
return false;
}
const resourcesImagesPath = imageResourcesPath;
const panelImagesPath = path.join(resourcesImagesPath, `panel/${name}`);
let success = 0;
let failed = 0;
for (const image of images) {
let fileName = new Date().getTime().toString();
let fileType = 'png';
if (image.file) {
fileName = image.file.substring(0, image.file.lastIndexOf('.'));
fileType = image.file.substring(image.file.lastIndexOf('.') + 1);
}
const filePath = path.join(panelImagesPath, `${fileName}.${fileType}`);
const result = await downloadFile(image.url, filePath);
if (result) {
success++;
} else {
failed++;
}
}
this.reply(`成功上传${success}张图片,失败${failed}张图片。`);
return false;
}