Files
konabot/konabot/plugins/solar_terms/__init__.py
2026-02-21 23:36:42 +08:00

66 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from borax.calendars import LunarDate
from nonebot import on_command
from nonebot.internal.adapter.event import Event
from nonebot_plugin_alconna import UniMessage
from nonebot_plugin_apscheduler import scheduler
from konabot.common.llm import get_llm
from konabot.plugins.poster.poster_info import PosterInfo, register_poster_info
from konabot.plugins.poster.service import broadcast
register_poster_info("二十四节气", PosterInfo(
{"节气", "24节气"},
"当有新的节气时,报告节气信息",
))
@scheduler.scheduled_job("cron", hour="8")
async def _():
today = LunarDate.today()
term: str | None = today.term
if term is not None:
llm = get_llm("qwen3-max")
prompt = f"请写两个四字词语,讲讲节气:{term}。以感叹号结尾,格式是「某某某某,某某某某!」,不要带有其他内容。"
result = await llm.chat([{
"role": "user", "content": prompt,
}])
result = result.content
if result is None:
result = ""
await broadcast("二十四节气", UniMessage.text(f"【今日节气】今天是 {term} 哦!{result}"))
cmd_next_term = on_command("下一个节气")
@cmd_next_term.handle()
async def _(event: Event):
date = LunarDate.today()
day_counter = 0
while date.term is None:
date = date.after(day_delta=1)
day_counter += 1
d_cn_format = date.strftime("%M月%D") # 相当于正月初一这样的格式
date_solar = date.to_solar_date()
d_glob_format = f"{date_solar.month}{date_solar.day}"
msg = UniMessage.text(
f"下一个节气是{date.term},在 {day_counter} 天后的 {d_glob_format}(农历{d_cn_format}"
)
await msg.send(event)
cmd_current_term = on_command("当前节气", aliases={"获取节气", "节气"})
@cmd_current_term.handle()
async def _(event: Event):
date = LunarDate.today()
while date.term is None:
date = date.before(day_delta=1)
msg = UniMessage.text(f"现在的节气是{date.term}")
await msg.send(event)