generated from A-kirami/nonebot-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. 添加事件预处理,用于提取小程序链接 2. 优化 链接/资源ID 提取逻辑
- Loading branch information
Showing
18 changed files
with
177 additions
and
130 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 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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
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,63 @@ | ||
import json | ||
|
||
from typing import Literal | ||
from nonebot.log import logger | ||
from nonebot.message import event_preprocessor | ||
from nonebot.typing import T_State | ||
from nonebot.rule import Rule | ||
from nonebot.adapters.onebot.v11 import MessageEvent | ||
|
||
|
||
R_KEYWORD_KEY: Literal["_r_keyword"] = "_r_keyword" | ||
R_EXTRACT_KEY: Literal["_r_extract"] = "_r_extract" | ||
|
||
@event_preprocessor | ||
def _(event: MessageEvent, state: T_State): | ||
message = event.get_message() | ||
text = message.extract_plain_text().strip() | ||
if json_seg := [seg for seg in message if seg.type == 'json']: | ||
try: | ||
data_str = json_seg[0].data.get('data') | ||
data_str = data_str.replace(',', ',') | ||
data = json.loads(data_str) | ||
meta = data.get('meta') | ||
if detail := meta.get('detail_1'): | ||
text = detail.get('qqdocurl') | ||
elif news := meta.get('news'): | ||
text = news.get('jumpUrl') | ||
text = text.replace('\\', '').replace("&", "&") | ||
except Exception: | ||
pass | ||
state[R_EXTRACT_KEY] = text | ||
|
||
class RKeywordsRule: | ||
"""检查消息是否含有关键词 增强版""" | ||
|
||
__slots__ = ("keywords",) | ||
|
||
def __init__(self, *keywords: str): | ||
self.keywords = keywords | ||
|
||
def __repr__(self) -> str: | ||
return f"RKeywords(keywords={self.keywords})" | ||
|
||
def __eq__(self, other: object) -> bool: | ||
return isinstance(other, RKeywordsRule) and frozenset( | ||
self.keywords | ||
) == frozenset(other.keywords) | ||
|
||
def __hash__(self) -> int: | ||
return hash(frozenset(self.keywords)) | ||
|
||
async def __call__(self, state: T_State) -> bool: | ||
text = state.get(R_EXTRACT_KEY) | ||
if not text: | ||
return False | ||
if key := next((k for k in self.keywords if k in text), None): | ||
state[R_KEYWORD_KEY] = key | ||
return True | ||
return False | ||
|
||
|
||
def r_keywords(*keywords: str) -> Rule: | ||
return Rule(RKeywordsRule(*keywords)) |
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
Oops, something went wrong.