mirror of
https://github.com/Shu-Ying/main_plugin_farm.git
synced 2026-10-29 05:29:58 +08:00
add # 新增Nonebot发布工作流
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
# 普通 NoneBot 接入预留位置
|
||||
# 普通 NoneBot 接入
|
||||
|
||||
发布目标:https://github.com/Shu-Ying/nonebot_plugin_farm.git
|
||||
负责 NoneBot 命令、会话、消息、配置与启停,不导入真寻。
|
||||
发布仓库保留 Python 包构建布局;PyPI 发布流程待实施。
|
||||
本目录尚不可安装。远程仓库本轮不再拉取或推送。
|
||||
本目录负责命令、统一会话、消息、配置与启停,不导入真寻。
|
||||
运行时在打包时复用 `integrations/zhenxun/runtime.py`,核心和渲染模块
|
||||
按白名单私有化。此源码目录不能直接加载,需先构建生成仓库。
|
||||
|
||||
`python scripts/build_nonebot.py --output dist/nonebot-preview` 生成标准
|
||||
包布局与 ZIP;进入输出的 `repository` 可运行 `python -m build`。
|
||||
配置、命令范围、数据迁移和发布说明见 `docs/12-nonebot.md`。
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
"""普通 NoneBot 农场插件入口。"""
|
||||
|
||||
from nonebot import require
|
||||
from nonebot.plugin import PluginMetadata, inherit_supported_adapters
|
||||
|
||||
require("nonebot_plugin_alconna")
|
||||
require("nonebot_plugin_uninfo")
|
||||
require("nonebot_plugin_localstore")
|
||||
|
||||
from ._version import __version__
|
||||
from .config import Config
|
||||
from .runtime import HELP
|
||||
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="农场", description="快乐的农场时光", usage=HELP,
|
||||
type="application", config=Config,
|
||||
homepage="https://github.com/Shu-Ying/nonebot_plugin_farm",
|
||||
supported_adapters=inherit_supported_adapters(
|
||||
"nonebot_plugin_alconna", "nonebot_plugin_uninfo",
|
||||
),
|
||||
extra={"author": "Shu-Ying", "version": __version__},
|
||||
)
|
||||
|
||||
from . import commands
|
||||
@@ -0,0 +1,66 @@
|
||||
"""NoneBot 命令、统一会话和消息;业务由生成的共享运行时处理。"""
|
||||
|
||||
import logging
|
||||
|
||||
from nonebot import get_driver, get_plugin_config, on_command
|
||||
from nonebot.adapters import Bot, Event, Message
|
||||
from nonebot.params import CommandArg
|
||||
from nonebot_plugin_alconna import At, UniMessage
|
||||
from nonebot_plugin_alconna.uniseg import get_message_id
|
||||
import nonebot_plugin_localstore as store
|
||||
from nonebot_plugin_uninfo import Uninfo
|
||||
|
||||
from .config import Config
|
||||
from .data_paths import data_directory
|
||||
from .runtime import COMMANDS, Runtime
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
config = get_plugin_config(Config)
|
||||
driver = get_driver()
|
||||
runtime = Runtime(
|
||||
data_directory(store.get_plugin_data_dir(), config.farm_data_dir),
|
||||
config.farm_font, config.farm_deployment, config.farm_image_format,
|
||||
font_setting="FARM_FONT",
|
||||
)
|
||||
driver.on_startup(runtime.start)
|
||||
driver.on_shutdown(runtime.close)
|
||||
|
||||
|
||||
def handler_for(action):
|
||||
async def handle(
|
||||
bot: Bot, event: Event, session: Uninfo,
|
||||
args: Message = CommandArg(),
|
||||
):
|
||||
try:
|
||||
message_id = get_message_id(event, bot)
|
||||
except Exception:
|
||||
message_id = None
|
||||
try:
|
||||
target_user_id = None
|
||||
if action == "steal":
|
||||
mentions = UniMessage.of(args, bot=bot)[At]
|
||||
if len(mentions) != 1 or mentions[0].flag != "user":
|
||||
await UniMessage("请只@一个具体偷菜目标。").send(
|
||||
event, bot, reply_to=bool(message_id),
|
||||
)
|
||||
return
|
||||
target_user_id = mentions[0].target
|
||||
response = await runtime.execute(
|
||||
action, args.extract_plain_text(),
|
||||
bot_id=str(bot.self_id), platform=bot.adapter.get_name(),
|
||||
user_id=str(session.user.id), message_id=message_id,
|
||||
display_name=session.user.name or "农场玩家",
|
||||
target_user_id=target_user_id,
|
||||
)
|
||||
except Exception as error:
|
||||
logger.error("farm request failed type=%s", type(error).__name__)
|
||||
response = "农场服务异常,请查看服务器日志。"
|
||||
message = (UniMessage.image(raw=response)
|
||||
if isinstance(response, bytes) else UniMessage(response))
|
||||
await message.send(event, bot, reply_to=bool(message_id))
|
||||
return handle
|
||||
|
||||
|
||||
for command, action in COMMANDS.items():
|
||||
matcher = on_command(command, priority=5, block=True)
|
||||
matcher.handle()(handler_for(action))
|
||||
@@ -0,0 +1,23 @@
|
||||
"""普通 NoneBot 配置;不依赖真寻配置系统。"""
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Literal, Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class Config(BaseModel):
|
||||
"""农场启动配置。"""
|
||||
|
||||
farm_data_dir: Optional[Path] = Field(
|
||||
None, description="数据目录;默认兼容旧版 localstore 农场目录",
|
||||
)
|
||||
farm_font: Optional[Path] = Field(
|
||||
None, description="中文字体路径;不可用时退回文字响应",
|
||||
)
|
||||
farm_deployment: str = Field(
|
||||
"nonebot", min_length=1, description="农场存档部署标识",
|
||||
)
|
||||
farm_image_format: Literal["AUTO", "JPEG", "PNG"] = Field(
|
||||
"AUTO", description="图片格式,AUTO 优先使用压缩 JPEG",
|
||||
)
|
||||
@@ -0,0 +1,9 @@
|
||||
"""兼容旧版 NoneBot localstore 存档位置。"""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def data_directory(base, configured=None):
|
||||
if configured is not None:
|
||||
return Path(configured)
|
||||
return Path(base) / "nonebot_plugin_farm" / "farm_db"
|
||||
@@ -1,4 +1,4 @@
|
||||
"""真寻接入编排;核心私有导入,测试数据与旧版隔离。"""
|
||||
"""真寻与 NoneBot 共用的运行编排;不导入宿主框架。"""
|
||||
|
||||
import hashlib
|
||||
from dataclasses import replace
|
||||
@@ -87,9 +87,11 @@ def operation_id(bot_id, platform, user_id, message_id, action):
|
||||
|
||||
class Runtime:
|
||||
def __init__(self, data_dir, font_path=None, deployment="zhenxun-preview",
|
||||
image_format="AUTO", *, font_dir=None):
|
||||
image_format="AUTO", *, font_dir=None,
|
||||
font_setting="FARM_PREVIEW_FONT"):
|
||||
self.data_dir = Path(data_dir).resolve()
|
||||
self.font_path = font_path
|
||||
self.font_setting = font_setting
|
||||
self.font_dir = Path(font_dir or "resources/font")
|
||||
self.deployment = deployment
|
||||
self.image_format = image_format
|
||||
@@ -273,7 +275,7 @@ class Runtime:
|
||||
if not self.rendering:
|
||||
if action == "plot_ids":
|
||||
return plot_ids_text(await self.service.snapshot(who))
|
||||
return "请配置 FARM_PREVIEW_FONT 为可用中文字体路径。"
|
||||
return f"请配置 {self.font_setting} 为可用中文字体路径。"
|
||||
now = self.service.clock()
|
||||
theme = season_for(now)
|
||||
logger.info("farm preview seasonal theme=%s", theme)
|
||||
|
||||
Reference in New Issue
Block a user