UNPKG

koishi-plugin-gh-og

Version:

发送GitHub项目名,生成预览图,基于og插件

57 lines (56 loc) 2.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.apply = exports.name = exports.Config = void 0; const koishi_1 = require("koishi"); exports.Config = koishi_1.Schema.object({ chick_url: koishi_1.Schema.computed(koishi_1.Schema.boolean()).default(false).description('是否同时检测github链接。'), show_url: koishi_1.Schema.computed(koishi_1.Schema.boolean()).default(false).description('出图成功后,是否展示github链接。'), }); exports.name = 'OpenGraph'; const extractRepoName = (url) => { const parts = url.split('/'); const username = parts[3]; const repoName = parts[4]; return `${username}/${repoName}`; }; async function fetchGitHubData(ctx, session, project, config) { try { const response = await ctx.http.get(`https://github.com/${project}`); // 如果项目存在,GitHub API 会返回项目的相关信息 console.log('项目存在:', response.data); const githubUrl = `https://github.com/${project}`; const githubcard = `https://opengraph.githubassets.com/githubcard/${project}`; if (config.show_url == true) { await session.send(`${githubUrl}<image url="${githubcard}"/>`); } else { await session.send(`<image url="${githubcard}"/>`); } } catch (error) { if (error.response && error.response.status === 404) { console.error('项目不存在'); } else { console.error('发生错误:', error); } } } function apply(ctx, config) { ctx.on('message', async (session) => { const input = session.content.trim(); const githubRepoPattern = /^[A-Za-z0-9_-]+\/[A-Za-z0-9_.-]+$/; let project; if (config.chick_url) { if (input.startsWith(`https://github.com/`)) { project = extractRepoName(input); await fetchGitHubData(ctx, session, project, config); } } if (githubRepoPattern.test(input)) { project = input; await fetchGitHubData(ctx, session, project, config); } }); } exports.apply = apply;