2022-07-30 21:06:00 +00:00
|
|
|
|
import fs from 'fs'
|
|
|
|
|
import { promisify } from 'util'
|
|
|
|
|
import { pipeline } from 'stream'
|
|
|
|
|
import { segment } from 'oicq'
|
|
|
|
|
import MD5 from 'md5'
|
|
|
|
|
import fetch from 'node-fetch'
|
|
|
|
|
import lodash from 'lodash'
|
2022-08-18 10:13:42 +00:00
|
|
|
|
import { Data } from '../../components/index.js'
|
|
|
|
|
import { Character } from '../../models/index.js'
|
2022-06-25 13:56:05 +00:00
|
|
|
|
|
2022-07-30 21:06:00 +00:00
|
|
|
|
const resPath = process.cwd() + '/plugins/miao-plugin/resources/'
|
|
|
|
|
let regex = /^#?\s*(?:喵喵)?(?:上传|添加)(.+)(?:照片|写真|图片|图像)\s*$/
|
2022-06-25 13:56:05 +00:00
|
|
|
|
|
|
|
|
|
export const rule = {
|
|
|
|
|
uploadCharacterImage: {
|
|
|
|
|
hashMark: true,
|
2022-07-30 21:06:00 +00:00
|
|
|
|
reg: '^#*喵喵(上传|添加)(.+)写真.*$',
|
|
|
|
|
describe: '喵喵上传角色写真'
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-25 13:56:05 +00:00
|
|
|
|
|
2022-07-30 21:06:00 +00:00
|
|
|
|
export async function uploadCharacterImg (e) {
|
|
|
|
|
let promise = await isAllowedToUploadCharacterImage(e)
|
2022-06-25 13:56:05 +00:00
|
|
|
|
if (!promise) {
|
2022-07-30 21:06:00 +00:00
|
|
|
|
return
|
2022-06-25 13:56:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-30 21:06:00 +00:00
|
|
|
|
let imageMessages = []
|
|
|
|
|
let msg = e.msg
|
|
|
|
|
let regRet = regex.exec(msg)
|
|
|
|
|
// 通过解析正则获取消息中的角色名
|
2022-06-27 20:46:49 +00:00
|
|
|
|
if (!regRet || !regRet[1]) {
|
2022-07-30 21:06:00 +00:00
|
|
|
|
return
|
2022-06-25 13:56:05 +00:00
|
|
|
|
}
|
2022-07-30 21:06:00 +00:00
|
|
|
|
let char = Character.get(regRet[1])
|
2022-06-27 20:46:49 +00:00
|
|
|
|
if (!char || !char.name) {
|
2022-07-30 21:06:00 +00:00
|
|
|
|
return
|
2022-06-27 20:46:49 +00:00
|
|
|
|
}
|
2022-07-30 21:06:00 +00:00
|
|
|
|
let name = char.name
|
2022-06-25 13:56:05 +00:00
|
|
|
|
for (let val of e.message) {
|
2022-07-30 21:06:00 +00:00
|
|
|
|
if (val.type === 'image') {
|
|
|
|
|
imageMessages.push(val)
|
2022-06-25 13:56:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (imageMessages.length <= 0) {
|
2022-06-27 20:46:49 +00:00
|
|
|
|
// TODO 支持at图片添加,以及支持后发送
|
2022-07-30 21:06:00 +00:00
|
|
|
|
e.reply('消息中未找到图片,请将要发送的图片与消息一同发送..')
|
|
|
|
|
return true
|
2022-06-25 13:56:05 +00:00
|
|
|
|
}
|
2022-07-30 21:06:00 +00:00
|
|
|
|
await saveImages(e, name, imageMessages)
|
|
|
|
|
return true
|
2022-06-25 13:56:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-30 21:06:00 +00:00
|
|
|
|
async function saveImages (e, name, imageMessages) {
|
2022-08-24 01:07:06 +00:00
|
|
|
|
let imgMaxSize = e?.groupConfig?.imgMaxSize || 5
|
2022-07-30 21:06:00 +00:00
|
|
|
|
let pathSuffix = `character-img/${name}/upload`
|
|
|
|
|
let path = resPath + pathSuffix
|
2022-06-25 13:56:05 +00:00
|
|
|
|
|
|
|
|
|
if (!fs.existsSync(path)) {
|
2022-09-04 09:33:14 +00:00
|
|
|
|
Data.createDir(pathSuffix, resPath)
|
2022-06-25 13:56:05 +00:00
|
|
|
|
}
|
2022-07-30 21:06:00 +00:00
|
|
|
|
let senderName = lodash.truncate(e.sender.card, { length: 8 })
|
|
|
|
|
let imgCount = 0
|
2022-06-25 13:56:05 +00:00
|
|
|
|
for (let val of imageMessages) {
|
2022-07-30 21:06:00 +00:00
|
|
|
|
const response = await fetch(val.url)
|
2022-06-25 13:56:05 +00:00
|
|
|
|
if (!response.ok) {
|
2022-07-30 21:06:00 +00:00
|
|
|
|
e.reply('图片下载失败。')
|
|
|
|
|
return true
|
2022-06-25 13:56:05 +00:00
|
|
|
|
}
|
2022-07-30 21:06:00 +00:00
|
|
|
|
if (response.headers.get('size') > 1024 * 1024 * imgMaxSize) {
|
|
|
|
|
e.reply([segment.at(e.user_id, senderName), '添加失败:图片太大了。'])
|
|
|
|
|
return true
|
2022-06-25 13:56:05 +00:00
|
|
|
|
}
|
2022-07-30 21:06:00 +00:00
|
|
|
|
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'
|
2022-06-25 13:56:05 +00:00
|
|
|
|
}
|
2022-07-30 21:06:00 +00:00
|
|
|
|
let imgPath = `${path}/${fileName}.${fileType}`
|
|
|
|
|
const streamPipeline = promisify(pipeline)
|
|
|
|
|
await streamPipeline(response.body, fs.createWriteStream(imgPath))
|
2022-06-27 20:46:49 +00:00
|
|
|
|
|
|
|
|
|
// 使用md5作为文件名
|
2022-07-30 21:06:00 +00:00
|
|
|
|
let buffers = fs.readFileSync(imgPath)
|
|
|
|
|
let base64 = Buffer.from(buffers, 'base64').toString()
|
|
|
|
|
let md5 = MD5(base64)
|
2022-06-27 20:46:49 +00:00
|
|
|
|
let newImgPath = `${path}/${md5}.${fileType}`
|
|
|
|
|
if (fs.existsSync(newImgPath)) {
|
|
|
|
|
fs.unlink(newImgPath, (err) => {
|
2022-07-30 21:06:00 +00:00
|
|
|
|
console.log('unlink', err)
|
|
|
|
|
})
|
2022-06-27 20:46:49 +00:00
|
|
|
|
}
|
|
|
|
|
fs.rename(imgPath, newImgPath, (err) => {
|
2022-07-30 21:06:00 +00:00
|
|
|
|
console.log('rename', err)
|
2022-06-27 20:46:49 +00:00
|
|
|
|
})
|
2022-07-30 21:06:00 +00:00
|
|
|
|
imgCount++
|
|
|
|
|
Bot.logger.mark(`添加成功: ${path}/${fileName}`)
|
2022-06-25 13:56:05 +00:00
|
|
|
|
}
|
2022-07-30 21:06:00 +00:00
|
|
|
|
e.reply([segment.at(e.user_id, senderName), `\n成功添加${imgCount}张${name}图片。`])
|
|
|
|
|
return true
|
2022-06-25 13:56:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-30 21:06:00 +00:00
|
|
|
|
async function isAllowedToUploadCharacterImage (e) {
|
2022-06-25 13:56:05 +00:00
|
|
|
|
if (!e.message) {
|
2022-07-30 21:06:00 +00:00
|
|
|
|
return false
|
2022-06-25 13:56:05 +00:00
|
|
|
|
}
|
|
|
|
|
if (!e.msg) {
|
2022-07-30 21:06:00 +00:00
|
|
|
|
return false
|
2022-06-25 13:56:05 +00:00
|
|
|
|
}
|
2022-06-27 20:46:49 +00:00
|
|
|
|
if (!e.isMaster) {
|
2022-07-30 21:06:00 +00:00
|
|
|
|
return false
|
2022-06-27 20:46:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 由于添加角色图是全局,暂时屏蔽非管理员的添加
|
2022-06-25 13:56:05 +00:00
|
|
|
|
if (e.isPrivate) {
|
|
|
|
|
if (!e.isMaster) {
|
2022-07-30 21:06:00 +00:00
|
|
|
|
e.reply('只有主人才能添加。')
|
|
|
|
|
return false
|
2022-06-25 13:56:05 +00:00
|
|
|
|
}
|
2022-07-30 21:06:00 +00:00
|
|
|
|
return true
|
2022-06-25 13:56:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-30 21:06:00 +00:00
|
|
|
|
let groupId = e.group_id
|
|
|
|
|
if (!groupId) {
|
|
|
|
|
return false
|
2022-06-25 13:56:05 +00:00
|
|
|
|
}
|
2022-09-04 09:33:14 +00:00
|
|
|
|
if (e.groupConfig?.imgAddLimit === 2) {
|
2022-06-25 13:56:05 +00:00
|
|
|
|
if (!e.isMaster) {
|
2022-07-30 21:06:00 +00:00
|
|
|
|
e.reply('只有主人才能添加。')
|
|
|
|
|
return false
|
2022-06-25 13:56:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-04 09:33:14 +00:00
|
|
|
|
if (e.groupConfig?.imgAddLimit === 1 && !e.isMaster) {
|
2022-07-30 21:06:00 +00:00
|
|
|
|
if (!(e.sender.role === 'owner' || e.sender.role === 'admin')) {
|
|
|
|
|
e.reply('只有管理员才能添加。')
|
|
|
|
|
return false
|
2022-06-25 13:56:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-30 21:06:00 +00:00
|
|
|
|
return true
|
2022-06-25 13:56:05 +00:00
|
|
|
|
}
|