import { definePlugin, http } from 'kivibot' const token = process.env.GITHUB_TOKEN || '' const gistId = '05b93ae9e37d0b68056438bf211942c5' const cache = { data: {} as any, } export default definePlugin({ name: 'gist', version: '1.2.0', setup(ctx) { ctx.handle('message', async (e) => { if (e.raw_message === '#插件市场') { if (!cache.data.files) await updateCache() const files = cache.data.files as any[] const updatedAt = ctx.dayjs(cache.data.updated_at).format('YYYY-MM-DD HH:mm:ss') e.reply( `〓 插件市场 〓\n\n${Object.keys(files) .sort() .map((name, idx) => `📦 ${idx + 1}. ${name.replace('.ts', '')}`) .join('\n')}\n\n更新时间: ${updatedAt}\n获取方式:#获取插件 <序号>`, ) } if (e.raw_message.startsWith('#获取插件')) { const index = Number.parseInt(e.raw_message.replace('#获取插件', '').trim(), 10) if (Number.isNaN(index)) { e.reply('请输入正确的序号', true) return } if (!cache.data.files) await updateCache() const targetKey = Object.keys(cache.data.files as any[]).sort()[index - 1] const target = cache.data.files[targetKey] if (!target) { e.reply('该序号对应插件不存在', true) return } if (ctx.isGroupMsg(e)) { await e.reply(`已通过私聊发送插件 ${target.filename.replace('.ts', '')},请查收`, true) } const friend = ctx.bot.pickFriend(e.sender.user_id) if (!friend) { e.reply('请先添加机器人为好友以便接收私聊消息', true) return } await friend.sendMsg( `〓 插件详情 〓\n\n文件名: ${target.filename}\n大小: ${ctx.filesize(target.size)}\n\n下载直链:${target.raw_url}`, ) if (!target.truncated && target.content.length <= 3_800) { await friend.sendMsg(target.content) } else { await friend.sendMsg('代码内容过长,请自行下载查看') } } }) }, }) async function updateCache() { const { data } = await http.get(`https://proxy.viki.moe/gists/${gistId}?proxy-host=api.github.com`, { headers: { Accept: 'application/vnd.github+json', 'X-GitHub-Api-Version': '2022-11-28', ...(token ? { Authorization: `Bearer ${token}` } : {}), }, }) cache.data = data setTimeout( () => { cache.data = {} }, 1 * 60 * 1_000, ) }