From d7d7e237e686e7e84532058667c66bcb4f9c27eb Mon Sep 17 00:00:00 2001 From: RTAkland Date: Thu, 3 Oct 2024 16:59:07 +0800 Subject: [PATCH] feat: add latest commmit info and add language name on github repo card --- src/main/kotlin/cn/rtast/fancybot/Const.kt | 3 +- .../lookup/GithubLatestCommitCommand.kt | 58 +++++++++++++++++++ .../commands/parse/GitHubParseCommand.kt | 8 +++ .../fancybot/entity/github/LatestCommit.kt | 23 ++++++++ 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/cn/rtast/fancybot/commands/lookup/GithubLatestCommitCommand.kt create mode 100644 src/main/kotlin/cn/rtast/fancybot/entity/github/LatestCommit.kt diff --git a/src/main/kotlin/cn/rtast/fancybot/Const.kt b/src/main/kotlin/cn/rtast/fancybot/Const.kt index 789ea6b..f3fbe33 100644 --- a/src/main/kotlin/cn/rtast/fancybot/Const.kt +++ b/src/main/kotlin/cn/rtast/fancybot/Const.kt @@ -105,5 +105,6 @@ val commands = listOf( QQMusicCommand(), BullShitGenerateCommand(), NiuziRankCommand(), NiuziBankRankCommand(), ShotSelfCommand(), TenSetuCommand(), - ReactionCommand(), MCSkinCommand() + ReactionCommand(), MCSkinCommand(), + GithubLatestCommitCommand() ) \ No newline at end of file diff --git a/src/main/kotlin/cn/rtast/fancybot/commands/lookup/GithubLatestCommitCommand.kt b/src/main/kotlin/cn/rtast/fancybot/commands/lookup/GithubLatestCommitCommand.kt new file mode 100644 index 0000000..da4aba9 --- /dev/null +++ b/src/main/kotlin/cn/rtast/fancybot/commands/lookup/GithubLatestCommitCommand.kt @@ -0,0 +1,58 @@ +/* + * Copyright © 2024 RTAkland + * Author: RTAkland + * Date: 2024/10/3 + */ + + +package cn.rtast.fancybot.commands.lookup + +import cn.rtast.fancybot.annotations.CommandDescription +import cn.rtast.fancybot.commands.parse.GitHubParseCommand +import cn.rtast.fancybot.configManager +import cn.rtast.fancybot.entity.github.LatestCommit +import cn.rtast.fancybot.util.Http +import cn.rtast.fancybot.util.str.fromArrayJson +import cn.rtast.rob.entity.GroupMessage +import cn.rtast.rob.util.BaseCommand +import cn.rtast.rob.util.ob.MessageChain +import cn.rtast.rob.util.ob.OneBotListener + +@CommandDescription("查询Github上的仓库的最新一次的提交信息") +class GithubLatestCommitCommand : BaseCommand() { + override val commandNames = listOf(".commit") + + private val userInfoUrl = "https://api.github.com/repos" + + override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List) { + if (args.isEmpty()) { + message.reply("发送`.commit <用户名/仓库名>`即可查询仓库最新一次的提交信息") + } + try { + val (user, repo) = if (args.size == 1) { + args.first().split("/").first() to args.first().split("/").last() + } else { + args.first() to args.last() + } + val response = Http.get( + "$userInfoUrl/$user/$repo/commits", + headers = mapOf("Authorization" to configManager.githubKey) + ).fromArrayJson>().first() + val image = GitHubParseCommand.creatRepoImage(user, repo) + val msg = MessageChain.Builder() + .addImage(image, true) + .addText("最新提交ID: ${response.sha.substring(0, 7)}") + .addNewLine() + .addText("提交者: ${response.commit.committer.name}(${response.commit.committer.email})") + .addNewLine() + .addText("提交日期: ${response.commit.committer.date}") + .addNewLine() + .addText("https://github.com/$user/$repo") + .build() + message.reply(msg) + } catch (e: Exception) { + e.printStackTrace() + message.reply("查询失败~") + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/cn/rtast/fancybot/commands/parse/GitHubParseCommand.kt b/src/main/kotlin/cn/rtast/fancybot/commands/parse/GitHubParseCommand.kt index 29c47d2..48cfa60 100644 --- a/src/main/kotlin/cn/rtast/fancybot/commands/parse/GitHubParseCommand.kt +++ b/src/main/kotlin/cn/rtast/fancybot/commands/parse/GitHubParseCommand.kt @@ -111,6 +111,8 @@ object GitHubParseCommand { g2d.drawString(repoStat.description ?: "暂无描述~", 80, 400, 800) val avatarImage = ImageIO.read(URI(repoStat.owner.avatarUrl).toURL()) g2d.drawCustomImage(avatarImage, 1200, 160, 300.0, 300.0, true) + g2d.color = this.getLanguageColor(repoStat.language) + g2d.drawString(repoStat.language, 30, CANVAS_HEIGHT - 50) g2d.dispose() return canvas.toByteArray().encodeToBase64() } @@ -123,4 +125,10 @@ object GitHubParseCommand { .build() message.reply(msg) } + + fun creatRepoImage(user: String, repo: String): String { + val repoStat = this.getRepoStat(user, repo) + val image = this.createImage(repoStat) + return image + } } \ No newline at end of file diff --git a/src/main/kotlin/cn/rtast/fancybot/entity/github/LatestCommit.kt b/src/main/kotlin/cn/rtast/fancybot/entity/github/LatestCommit.kt new file mode 100644 index 0000000..f0c29b0 --- /dev/null +++ b/src/main/kotlin/cn/rtast/fancybot/entity/github/LatestCommit.kt @@ -0,0 +1,23 @@ +/* + * Copyright © 2024 RTAkland + * Author: RTAkland + * Date: 2024/10/3 + */ + + +package cn.rtast.fancybot.entity.github + +data class LatestCommit( + val sha: String, + val commit: Commit +) { + data class Commit( + val committer: Committer + ) + + data class Committer( + val email: String, + val name: String, + val date: String, + ) +} \ No newline at end of file