Skip to content

Commit

Permalink
fix:修改帮助图样式,删除功能,增加输入为空提示
Browse files Browse the repository at this point in the history
  • Loading branch information
hanhan258 committed Jul 27, 2023
1 parent 0f868b5 commit b5901d2
Show file tree
Hide file tree
Showing 38 changed files with 1,715 additions and 56 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# 1.1.0
* 删除了一些功能,修改了帮助图样式
2 changes: 1 addition & 1 deletion apps/girl.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class girl extends plugin {
console.log(data.images[0].url)
// 发送消息
try {
sendmsg.push(data.images[0].url)
// sendmsg.push(data.images[0].url)
sendmsg.push(segment.image(data.images[0].url))
e.reply(sendmsg)
} catch (err) {
Expand Down
77 changes: 58 additions & 19 deletions apps/help.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,80 @@
import plugin from '../../../lib/plugins/plugin.js'
import lodash from 'lodash'
import { Data } from '../components/index.js'
import HelpTheme from './help/HelpTheme.js'
import runtimeRender from '../common/runtimeRender.js'

export class help extends plugin {
constructor () {
super({
/** 功能名称 */
name: '憨憨Help',
/** 功能描述 */
dsc: '憨憨Help',
/** https://oicqjs.github.io/oicq/#events */
name: '憨憨帮助',
dsc: '憨憨帮助',
event: 'message',
/** 优先级,数字越小等级越高 */
priority: 6,
priority: 100,
rule: [
{
/** 命令正则匹配 */
reg: '#?(nav|憨憨帮助)$',
/** 执行方法 */
fnc: 'hanhanHelp'
fnc: 'help'
},
{
/** 命令正则匹配 */
reg: '^#?搜一搜帮助$',
/** 执行方法 */
fnc: 'so_help'
}
]
})
}

// 憨憨帮助
async hanhanHelp (e) {
await e.runtime.render('hanhan-plugin', '/help/help.html')
async help (e) {
let custom = {}
let help = {}
let { diyCfg, sysCfg } = await Data.importCfg('help')

// 兼容一下旧字段
if (lodash.isArray(help.helpCfg)) {
custom = {
helpList: help.helpCfg,
helpCfg: {}
}
} else {
custom = help
}

let helpConfig = lodash.defaults(diyCfg.helpCfg || {}, custom.helpCfg, sysCfg.helpCfg)
let helpList = diyCfg.helpList || custom.helpList || sysCfg.helpList

let helpGroup = []

lodash.forEach(helpList, (group) => {
if (group.auth && group.auth === 'master' && !e.isMaster) {
return true
}

lodash.forEach(group.list, (help) => {
let icon = help.icon * 1
if (!icon) {
help.css = 'display:none'
} else {
let x = (icon - 1) % 10
let y = (icon - x - 1) / 10
help.css = `background-position:-${x * 50}px -${y * 50}px`
}
})

helpGroup.push(group)
})
let themeData = await HelpTheme.getThemeData(diyCfg.helpCfg || {}, sysCfg.helpCfg || {})
return await runtimeRender(e, 'help/index', {
helpCfg: helpConfig,
helpGroup,
...themeData,
element: 'default'
}, {
scale: 1.6
})
}

async so_help (e) {
/** e.msg 用户的命令消息 */
logger.info('[用户命令]', e.msg)
await e.runtime.render('hanhan-plugin', '/help/sys.html')
/** e.msg 用户的命令消息 */
logger.info('[用户命令]', e.msg)
await e.runtime.render('hanhan-plugin', '/help/sys.html')
}
}
68 changes: 68 additions & 0 deletions apps/help/HelpTheme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import lodash from 'lodash'
import fs from 'fs'
import { Data } from '../../components/index.js'

let HelpTheme = {
async getThemeCfg (theme, exclude) {
let dirPath = './plugins/hanhan-plugin/resources/help/theme/'
let ret = []
let names = []
let dirs = fs.readdirSync(dirPath)
lodash.forEach(dirs, (dir) => {
if (fs.existsSync(`${dirPath}${dir}/main.png`)) {
names.push(dir)
}
})
if (lodash.isArray(theme)) {
ret = lodash.intersection(theme, names)
} else if (theme === 'all') {
ret = names
}
if (exclude && lodash.isArray(exclude)) {
ret = lodash.difference(ret, exclude)
}
if (ret.length === 0) {
ret = ['default']
}
let name = lodash.sample(ret)
let resPath = '{{_res_path}}/help/theme/'
return {
main: `${resPath}${name}/main.png`,
bg: fs.existsSync(`${dirPath}${name}/bg.jpg`) ? `${resPath}${name}/bg.jpg` : `${resPath}default/bg.jpg`,
style: (await Data.importModule(`resources/help/theme/${name}/config.js`)).style || {}
}
},
async getThemeData (diyStyle, sysStyle) {
let helpConfig = lodash.extend({}, sysStyle, diyStyle)
let colCount = Math.min(5, Math.max(parseInt(helpConfig?.colCount) || 3, 2))
let colWidth = Math.min(500, Math.max(100, parseInt(helpConfig?.colWidth) || 265))
let width = Math.min(2500, Math.max(800, colCount * colWidth + 30))
let theme = await HelpTheme.getThemeCfg(diyStyle.theme || sysStyle.theme, diyStyle.themeExclude || sysStyle.themeExclude)
let themeStyle = theme.style || {}
let ret = [`
body{background-image:url(${theme.bg});width:${width}px;}
.container{background-image:url(${theme.main});width:${width}px;}
.help-table .td,.help-table .th{width:${100 / colCount}%}
`]
let css = function (sel, css, key, def, fn) {
let val = Data.def(themeStyle[key], diyStyle[key], sysStyle[key], def)
if (fn) {
val = fn(val)
}
ret.push(`${sel}{${css}:${val}}`)
}
css('.help-title,.help-group', 'color', 'fontColor', '#ceb78b')
css('.help-title,.help-group', 'text-shadow', 'fontShadow', 'none')
css('.help-desc', 'color', 'descColor', '#eee')
css('.cont-box', 'background', 'contBgColor', 'rgba(43, 52, 61, 0.8)')
css('.cont-box', 'backdrop-filter', 'contBgBlur', 3, (n) => diyStyle.bgBlur === false ? 'none' : `blur(${n}px)`)
css('.help-group', 'background', 'headerBgColor', 'rgba(34, 41, 51, .4)')
css('.help-table .tr:nth-child(odd)', 'background', 'rowBgColor1', 'rgba(34, 41, 51, .2)')
css('.help-table .tr:nth-child(even)', 'background', 'rowBgColor2', 'rgba(34, 41, 51, .4)')
return {
style: `<style>${ret.join('\n')}</style>`,
colCount
}
}
}
export default HelpTheme
2 changes: 2 additions & 0 deletions apps/morse.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class morse extends plugin {
// 莫斯加密
async morseEn (e) {
let encode = e.msg.replace(/^#?(|)/, '').trim()
if (!encode) return e.reply("输入不能为空", true)
// standart morse
let result = xmorse.encode(`${encode}`)
await this.reply(result, true)
Expand All @@ -40,6 +41,7 @@ export class morse extends plugin {
// 莫斯解密
async morseDe (e) {
let encode = e.msg.replace(/^#?(|)/, '').trim()
if (!encode) return e.reply("输入不能为空", true)
// standart morse
let result = xmorse.decode(`${encode}`)
await this.reply(result, true)
Expand Down
30 changes: 0 additions & 30 deletions apps/photo.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ export class photo extends plugin {
/** 优先级,数字越小等级越高 */
priority: 6,
rule: [
{
/** 命令正则匹配 */
reg: '^#?天气',
/** 执行方法 */
fnc: 'tianqi'
},
{
/** 命令正则匹配 */
reg: '^#?mc酱$',
Expand Down Expand Up @@ -59,12 +53,6 @@ export class photo extends plugin {
/** 执行方法 */
fnc: 'sjai'
},
{
/** 命令正则匹配 */
reg: '^#?手写',
/** 执行方法 */
fnc: 'sx'
},
{
/** 命令正则匹配 */
reg: '^#?每日英语$',
Expand Down Expand Up @@ -173,24 +161,6 @@ export class photo extends plugin {
await this.reply(sendmsg)
}

// 手写模拟器
async sx (e) {
let encode = e.msg.replace(/^#?/, '').trim()
// 发送消息
await this.reply(segment.image(`https://zj.v.api.aa1.cn/api/zuoye/?msg=${encode}`))
return true // 返回true 阻挡消息不再往下
}

// 猫羽雫图片天气
async tianqi (e) {
let encode = e.msg.replace(/^#?/, '').trim()
let img = segment.image(`http://api.caonm.net/api/qqtq/t?msg=${encode}&key=9eEVLhmjy98VKTkg4jSuUo2vVO`)
// 发送消息
if (img) this.reply(img)
else this.reply('图片裂开了捏~')
return true // 返回true 阻挡消息不再往下
}

// mc酱
async mc (e) {
// 发送消息
Expand Down
6 changes: 0 additions & 6 deletions apps/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ export class text extends plugin {
/** 优先级,数字越小等级越高 */
priority: 6,
rule: [
{
/** 命令正则匹配 */
reg: '^#?发癫',
/** 执行方法 */
fnc: 'fd'
},
{
/** 命令正则匹配 */
reg: '^#?今天是几号$',
Expand Down
4 changes: 4 additions & 0 deletions apps/urlandbase.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,31 @@ export class urlAndBase extends plugin {
// url编码
async urlEn (e) {
let encode = e.msg.replace(/^#?(url|URL)/, '').trim()
if (!encode) return e.reply("输入不能为空", true)
let result = encodeURI(encode)
await this.reply(result, true)
}

// url解码
async urlDe (e) {
let encode = e.msg.replace(/^#?(url|URL)/, '').trim()
if (!encode) return e.reply("输入不能为空", true)
let result = decodeURI(encode)
await this.reply(result, true)
}

// base64编码
async baseEn (e) {
let encode = e.msg.replace(/^#?(base64|Base64)/, '').trim()
if (!encode) return e.reply("输入不能为空", true)
let result = Buffer.from(encode).toString('base64')
await this.reply(result, true)
}

// base64解码
async baseDe (e) {
let encode = e.msg.replace(/^#?(base64|Base64)/, '').trim()
if (!encode) return e.reply("输入不能为空", true)
let result = Buffer.from(encode, 'base64').toString()
await this.reply(result, true)
}
Expand Down
65 changes: 65 additions & 0 deletions common/runtimeRender.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import setting from '../utils/setting.js'
import Version from '../components/Version.js'
const decimalAdjust = (type, value, exp = 0) => {
type = String(type)
if (!['round', 'floor', 'ceil'].includes(type)) {
throw new TypeError(
"The type of decimal adjustment must be one of 'round', 'floor', or 'ceil'."
)
}
exp = Number(exp)
value = Number(value)
if (exp % 1 !== 0 || Number.isNaN(value)) {
return NaN
} else if (exp === 0) {
return Math[type](value)
}
const [magnitude, exponent = 0] = value.toString().split('e')
const adjustedValue = Math[type](`${magnitude}e${exponent - exp}`)
// Shift back
const [newMagnitude, newExponent = 0] = adjustedValue.toString().split('e')
return Number(`${newMagnitude}e${+newExponent + exp}`)
}
const MathPro = {
floor10: function (value, exp) {
return decimalAdjust('floor', value, exp)
},
ceil10: function (value, exp) {
return decimalAdjust('ceil', value, exp)
},
round10: function (value, exp) {
return decimalAdjust('round', value, exp)
}
}
export default function runtimeRender (e, path, renderData = {}, cfg = {}) {
if (!e.runtime) {
console.log('未找到e.runtime,请升级至最新版Yunzai')
}
let scale = setting.getConfig('gachaHelp').renderScale || 100
scale = Math.min(2, Math.max(0.5, scale / 100))
scale = (cfg.scale || 1) * scale
const pct = `style='transform:scale(${scale})'`
const layoutPath =
process.cwd() + '/plugins/hanhan-plugin/resources/common/layout/'
return e.runtime.render('hanhan-plugin', path, renderData, {
...cfg,
beforeRender ({ data }) {
let resPath = data.pluResPath
return {
...data,
_res_path: resPath,
_layout_path: layoutPath,
defaultLayout: layoutPath + 'default.html',
elemLayout: layoutPath + 'elem.html',
sys: {
scale: pct,
copyright: `Created By ${Version.name}<span class="version">${Version.yunzai}</span> & hanhan-plugin<span class="version">${Version.version}</span>`,
createdby: `Created By ${Version.name} & hanhan-plugin`,
},
Math,
MathPro,
quality: 100
}
}
})
}
Loading

0 comments on commit b5901d2

Please sign in to comment.