feat:doc
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import re
|
||||
from astrbot.api import logger
|
||||
|
||||
# 输入内容,获取过滤后的字符串与定时秒数
|
||||
class Pattern:
|
||||
_pattern = r'<wt>((?!wt).)*</wt>'
|
||||
|
||||
_MAX_SEC = 1800
|
||||
_MIN_SEC = 10
|
||||
# 定义最大与最小的范围,固定输入的规范
|
||||
def __init__(self,o_str:str) -> None:
|
||||
logger.debug(f"模板消息:{o_str}")
|
||||
res = self.extract(o_str)
|
||||
self.count:int|None = res['second']
|
||||
self.string:str = res["content"]
|
||||
|
||||
|
||||
def sec_range(self,i)->int:
|
||||
if i > Pattern._MAX_SEC:
|
||||
return Pattern._MAX_SEC
|
||||
if i < Pattern._MIN_SEC :
|
||||
return Pattern._MIN_SEC
|
||||
return i
|
||||
|
||||
|
||||
def load(self,pattern_str:str)->int|None:
|
||||
content = pattern_str.strip().removeprefix("<wt>").removesuffix("</wt>").strip()
|
||||
|
||||
try:
|
||||
num = int(content)
|
||||
return self.sec_range(num)
|
||||
except:
|
||||
logger.warning ("模板内容错误:{}".format(content))
|
||||
return None
|
||||
|
||||
def extract(self,o_str:str):
|
||||
# 多次匹配防止重复模板
|
||||
timerSec :None|int = None
|
||||
res=re.finditer(Pattern._pattern,o_str)
|
||||
for i in res :
|
||||
# 对结果和源字符串处理
|
||||
o_str = o_str.replace(i.group(),"",1)
|
||||
num = self.load(i.group())
|
||||
logger.info("获得模板:{0}".format(i.group()))
|
||||
|
||||
if num and not timerSec:
|
||||
timerSec = num
|
||||
#消除不完整/非法模板
|
||||
o_str= o_str.replace("<wt>","")
|
||||
o_str=o_str.replace("</wt>","")
|
||||
logger.info(f"过滤后消息:{o_str}")
|
||||
|
||||
return {
|
||||
"content":o_str,
|
||||
"second":timerSec
|
||||
}
|
||||
Reference in New Issue
Block a user