-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!(iamai): remove unused libcore module (#335)
- Loading branch information
1 parent
821f9bb
commit 2d71ea3
Showing
38 changed files
with
2,723 additions
and
2,612 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from iamai import Plugin | ||
from iamai.adapter.cqhttp.message import CQHTTPMessageSegment as ms | ||
from iamai.log import logger, error_or_exception | ||
|
||
|
||
class Echo(Plugin): | ||
async def handle(self) -> None: | ||
try: | ||
await self.event.reply(eval(str(self.event.message.replace("/echo ", "")))) | ||
except Exception as e: | ||
await self.event.reply(f"Error: {e}") | ||
|
||
async def rule(self) -> bool: | ||
if self.event.adapter.name != "cqhttp": | ||
return False | ||
if self.event.type != "message": | ||
return False | ||
return self.event.message.startswith("/echo ") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import re | ||
import json | ||
import base64 | ||
import secrets | ||
|
||
from iamai import Plugin | ||
from iamai.log import logger | ||
from iamai.adapter.cqhttp.message import CQHTTPMessageSegment as ms | ||
|
||
from .config import match_github_links | ||
from .lib.opengraph import get_opengraph_image, indentify_message_tag | ||
|
||
|
||
class Github(Plugin): | ||
async def handle(self) -> None: | ||
message_text = self.event.message.get_plain_text() | ||
|
||
# 匹配 GitHub 链接 | ||
match = match_github_links(message_text) | ||
if not match: | ||
logger.warning("No valid GitHub link found in the message.") | ||
return | ||
|
||
# 解析匹配到的链接信息 | ||
groups = match.groupdict() | ||
# message = {"id": self.event.message_id} | ||
# message = message | groups | ||
|
||
logger.info(f"message: {groups}") | ||
|
||
tag = indentify_message_tag(groups) | ||
|
||
if not tag: | ||
logger.warning("No valid tag found in the message.") | ||
return | ||
|
||
logger.info(f"Tag: {tag}") | ||
# 获取 OpenGraph 图片(示例代码) | ||
image = await get_opengraph_image(tag, secrets.token_urlsafe(16)) | ||
if image: | ||
logger.info("Image fetched successfully!") | ||
|
||
# 将图片转换为 Base64 编码 | ||
encoded_image = base64.b64encode(image).decode("utf-8") | ||
|
||
# 构造 CQHTTP 的图片消息格式 | ||
cq_message = ms.image(file=f"base64://{encoded_image}") | ||
|
||
# 发送图片消息 | ||
await self.event.reply(cq_message) | ||
|
||
async def rule(self) -> bool: | ||
# 判断是否为 GitHub 相关消息 | ||
# return "github.com" in self.event.message.get_plain_text() | ||
return self.event.type == "message" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import re | ||
from examples.plugins._github.lib.opengraph import Tag | ||
|
||
URL_PCHAR = r"(?:[a-zA-Z0-9\-._~!$&'()*+,;=:@]|%[0-9a-fA-F]{2})" | ||
URL_QUERY_REGEX = rf"(?:\?(?:{URL_PCHAR}|[/?])*)?" | ||
|
||
OWNER_REGEX = r"(?P<owner>[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?)" | ||
REPO_REGEX = r"(?P<repo>[a-zA-Z0-9_\-\.]+)" | ||
FULLREPO_REGEX = rf"{OWNER_REGEX}/{REPO_REGEX}" | ||
COMMIT_HASH_REGEX = r"(?P<commit>[0-9a-f]{5,40})" | ||
ISSUE_REGEX = r"(?P<issue>\d+)" | ||
ISSUE_COMMENT_REGEX = r"(?:#issuecomment-(?P<comment>\d+))?" | ||
|
||
GITHUB_LINK_REGEX = r"github\.com" | ||
GITHUB_REPO_LINK_REGEX = rf"{GITHUB_LINK_REGEX}/{FULLREPO_REGEX}" | ||
GITHUB_COMMIT_LINK_REGEX = rf"{GITHUB_REPO_LINK_REGEX}/commit/{COMMIT_HASH_REGEX}" | ||
GITHUB_ISSUE_LINK_REGEX = ( | ||
rf"{GITHUB_REPO_LINK_REGEX}/issues/{ISSUE_REGEX}" | ||
rf"{URL_QUERY_REGEX}{ISSUE_COMMENT_REGEX}" | ||
) | ||
GITHUB_PR_LINK_REGEX = ( | ||
rf"{GITHUB_REPO_LINK_REGEX}/pull/{ISSUE_REGEX}" | ||
rf"{URL_QUERY_REGEX}{ISSUE_COMMENT_REGEX}" | ||
) | ||
GITHUB_ISSUE_OR_PR_LINK_REGEX = ( | ||
rf"{GITHUB_REPO_LINK_REGEX}/(?:issues|pull)/{ISSUE_REGEX}" | ||
rf"{URL_QUERY_REGEX}{ISSUE_COMMENT_REGEX}" | ||
) | ||
GITHUB_PR_COMMIT_LINK_REGEX = rf"{GITHUB_PR_LINK_REGEX}/commits/{COMMIT_HASH_REGEX}" | ||
GITHUB_PR_FILE_LINK_REGEX = rf"{GITHUB_PR_LINK_REGEX}/files" | ||
GITHUB_RELEASE_LINK_REGEX = ( | ||
rf"{GITHUB_REPO_LINK_REGEX}/releases/tag/(?P<tag>{URL_PCHAR}+)" | ||
) | ||
|
||
|
||
def match_github_links(url): | ||
""" | ||
根据优先级匹配 GitHub 链接。 | ||
按照以下优先级进行匹配: | ||
1. GITHUB_REPO_LINK_REGEX | ||
2. FULLREPO_REGEX | ||
3. GITHUB_COMMIT_LINK_REGEX | ||
4. GITHUB_ISSUE_LINK_REGEX | ||
5. GITHUB_PR_LINK_REGEX | ||
6. GITHUB_ISSUE_OR_PR_LINK_REGEX | ||
7. GITHUB_PR_COMMIT_LINK_REGEX | ||
8. GITHUB_PR_FILE_LINK_REGEX | ||
9. GITHUB_RELEASE_LINK_REGEX | ||
""" | ||
patterns = [ | ||
re.compile(GITHUB_RELEASE_LINK_REGEX), | ||
re.compile(GITHUB_PR_FILE_LINK_REGEX), | ||
re.compile(GITHUB_COMMIT_LINK_REGEX), | ||
re.compile(GITHUB_ISSUE_LINK_REGEX), | ||
re.compile(GITHUB_PR_LINK_REGEX), | ||
re.compile(GITHUB_ISSUE_OR_PR_LINK_REGEX), | ||
re.compile(GITHUB_PR_COMMIT_LINK_REGEX), | ||
re.compile(GITHUB_REPO_LINK_REGEX), | ||
re.compile(FULLREPO_REGEX), | ||
] | ||
|
||
for pattern in patterns: | ||
match = pattern.search(url) | ||
if match: | ||
return match | ||
|
||
return None | ||
|
||
|
||
repo_link = match_github_links( | ||
"https://github.com/HydroRoll-Team/infini/pull/55" | ||
) # {'owner': 'HydroRoll-Team', 'repo': 'infini', 'issue': '55', 'comment': None} | ||
|
||
if __name__ == "__main__": | ||
print(repo_link) | ||
print(Tag(**repo_link.groupdict())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from time import strftime, localtime | ||
|
||
from iamai import Plugin | ||
from iamai.adapter.apscheduler import scheduler_decorator | ||
import random | ||
|
||
|
||
@scheduler_decorator( | ||
trigger="interval", | ||
trigger_args={"seconds": random.randint(10, 20)}, | ||
override_rule=True, | ||
) | ||
class Schedule(Plugin): | ||
async def handle(self) -> None: | ||
await self.bot.get_adapter("cqhttp").send( | ||
f"Time: {strftime('%Y-%m-%d %H:%M:%S', localtime())}", | ||
message_type="group", | ||
id_=126211793, # Group ID | ||
) | ||
|
||
async def rule(self) -> bool: | ||
return ( | ||
self.event.type == "apscheduler" and type(self) is self.event.plugin_class | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from iamai import Plugin | ||
from iamai.log import logger, error_or_exception | ||
|
||
|
||
class Commander(Plugin): | ||
async def handle(self) -> None: | ||
if self.event.message == "/reload": | ||
logger.info("正在重载插件") | ||
await self.bot.reload_plugins() | ||
await self.event.reply("插件已重载") | ||
elif self.event.message == "/restart": | ||
logger.info("正在重启 Bot") | ||
await self.bot.restart() | ||
await self.event.reply("Bot 已重启") | ||
|
||
async def rule(self) -> bool: | ||
error_or_exception(f"Commander: {self.event.message}", None, True) | ||
return str(self.event.message) in ["/reload", "/restart", "/res"] |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import sys | ||
|
||
from .cli import cli_func | ||
from iamai.cli import cli_func | ||
|
||
|
||
def main(*args): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from __future__ import annotations | ||
|
||
def sum_as_string(self, a: int, b: int) -> int: | ||
"""This function returns the sum of two numbers.""" |
Oops, something went wrong.