${visibleCommands.map(cmd => {
const desc = session.text([`commands.${cmd.name}.description`, ''], cmd.config['params'])
const hasDesc = desc && desc !== '暂无描述'
return `
${cmd.displayName}
${hasDesc ? `
${desc}
` : ''}
`
}).join('')}
`
// 5. 渲染图片
let page: any
try {
page = await ctx.puppeteer.page()
await page.setViewport({ width: 1280, height: 100, deviceScaleFactor: 1 })
await page.setContent(html)
await page.waitForNetworkIdle()
const image = await page.screenshot({
type: 'jpeg',
quality: config.screenshotQuality,
encoding: 'binary',
fullPage: true
})
// 6. 存入缓存并清理旧缓存
try {
const files = await readdir(cacheDir)
for (const file of files) {
await unlink(path.resolve(cacheDir, file))
}
await writeFile(cachePath, image)
} catch (e) {
logger.error('写入缓存失败:', e)
}
const base64 = await toBase64(cachePath)
return h.image(base64)
} catch (err) {
logger.error('渲染图片失败:', err)
return '渲染图片时发生错误,请稍后再试。'
} finally {
if (page) await page.close()
}
})
}