From 393c4dcec5c6d7dc6ce07a1fb2f067de3e62e2b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E5=B9=95?= <1041291@qq.com> Date: Fri, 18 Jul 2025 19:56:06 +0800 Subject: [PATCH] Update main.py --- main.py | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/main.py b/main.py index 99ca4be..2717571 100644 --- a/main.py +++ b/main.py @@ -1,24 +1,33 @@ -from astrbot.api.event import filter, AstrMessageEvent, MessageEventResult +import re +import uuid +from astrbot.api.event import filter, AstrMessageEvent from astrbot.api.star import Context, Star, register from astrbot.api import logger -@register("helloworld", "YourName", "一个简单的 Hello World 插件", "1.0.0") +GITHUB_URL_PATTERN = r"https://github\.com/[\w\-]+/[\w\-]+(?:/(pull|issues)/\d+)?" +GITHUB_REPO_OPENGRAPH = "https://opengraph.githubassets.com/{hash}/{appendix}" +STAR_HISTORY_URL = "https://api.star-history.com/svg?repos={identifier}&type=Date" + +@register("astrbot_plugin_github_sub", "XieMu", "根据群聊中 GitHub 相关链接自动发送 GitHub OpenGraph 图片", "1.0.0", "https://github.com/xiemu-c/astrbot_plugin_github_sub") class MyPlugin(Star): def __init__(self, context: Context): super().__init__(context) - async def initialize(self): - """可选择实现异步的插件初始化方法,当实例化该插件类之后会自动调用该方法。""" - - # 注册指令的装饰器。指令名为 helloworld。注册成功后,发送 `/helloworld` 就会触发这个指令,并回复 `你好, {user_name}!` - @filter.command("helloworld") - async def helloworld(self, event: AstrMessageEvent): - """这是一个 hello world 指令""" # 这是 handler 的描述,将会被解析方便用户了解插件内容。建议填写。 - user_name = event.get_sender_name() - message_str = event.message_str # 用户发的纯文本消息字符串 - message_chain = event.get_messages() # 用户所发的消息的消息链 # from astrbot.api.message_components import * - logger.info(message_chain) - yield event.plain_result(f"Hello, {user_name}, 你发了 {message_str}!") # 发送一条纯文本消息 - async def terminate(self): - """可选择实现异步的插件销毁方法,当插件被卸载/停用时会调用。""" + @filter.regex(GITHUB_URL_PATTERN) + async def github_repo(self, event: AstrMessageEvent): + '''解析 Github 仓库信息''' + msg = event.message_str + match = re.search(GITHUB_URL_PATTERN, msg) + repo_url = match.group(0) + repo_url = repo_url.replace("https://github.com/", "") + hash_value = uuid.uuid4().hex + opengraph_url = GITHUB_REPO_OPENGRAPH.format(hash=hash_value, appendix=repo_url) + logger.info(f"生成的 OpenGraph URL: {opengraph_url}") + + try: + yield event.image_result(opengraph_url) + except Exception as e: + logger.error(f"下载图片失败: {e}") + yield event.plain_result("下载 GitHub 图片失败: " + str(e)) + return