Update main.py

This commit is contained in:
谢幕 2025-07-18 19:56:06 +08:00 committed by GitHub
parent e7fc605465
commit 393c4dcec5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

41
main.py
View File

@ -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