先调整到一个可用的状态

This commit is contained in:
2025-09-30 01:59:40 +08:00
parent 818f2b64ec
commit 0dbe164703
2 changed files with 364 additions and 146 deletions

View File

@ -1,6 +1,5 @@
import asyncio
import datetime
import re
from pathlib import Path
from typing import Any, Literal, cast
@ -20,149 +19,7 @@ from nonebot.adapters.onebot.v11.event import \
from nonebot_plugin_alconna import UniMessage, UniMsg
from pydantic import BaseModel
PATTERN_DELTA_HMS = re.compile(r"^((\d+|[零一两二三四五六七八九十]+) ?天)?((\d+|[零一两二三四五六七八九十]+) ?个?小?时)?((\d+|[零一两二三四五六七八九十]+) ?分钟?)?((\d+|[零一两二三四五六七八九十]+) ?秒钟?)? ?后 ?$")
PATTERN_DATE_SPECIFY = re.compile(r"(\d{1,2}|[零一二三四五六七八九十]+) ?[日号]")
PATTERN_MONTH_SPECIFY = re.compile(r"(\d{1,2}|[零一二三四五六七八九十]+) ?月")
PATTERN_YEAR_SPECIFY = re.compile(r"(\d|[零一二三四五六七八九十]+) ?年")
PATTERN_HOUR_SPECIFY = re.compile(r"(\d|[零一二三四五六七八九十]+) ?[点时](半?)钟?")
PATTERN_MINUTE_SPECIFY = re.compile(r"(\d|[零一二三四五六七八九十]+) ?分(钟)?")
PATTERN_SECOND_SPECIFY = re.compile(r"(\d|[零一二三四五六七八九十]+) ?秒(钟)?")
PATTERN_HMS_SPECIFY = re.compile(r"\d\d[:]\d\d([:]\d\d)?")
PATTERN_PM_SPECIFY = re.compile(r"(下午|PM|晚上)")
def parse_chinese_or_digit(s: str) -> int:
if set(s) <= set("0123456789"):
return int(s)
s = s.replace("", "")
chinese_to_arabic = {
'': 0, '': 1, '': 2, '': 3, '': 4,
'': 5, '': 6, '': 7, '': 8, '': 9,
'': 10
}
if s in chinese_to_arabic:
return chinese_to_arabic[s]
if len(s) == 2 and s[0] == '':
if s[1] not in chinese_to_arabic:
return -1
return 10 + chinese_to_arabic.get(s[1], 0)
elif len(s) == 2 and s[1] == '':
if s[0] not in chinese_to_arabic:
return -1
return 10 * chinese_to_arabic.get(s[0], 0)
elif len(s) == 3 and s[1] == '':
if s[0] not in chinese_to_arabic or s[2] not in chinese_to_arabic:
return -1
return 10 * chinese_to_arabic.get(s[0], 0) + chinese_to_arabic.get(s[2], 0)
try:
return int(s)
except ValueError:
return -1
def get_target_time(content: str) -> datetime.datetime | None:
if match := re.match(PATTERN_DELTA_HMS, content.strip()):
days = parse_chinese_or_digit(match.group(2) or "0")
hours = parse_chinese_or_digit(match.group(4) or "0")
minutes = parse_chinese_or_digit(match.group(6) or "0")
seconds = parse_chinese_or_digit(match.group(8) or "0")
return datetime.datetime.now() + datetime.timedelta(days=days, hours=hours, minutes=minutes, seconds=seconds)
t = datetime.datetime.now()
content_to_match = content
if "明天" in content_to_match:
content_to_match = "".join(content_to_match.split("明天"))
t += datetime.timedelta(days=1)
elif "后天" in content_to_match:
content_to_match = "".join(content_to_match.split("后天"))
t += datetime.timedelta(days=2)
elif "今天" in content_to_match:
content_to_match = "".join(content_to_match.split("今天"))
if match1 := re.match(PATTERN_DATE_SPECIFY, content_to_match):
content_to_match = "".join(content_to_match.split(match1.group(0)))
day = parse_chinese_or_digit(match1.group(1))
if day <= 0 or day > 31:
return
if day < t.day:
if t.month == 12:
t = t.replace(year=t.year + 1, month=1, day=day)
else:
t = t.replace(month=t.month + 1, day=day)
else:
t = t.replace(day=day)
if match2 := re.match(PATTERN_MONTH_SPECIFY, content_to_match):
content_to_match = "".join(content_to_match.split(match2.group(0)))
month = parse_chinese_or_digit(match2.group(1))
if month <= 0 or month > 12:
return
if month < t.month:
t = t.replace(year=t.year + 1, month=month)
else:
t = t.replace(month=month)
if match3 := re.match(PATTERN_YEAR_SPECIFY, content_to_match):
content_to_match = "".join(content_to_match.split(match3.group(0)))
year = parse_chinese_or_digit(match3.group(1))
if year < 100:
year += 2000
if year < t.year:
return
t = t.replace(year=year)
if match4 := re.match(PATTERN_HOUR_SPECIFY, content_to_match):
content_to_match = "".join(content_to_match.split(match4.group(0)))
hour = parse_chinese_or_digit(match4.group(1))
if hour < 0 or hour > 23:
return
t = t.replace(hour=hour, minute=0, second=0)
if match4.group(2) != None:
t = t.replace(minute=30)
if match5 := re.match(PATTERN_MINUTE_SPECIFY, content_to_match):
content_to_match = "".join(content_to_match.split(match5.group(0)))
minute = parse_chinese_or_digit(match5.group(1))
if minute < 0 or minute > 59:
return
t = t.replace(minute=minute, second=0)
if match6 := re.match(PATTERN_SECOND_SPECIFY, content_to_match):
content_to_match = "".join(content_to_match.split(match6.group(0)))
second = parse_chinese_or_digit(match6.group(1))
if second < 0 or second > 59:
return
t = t.replace(second=second)
if match7 := re.match(PATTERN_HMS_SPECIFY, content_to_match):
content_to_match = "".join(content_to_match.split(match7.group(0)))
hms = match7.group(0).replace("", ":").split(":")
if len(hms) >= 2:
hour = int(hms[0])
minute = int(hms[1])
if hour < 0 or hour > 23 or minute < 0 or minute > 59:
return
t = t.replace(hour=hour, minute=minute)
if len(hms) == 3:
second = int(hms[2])
if second < 0 or second > 59:
return
t = t.replace(second=second)
content_to_match = content_to_match.replace("上午", "").replace("AM", "").replace("凌晨", "")
if match8 := re.match(PATTERN_PM_SPECIFY, content_to_match):
content_to_match = "".join(content_to_match.split(match8.group(0)))
if t.hour < 12:
t = t.replace(hour=t.hour + 12)
if t.hour == 12:
t += datetime.timedelta(hours=12)
if len(content_to_match.strip()) != 0:
return
if t < datetime.datetime.now():
t += datetime.timedelta(days=1)
return t
from konabot.plugins.simple_notify.parse_time import get_target_time
evt = on_message()
@ -268,11 +125,14 @@ async def create_notify_task(notify: Notify, fail2remove: bool = True):
@evt.handle()
async def _(msg: UniMsg, mEvt: Event):
if mEvt.get_user_id() in nonebot.get_bots():
return
text = msg.extract_plain_text()
if "提醒我" not in text:
return
segments = text.split("提醒我")
segments = text.split("提醒我", maxsplit=1)
if len(segments) != 2:
return
@ -318,7 +178,7 @@ async def _(msg: UniMsg, mEvt: Event):
cfg.notifies.append(notify)
save_notify_config(cfg)
DATA_FILE_LOCK.release()
await evt.send(await UniMessage().at(mEvt.get_user_id()).text(
f" 了解啦!将会在 {notify.notify_time} 提醒你哦~").export())