2024-02-04 01:27:34 +00:00
|
|
|
|
import { Button, Character, MysApi, Player } from '#miao.models'
|
2023-10-20 08:59:06 +00:00
|
|
|
|
import { Cfg, Common, Meta } from '#miao'
|
2022-07-23 20:32:10 +00:00
|
|
|
|
import lodash from 'lodash'
|
2023-02-08 20:55:54 +00:00
|
|
|
|
import moment from 'moment'
|
2024-05-21 18:05:37 +00:00
|
|
|
|
import { miaoPath } from '#miao.path'
|
2022-06-25 23:45:43 +00:00
|
|
|
|
|
2023-02-13 19:47:22 +00:00
|
|
|
|
let Avatar = {
|
|
|
|
|
render (e) {
|
|
|
|
|
if (!e.char) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
return false
|
2022-06-25 23:45:43 +00:00
|
|
|
|
}
|
2023-02-13 19:47:22 +00:00
|
|
|
|
return Avatar.renderAvatar(e, e.char?.name)
|
|
|
|
|
},
|
|
|
|
|
async renderAvatar (e, avatar, renderType = 'card') {
|
|
|
|
|
// 如果传递的是名字,则获取
|
|
|
|
|
if (typeof (avatar) === 'string') {
|
|
|
|
|
// 检查角色
|
|
|
|
|
let char = Character.get(avatar)
|
|
|
|
|
if (!char) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
let mys = await MysApi.init(e)
|
|
|
|
|
if (!mys) return true
|
|
|
|
|
if (!char.isRelease) {
|
2023-02-08 20:55:54 +00:00
|
|
|
|
avatar = { id: char.id, name: char.name, detail: false }
|
2023-02-13 19:47:22 +00:00
|
|
|
|
} else {
|
|
|
|
|
let player = Player.create(e)
|
2023-02-19 04:05:42 +00:00
|
|
|
|
await player.refreshMysDetail(1)
|
|
|
|
|
await player.refreshTalent(char.id, 1)
|
2023-02-13 19:47:22 +00:00
|
|
|
|
avatar = player.getAvatar(char.id)
|
|
|
|
|
if (!avatar) {
|
|
|
|
|
avatar = { id: char.id, name: char.name, detail: false }
|
|
|
|
|
}
|
2022-06-29 23:05:31 +00:00
|
|
|
|
}
|
2022-06-25 23:45:43 +00:00
|
|
|
|
}
|
2023-02-13 19:47:22 +00:00
|
|
|
|
return await Avatar.renderCard(e, avatar, renderType)
|
|
|
|
|
},
|
2022-06-25 23:45:43 +00:00
|
|
|
|
|
2023-02-13 19:47:22 +00:00
|
|
|
|
async renderCard (e, avatar, renderType = 'card') {
|
|
|
|
|
let char = Character.get(avatar.id)
|
|
|
|
|
if (!char) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
let bg = char.getCardImg(Cfg.get('charPicSe', false))
|
2023-10-31 20:07:47 +00:00
|
|
|
|
if (!bg) {
|
|
|
|
|
e.reply(`${char.name}暂无角色图片`)
|
|
|
|
|
return true
|
|
|
|
|
}
|
2023-02-13 19:47:22 +00:00
|
|
|
|
if (renderType === 'photo') {
|
2024-05-21 18:05:37 +00:00
|
|
|
|
e.reply(segment.image(`file://${miaoPath}/resources/${bg.img}`))
|
2023-02-13 19:47:22 +00:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
let uid = e.uid || (e.targetUser && e.targetUser.uid)
|
|
|
|
|
let data = {}
|
|
|
|
|
let custom = char.isCustom
|
|
|
|
|
let isRelease = char.isRelease
|
2023-02-20 03:52:13 +00:00
|
|
|
|
if (isRelease && avatar.hasData) {
|
2023-02-13 19:47:22 +00:00
|
|
|
|
data = avatar.getDetail()
|
|
|
|
|
data.imgs = char.imgs
|
|
|
|
|
data.source = avatar._source
|
2023-10-19 16:31:35 +00:00
|
|
|
|
data.artis = avatar.getArtisDetail(true)
|
2023-02-13 19:47:22 +00:00
|
|
|
|
data.updateTime = moment(new Date(avatar._time)).format('MM-DD HH:mm')
|
|
|
|
|
if (data.hasTalent) {
|
|
|
|
|
data.talent = avatar.talent
|
|
|
|
|
data.talentMap = ['a', 'e', 'q']
|
|
|
|
|
// 计算皇冠个数
|
|
|
|
|
data.crownNum = lodash.filter(lodash.map(data.talent, (d) => d.original), (d) => d >= 10).length
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
data = char.getData('id,name,sName')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let width = 600
|
|
|
|
|
let imgCss = ''
|
|
|
|
|
let scale = 1.2
|
|
|
|
|
if (bg.mode === 'left') {
|
|
|
|
|
const height = 480
|
|
|
|
|
width = height * bg.width / bg.height
|
|
|
|
|
imgCss = `img.bg{width:auto;height:${height}px;}`
|
|
|
|
|
scale = 1.45
|
|
|
|
|
}
|
|
|
|
|
// 渲染图像
|
2024-02-04 01:27:34 +00:00
|
|
|
|
let msgRes = await e.reply([await Common.render('character/character-card', {
|
2023-02-13 19:47:22 +00:00
|
|
|
|
saveId: uid,
|
|
|
|
|
uid,
|
|
|
|
|
bg,
|
|
|
|
|
widthStyle: `<style>html,body,#container{width:${width}px} ${imgCss}</style>`,
|
|
|
|
|
mode: bg.mode,
|
|
|
|
|
custom,
|
|
|
|
|
isRelease,
|
|
|
|
|
data
|
2024-05-21 18:05:37 +00:00
|
|
|
|
}, { e, scale, retType: 'base64' }), new Button(e).profile(char, uid)])
|
2023-07-20 00:31:09 +00:00
|
|
|
|
if (msgRes) {
|
2023-02-13 19:47:22 +00:00
|
|
|
|
// 如果消息发送成功,就将message_id和图片路径存起来,3小时过期
|
2023-07-20 00:31:09 +00:00
|
|
|
|
const message_id = [e.message_id]
|
|
|
|
|
if (Array.isArray(msgRes.message_id)) {
|
|
|
|
|
message_id.push(...msgRes.message_id)
|
|
|
|
|
} else {
|
|
|
|
|
message_id.push(msgRes.message_id)
|
|
|
|
|
}
|
|
|
|
|
for (const i of message_id) {
|
2023-10-20 08:59:06 +00:00
|
|
|
|
await redis.set(`miao:original-picture:${i}`, JSON.stringify({
|
|
|
|
|
type: 'character', img: bg.img
|
|
|
|
|
}), { EX: 3600 * 3 })
|
2023-07-20 00:31:09 +00:00
|
|
|
|
}
|
2023-02-13 19:47:22 +00:00
|
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
|
return true
|
2023-02-13 19:47:22 +00:00
|
|
|
|
},
|
|
|
|
|
check (e) {
|
|
|
|
|
let msg = e.original_msg || e.msg
|
|
|
|
|
if (!msg || !/^#/.exec(msg)) {
|
|
|
|
|
return false
|
2022-09-18 20:54:01 +00:00
|
|
|
|
}
|
2023-02-13 19:47:22 +00:00
|
|
|
|
if (!Common.cfg('avatarCard')) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2024-08-18 09:07:26 +00:00
|
|
|
|
let uidRet = /(18|[1-9])[0-9]{8}/g.exec(msg)
|
2023-02-13 19:47:22 +00:00
|
|
|
|
if (uidRet) {
|
|
|
|
|
e.uid = uidRet[0]
|
|
|
|
|
msg = msg.replace(uidRet[0], '')
|
|
|
|
|
}
|
|
|
|
|
let name = msg.replace(/#|老婆|老公|卡片/g, '').trim()
|
2023-02-08 20:55:54 +00:00
|
|
|
|
|
2023-10-20 08:59:06 +00:00
|
|
|
|
if (e?.runtime?.gsCfg) {
|
|
|
|
|
let gsCfg = e?.runtime?.gsCfg
|
|
|
|
|
Meta.addAliasFn('gs', 'char', (txt) => {
|
2023-11-07 19:07:56 +00:00
|
|
|
|
let roleRet
|
|
|
|
|
if (gsCfg._getRole) {
|
|
|
|
|
roleRet = gsCfg._getRole(txt)
|
|
|
|
|
} else {
|
|
|
|
|
roleRet = gsCfg.getRole(txt)
|
|
|
|
|
}
|
2023-10-20 08:59:06 +00:00
|
|
|
|
if (roleRet.name) {
|
|
|
|
|
return roleRet.name
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-10-23 14:14:09 +00:00
|
|
|
|
let char = Character.get(name.trim(), e.game)
|
2023-02-13 19:47:22 +00:00
|
|
|
|
if (!char) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
e.msg = '#喵喵角色卡片'
|
|
|
|
|
e.char = char
|
|
|
|
|
return true
|
2022-06-25 23:45:43 +00:00
|
|
|
|
}
|
2023-02-13 19:47:22 +00:00
|
|
|
|
|
2022-06-25 23:45:43 +00:00
|
|
|
|
}
|
2023-02-13 19:47:22 +00:00
|
|
|
|
export default Avatar
|